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 all 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
25 changes: 22 additions & 3 deletions jina/serve/runtimes/gateway/composite/gateway.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import copy
from typing import List, Optional
from typing import Any, List, Optional

from jina.serve.gateway import BaseGateway

Expand All @@ -21,10 +21,13 @@ 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 = self._deepcopy_with_ignore_attrs(
self.runtime_args, ['metrics_registry']
)
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 All @@ -46,6 +49,22 @@ async def run_server(self):
for gateway in self.gateways:
await gateway.run_server()

@staticmethod
def _deepcopy_with_ignore_attrs(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
"""

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

return copy.deepcopy(obj, memo)

@property
def _should_exit(self) -> bool:
should_exit_values = [
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()])