8000 refactor: merge grpc and rest client by bwanglzu · Pull Request #2565 · jina-ai/serve · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: merge grpc and rest client #2565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jun 10, 2021
Merged

refactor: merge grpc and rest client #2565

merged 32 commits into from
Jun 10, 2021

Conversation

bwanglzu
Copy link
Member
@bwanglzu bwanglzu commented Jun 5, 2021

This PR aims at simplify C/S mode (Flow as service) Jina Python client usage with a uniform facade, before and after:

# before
from jina import Client
from jina.clients import WebsocketClient

client = Client('localhost', 12345) # connect to grpc gateway
client = WebsocketClient('localhost', 12345) # connect to restful gateway

# now
from jina import Client
client = Client('localhost', 12345)  # connect to grpc gateway
client = Client('localhost', 12345, restful=True)  # connect to restful gateway

# it uses the same semantics as what we have defined for the flow:
f = Flow()
f = Flow(restful=True)

@jina-bot jina-bot added size/S area/core This issue/PR affects the core codebase component/client labels Jun 5, 2021
@codecov
Copy link
codecov bot commented Jun 7, 2021

Codecov Report

Merging #2565 (68a77d0) into master (0bec2e6) will increase coverage by 25.62%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master    #2565       +/-   ##
===========================================
+ Coverage   61.73%   87.36%   +25.62%     
===========================================
  Files         135      154       +19     
  Lines        9022     9649      +627     
===========================================
+ Hits         5570     8430     +2860     
+ Misses       3452     1219     -2233     
Flag Coverage Δ
daemon 46.45% <73.68%> (+2.43%) ⬆️
jina 87.44% <100.00%> (+25.94%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
jina/clients/mixin.py 100.00% <ø> (ø)
jina/clients/__init__.py 100.00% <100.00%> (+9.09%) ⬆️
jina/flow/base.py 90.70% <100.00%> (+3.20%) ⬆️
jina/parsers/client.py 100.00% <100.00%> (ø)
jina/proto/jina_pb2.py 100.00% <0.00%> (ø)
daemon/models/custom.py 87.23% <0.00%> (ø)
daemon/api/endpoints/pea.py 86.11% <0.00%> (ø)
daemon/stores/pod.py 100.00% <0.00%> (ø)
daemon/__init__.py 60.00% <0.00%> (ø)
daemon/excepts.py 100.00% <0.00%> (ø)
... and 91 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0bec2e6...68a77d0. Read the comment docs.

@jina-bot jina-bot added size/M area/testing This issue/PR affects testing and removed size/S labels Jun 8, 2021
@jina-bot jina-bot added the area/housekeeping This issue/PR is housekeeping label Jun 8, 2021
@jina-bot jina-bot added size/S and removed size/M labels Jun 8, 2021
@jina-bot jina-bot added size/M and removed size/S labels Jun 8, 2021
@bwanglzu bwanglzu marked this pull request as ready for review June 8, 2021 13:16
@bwanglzu bwanglzu requested a review from a team as a code owner June 8, 2021 13:16
@bwanglzu bwanglzu requested review from mapleeit and BastinJafari June 8, 2021 13:16
@@ -912,7 +914,7 @@ def use_grpc_gateway(self, port: Optional[int] = None):

def _switch_gateway(self, gateway: str, port: int):
restful = gateway == 'RESTRuntime'
client = WebSocketClient if gateway == 'RESTRuntime' else Client
client = WebSocketClient if gateway == 'RESTRuntime' else GrpcClient
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can we not use Client here passing restful?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deepankarm we can, while it does not make a lot difference since Client is actually initialising GrpcClient and WebSocketClient

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then lets have a single poijt of entry, change it to Client with the proper argument, u can use partial functions if needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant, not using GrpcClient & WebSocketClient at all in flow/base.py and using Client directly. Not important though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gave a try, and I broke a lot of tests. I'll look into if I can optimise this part in a future PR.

Copy link
Member
@hanxiao hanxiao Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and I broke a lot of tests

I believe the reason was because your Client() function does not accept the full arguments that CLI defined (as I mentioned here #2565 (comment)). This oversimplification causes incomplete setup of the client and yields the broken test.

Comment on lines 20 to 22
args = set_client_cli_parser().parse_args(
['--host', host, '--port-expose', str(port_expose)]
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when written in this way, all other client arguments are not passed. better use **kwargs and use ArgNamespace.kwargs2list()

usage: jina client [-h] [--request-size] [--continue-on-error]
                   [--show-progress] [--return-results] [--host]
                   [--port-expose] [--proxy]

Start a Python client that connects to a remote Jina gateway

⚙️  Optional Arguments

  -h, --help           show this help message and exit

⚙️  Client Arguments

  --request-size       type: int; default: 100

                       The number of Documents in each Request.

  --continue-on-error  default: disabled, use "--continue-on-error" to
                       enable it

                       If set, a Request that causes error will be logged only
                       without blocking the further requests.

  --show-progress      default: disabled, use "--show-progress" to enable
                       it

                       If set, client will show a progress bar on receiving
                       every request.

  --return-results     default: disabled, use "--return-results" to enable
                       it

                       This feature is only used for AsyncClient.

                       If set, the results of all Requests will be returned as
                       a list. This is useful when one wants process Responses
                       in bulk instead of using callback.

⚙️  Expose Arguments

  --host               type: str; default: 0.0.0.0

                       The host address of the runtime, by default it is
                       0.0.0.0.

  --port-expose        type: int; default: 58570

                       The port of the host exposed to the public

  --proxy              default: disabled, use "--proxy" to enable it

                       If set, respect the http_proxy and https_proxy
                       environment variables. otherwise, it will unset these
                       proxy variables before start. gRPC seems to prefer no
                       proxy

Comment on lines 174 to 175
runtime_cls='GRPCRuntime'
if self._cls_client == Client
if self._cls_client == GrpcClient
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better uniform the case, GRPCRuntime then GRPCClient or GrpcRuntime and GrpcClient

@hanxiao
Copy link
Member
hanxiao commented Jun 9, 2021

Could you inject jina client CLI to def Client?

@hanxiao
Copy link
Member
hanxiao commented Jun 9, 2021

note that, one has to run scripts/inject-cli-as-overload.py manually and precommit hook will black -S the modified files

@bwanglzu
Copy link
Member Author
bwanglzu commented Jun 9, 2021

applied some changes to scripts/inject-cli-as-overload.py , method defined inside / outside class should have different indentation.

@@ -76,37 +87,66 @@ def fill_overload(
return_type,
filepath,
overload_fn,
class_method,
indent=' ' * 4,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, isn't the modification the same as fill_overload(..., indent=' '*2)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you revert it and simply pass indent=' '*2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you're right, working on it now

Copy link
Member Author
@bwanglzu bwanglzu Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hanxiao it's not only about indent, the script will also insert a self inside the method etc..and we have different levels of indent such as no indent (before overload and method name), 4 indents for docstrings etc..

this is what happened after i change indent = ' ' * 2:

C48D9157-14A7-40FA-88AF-8367A1A16BC8

@hanxiao hanxiao merged commit 2fe1479 into master Jun 10, 2021
@hanxiao hanxiao deleted the refactor-client branch June 10, 2021 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core This issue/PR affects the core codebase area/housekeeping This issue/PR is housekeeping area/testing This issue/PR affects testing component/client component/flow size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Merge jina restful and grpc client usage
5 participants
0