예제 #1
0
  def stabilize (self):
    """
      implements stabilize code of Chord's paper, fig 6
      with some error handling
      returns: naught
    """
    self.debug (1, "stab!")
    # * we must have a head to talk to
    succ= self.getSucc ()
    if succ:
      self._succLock.acquire ()
      self.debug (2, "stab-2!")
      try:
        (url, key)= succ.pred ()
        self.debug (1, "stab-3! key: %s" % str(key))
        # None,None means he has none
        if key and ooBetween (self.key (), key, succ.key ()):
          # save it for restoring on exception
          self._prevSucc= succ
          succ= self._peers.getNavel (url, key)
          self.debug (1, "stab-4!")
          self.setSucc (succ)
          self.debug (1, "stab-5!")
          try:
            # do not give data to ourselves
            self.debug (1, 'notifying w/ %s' % succ)
            if succ.notify (self.params ()):
              self.keypass ()
          except UmbDead:
            self.debug (1, "%s:%s's dead, Jim!" % (url, key))
            # self.delNavel (key)
            # this one's dead; restore the previous one
            self.setSucc (self._prevSucc)
            # should we notify the old one?
        else:
          # don't care about this
          self.debug (1, 'notifying w/ %s' % succ)
          succ.notify (self.params ())

      except UmbDead:
        self.debug (1, "%s's dead, Jim!" % (self._succ.key ()))
        # self.delNavel (self._succ.key ())
        self.setSucc (None)

      self._succLock.release ()
      self.debug (1, 'stab finished')
예제 #2
0
  def notify (self, url, key):
    """
      implements chord's notify.
      returns: True if the notifying peer is now our pred, False otherwise
    """
    result= False
    self.debug (1, "notify!")
    self._predLock.acquire ()
    self.debug (2, "pred locked")
    pred= self.getPred ()
    prevPred= pred

    if not pred or ooBetween (pred.key (), key, self.key ()):
      # self.debug (1, "set %s,%s as new pred" % (url, key))
      pred= self._peers.getNavel (url, key)
      self.setPred (pred)

      # don't talk in the brain
      if not pred==self._self:
        # give our data so he makes a backup
        self.debug (1, 'giving data as backup')
        # this may fail
        try:
          given= self.giveData (self.key (), self._succ.key (), self.Set)
          pred.backupData (given)
          # shouldn't we also tell him to forget about anything else?
          # nope
          if prevPred:
            # no more business w/ him
            prevPred.close ()
          # gotta save it for later ref.
          self._prevPred= prevPred
        except UmbDead:
          self.debug (1, '\'tis dead, Jim!')
          self.setPred (prevPred)
      result= True

    self._predLock.release ()
    self.debug (2, "pred released")
    return result