Beispiel #1
0
    def initialise_hashring(self):
        """ Initialize the hash ring.
            If `self.remote_server` is not note, get it from remote_server
            or else just create a new one.
            Also set the hash_ring in `self.dht_table`
        """
        if self.remote_server:
            remote_host, remote_port = self.remote_server.split(":")
            remote_server = Server(remote_host, remote_port)
            # Send a join command to the existing server
            command = DHTCommand(DHTCommand.JOIN, self.this_server)
            ring = self.client.sendcommand(remote_server, command)
            logging.debug("got ring from server: %s", str(ring))
            # Get replicas
            if not ring:
                raise RuntimeError(
                    ("Could not reach server: %s" % str(remote_server)))
            nodes, replicas = ring.split(",")
            # Convert |-separated list to Server-instances
            nodes = map(
                lambda server_string: Server(
                    server_string.split(":")[0],
                    server_string.split(":")[1]), nodes.split("|"))
            # Initialize local hash ring
            self.hash_ring = HashRing(nodes, int(replicas))
            self.hash_ring.add_node(self.this_server)
        else:
            # First server so this server is added
            self.hash_ring = HashRing([self.this_server], self.replicas)

        # Initialize the hash map
        self.dht_table = MyDHTTable(self.this_server, self.hash_ring)
Beispiel #2
0
 def setUp(self):
     host = "localhost"
     self.ports = [50140]  #range(50140,50144)
     self.servers = []
     for port in self.ports:
         self.servers.append(Server(host, port))
     self.dht = MyDHTClient(True)
Beispiel #3
0
 def start(self,
           host,
           port,
           replicas,
           remote_server=None,
           is_process=False):
     """ Starts the server with `hostname`, `port`
         If `remove_server` is not None it will be contacted to join an existing ring
         `is_process` is used when starting servers as processes, it is used to exit
         in a way pyunit likes if True.
     """
     self.this_server = Server(host, port)
     self.remote_server = remote_server
     self.replicas = replicas
     self.is_process = is_process
     self.serve()
Beispiel #4
0
    def cmdlinestart(self):
        """ Parse command line parameters and start client
        """
        try:
            port = int( self.getarg("-p") or self.getarg("--port",50140))
            host = self.getarg("-h") or self.getarg("--hostname","localhost")
            key = self.getarg("-k") or self.getarg("--key")
            server = Server(host,port)
            command = self.getarg('-c') or self.getarg('--command')
            value = self.getarg("-val") or self.getarg("--value")
            file = self.getarg("-f") or self.getarg("--file")
            outfile = self.getarg("-o") or self.getarg("--outfile")

            logging.debug("command: %s %s %s %s", str(server), command, key, value)
            if command is None or server is None or file and value:
                self.help()
                
            command = self.get_command(command)
            if file:
                f = open(file, "rb")
                command = DHTCommand(command,key,f)
            else:
                command = DHTCommand(command,key,value)

            if outfile:
                # File was an argument, supply it
                with open(outfile, "wb") as out:
                    print self.sendcommand(server,command,out)
            else:
                # Print output
                data = self.sendcommand(server,command)
                if data and len(data) < 1024:
                    print data
                elif data:
                    print "Return data is over 1K, please use the -o option to redirect it to a file"
            if file:
                f.close()
        except TypeError:
            self.help()
Beispiel #5
0
    def cmdlinestart(self):
        """ Parse command line parameters and start client
        """
        try:
            if self.getopt("--help"):
                self.help()
            port = int(self.getarg("-p") or self.getarg("--port", 50140))
            host = self.getarg("-h") or self.getarg("--hostname", "localhost")
            server = Server(host, port)
            no_serves = self.getarg("-n") or self.getarg("--no_servers", 5)
            no_serves = int(no_serves)
            remoteserver = self.getarg("-s") or self.getarg("-server")
            if not remoteserver:
                remoteserver = str(server)
                print self.get_start_string(host, port)
            else:
                print self.get_start_string(host, port, remoteserver)

            for i in range(1, no_serves):
                print self.get_start_string(host, port + i, remoteserver)

        except TypeError:
            self.help()
Beispiel #6
0
    def add_new_node(self, new_node):
        """ Adds a new server to all existing nodes and
            returns a |-separated list of the current ring
            ","  the number of replicas used.
            Example:
            localhost:50140|localhost:50141,3
        """
        self.ring_lock.acquire()
        logging.debug("adding: %s", new_node)
        host, port = new_node.split(":")
        newserver = Server(host, port)
        self.hash_ring.remove_node(newserver)

        # Add the new server to all existing nodes
        command = DHTCommand(DHTCommand.ADDNODE, newserver)
        self.forward_command(command)

        # Convert to a string list
        ring = map(lambda serv: str(serv), self.hash_ring.get_nodelist())
        # Add new server to this ring
        self.hash_ring.add_node(newserver)
        # Return |-separated list of nodes
        self.ring_lock.release()
        return "|".join(ring) + "," + str(self.hash_ring.replicas)