def run(Addr): start = time.time() # TODO: Implement PUT and GET myKey = "1" * 1024 myValue = "1" * 1024 start = time.time() seq_num = 1 for _ in range(100): try: while True: channel = grpc.insecure_channel(Addr) stub = server_pb2_grpc.ServerStub(channel) # print("Try to put") stub.put(server_pb2.PutRequest(key=myKey, value=myValue)) except Exception as e: print(e) seq_num += 1 print(time.time() - start) start = time.time() try: while True: channel = grpc.insecure_channel(Addr) stub = server_pb2_grpc.ServerStub(channel) print("Try to get") stub.get(server_pb2.GetRequest(key=myKey)) except Exception as e: print(e) print(time.time() - start)
def getReadings(self): with grpc.insecure_channel('127.0.0.1:50050') as channel: stub = server_pb2_grpc.ServerStub(channel) request = server_pb2.FetchRequest(s1="Test") readings = [] for reading in stub.Fetch(request): readings.append(reading) return readings
def find_successor(addr, key): find_request = server_pb2.FindSucRequest(id=key, inc_id=0, inc_ip="127.0.0.1:7000") channel = grpc.insecure_channel(addr) stub = server_pb2_grpc.ServerStub(channel) find_resp = stub.find_successor(find_request) print(find_resp.id) print(find_resp.ip)
def init_rectify(self): while True: with self.rectify_cond: self.rectify_cond.wait() rectify_request = server_pb2.RectifyRequest(id=self.id, ip=self.local_addr) channel4 = grpc.insecure_channel(self.successor_list[0][1]) # grpc.channel_ready_future(channel4).result() stub4 = server_pb2_grpc.ServerStub(channel4) rectify_resp = stub4.rectify(rectify_request, timeout=self.GLOBAL_TIMEOUT) if rectify_resp.ret == server_pb2.SUCCESS: self.logger.debug(f'[Rectify]: rectified <{self.successor_list[0][0]}> with <{self.id}>')
def init_find_successorlist(self, id, ip, need_pred): find_sl_request = server_pb2.FindSucclistRequest(inc_id=self.id, inc_ip=self.local_addr) channel = grpc.insecure_channel(ip) # grpc.channel_ready_future(channel).result() stub = server_pb2_grpc.ServerStub(channel) pred_resp = 0 if need_pred: empty_request = server_pb2.EmptyRequest() pred_resp = stub.find_predecessor(empty_request, timeout=self.GLOBAL_TIMEOUT) find_sl_resp = stub.find_succlist(find_sl_request, timeout=self.GLOBAL_TIMEOUT) return find_sl_resp, pred_resp
def getNodeStatus(addr): find_request = server_pb2.EmptyRequest() channel = grpc.insecure_channel(addr) stub = server_pb2_grpc.ServerStub(channel) find_resp = stub.get_node_status(find_request) print("id:\t", find_resp.id) print("ip:\t", find_resp.ip) print("pred_id:\t", find_resp.pred_id) print("pred_ip:\t", find_resp.pred_ip) print("suclist:") for i in range(len(find_resp.suclist_id)): print(find_resp.suclist_id[i], find_resp.suclist_ip[i]) print("finger table:") for i in range(len(find_resp.finger_id)): print(find_resp.finger_id[i], find_resp.finger_ip[i])
def check_predecessor(self): while True: # self.logger.debug("[Check pred]") try: check_request = server_pb2.PredecessorRequest(id=self.id) channel = grpc.insecure_channel(self.predecessor[1]) # grpc.channel_ready_future(channel).result() stub = server_pb2_grpc.ServerStub(channel) check_resp = stub.live_predecessor(check_request, timeout=self.GLOBAL_TIMEOUT) if check_resp.ret != server_pb2.SUCCESS: self.predecessor = [-1, ""] except Exception: self.predecessor = [-1, ""] with self.check_pred_cond: self.check_pred_cond.wait(self.CHECKPRE_PERIOD / 1000.0)
def init_find_successor(self, id, ip): # TODO: find successor for first node find_request = server_pb2.FindSucRequest(id=id, inc_id=self.id, inc_ip=self.local_addr) if self.only_node_phase == 1: self.only_node_phase = 2 find_request.id = -1 self.logger.debug(f'[First node]: find_request <{self.id}> == -1?') elif self.id == self.successor_list[0][0]: self.logger.debug(f'[First node]: successor_list[0] <{self.successor_list[0]}>') return self.successor_list[0] channel = grpc.insecure_channel(ip) # grpc.channel_ready_future(channel).result() stub = server_pb2_grpc.ServerStub(channel) find_resp = stub.find_successor(find_request, timeout=self.GLOBAL_TIMEOUT) return [find_resp.id, find_resp.ip]
def rectify(self, request, context): if self.predecessor[0] == -1: self.predecessor = [request.id, request.ip] self.logger.debug(f'[Rectify]: Pred is now id: <{request.id}> ip: <{request.ip}>, no pred') else: try: # query pred to see if live check_request = server_pb2.PredecessorRequest(id=self.id) channel = grpc.insecure_channel(self.predecessor[1]) # grpc.channel_ready_future(channel).result() stub = server_pb2_grpc.ServerStub(channel) resp = stub.live_predecessor(check_request, timeout=self.GLOBAL_TIMEOUT) if resp.ret == server_pb2.SUCCESS and self.between(self.predecessor[0], request.id, self.id): self.predecessor = [request.id, request.ip] # self.logger.debug(f'[Rectify]: Predecessor is now id: <{request.id}>, between ' # f'<{self.predecessor[0]}> <{request.id}> <{self.id}>') else: self.predecessor = [request.id, request.ip] except Exception as e: self.predecessor = [request.id, request.ip] self.logger.debug(f'[Rectify]: Predecessor is now id: <{request.id}> ip: <{request.ip}>, old dead' f'\nerror: <{e}>') return server_pb2.RectifyResponse(ret=server_pb2.SUCCESS)
def replicate_entries(self, request, context): for row in request.entries: r = [[row.key, row.val]] self.logs += r threading.Thread(target=self.applyToStateMachine, args=()) return server_pb2.ReplicateResponse(ret=server_pb2.SUCCESS) def thread_send_replicate(self, id, ip, req): try: <<<<<<< HEAD while True: # TODO: this need to be done # check_request = server_pb2.ReplicateRequest channel = grpc.insecure_channel(ip) # grpc.channel_ready_future(channel).result() stub = server_pb2_grpc.ServerStub(channel) replicate_resp = stub.replicate_entries(req, timeout=self.GLOBAL_TIMEOUT) if not replicate_resp.SUCCESS: with self.stabilize_cond: self.stabilize_cond.notify() else: return except Exception: pass ======= # TODO: this need to be done # check_request = server_pb2.ReplicateRequest channel = grpc.insecure_channel(ip) # grpc.channel_ready_future(channel).result() stub = server_pb2_grpc.ServerStub(channel) replicate_resp = stub.replicate_entries(req, timeout=self.GLOBAL_TIMEOUT)
def run(): host = input("Host: ") channel = grpc.insecure_channel(host + ':50051') stub = server_pb2_grpc.ServerStub(channel) # estruturas de dados auxiliares n_tests = 512 stringList = ['abcdef' for i in range(200)] numbers = [32 for i in range(200)] float_numbers = [2.5 for i in range(200)] array_1 = server_pb2.FloatArray(numbers=[15.63, 1.65, 0.69]) array_2 = server_pb2.FloatArray(numbers=[15.10, 1.25, 0.89]) boolean = True string_32 = "a0APw0Di2UpuX8q4Z1ha8QHSvRu7U3UV" string_512 = "wGKH8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vEarkxihnNYyJLD7b918QD6qduDr6ALpJwJ8zJ" \ "tgaifDabexcqsbVAiQpisJ72ROsI4JqlGyw6M17sNDFqUAmWH5YW6bjpRMVtyW28Z4xAkwEM1woReDZIMapSF5T8a3a1yG" \ "LBDDAzGTnBLE9U77TM3dk85YDoIUdxSVzPGTGe1knOTQD7ShgmFnvEuuhvTY3QhUzGhFmfiSWmUlpQ9Fnpuf4idtXQnTNu" \ "2M4nL6eMeCbO2Ggjn1bQWtrnp6f9ma3Z9WBHAbWx7GLAlb9NV79ZHUnVonCSSfddDlY4qmohttERA3AdBz4s9cOIfGUUci" \ "IX5dUuR2QkU7XNfTxKXKpbLSSzreH8m2WuMCSwiEb63UqKphzQvK2xBwnMTyMqxoTgPBKb8x7r051YTwKWWfAJldsrIGnM" \ "XGiS1jRTcT7aKcCB9fL0Id0Zh7k3foLbamAnjYNnMADo3r7XLUtsvKujRUT6QnMNGRdh5zvyW6sSE7O9ulnx" # testes void_times = [] for i in range(n_tests): s = time.time() stub.voidFunction(server_pb2.void()) # void function e = time.time() void_times.append(e - s) bool_times = [] for i in range(n_tests): s = time.time() stub.boolFunction(server_pb2.Bool(boolean=boolean)) # void function e = time.time() bool_times.append(e - s) string32_times = [] for i in range(n_tests): s = time.time() stub.stringArg(server_pb2.String(text=string_32)) e = time.time() string32_times.append(e - s) string512_times = [] for i in range(n_tests): s = time.time() stub.stringArg(server_pb2.String(text=string_512)) e = time.time() string512_times.append(e - s) string1024_times = [] for i in range(n_tests): s = time.time() stub.stringArg(server_pb2.String(text=string_1024)) e = time.time() string1024_times.append(e - s) int_arg_times = [] for i in range(n_tests): s = time.time() stub.intArg(server_pb2.Integer(integer=10)) # int arg e = time.time() int_arg_times.append(e - s) long_arg_times = [] for i in range(n_tests): s = time.time() stub.longArg(server_pb2.Long(longValue=665656565656565)) # long arg e = time.time() long_arg_times.append(e - s) string_array_times = [] for i in range(n_tests): s = time.time() stub.stringListArg( server_pb2.StringArray(texts=stringList)) # string array e = time.time() string_array_times.append(e - s) int_array_times = [] for i in range(n_tests): s = time.time() stub.intArray(server_pb2.IntArray(numbers=numbers)) # int array e = time.time() int_array_times.append(e - s) float_array_times = [] for i in range(n_tests): s = time.time() stub.floatArray( server_pb2.FloatArray(numbers=float_numbers)) # float array e = time.time() float_array_times.append(e - s) object_arg_times = [] for i in range(n_tests): s = time.time() stub.objectArg(server_pb2.Object(name='alexandre', age=10, weight=72.5)) # passing object e = time.time() object_arg_times.append(e - s) euclidean_times = [] for i in range(n_tests): s = time.time() stub.euclideanDistance( server_pb2.EuclidianVectors( vectors=[array_1, array_2])) # euclidean distance e = time.time() euclidean_times.append(e - s) # dataset void_times = np.array(void_times) bool_times = np.array(bool_times) int_arg_times = np.array(int_arg_times) long_arg_times = np.array(long_arg_times) string32_times = np.array(string32_times) string512_times = np.array(string512_times) string1024_times = np.array(string1024_times) int_array_times = np.array(int_array_times) float_array_times = np.array(float_array_times) string_array_times = np.array(string_array_times) object_arg_times = np.array(object_arg_times) euclidean_times = np.array(euclidean_times) df = pd.DataFrame({ 'void': void_times, 'boolean': bool_times, 'int_arg': int_arg_times, 'long_arg': long_arg_times, 'string32_arg': string32_times, 'string512_arg': string512_times, 'string1024_arg': string1024_times, 'int_array': int_array_times, 'float_array': float_array_times, 'string_array': string_array_times, 'object_arg': object_arg_times, 'euclidean_arg': euclidean_times }) df.to_csv('grpc_metrics_' + host + '.csv', index=False) print('client finished...')