Skip to content
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

fix: multi protocol gateway supports monitoring #5570

Merged
merged 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions jina/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'get_public_ip',
'get_internal_ip',
'convert_tuple_to_list',
'deepcopy_with_ignore_attrs',
'run_async',
'deprecated_alias',
'retry',
Expand Down Expand Up @@ -1265,6 +1266,25 @@ def convert_tuple_to_list(d: Dict):
convert_tuple_to_list(v)


def deepcopy_with_ignore_attrs(
AnneYang720 marked this conversation as resolved.
Show resolved Hide resolved
obj: Any, ignore_attrs: List[str]
) -> Any:
"""Deep copy an object and ignore some attributes

:param obj: the object to copy
:param ignore_attrs: the attributes to ignore
:return: the copied object
"""
import copy

memo = {}
for k in ignore_attrs:
if hasattr(obj, k):
memo[id(getattr(obj, k))] = None # getattr(obj, k)

return copy.deepcopy(obj, memo)


def is_jupyter() -> bool: # pragma: no cover
"""
Check if we're running in a Jupyter notebook, using magic command `get_ipython` that only available in Jupyter.
Expand Down
6 changes: 4 additions & 2 deletions jina/serve/runtimes/gateway/composite/gateway.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
from typing import List, Optional

from jina.helper import deepcopy_with_ignore_attrs
from jina.serve.gateway import BaseGateway


Expand All @@ -21,10 +22,11 @@ def __init__(
self.gateways: List[BaseGateway] = []
for port, protocol in zip(self.ports, self.protocols):
gateway_cls = _get_gateway_class(protocol)
runtime_args = copy.deepcopy(self.runtime_args)
# ignore metrics_registry since it is not copyable
runtime_args = deepcopy_with_ignore_attrs(self.runtime_args, ['metrics_registry'])
Copy link
Member

Choose a reason for hiding this comment

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

are we sure deepcopy is 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 think we need to copy the runtime_args object, otherwise, how can different gateway objects hold the same reference of runtime_args but have different values of port and protocol ?

runtime_args.port = [port]
runtime_args.protocol = [protocol]
gateway_kwargs = copy.deepcopy(kwargs)
gateway_kwargs = {k: v for k, v in kwargs.items() if k != 'runtime_args'}
gateway_kwargs['runtime_args'] = dict(vars(runtime_args))
gateway = gateway_cls(**gateway_kwargs)
self.gateways.append(gateway)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,14 @@ def test_flow_multiprotocol_ports_protocols_mismatch():
'You need to specify as much protocols as ports if you want to use a jina built-in gateway'
in err_info.value.args[0]
)


def test_flow_multiprotocol_with_monitoring():
ports = [random_port(), random_port(), random_port()]
protocols = PROTOCOLS
flow = Flow().config_gateway(port=ports, protocol=protocols, monitoring=True)

with flow:
for port, protocol in zip(ports, protocols):
client = Client(port=port, protocol=protocol)
client.post('/', inputs=[Document()])