From 1a72ee6414c77f4aa177c8e578fbeb5c84adb5ab Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Mon, 4 Jun 2018 02:22:59 -0400 Subject: [PATCH] Get flush working properly --- NodeInstance.py | 34 +++++++++++++++++++++------------- pvcd.py | 14 +++++++------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/NodeInstance.py b/NodeInstance.py index 30990430..1fbbe682 100644 --- a/NodeInstance.py +++ b/NodeInstance.py @@ -58,21 +58,30 @@ class NodeInstance(threading.Thread): # Flush all VMs on the host def flush(self): for domain in self.domain_list: - print(domain) # Determine the best target hypervisor - least_mem = (2^64)/8 - least_load = 999.0 - least_host = "" - for node in self.t_node: - node_freemem = node.getfreemem() + least_mem = 2**64 + least_host = None + for node_name, node in self.t_node.items(): + if node_name == self.name: + continue + node_freemem = int(node.getfreemem()) if node_freemem < least_mem: least_mem = node_freemem - least_host = node.getname() + least_host = node_name - transaction = self.zk.transaction() - transaction.set_data('/domains/' + domain + '/state', 'migrate'.encode('ascii')) - transaction.set_data('/domains/' + domain + '/hypervisor', least_host.encode('ascii')) - transaction.commit() + if least_host == None: + print(">>> Failed to find valid migration target for %s" % domain) + transaction = self.zk.transaction() + transaction.set_data('/domains/' + domain + '/state', 'shutdown'.encode('ascii')) + transaction.commit() + else: + transaction = self.zk.transaction() + transaction.set_data('/domains/' + domain + '/state', 'migrate'.encode('ascii')) + transaction.set_data('/domains/' + domain + '/hypervisor', least_host.encode('ascii')) + transaction.commit() + + # Wait 1s between migrations + time.sleep(1) def run(self): if self.name == socket.gethostname(): @@ -136,8 +145,7 @@ class NodeInstance(threading.Thread): flushed_node_list = [] inactive_node_list = [] - for node in self.t_node: - node_name = node.getname() + for node_name in self.t_node: state, stat = self.zk.get('/nodes/%s/state' % node_name) node_state = state.decode('ascii') if node_state == 'start': diff --git a/pvcd.py b/pvcd.py index 4cd1d77c..236060bd 100755 --- a/pvcd.py +++ b/pvcd.py @@ -68,25 +68,22 @@ else: t_node = dict() s_domain = dict() node_list = [] +domain_list = [] @zk.ChildrenWatch('/nodes') def updatenodes(new_node_list): + global node_list node_list = new_node_list print('Node list: %s' % node_list) for node in node_list: if node in t_node: t_node[node].updatenodelist(t_node) else: - t_node[node] = NodeInstance.NodeInstance(node, t_node, s_domain, zk); - if node == myhostname: - t_node[node].start() - time.sleep(0.2) - for node in node_list: - if node in t_node: - t_node[node].updatenodelist(t_node) + t_node[node] = NodeInstance.NodeInstance(node, t_node, s_domain, zk) @zk.ChildrenWatch('/domains') def updatedomains(new_domain_list): + global domain_list domain_list = new_domain_list print('Domain list: %s' % domain_list) for domain in domain_list: @@ -96,6 +93,9 @@ def updatedomains(new_domain_list): if node in t_node: t_node[node].updatedomainlist(s_domain) +t_node[myhostname].start() +time.sleep(0.2) + while True: # Tick loop try: