From ca554cc9f1012cf5dae69e6fa6d5ab25d2239911 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Wed, 18 Jul 2018 12:09:07 -0400 Subject: [PATCH] Implement alternate node target fields Implements the last additional hypervisor selector fields: vCPUs. Addresses #11 --- pvcd.py | 1 + pvcd/NodeInstance.py | 21 ++++++++++++++++++++- pvcd/VMInstance.py | 8 ++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pvcd.py b/pvcd.py index c7502ff8..8450be45 100755 --- a/pvcd.py +++ b/pvcd.py @@ -201,6 +201,7 @@ else: transaction.create('/nodes/{}/memfree'.format(myhostname), '0'.encode('ascii')) transaction.create('/nodes/{}/memused'.format(myhostname), '0'.encode('ascii')) transaction.create('/nodes/{}/memalloc'.format(myhostname), '0'.encode('ascii')) + transaction.create('/nodes/{}/vcpualloc'.format(myhostname), '0'.encode('ascii')) transaction.create('/nodes/{}/cpuload'.format(myhostname), '0.0'.encode('ascii')) transaction.create('/nodes/{}/runningdomains'.format(myhostname), ''.encode('ascii')) transaction.create('/nodes/{}/domainscount'.format(myhostname), '0'.encode('ascii')) diff --git a/pvcd/NodeInstance.py b/pvcd/NodeInstance.py index c6da43bb..bdb87021 100644 --- a/pvcd/NodeInstance.py +++ b/pvcd/NodeInstance.py @@ -45,6 +45,7 @@ class NodeInstance(): self.memused = 0 self.memfree = 0 self.memalloc = 0 + self.vcpualloc = 0 self.inflush = False # Zookeeper handlers for changed states @@ -92,6 +93,13 @@ class NodeInstance(): except AttributeError: self.memalloc = 0 + @zk_conn.DataWatch('/nodes/{}/vcpualloc'.format(self.name)) + def watch_hypervisor_vcpualloc(data, stat, event=""): + try: + self.vcpualloc = data.decode('ascii') + except AttributeError: + self.vcpualloc = 0 + @zk_conn.DataWatch('/nodes/{}/runningdomains'.format(self.name)) def watch_hypervisor_runningdomains(data, stat, event=""): try: @@ -113,6 +121,9 @@ class NodeInstance(): def getallocmem(self): return self.memalloc + def getallocvcpu(self): + return self.vcpualloc + def getcpuload(self): return self.cpuload @@ -209,10 +220,12 @@ class NodeInstance(): # Toggle state management of dead VMs to restart them memalloc = 0 + vcpualloc = 0 for domain, instance in self.s_domain.items(): if instance.inshutdown == False and domain in self.domain_list: # Add the allocated memory to our memalloc value memalloc += instance.getmemory() + vcpualloc += instance.getvcpus() if instance.getstate() == 'start' and instance.gethypervisor() == self.name: if instance.getdom() != None: try: @@ -234,6 +247,7 @@ class NodeInstance(): self.memused = int(psutil.virtual_memory().used / 1024 / 1024) self.memfree = int(psutil.virtual_memory().free / 1024 / 1024) self.memalloc = memalloc + self.vcpualloc = vcpualloc self.cpuload = os.getloadavg()[0] self.domains_count = len(lv_conn.listDomainsID()) keepalive_time = int(time.time()) @@ -242,6 +256,7 @@ class NodeInstance(): '/nodes/{}/memused'.format(self.name): str(self.memused), '/nodes/{}/memfree'.format(self.name): str(self.memfree), '/nodes/{}/memalloc'.format(self.name): str(self.memalloc), + '/nodes/{}/vcpualloc'.format(self.name): str(self.vcpualloc), '/nodes/{}/cpuload'.format(self.name): str(self.cpuload), '/nodes/{}/runningdomains'.format(self.name): ' '.join(self.domain_list), '/nodes/{}/domainscount'.format(self.name): str(self.domains_count), @@ -392,7 +407,11 @@ def findTargetHypervisorVCPUs(zk_conn, dom_uuid): hypervisor_list = getHypervisors(zk_conn, dom_uuid) for hypervisor in hypervisor_list: - pass + vcpus = int(zkhandler.readdata(zk_conn, '/nodes/{}/vcpualloc'.format(hypervisor))) + + if vcpus < least_vcpus: + least_vcpus = vcpus + target_hypervisor = hypervisor return target_hypervisor diff --git a/pvcd/VMInstance.py b/pvcd/VMInstance.py index d3c41f21..91ef9a22 100644 --- a/pvcd/VMInstance.py +++ b/pvcd/VMInstance.py @@ -73,6 +73,14 @@ class VMInstance: return memory + def getvcpus(self): + try: + vcpus = int(self.dom.info()[3]) + except: + vcpus = 0 + + return vcpus + # Manage local node domain_list def addDomainToList(self): if not self.domuuid in self.thishypervisor.domain_list: