Beispiel #1
0
def get_replica_buckets(dest_host, dest_port_low, dest_port_high, timeout,
                        key):
    viewleader_sock = common_functions.create_connection(
        dest_host, dest_port_low, dest_port_high, timeout, True)
    view, epoch = get_viewleader_info(viewleader_sock)
    viewleader_sock.close()
    replica_buckets = DHT.bucket_allocator(key, view)
    return replica_buckets
    def process_msg(self, recvd_msg, addr, sock):
        function_from_cmd = recvd_msg[
            "cmd"]  # takes function arguments from received dict

        if (function_from_cmd == 'query_servers'):
            common_functions.send_msg(sock,
                                      viewleader_rpc.query_servers(self.epoch),
                                      False)
        elif (function_from_cmd == 'heartbeat'):
            new_id = recvd_msg["args"][0]
            port = recvd_msg["args"][1]  # src port
            viewleader_rpc.heartbeat(new_id, port, addr, sock)
        elif (function_from_cmd == 'lock_get'):
            lock_name = recvd_msg["lock_name"]
            requester_id = recvd_msg["requester_id"]

            if (viewleader_rpc.lock_get(lock_name, requester_id) == True):
                common_functions.send_msg(sock, "{'status': 'granted'}", False)
            else:
                common_functions.send_msg(sock, "{'status': 'retry'}", False)
                print("Sending retry message to client.")
        elif (function_from_cmd == 'lock_release'):
            lock_name = recvd_msg["lock_name"]
            requester_id = recvd_msg["requester_id"]
            if (viewleader_rpc.lock_release(lock_name, requester_id) == True):
                common_functions.send_msg(sock, "{'status': 'ok'}", False)
            else:
                common_functions.send_msg(sock, "{'status': 'not ok'}", False)
        elif (function_from_cmd == 'get_buckets'):
            key = recvd_msg["key"]
            replica_buckets = DHT.bucket_allocator(key, viewleader_rpc.view)
            common_functions.send_msg(sock, replica_buckets, False)
        elif (function_from_cmd == 'update_view'):
            curr_epoch = self.update_view()
            common_functions.send_msg(sock, {'Current Epoch': curr_epoch},
                                      False)
        elif (function_from_cmd == 'rebalance'):
            msg = recvd_msg['msg']
            print(msg)
        else:
            print("Rejecting RPC request because function is unknown.")
Beispiel #3
0
    def rebalance(self, new_view, old_view, epoch_op):
        key_to_delete = ''

        global key_value_replica
        global data_in_view

        for [[addr, port], server_id] in new_view:
            try:
                sock = common_functions.create_connection(
                    addr, port, port, 5, False)
            except Exception as e:
                print("Couldn't establish a connection with replica: ", e)
            common_functions.send_msg(sock, {'cmd': 'get_data'}, False)
            recvd_msg = common_functions.recv_msg(sock, False)
            if (recvd_msg is not None):
                for key, value in recvd_msg.items():
                    if (key not in data_in_view):
                        with self.lock:
                            data_in_view[key] = value
            sock.close()

        for key, value in data_in_view.items():
            old_replicas = DHT.bucket_allocator(key, old_view)
            new_replicas = DHT.bucket_allocator(key, new_view)

            for [[addr, port], server_id] in new_replicas:
                try:
                    sock = common_functions.create_connection(
                        addr, port, port, 5, False)
                except Exception as e:
                    print("Couldn't establish a connection with replica: ", e)
                common_functions.send_msg(sock, {
                    'cmd': 'get_data',
                    'key': key
                }, False)
                recvd_msg = common_functions.recv_msg(sock, False)
                if (recvd_msg is not None) or (recvd_msg != ''):
                    key_value_replica = recvd_msg
                sock.close()

            with self.lock:
                # print (key_value_replica)
                try:
                    new_key, new_value = key_value_replica
                    if (new_key not in self.bucket):
                        try:
                            self.bucket[new_key] = new_value
                            print("Adding {}:{} to current replica...".format(
                                new_key, new_value))
                        except LookupError as e:
                            print(
                                "Couldn't set the key since there was no such key..."
                            )
                    else:
                        if (epoch_op == 'add'):
                            if (old_view is not None):
                                # print ("Old view: {}".format(old_view))
                                # print ("New view: {}".format(new_view))
                                # print ("unique_id: {}".format(self.unique_id))
                                for [[addr, port], server_id] in old_view:
                                    # print ("tuple: {}".format([[addr, port], server_id]))
                                    if (self.unique_id == server_id) and ([[
                                            addr, port
                                    ], server_id] not in new_view):
                                        print(
                                            "Deleting {}:{} on old replica...".
                                            format(new_key, new_value))
                                        key_to_delete = new_key
                    try:
                        del self.bucket[key_to_delete]
                    except LookupError:
                        print(
                            "Couldn't delete the key since there was no such key..."
                        )
                except Exception as e:
                    print("No key_value found: ", e)
