Example #1
0
class WeightedUniform(Random):
    """
    The policy that just selects ramdon things
  """
    def vicesForNewFile(self, master):
        """
      returns: a list of sm's where a new file will reside.
    """
        totalFreeBlocks = 0
        index = 0
        statedVices = []

        vices = master.peers().vices()
        for key in vices.keys():
            vice = vices[key]
            try:
                fsstat = vice.statfs()
                blocks = fsstat[0]
                bfree = fsstat[1]

                totalFreeBlocks += bfree

                self.debug(2, 'usage: %f' % ((1.0 * bfree) / blocks))
                statedVices.append((vice, totalFreeBlocks))
            except Exception, e:
                pass

        i = random(totalFreeBlocks)
        index = 0

        # now check where has it fell
        vice = None
        item = next(statedVices)
        if item:
            (vice, index) = item
            self.debug(
                2, 'i: %d; tfb: %d; index: %d' % (i, totalFreeBlocks, index))
        while item is not None and i > index:
            item = next(statedVices)
            if item:
                (vice, index) = item
                self.debug(
                    2,
                    'i: %d; tfb: %d; index: %d' % (i, totalFreeBlocks, index))

        self.debug(1, 'vice: %s' % vice)
        return (vice, [])
Example #2
0
 def vicesForIno (self, ino):
   urls= self.getData (ino)
   ok= next (urls)
   if ok:
     vices= []
     for url in urls:
       vices.append (self.peers.getVice (url))
     self.debug (1, 'ino %d vices: %s' % (ino, vices))
     return vices
   else:
     return None
Example #3
0
 def vicesForIno(self, ino):
     urls = self.getData(ino)
     ok = next(urls)
     if ok:
         vices = []
         for url in urls:
             vices.append(self.peers.getVice(url))
         self.debug(1, 'ino %d vices: %s' % (ino, vices))
         return vices
     else:
         return None
Example #4
0
  def giveData (self, m, n, what=Set):
    """
      returns: a hash
    """
    self.debug (1, "giveData called", 2)

    keys= self._data.keys ()
    keys.sort ()
    self.debug (1, 'my know keys are: %s' % keys)
    h= {}

    nextKey= next (keys)
    while not nextKey==None:
      self.debug (2, "m: %d, k: %d, n: %d" % (m, nextKey, n))
      if (what==self.Set and coBetween (m, nextKey, n)) or (what==self.Complement and coBetween (n, nextKey, m)):
        h[nextKey]= self._data.getValues (nextKey)

      nextKey= next (keys)

    if what==self.Set:
      self.debug (1, 'giving Set %d:%d %s' % (m, n, h.keys ()))
    else:
      self.debug (1, 'giving Complement %d:%d %s' % (n, m, h.keys ()))
    return h
Example #5
0
  def addServer (self, client):
    # this server answers to that client
    peer= None
    server= None
    # this ReqSock will be thrown away.
    socket= RequestSocket (client)
    socket.write (['hit here'])

    args= socket.read ()
    self.debug (1, "args are: %s" % str(args))
    what= next (args)
    if what:
      if what=='i am':
        try:
          url= args[0]
          self.debug (1, "%s" % url.__class__)
          if not isinstance (url, URL):
            url= URL (url)
          proto= url.proto ()
          if proto=='umbie':
            key= int (args[1])
            peer= self.getNavel (str(url), key)
          elif proto=='vice':
            peer= self.getVice (str(url))
          elif proto=='virtue':
            peer= self.getVirtue (str(url))
          else:
            # bark!
            pass
        except NoParse:
          socket.write (['bye'])
          socket.close ()
      else:
        socket.write (['bye'])
        socket.close ()

    if peer:
      socket.write (['ok'])
      # create a Server of the correct class
      # as defined in serverType
      # TODO: I could pass the ReqSock() instead of the socket()
      server= self._master._serverType (self._master, client)
      # this server is the one answering to that client
      peer.setServer (server)
      server.start ()
    else:
      self.debug (1, 'no peer!')
    self.debug (1, 'server %s added' % peer)
Example #6
0
    def propalate(self):
        navels = self._peers.navels()
        keys = navels.keys()
        keys.sort()
        m = next(keys)
        # just in case there's no more than one navel
        n = m
        # just in case there is more than one navel
        first = m
        for n in keys:
            peer = navels[m]
            self.debug(1, "propalating to %s" % peer)
            self._ring.addData(self.giveData(m, n), n, peer)
            m = n

        # the last one is the one with the WAS.
        peer = navels[m]
        self.debug(1, "propalating to %s" % peer)
        self._ring.addData(self.giveData(m, first), first, peer)
Example #7
0
  def propalate (self):
    navels= self._peers.navels ()
    keys= navels.keys ()
    keys.sort ()
    m= next (keys)
    # just in case there's no more than one navel
    n= m
    # just in case there is more than one navel
    first= m
    for n in keys:
      peer= navels[m]
      self.debug (1, "propalating to %s" % peer)
      self._ring.addData (self.giveData (m, n), n, peer)
      m= n

    # the last one is the one with the WAS.
    peer= navels[m]
    self.debug (1, "propalating to %s" % peer)
    self._ring.addData (self.giveData (m, first), first, peer)
Example #8
0
    def run(self):
        """
      main loop that should take care of from-the-bottom termination.
      returns: once the client has quited.
    """
        try:
            args = self.read()
            # main loop quited or EOF
            self.finished = self._master._terminate or args == []
            while not self.finished:
                if args:
                    # it might not read anything
                    try:
                        what = next(args)
                        self.finished = self.attend(what, args)
                    except:
                        (e, v, tb) = sys.exc_info()
                        self.debug(5, 'exception caugh in server: %s' % e)
                        print_exception(e, v, tb)
                        self.debug(5, '---')
                        self.write(['error'])
                        self.finished = False

                if not self.finished:
                    args = self.read()
                    # recalculate finished condition
                    # main loop quited or EOF
                    self.finished = self._master._terminate or args == []

        except:
            self.debug(5, 'exception caugh out server: %s' % e)
            (e, v, tb) = sys.exc_info()
            print_exception(e, v, tb)

        # he quited, or terminated by main loop, remove ourselves from the system
        self._dead = True
        self.close()
        self.debug(1, "dying")
        self._master.delServer(self)
Example #9
0
  def run (self):
    """
      main loop that should take care of from-the-bottom termination.
      returns: once the client has quited.
    """
    try:
      args= self.read ()
      # main loop quited or EOF
      self.finished= self._master._terminate or args==[]
      while not self.finished:
        if args:
          # it might not read anything
          try:
            what= next (args)
            self.finished= self.attend (what, args)
          except:
            (e, v, tb)= sys.exc_info ()
            self.debug (5, 'exception caugh in server: %s' % e)
            print_exception (e, v, tb)
            self.debug (5, '---')
            self.write (['error'])
            self.finished= False

        if not self.finished:
          args= self.read ()
          # recalculate finished condition
          # main loop quited or EOF
          self.finished= self._master._terminate or args==[]

    except:
      self.debug (5, 'exception caugh out server: %s' % e)
      (e, v, tb)= sys.exc_info ()
      print_exception (e, v, tb)

    # he quited, or terminated by main loop, remove ourselves from the system
    self._dead= True
    self.close ()
    self.debug (1, "dying")
    self._master.delServer (self)