def maintain(self, id, nid): """ Maintain""" logging.info("Maintain called from %032x with id %032x", nid.int_id, NodeID(id).int_id) closest = self.router.closest_predecessor(NodeID(id)) if closest.id is not self.node.id: logging.debug(" we are not the closest making a call") try: cur = WDHTClient(closest.ip, closest.port).maintain(id, nid) except TTransportException: logging.info("Call to %032x failed", closest.int_id) self.router.remove([closest]) return self.maintain(id, nid) else: logging.debug("We are the closest") cur = self.router.neighbor_set.get_neighbors() cur.append(self.node) if self.node.id != nid.id: # no need to update yourself self.router.update([nid]) logging.debug("Maintain will return %s", ' '.join(["%032x"%(x.int_id) for x in cur])) return cur
def join(self, nid): logging.info("%032x is Joining, Currently at %032x", nid.int_id, self.node.int_id) next_node = self.router.route(nid, False) if next_node.id == self.node.id: nodes = self.router.get_nodes() else: try: nodes = WDHTClient(next_node.ip, next_node.port).join(nid) except TTransportException: logging.info("Call to %032x failed", next_node.int_id) self.router.remove([next_node]) return self.join(nid) nodes.append(self.node) self.router.update([nid]) return unique(nodes)
def helper(L, t): """ L is the list and t is 0 for cw and 1 for ccw. If L has no elements, finds the closest node by looking at all the elements on the table. If L has one element, finds the closest node by gossiping on the other existing neighbor """ closestNode = None if (len(L)==0): if (t==0): closestNode = self.router.closest_successor(self.node) else: closestNode = self.router.closest_predecessor(self.node) client = WDHTClient(closestNode.ip, closestNode.port) if (t==0): closestNode = client.closest_node_cr(self.node.id) else: closestNode = client.closest_node_ccr(self.node.id) if closestNode is None: logging.debug("The closest node was None") if closestNode.id != self.node.id: self.router.update([closestNode]) if (len(L)==1): neighbors = self.router.neighbor_set.get_neighbors() if (t==0): closestNode = self.router.closest_successor(self.node) else: closestNode = self.router.closest_predecessor(self.node) if (closestNode.id != self.node.id): client = WDHTClient(closestNode.ip, closestNode.port) cur = client.gossip_neighbors(self.node, neighbors) cur.remove(self.node) self.router.neighbor_set.update(cur)
def prv_maintain_neighbors(self): """ Performs periodic neighbor maintenance by gossiping """ def helper(L, t): """ L is the list and t is 0 for cw and 1 for ccw. If L has no elements, finds the closest node by looking at all the elements on the table. If L has one element, finds the closest node by gossiping on the other existing neighbor """ closestNode = None if (len(L)==0): if (t==0): closestNode = self.router.closest_successor(self.node) else: closestNode = self.router.closest_predecessor(self.node) client = WDHTClient(closestNode.ip, closestNode.port) if (t==0): closestNode = client.closest_node_cr(self.node.id) else: closestNode = client.closest_node_ccr(self.node.id) if closestNode is None: logging.debug("The closest node was None") if closestNode.id != self.node.id: self.router.update([closestNode]) if (len(L)==1): neighbors = self.router.neighbor_set.get_neighbors() if (t==0): closestNode = self.router.closest_successor(self.node) else: closestNode = self.router.closest_predecessor(self.node) if (closestNode.id != self.node.id): client = WDHTClient(closestNode.ip, closestNode.port) cur = client.gossip_neighbors(self.node, neighbors) cur.remove(self.node) self.router.neighbor_set.update(cur) # Find out who is dead. neighbors = self.router.neighbor_set.get_neighbors() isDead = [] for node in neighbors: client = WDHTClient(node.ip, node.port) try: cur = client.gossip_neighbors(self.node,neighbors) to_update = [node for node in cur if node.id != self.node.id] self.router.neighbor_set.update(to_update) except TTransportException as e: isDead.append(node) logging.exception(e) isDead = unique(isDead) if (len(isDead)>0): logging.debug("Removing the neighbors %s"%( ', '.join([ str(x.int_id) for x in isDead]))) self.router.remove([x for x in isDead]) ### Add the proper node to each side if we are mising a node. logging.debug("Going to do CW side") (cw,ccw) = self.router.neighbor_set.get_candidate_list() tries =0 while (len(cw)<2) and tries<2: helper(cw, 0) (cw,ccw) = self.router.neighbor_set.get_candidate_list() tries +=1 logging.debug("Going to do CCW side") tries = 0 while (len(ccw)<2) and tries<2: helper(ccw,1) (cw,ccw) = self.router.neighbor_set.get_candidate_list() tries +=1 logging.info("Done Maintaining Neighbors") self.router.neighbor_set.debug()