Repository navigation
Drain gRPC channels removed by endpoint discovery - #950
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #950 +/- ##
==========================================
+ Coverage 84.16% 84.53% +0.37%
==========================================
Files 102 102
Lines 13974 14137 +163
Branches 1450 1489 +39
==========================================
+ Hits 11761 11951 +190
+ Misses 1758 1739 -19
+ Partials 455 447 -8
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
ee4affd to
3804f32
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Channel-retirement races can bypass SDK retries, while discovery handling can repeatedly replace valid connections or leave the driver unable to recover.
5 open issues
What changed in this PR
Adds deadline-based draining of discovery-removed gRPC channels to both YDB drivers, allowing active calls to finish after their endpoint leaves routing.
Changes:
- Adds
grpc_channel_shutdown_timeout, defaulting to 60 seconds, with0requesting immediate closure. - Tracks active RPCs through completion, retires channels in the background, and isolates replacement transports.
- Adds lifecycle tests, configuration documentation, an example, and release notes.
| File | Description |
|---|---|
ydb/query/pool_test.py |
Adds live gRPC tests for draining, deadlines, cancellation, and transport replacement. |
ydb/pool.py |
Adds synchronous channel retirement and discovery cleanup handling. |
ydb/driver.py |
Defines, validates, and documents the shutdown timeout. |
ydb/connection.py |
Tracks synchronous calls and streams during timed retirement. |
ydb/aio/query/pool_test.py |
Tests async retirement cleanup, cancellation, and discovery recovery. |
ydb/aio/pool.py |
Manages background retirement tasks and async cache cleanup. |
ydb/aio/connection.py |
Tracks async RPC completion and translates retirement cancellations. |
tests/test_discovery_detect_local_dc.py |
Updates the synchronous connection double to support retirement. |
tests/aio/test_discovery_detect_local_dc.py |
Updates the async connection double to support retirement. |
examples/README.md |
Adds an example configuring a shorter drain timeout. |
docs/driver.rst |
Documents the timeout and discovery-driven draining behavior. |
CHANGELOG.md |
Adds a release note for configurable channel draining. |
Review effort: Max (adaptive)
| except BaseException: | ||
| self._finish_call(rpc_state) | ||
| raise |
| discovered = endpoints.get(cached_endpoint.endpoint) | ||
| if discovered is None or discovered.node_id != cached_endpoint.node_id: | ||
| self._cache.retire(cached_endpoint, self._driver_config.grpc_channel_shutdown_timeout) |
| try: | ||
| with self.condition: | ||
| while not self._should_stop.is_set(): | ||
| successful = self.execute_discovery() |
| assert not finished.is_set() | ||
|
|
||
| # The node is still reachable, and the replacement has its own transport. | ||
| assert await call(replacement, b"", StreamStub, "Peer") != old_peer |
| ```python | ||
| import ydb | ||
|
|
||
| with ydb.Driver(endpoint="grpc://localhost:2136", database="/local", grpc_channel_shutdown_timeout=1) as driver: |
3804f32 to
f798d55
Compare
f798d55 to
0e53889
Compare
🌋 SLO Test Results🟡 6 workload(s) tested — 1 workload(s) exceeded warning thresholds
Threshold violations: sync-table:
Generated by ydb-slo-action |


When a successful discovery response drops an endpoint, both drivers remove its channel from routing immediately and let active RPCs finish in the background. Active streams count until gRPC completes them. The driver closes the channel at the deadline and reports interrupted calls as connection loss.
grpc_channel_shutdown_timeoutdefaults to 60 seconds. Set it to0to close removed channels immediately. If discovery reuses an address with a different node ID, the replacement channel opens a fresh transport.