client.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. import sys
  18. import time
  19. import grpc
  20. from fate_arch.protobuf.python import proxy_pb2_grpc
  21. from fate_flow.utils.log_utils import audit_logger, schedule_logger
  22. from fate_flow.utils.grpc_utils import wrap_grpc_packet, gen_routing_metadata
  23. def get_command_federation_channel(host, port):
  24. print(f"connect {host}:{port}")
  25. channel = grpc.insecure_channel('{}:{}'.format(host, port))
  26. stub = proxy_pb2_grpc.DataTransferServiceStub(channel)
  27. return channel, stub
  28. def remote_api(host, port, job_id, method, endpoint, src_party_id, dest_party_id, src_role, json_body, api_version="v1",
  29. overall_timeout=30*1000, try_times=3):
  30. endpoint = f"/{api_version}{endpoint}"
  31. json_body['src_role'] = src_role
  32. json_body['src_party_id'] = src_party_id
  33. _packet = wrap_grpc_packet(json_body, method, endpoint, src_party_id, dest_party_id, job_id,
  34. overall_timeout=overall_timeout)
  35. print(_packet)
  36. _routing_metadata = gen_routing_metadata(src_party_id=src_party_id, dest_party_id=dest_party_id)
  37. exception = None
  38. for t in range(try_times):
  39. try:
  40. channel, stub = get_command_federation_channel(host, port)
  41. _return, _call = stub.unaryCall.with_call(_packet, metadata=_routing_metadata, timeout=(overall_timeout/1000))
  42. audit_logger(job_id).info("grpc api response: {}".format(_return))
  43. channel.close()
  44. response = json.loads(_return.body.value)
  45. return response
  46. except Exception as e:
  47. exception = e
  48. schedule_logger(job_id).warning(f"remote request {endpoint} error, sleep and try again")
  49. time.sleep(2 * (t+1))
  50. else:
  51. tips = 'Please check rollSite and fateflow network connectivity'
  52. raise Exception('{}rpc request error: {}'.format(tips, exception))
  53. host = sys.argv[1]
  54. port = int(sys.argv[2])
  55. src_role = sys.argv[3]
  56. src_party_id = sys.argv[4]
  57. dest_party_id = sys.argv[5]
  58. response = remote_api(host, port, "test_job_command", "POST", "/version/get", src_party_id, dest_party_id, src_role, {"src_role": src_role, "src_party_id": src_party_id})
  59. print(response)