Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
feat: transparent leader hint reconnect on consume#2
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -13,6 +13,25 @@ | ||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterator | ||
| _LEADER_HINT_KEY = "x-fila-leader-addr" | ||
| def _extract_leader_hint(err: grpc.RpcError) -> str | None: | ||
| """Return the leader address from trailing metadata, if present. | ||
| The server sets ``x-fila-leader-addr`` in trailing metadata alongside an | ||
| UNAVAILABLE status when the node is not the leader for the requested queue. | ||
| """ | ||
| if err.code() != grpc.StatusCode.UNAVAILABLE: | ||
| return None | ||
| trailing = err.trailing_metadata() | ||
| if trailing is None: | ||
| return None | ||
| for key, value in trailing: | ||
| if key == _LEADER_HINT_KEY: | ||
| return str(value) | ||
| return None | ||
| class _ClientCallDetails( | ||
| grpc.ClientCallDetails, # type: ignore[misc] | ||
| @@ -143,28 +162,40 @@ def __init__( | ||
| api_key: API key for authentication. When set, every RPC includes an | ||
| ``authorization: Bearer <key>`` metadata header. | ||
| """ | ||
| use_tls = tls or ca_cert is not None | ||
| self._tls = tls | ||
| self._ca_cert = ca_cert | ||
| self._client_cert = client_cert | ||
| self._client_key = client_key | ||
| self._api_key = api_key | ||
| use_tls = tls or ca_cert is not None | ||
| if (client_cert is not None or client_key is not None) and not use_tls: | ||
| raise ValueError( | ||
| "client_cert and client_key require ca_cert or tls=True to establish a TLS channel" | ||
| ) | ||
| self._channel = self._make_channel(addr) | ||
| self._stub = service_pb2_grpc.FilaServiceStub(self._channel) # type: ignore[no-untyped-call] | ||
| def _make_channel(self, addr: str) -> grpc.Channel: | ||
| """Create a gRPC channel to the given address using stored credentials.""" | ||
| use_tls = self._tls or self._ca_cert is not None | ||
| if use_tls: | ||
| creds = grpc.ssl_channel_credentials( | ||
| root_certificates=ca_cert, | ||
| private_key=client_key, | ||
| certificate_chain=client_cert, | ||
| root_certificates=self._ca_cert, | ||
| private_key=self._client_key, | ||
| certificate_chain=self._client_cert, | ||
| ) | ||
| self._channel = grpc.secure_channel(addr, creds) | ||
| channel: grpc.Channel = grpc.secure_channel(addr, creds) | ||
| else: | ||
| self._channel = grpc.insecure_channel(addr) | ||
| channel = grpc.insecure_channel(addr) | ||
| if api_key is not None: | ||
| interceptor = _ApiKeyInterceptor(api_key) | ||
| self._channel = grpc.intercept_channel(self._channel, interceptor) | ||
| if self._api_key is not None: | ||
| interceptor = _ApiKeyInterceptor(self._api_key) | ||
| channel = grpc.intercept_channel(channel, interceptor) | ||
| self._stub = service_pb2_grpc.FilaServiceStub(self._channel) # type: ignore[no-untyped-call] | ||
| return channel | ||
| def close(self) -> None: | ||
| """Close the underlying gRPC channel.""" | ||
| @@ -215,6 +246,10 @@ def consume(self, queue: str) -> Iterator[ConsumeMessage]: | ||
| server stream closes or an error occurs. Skip nil message frames | ||
| (keepalive signals) automatically. | ||
| If the server returns UNAVAILABLE with an ``x-fila-leader-addr`` | ||
| trailing metadata entry, the client transparently reconnects to the | ||
| leader address and retries the consume call once. | ||
| Args: | ||
| queue: Queue to consume from. | ||
| @@ -230,10 +265,26 @@ def consume(self, queue: str) -> Iterator[ConsumeMessage]: | ||
| service_pb2.ConsumeRequest(queue=queue) | ||
| ) | ||
| except grpc.RpcError as e: | ||
| raise _map_consume_error(e) from e | ||
| leader_addr = _extract_leader_hint(e) | ||
| if leader_addr is not None: | ||
| stream = self._reconnect_and_consume(leader_addr, queue) | ||
| else: | ||
| raise _map_consume_error(e) from e | ||
| return self._consume_iter(stream) | ||
| def _reconnect_and_consume(self, leader_addr: str, queue: str) -> Any: | ||
| """Create a new channel to *leader_addr* and retry the consume call.""" | ||
| self._channel.close() | ||
| self._channel = self._make_channel(leader_addr) | ||
cubic-dev-ai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self._stub = service_pb2_grpc.FilaServiceStub(self._channel) # type: ignore[no-untyped-call] | ||
| try: | ||
| return self._stub.Consume( | ||
| service_pb2.ConsumeRequest(queue=queue) | ||
| ) | ||
| except grpc.RpcError as e: | ||
| raise _map_consume_error(e) from e | ||
| def _consume_iter( | ||
| self, | ||
| stream: Any, | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.