示例#1
0
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))
示例#2
0
def forward_api(role, request_config):
    endpoint = request_config.get('header', {}).get('endpoint')
    ip = get_base_config(role, {}).get("host", "127.0.0.1")
    port = get_base_config(role, {}).get("port")
    url = "http://{}:{}{}".format(ip, port, endpoint)
    method = request_config.get('header', {}).get('method', 'post')
    audit_logger().info('api request: {}'.format(url))
    action = getattr(requests, method.lower(), None)
    http_response = action(url=url, json=request_config.get('body'), headers=HEADERS)
    response = http_response.json()
    audit_logger().info(response)
    return response
示例#3
0
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))
示例#4
0
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))
示例#5
0
    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
        param_dict = json.loads(param)
        param_dict['src_party_id'] = str(src.partyId)
        source_routing_header = []
        for key, value in context.invocation_metadata():
            source_routing_header.append((key, value))
        stat_logger.info(
            f"grpc request routing header: {source_routing_header}")

        _routing_metadata = get_routing_metadata(src_party_id=src.partyId,
                                                 dest_party_id=dst.partyId)
        context.set_trailing_metadata(trailing_metadata=_routing_metadata)
        try:
            nodes_check(param_dict.get('src_party_id'),
                        param_dict.get('_src_role'), param_dict.get('appKey'),
                        param_dict.get('appSecret'), str(dst.partyId))
        except Exception as e:
            resp_json = {"retcode": 100, "retmsg": str(e)}
            return wrap_grpc_packet(resp_json, method, _suffix, dst.partyId,
                                    src.partyId, job_id)
        param = bytes.decode(bytes(json.dumps(param_dict), 'utf-8'))

        action = getattr(requests, method.lower(), None)
        audit_logger(job_id).info('rpc receive: {}'.format(packet))
        if action:
            audit_logger(job_id).info("rpc receive: {} {}".format(
                get_url(_suffix), param))
            resp = action(url=get_url(_suffix), data=param, headers=HEADERS)
        else:
            pass
        resp_json = resp.json()
        return wrap_grpc_packet(resp_json, method, _suffix, dst.partyId,
                                src.partyId, job_id)
示例#6
0
def local_api(job_id,
              method,
              endpoint,
              json_body,
              api_version=API_VERSION,
              try_times=3):
    endpoint = f"/{api_version}{endpoint}"
    exception = None
    for t in range(try_times):
        try:
            url = "http://{}:{}{}".format(RuntimeConfig.JOB_SERVER_HOST,
                                          RuntimeConfig.HTTP_PORT, endpoint)
            audit_logger(job_id).info('local api request: {}'.format(url))
            action = getattr(requests, method.lower(), None)
            http_response = action(url=url,
                                   data=json_dumps(json_body),
                                   headers=HEADERS)
            audit_logger(job_id).info(http_response.text)
            response = http_response.json()
            audit_logger(job_id).info('local api response: {} {}'.format(
                endpoint, response))
            return response
        except Exception as e:
            schedule_logger(job_id).exception(e)
            exception = e
    else:
        raise Exception('local request error: {}'.format(exception))
示例#7
0
def federated_coordination_on_http(
        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}"
    exception = None
    json_body['src_role'] = src_role
    json_body['src_party_id'] = src_party_id
    for t in range(try_times):
        try:
            url = "http://{}:{}{}".format(host, port, endpoint)
            audit_logger(job_id).info(
                'remote http api request: {}'.format(url))
            action = getattr(requests, method.lower(), None)
            headers = HEADERS.copy()
            headers["dest-party-id"] = str(dest_party_id)
            headers["src-party-id"] = str(src_party_id)
            headers["src-role"] = str(src_role)
            http_response = action(url=url,
                                   data=json_dumps(json_body),
                                   headers=headers)
            audit_logger(job_id).info(http_response.text)
            response = http_response.json()
            audit_logger(job_id).info('remote http api response: {} {}'.format(
                endpoint, response))
            return response
        except Exception as e:
            exception = e
            schedule_logger(job_id).warning(
                f"remote http request {endpoint} error, sleep and try again")
            time.sleep(2 * (t + 1))
    else:
        raise Exception('remote http request error: {}'.format(exception))