From 8479ccd2c563d47386e6cf73c3ffa6bae84d6cfb Mon Sep 17 00:00:00 2001 From: Tom Goff Date: Wed, 12 Oct 2016 17:36:30 +0000 Subject: [PATCH] daemon: Use a set to store the broker node map. --- daemon/core/broker.py | 75 +++++++++++++++++++---------------------- daemon/core/mobility.py | 5 +-- 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/daemon/core/broker.py b/daemon/core/broker.py index 688e5a54..de06e49a 100644 --- a/daemon/core/broker.py +++ b/daemon/core/broker.py @@ -399,45 +399,41 @@ class CoreBroker(ConfigurableManager): def addnodemap(self, server, nodenum): ''' Record a node number to emulation server mapping. ''' - self.nodemap_lock.acquire() - if nodenum in self.nodemap: - if server in self.nodemap[nodenum]: - self.nodemap_lock.release() - return - self.nodemap[nodenum].append(server) - else: - self.nodemap[nodenum] = [server,] - if server in self.nodecounts: - self.nodecounts[server] += 1 - else: - self.nodecounts[server] = 1 - self.nodemap_lock.release() - + with self.nodemap_lock: + if nodenum in self.nodemap: + if server in self.nodemap[nodenum]: + return + self.nodemap[nodenum].add(server) + else: + self.nodemap[nodenum] = {server} + if server in self.nodecounts: + self.nodecounts[server] += 1 + else: + self.nodecounts[server] = 1 + def delnodemap(self, sock, nodenum): ''' Remove a node number to emulation server mapping. Return the number of nodes left on this server. ''' - self.nodemap_lock.acquire() count = None - if nodenum not in self.nodemap: - self.nodemap_lock.release() - return count - found = False - for server in self.nodemap[nodenum]: - (host, port, srvsock) = self.getserver(server) - if srvsock == sock: - found = True - break - if server in self.nodecounts: - count = self.nodecounts[server] - if found: - self.nodemap[nodenum].remove(server) + with self.nodemap_lock: + if nodenum not in self.nodemap: + return count + found = False + for server in self.nodemap[nodenum]: + (host, port, srvsock) = self.getserver(server) + if srvsock == sock: + found = True + break if server in self.nodecounts: - count -= 1 - self.nodecounts[server] = count - self.nodemap_lock.release() - return count - + count = self.nodecounts[server] + if found: + self.nodemap[nodenum].remove(server) + if server in self.nodecounts: + count -= 1 + self.nodecounts[server] = count + return count + def incrbootcount(self): ''' Count a node that has booted. ''' @@ -450,15 +446,12 @@ class CoreBroker(ConfigurableManager): return self.bootcount def getserversbynode(self, nodenum): - ''' Retrieve a list of emulation servers given a node number. + ''' Retrieve a set of emulation servers given a node number. ''' - self.nodemap_lock.acquire() - if nodenum not in self.nodemap: - self.nodemap_lock.release() - return [] - r = self.nodemap[nodenum] - self.nodemap_lock.release() - return r + with self.nodemap_lock: + if nodenum not in self.nodemap: + return set() + return self.nodemap[nodenum] def addnet(self, nodenum): ''' Add a node number to the list of link-layer nodes. diff --git a/daemon/core/mobility.py b/daemon/core/mobility.py index 3da4dab0..b5a8eb45 100644 --- a/daemon/core/mobility.py +++ b/daemon/core/mobility.py @@ -237,8 +237,9 @@ class MobilityManager(ConfigurableManager): return for nodenum in nodenums: node = self.phys[nodenum] - servers = self.session.broker.getserversbynode(nodenum) - (host, port, sock) = self.session.broker.getserver(servers[0]) + for server in self.session.broker.getserversbynode(nodenum): + break + (host, port, sock) = self.session.broker.getserver(server) netif = self.session.broker.gettunnel(net.objid, IPAddr.toint(host)) node.addnetif(netif, 0) netif.node = node