def remote_api(job_id, method, endpoint, src_party_id, dest_party_id, json_body, overall_timeout=DEFAULT_GRPC_OVERALL_TIMEOUT): _packet = wrap_grpc_packet(json_body, method, endpoint, src_party_id, dest_party_id, job_id, overall_timeout=overall_timeout) try: channel, stub = get_proxy_data_channel() # stat_logger.info("grpc api request: {}".format(_packet)) _return = stub.unaryCall(_packet) stat_logger.info("grpc api response: {}".format(_return)) channel.close() json_body = json.loads(_return.body.value) return json_body except grpc.RpcError as e: raise Exception('rpc request error: {}'.format(e)) except Exception as e: raise Exception('rpc request error: {}'.format(e))
def remote_api(job_id, method, endpoint, src_party_id, dest_party_id, src_role, json_body, overall_timeout=DEFAULT_GRPC_OVERALL_TIMEOUT): json_body['src_role'] = src_role if CHECK_NODES_IDENTITY: get_node_identity(json_body, src_party_id) _packet = wrap_grpc_packet(json_body, method, endpoint, src_party_id, dest_party_id, job_id, overall_timeout=overall_timeout) try: channel, stub = get_proxy_data_channel() _return = stub.unaryCall(_packet) audit_logger.info("grpc api response: {}".format(_return)) channel.close() json_body = json.loads(_return.body.value) return json_body except Exception as e: raise Exception('rpc request error: {}'.format(e))
def remote_api(job_id, method, endpoint, src_party_id, dest_party_id, src_role, json_body, overall_timeout=DEFAULT_GRPC_OVERALL_TIMEOUT): json_body['src_role'] = src_role if CHECK_NODES_IDENTITY: get_node_identity(json_body, src_party_id) _packet = wrap_grpc_packet(json_body, method, endpoint, src_party_id, dest_party_id, job_id, overall_timeout=overall_timeout) try: channel, stub = get_proxy_data_channel() _return = stub.unaryCall(_packet) audit_logger(job_id).info("grpc api response: {}".format(_return)) channel.close() json_body = json.loads(_return.body.value) return json_body except Exception as e: tips = '' if 'Error received from peer' in str(e): tips = 'Please check if the fate flow server of the other party is started. ' if 'failed to connect to all addresses' in str(e): tips = 'Please check whether the rollsite service(port: 9370) is started. ' raise Exception('{}rpc request error: {}'.format(tips, e))
def federated_coordination_on_grpc(job_id, method, host, port, endpoint, src_party_id, src_role, dest_party_id, json_body, api_version=API_VERSION, overall_timeout=DEFAULT_REMOTE_REQUEST_TIMEOUT, try_times=3): endpoint = f"/{api_version}{endpoint}" json_body['src_role'] = src_role json_body['src_party_id'] = src_party_id if CHECK_NODES_IDENTITY: get_node_identity(json_body, src_party_id) _packet = wrap_grpc_packet(json_body, method, endpoint, src_party_id, dest_party_id, job_id, overall_timeout=overall_timeout) _routing_metadata = gen_routing_metadata(src_party_id=src_party_id, dest_party_id=dest_party_id) exception = None for t in range(try_times): try: channel, stub = get_command_federation_channel(host, port) _return, _call = stub.unaryCall.with_call(_packet, metadata=_routing_metadata, timeout=(overall_timeout/1000)) audit_logger(job_id).info("grpc api response: {}".format(_return)) channel.close() response = json_loads(_return.body.value) return response except Exception as e: exception = e schedule_logger(job_id).warning(f"remote request {endpoint} error, sleep and try again") time.sleep(2 * (t+1)) else: tips = 'Please check rollSite and fateflow network connectivity' """ if 'Error received from peer' in str(exception): tips = 'Please check if the fate flow server of the other party is started. ' if 'failed to connect to all addresses' in str(exception): tips = 'Please check whether the rollsite service(port: 9370) is started. ' """ raise Exception('{}rpc request error: {}'.format(tips, exception))
def remote_api(host, port, job_id, method, endpoint, src_party_id, dest_party_id, src_role, json_body, api_version="v1", overall_timeout=30 * 1000, try_times=3): endpoint = f"/{api_version}{endpoint}" json_body['src_role'] = src_role json_body['src_party_id'] = src_party_id _packet = wrap_grpc_packet(json_body, method, endpoint, src_party_id, dest_party_id, job_id, overall_timeout=overall_timeout) print(_packet) _routing_metadata = gen_routing_metadata(src_party_id=src_party_id, dest_party_id=dest_party_id) exception = None for t in range(try_times): try: channel, stub = get_command_federation_channel(host, port) _return, _call = stub.unaryCall.with_call( _packet, metadata=_routing_metadata, timeout=(overall_timeout / 1000)) audit_logger(job_id).info("grpc api response: {}".format(_return)) channel.close() response = json.loads(_return.body.value) return response except Exception as e: exception = e schedule_logger(job_id).warning( f"remote request {endpoint} error, sleep and try again") time.sleep(2 * (t + 1)) else: tips = 'Please check rollSite and fateflow network connectivity' raise Exception('{}rpc request error: {}'.format(tips, exception))
def remote_api(job_id, method, endpoint, src_party_id, dest_party_id, src_role, json_body, api_version=API_VERSION, overall_timeout=DEFAULT_GRPC_OVERALL_TIMEOUT, try_times=3): endpoint = f"/{api_version}{endpoint}" json_body['src_role'] = src_role if CHECK_NODES_IDENTITY: get_node_identity(json_body, src_party_id) _packet = wrap_grpc_packet(json_body, method, endpoint, src_party_id, dest_party_id, job_id, overall_timeout=overall_timeout) _routing_metadata = get_routing_metadata(src_party_id=src_party_id, dest_party_id=dest_party_id) exception = None for t in range(try_times): try: engine, channel, stub = get_command_federation_channel() # _return = stub.unaryCall(_packet) _return, _call = stub.unaryCall.with_call( _packet, metadata=_routing_metadata, timeout=(overall_timeout / 1000)) audit_logger(job_id).info("grpc api response: {}".format(_return)) channel.close() response = json.loads(_return.body.value) return response except Exception as e: exception = e else: tips = '' if 'Error received from peer' in str(exception): tips = 'Please check if the fate flow server of the other party is started. ' if 'failed to connect to all addresses' in str(exception): tips = 'Please check whether the rollsite service(port: 9370) is started. ' raise Exception('{}rpc request error: {}'.format(tips, exception))
def unaryCall(self, _request, context): packet = _request header = packet.header _suffix = packet.body.key param_bytes = packet.body.value param = bytes.decode(param_bytes) job_id = header.task.taskId src = header.src dst = header.dst method = header.operator resp_json = "xczczcz" for key, value in context.invocation_metadata(): print(key, value) context.set_trailing_metadata(( ('retcode', "0"), ('msg', 'ok'), )) return wrap_grpc_packet(resp_json, method, _suffix, dst.partyId, src.partyId, job_id)