Beispiel #4
0
    def process_msg_from_client(self, recvd_msg):
        function_from_cmd = recvd_msg[
            "cmd"]  # takes function arguments from received dict

        try:
            unique_id = recvd_msg["id"]  # takes unique_id from received dict
        except LookupError:
            unique_id = None

        # checks if function is print; if so, then take the text from the received dict
        if (function_from_cmd == "print"):
            text = recvd_msg["text"]  # print's argument
            # text length will always be > 0; if text length == 1,
            # then make text into string rather than a list.
            if (len(text) == 1):
                text = text[0]

            print("Printing " + str(text))
            response = {'status': 'success', 'result': text, 'id': unique_id}
        elif (function_from_cmd == "set"):
            server_rpc.set(recvd_msg["key"], recvd_msg["val"])
            response = {'status': 'success', 'id': unique_id}
        elif (function_from_cmd == "get"):
            result = server_rpc.get(recvd_msg["key"])
            if (result is None):
                response = {
                    'status': 'fail',
                    'result': result,
                    'id': unique_id
                }
            else:
                response = {
                    'status': 'success',
                    'result': result,
                    'id': unique_id
                }
        elif (function_from_cmd == "query_all_keys"):
            result = server_rpc.query_all_keys()
            if (result is None):
                response = {
                    'status': 'fail',
                    'result': result,
                    'id': unique_id
                }
            else:
                response = {
                    'status': 'success',
                    'result': result,
                    'id': unique_id
                }
        elif (function_from_cmd == "get_id"):
            # print ("Returning service_id to viewleader...".format(self.unique_id))
            response = str(self.unique_id)
        elif (function_from_cmd == "getr"):
            with self.lock:
                key = recvd_msg["key"]
                try:
                    response = {
                        'status': 'success',
                        'result': self.bucket[key],
                        'id': unique_id
                    }
                except LookupError:
                    response = {
                        'status': 'fail',
                        'result': '',
                        'id': unique_id
                    }
        elif (function_from_cmd == "setr"):
            with self.lock:
                key = recvd_msg["key"]
                value = recvd_msg["val"]
                try:
                    self.bucket[key] = value
                    print("Stored {}:{} in this replica.".format(key, value))
                    response = {'status': 'success', 'id': unique_id}
                except LookupError:
                    response = {'status': 'fail', 'id': unique_id}
        elif (function_from_cmd == "request_vote"):
            key = recvd_msg["key"]
            if (key not in self.in_commit_phase):
                self.in_commit_phase.append(key)
                # print ("Added key to in_commit_phase: {}".format(self.in_commit_phase))
                viewleader_epoch = recvd_msg["epoch"]
                server_id = uuid.UUID(recvd_msg["server_id"]).hex
                response = self.vote(viewleader_epoch, server_id)
            else:
                print("Aborting because there is a pending commit...")
                response = 'abort'
            print("Sending vote back to client...")
        elif (function_from_cmd == "remove_commit"):
            try:
                key = recvd_msg["key"]
            except LookupError:
                print("No such key.")
            # print ("Keys in in_commit_phase: {}".format(self.in_commit_phase))
            # print ("Key: {}".format(key))
            try:
                self.in_commit_phase.remove(key)
            except ValueError:
                print("Key is not currently being committed.")
            response = {'status': 'success', 'id': unique_id}
        elif (function_from_cmd == "get_data"):
            try:
                key = recvd_msg["key"]
                try:
                    response = (key, self.bucket[key])
                except LookupError as e:
                    response = ''
            except LookupError:
                response = self.bucket
                # print ("Couldn't find the key...")
        elif (function_from_cmd == "rebalance"):
            epoch_op = recvd_msg['op']
            new_view = recvd_msg['new_view']
            old_view = recvd_msg['old_view']

            for key, value in self.bucket.items():
                old_replicas = DHT.bucket_allocator(key, old_view)
                new_replicas = DHT.bucket_allocator(key, new_view)

            thread = threading.Thread(target=self.rebalance,
                                      args=(new_view, old_view, epoch_op))
            thread.start()
            response = ''
        else:
            print("Rejecting RPC request because function is unknown.")
        return response