def _initialize_finger_table(self): for i in range(self.m): id = (self.node.id + (2 ** i)) % (2 ** self.m) try: with grpc.insecure_channel(f'{self.connect_host}:{self.connect_port}') as channel: stub = API_pb2_grpc.APIStub(channel) pb_request_find_successor = API_pb2.RequestFindSuccessor(key=str(id)) pb_node = stub.FindSuccessor(pb_request_find_successor) node = Node(pb=pb_node) self.finger_table[i] = { 'id': id, 'succ': node } except Exception as e: print(e)
def _join(self): print(f'Joining: {self.connect_host} {self.connect_port}') while True: try: with grpc.insecure_channel(f'{self.connect_host}:{self.connect_port}') as channel: stub = API_pb2_grpc.APIStub(channel) pb_request_find_successor = API_pb2.RequestFindSuccessor(key=str(self.node.id)) pb_node_succ = stub.FindSuccessor(pb_request_find_successor) node_succ = Node(pb=pb_node_succ) self.finger_table[0]['succ'] = node_succ break except Exception as e: pass self._initialize_stabilize() self._initialize_fix_fingers()
def _find_successor(self, key): if self.node.predecessor is not None and self._in_interval(key, self.node.predecessor.id, self.node.id, inclusive_right=True): return self.node elif self._in_interval(key, self.node.id, self.finger_table[0]['succ'].id, inclusive_right=True): return self.finger_table[0]['succ'] else: preceding_node = self._closest_preceding_node(key) try: with grpc.insecure_channel(f'{preceding_node.host}:{preceding_node.port}') as channel: stub = API_pb2_grpc.APIStub(channel) pb_request_find_successor = API_pb2.RequestFindSuccessor(key=str(preceding_node.id)) pb_node = stub.FindSuccessor(pb_request_find_successor) node = Node(pb=pb_node) return node except Exception as e: print('Node not working') print(e) return None