Пример #1
0
        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) 
Пример #2
0
    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()