Beispiel #1
0
  def join(self, newnode) :
    # Base case: First node
    if self.__start is None :
      self.__start = newnode
      self.__start.fingers = [newnode]*len(self.__start.fingers)
      self.__start.initialized = True
      return

    predecessor = dyschord.find_predecessor(self.__start, newnode.id)
    successor = predecessor.next
    if successor.id == newnode.id :
      raise Exception("Node already exists with same id")
    successor.prepend_node(newnode)

    # Optimize fingers.  Don't need to lock the nodes while this is
    # being done.
    newnode.update_fingers()
    dyschord.announce(newnode)
Beispiel #2
0
  def leave(self, node) :
    if self.num_nodes() == 0 :
      return

    # Note I am looking up, because then I can get a node to leave by
    # passing in another instance with the same id.  Might be useful
    # for testing.
    predecessor = dyschord.find_predecessor(self.__start, node.id)
    # print "Leaving node %d has predecessor %d" % (node.id, predecessor.id)
    if predecessor.next.id != node.id :
      # No joined node with this id.  Maybe log the missing node, but
      # work is done
      return
    
    leaving = predecessor.next
    # Check we aren't removing first item
    if leaving.id == self.__start.id :
      self.__start = leaving.next
    leaving.leave()