def setr(key, value, dest_host, dest_port_low, dest_port_high, timeout): try: viewleader_sock = common_functions.create_connection(dest_host, dest_port_low, dest_port_high, timeout, True) active_servers, epoch = get_viewleader_info(viewleader_sock) except Exception as e: print ("Couldn't establish a connection with viewleader: ", e) viewleader_sock.close() try: viewleader_sock = common_functions.create_connection(dest_host, dest_port_low, dest_port_high, timeout, True) replica_buckets = get_replica_buckets(viewleader_sock, {'cmd': 'get_buckets', 'key': key, 'val': value}) print ("Replicas : {}".format(replica_buckets)) except Exception as e: print ("Couldn't establish a connection with viewleader: ", e) viewleader_sock.close() length_of_bucket = len(replica_buckets) if (length_of_bucket == 0): return "Cannot store value because no servers are available." else: if (distributed_commit(replica_buckets, key, dest_host, dest_port_low, dest_port_high, 3)): broadcast(replica_buckets, {'cmd': 'setr', 'key': key, 'val': value, 'id': 0}, epoch, 5) broadcast(replica_buckets, {'cmd': 'remove_commit', 'key': key, 'id': 0}, epoch, 5) return "Stored values in replica servers." else: broadcast(replica_buckets, {'cmd': 'remove_commit', 'key': key, 'id': 0}, epoch, 5) return "Cannot store value because one of the servers aborted."
def send_and_recv_heartbeat(self, src_port): # print ('Sending heartbeat msg to viewleader...') try: sock = common_functions.create_connection(self.view_leader_ip, 39000, 39010, 1, False) except Exception as e: print("Couldn't establish a connection with viewleader: ", e) common_functions.send_msg(sock, { 'cmd': 'heartbeat', 'args': [str(self.unique_id), src_port] }, False) recvd_msg = common_functions.recv_msg(sock, False) sock.close() if (recvd_msg is not None): status = recvd_msg[0] try: sock = common_functions.create_connection(self.view_leader_ip, 39000, 39010, 1, False) except Exception as e: print("Couldn't establish a connection with viewleader: ", e) common_functions.send_msg(sock, {'cmd': 'update_view'}, False) curr_epoch = common_functions.recv_msg(sock, False)['Current Epoch'] # print ("Updating our epoch to {}...".format(curr_epoch)) self.epoch = curr_epoch sock.close()
def getr(key, dest_host, dest_port_low, dest_port_high, timeout): try: viewleader_sock = common_functions.create_connection(dest_host, dest_port_low, dest_port_high, timeout, True) except Exception as e: print ("Couldn't establish a connection with viewleader: ", e) active_servers, epoch = get_viewleader_info(viewleader_sock) viewleader_sock.close() viewleader_sock = common_functions.create_connection(dest_host, dest_port_low, dest_port_high, timeout, True) replica_buckets = get_replica_buckets(viewleader_sock, {'cmd': 'get_buckets', 'key': key}) viewleader_sock.close() response = broadcast(replica_buckets, {'cmd': 'getr', 'key': key, 'id': 0}, epoch, 5) return response
def accept_and_handle_messages(self, bound_socket, src_port, view_leader_ip): # Accept connections forever while True: try: sock, (addr, accepted_port) = bound_socket.accept( ) # Returns the socket, address and port of the connection if (accepted_port is not None): # checks if there is an accepted_port print("Accepting connection from host " + addr) # receives decoded message length and message from client; if can't throw's an error try: recvd_msg = common_functions.recv_msg(sock) except ConnectionResetError: print("Connection dropped.") except AttributeError: print("Cannot decode message.") response_dict = self.process_msg_from_client(recvd_msg) # sends encoded message length and message to client; if can't throw's an error try: common_functions.send_msg(sock, response_dict) except: print("Can't send over whole message.") sock.close() except socket.timeout: try: print('Sending heartbeat msg to viewleader...') sock = common_functions.create_connection( 'localhost', 39000, 39010, 1, False) try: common_functions.send_msg( sock, { 'cmd': 'heartbeat', 'args': [ str(self.unique_id), src_port, self.view_leader_ip ] }) except: print("Can't send over whole message.") sock.close() try: recvd_msg = common_functions.recv_msg(sock) except ConnectionResetError: print("Connection dropped.") except AttributeError: print("Cannot decode message.") print('Receiving response...') if (recvd_msg is not None): print(str(recvd_msg)) sock.close() except Exception as e: print( 'Heartbeat rejected, will try again in 10 seconds...') continue
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 start(self): args = self.parse_cmd_arguments() # if the optional argument "--server" is used, # then set localhost as this computer's IP. else, return error and exit. if (args.server is not None): if (args.cmd is None): print ("RPC command not provided.") sys.exit() viewleader_args = ['query_servers', 'lock_get', 'lock_release'] self.timeout = 1 # sets destination port ranges and destination hosts based on the RPC functions called if (args.cmd in viewleader_args): self.dest_host = str(args.viewleader) self.dest_port_low = 39000 self.dest_port_high = 39010 self.dest_name = 'viewleader' elif (args.cmd == 'getr' or args.cmd == 'setr'): self.dest_host = str(args.viewleader) self.dest_port_low = 39000 self.dest_port_high = 39010 self.dest_name = 'viewleader_and_server' else: self.dest_host = str(args.server) self.dest_port_low = 38000 self.dest_port_high = 38010 self.dest_name = 'server' args_dict = self.create_dict(args) stop = False sock = None if (self.dest_name == 'viewleader_and_server'): stop = True if (args.cmd == 'getr'): print (client_rpc.getr(args.key, self.dest_host, self.dest_port_low, self.dest_port_high, self.timeout)) else: print (client_rpc.setr(args.key, args.val, self.dest_host, self.dest_port_low, self.dest_port_high, self.timeout)) while (stop == False): sock = common_functions.create_connection(self.dest_host, self.dest_port_low, self.dest_port_high, self.timeout, True) # sends encoded message length and message to server/viewleader; if can't throw's an error common_functions.send_msg(sock, args_dict, True) recvd_msg = common_functions.recv_msg(sock, True) if (recvd_msg == "{'status': 'retry'}"): print (str(recvd_msg)) time.sleep(5) # delays for 5 seconds and then tries again else: print (str(recvd_msg)) stop = True if (sock is not None): sock.close() sys.exit()
def run_consensus_alg(self, sock, cmd, args): proposal_num = len(self.log) # proposal number is equivalent to length of log msg = {'cmd': 'prepare', 'proposal_num': proposal_num} # prepare message; Phase 1 print ("Broadcasting prepare message to replicas...") responses = self.broadcast(msg, self.view_leader_list) if (self.has_quorum(responses)): for response in responses: status = None num_logs_replica_needs = None logs_leader_is_missing = None try: replica_addr = response['addr'] replica_port = response['port'] status = response['status'] if (status == 'ok'): status = 'accept' # accept message; Phase 2 else: status = 'reject' except Exception: continue try: num_logs_replica_needs = response['num_logs_replica_needs'] except Exception: pass try: logs_leader_is_missing = response['logs_leader_is_missing'] except Exception: pass # if there are logs that the replicas need, sends it to them. Also # sends updated proposal numbers and the requested command if (num_logs_replica_needs): print ("num_logs_replica_needs: {}".format(num_logs_replica_needs)) logs_to_replay = self.log[len(self.log) - num_logs_replica_needs:] print ("logs_to_replay: {}".format(len(logs_to_replay))) msg = {'cmd' : status, 'logs_replica_needs' : logs_to_replay, 'new_proposal_num': len(self.log), 'new_cmd': cmd, 'args': args} elif (logs_leader_is_missing): msg = {'cmd' : status, 'new_proposal_num': len(self.log), 'new_cmd': cmd, 'args': args} elif (status): msg = {'cmd' : status, 'new_proposal_num': len(self.log), 'new_cmd': cmd, 'args': args} if (status == 'accept'): sock = common_functions.create_connection(replica_addr, replica_port, replica_port, 1, False) common_functions.send_msg(sock, msg, False) if (sock): sock.close() return True else: return False
def rebalance(self, old_view, new_view, epoch_op): for ((addr, port), server_id) in new_view: try: server_sock = common_functions.create_connection( addr, port, port, None, False) except Exception as e: print("Couldn't establish a connection with replica: ", e) common_functions.send_msg( server_sock, { 'cmd': 'rebalance', 'old_view': old_view, 'new_view': new_view, 'op': epoch_op }, False) server_sock.close()
def broadcast(self, msg, replicas): responses = [] leader_hostname = self.leader[0] leader_port = str(self.leader[1]) for replica in replicas: addr, port = replica # checks to see if the replica has the same addr/port as the leader; if so, # don't broadcast to it if ((addr, port) != (leader_hostname, leader_port)): sock = common_functions.create_connection(addr, port, port, 1, False) if (sock): common_functions.send_msg(sock, msg, False) recvd_msg = common_functions.recv_msg(sock, False) if (recvd_msg): responses.append(recvd_msg) sock.close() return responses
def broadcast(replicas, object_to_send, epoch, timeout): response_key = None rpc_command = object_to_send['cmd'] abort = False votes = [] for ((addr, port), server_id) in replicas: if (rpc_command == 'request_vote'): object_to_send['epoch'] = epoch object_to_send['server_id'] = str(server_id) try: server_sock = common_functions.create_connection( addr, port, port, timeout, False) common_functions.send_msg(server_sock, object_to_send, False) if (rpc_command == 'request_vote'): vote = common_functions.recv_msg(server_sock, False) print("Accepting vote from " + addr) if (vote == 'abort'): abort = True return {'cmd': 'abort'} if (rpc_command == 'getr'): response_key = common_functions.recv_msg( server_sock, False ) # desired value associated with the given key from DHT if (response_key is not None): return response_key elif (rpc_command == 'setr'): response_key = common_functions.recv_msg(server_sock, False) server_sock.close() except socket.timeout: if (rpc_command == 'request_vote'): abort = True return {'cmd': 'abort'} if (sock is None): print( "Couldn't connect to current replica server...will continue on remaining replicas: " ) if (rpc_command == 'request_vote'): return {'cmd': 'commit'} if (response_key is not None): result = "No key found in any of the replica servers." response_key = {'status': 'fail', 'result': result} return response_key
def distributed_commit(replicas, key, dest_host, dest_port_low, dest_port_high, timeout): viewleader_sock = common_functions.create_connection( dest_host, dest_port_low, dest_port_high, timeout, True) active_servers, epoch = get_viewleader_info(viewleader_sock) viewleader_sock.close() votes_received = 0 votes_expected = len(active_servers) vote_request = {'cmd': 'request_vote', 'key': key} response = broadcast(replicas, vote_request, epoch, 5) vote = response['cmd'] # sending global abort if (vote == 'abort'): print("Commit failed. Aborting...") return False # sending global commit else: print("Commit succeeded.") return True
def rebalance(self, old_view, new_view, epoch_op): for ((addr, port), server_id) in new_view: server_sock = common_functions.create_connection(addr, port, port, None, False) common_functions.send_msg(server_sock, {'cmd': 'rebalance', 'old_view': old_view, 'new_view': new_view, 'op': epoch_op}, False) server_sock.close()
def start(self): args = self.parse_cmd_arguments() # if the optional argument "--server" is used, # then set localhost as this computer's IP. else, return error and exit. if (args.server is not None): if (args.cmd is None): print("RPC command not provided.") sys.exit() # sets destination port ranges and destination hosts based on the RPC functions called if (args.cmd == 'query_servers') or (args.cmd == 'lock_get') or ( args.cmd == 'lock_release'): dest_host = str(args.viewleader) dest_port_low = 39000 dest_port_high = 39010 timeout = 1 else: dest_host = str(args.server) dest_port_low = 38000 dest_port_high = 38010 timeout = 1 args_dict = self.create_dict(args) stop = False sock = None while (stop == False): sock = common_functions.create_connection(dest_host, dest_port_low, dest_port_high, timeout, True) try: print("Sending RPC msg to viewleader...") # sends encoded message length and message to server/viewleader; if can't throw's an error common_functions.send_msg(sock, args_dict) # receives decoded message length and message from server/viewleader; if can't throw's an error try: recvd_msg = common_functions.recv_msg(sock) if (recvd_msg == "{'status': 'retry'}"): print(str(recvd_msg)) time.sleep( 5) # delays for 5 seconds and then tries again else: print(str(recvd_msg)) stop = True except ConnectionResetError: print("Connection dropped.") sys.exit() except AttributeError: print("Cannot decode message.") if (sock is not None): sock.close() sys.exit() except Exception as e: print("Failed send over whole message.", e) if (sock is not None): sock.close() sys.exit() if (sock is not None): sock.close() sys.exit()
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)