def proxy_api(role, _job_id, request_config): job_id = request_config.get('header').get('job_id', _job_id) method = request_config.get('header').get('method', 'POST') endpoint = request_config.get('header').get('endpoint') src_party_id = request_config.get('header').get('src_party_id') dest_party_id = request_config.get('header').get('dest_party_id') json_body = request_config.get('body') _packet = forward_grpc_packet( json_body, method, endpoint, src_party_id, dest_party_id, job_id=job_id, role=role, overall_timeout=DEFAULT_REMOTE_REQUEST_TIMEOUT) _routing_metadata = gen_routing_metadata(src_party_id=src_party_id, dest_party_id=dest_party_id) host, port, protocol = get_federated_proxy_address(src_party_id, dest_party_id) channel, stub = get_command_federation_channel(host, port) _return, _call = stub.unaryCall.with_call(_packet, metadata=_routing_metadata) channel.close() json_body = json_loads(_return.body.value) return json_body
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))