def ReplicateRejectMatch(self, request, context): key = self.CH.hash(request.recruiter.username) print(f'ReplicateRejectMatch - Recruiter Server {SERVER_ID} / Username {request.recruiter.username} / Username key: {key}') node = self.CH.find_successor(key) if node.server_id != SERVER_ID: print(f'ReplicateRejectMatch - Redirected to server {node.server_id}') try: with grpc.insecure_channel(f'{node.host}:{node.port}') as channel: stub = API_pb2_grpc.APIStub(channel) return stub.ReplicateRejectMatch(request) except Exception as e: print(e) else: print(f'ReplicateRejectMatch - Server {SERVER_ID} executing the action') empty = API_pb2.Empty() matches.reject_match(self.LSMT, request, context) return empty
def ReplicateMessage(self, request, context): print(f'ReplicateMessage - Server {SERVER_ID} executing the action') messages.send_message(self.LSMT, request.message, context) replicated_message = request nodes = self.CH.get_reachable_nodes() for node in nodes: exists = next((x for x in replicated_message.nodes if x.id == str(node.id)), None) if not exists: pb_node = replicated_message.nodes.add() pb_node.id = str(node.id) pb_node.server_id = node.server_id try: with grpc.insecure_channel(f'{node.host}:{node.port}') as channel: stub = API_pb2_grpc.APIStub(channel) stub.ReplicateMessage(replicated_message) except Exception as e: print(e) continue empty = API_pb2.Empty() return empty
def _stabilize(self): skip = False successor = self.finger_table[0]['succ'] if not skip: with grpc.insecure_channel(f'{successor.host}:{successor.port}') as channel: stub = API_pb2_grpc.APIStub(channel) pb_node = stub.GetPredecessor(API_pb2.Empty()) x = Node(pb=pb_node) # print(f"In stabilize receive predecessor {x.server_id} from successor {successor.server_id}") if x.id == -1: skip = True if not skip: if x is not None and self._in_interval(x.id, self.node.id, successor.id): self.finger_table[0]['succ'] = x self._notify(self.finger_table[0]['succ']) t = Timer(ConsistentHashing.STABILIZE_TIME, self._stabilize) t.start()
def Notify(self, request, context): self.CH.notify(request) return API_pb2.Empty()