Skip to content

Drain gRPC channels removed by endpoint discovery - #950

Merged
vgvoleg merged 1 commit into
mainfrom
discovery-channel-shutdown-timeout
Dec 3, 2026
Merged

vgvoleg merged 1 commit into
mainfrom
discovery-channel-shutdown-timeout

Conversation

@vgvoleg

@vgvoleg vgvoleg commented Nov 26, 2026

Copy link
Copy Markdown
Member

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_timeout defaults to 60 seconds. Set it to 0 to close removed channels immediately. If discovery reuses an address with a different node ID, the replacement channel opens a fresh transport.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.54545% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.53%. Comparing base (51f8d84) to head (0e53889).

Files with missing lines Patch % Lines
ydb/aio/connection.py 96.92% 0 Missing and 2 partials ⚠️
ydb/resolver.py 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            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     
Flag Coverage Δ
integration 79.91% <68.72%> (-0.30%) ⬇️
unit 57.29% <94.54%> (+4.18%) ⬆️

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

Files with missing lines Coverage Δ
ydb/aio/pool.py 84.15% <100.00%> (+4.31%) ⬆️
ydb/connection.py 86.88% <100.00%> (+5.96%) ⬆️
ydb/driver.py 75.00% <100.00%> (+3.35%) ⬆️
ydb/pool.py 91.26% <100.00%> (+1.38%) ⬆️
ydb/aio/connection.py 94.11% <96.92%> (+4.46%) ⬆️
ydb/resolver.py 87.32% <50.00%> (+0.70%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vgvoleg
vgvoleg force-pushed the discovery-channel-shutdown-timeout branch from ee4affd to 3804f32 Compare November 27, 2026 09:28
@vgvoleg
vgvoleg requested an adaptive review from Copilot November 30, 2026 08:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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, with 0 requesting 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)

Comment thread ydb/connection.py
Comment on lines +535 to +537
except BaseException:
self._finish_call(rpc_state)
raise
Comment thread ydb/pool.py Outdated
Comment on lines +299 to +301
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)
Comment thread ydb/pool.py Outdated
try:
with self.condition:
while not self._should_stop.is_set():
successful = self.execute_discovery()
Comment thread ydb/query/pool_test.py Outdated
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
Comment thread examples/README.md
```python
import ydb

with ydb.Driver(endpoint="grpc://localhost:2136", database="/local", grpc_channel_shutdown_timeout=1) as driver:
@vgvoleg
vgvoleg force-pushed the discovery-channel-shutdown-timeout branch from 3804f32 to f798d55 Compare November 30, 2026 10:51
@vgvoleg vgvoleg added the SLO label Dec 3, 2026
@github-actions github-actions Bot removed the SLO label Dec 3, 2026
@vgvoleg
vgvoleg force-pushed the discovery-channel-shutdown-timeout branch from f798d55 to 0e53889 Compare December 3, 2026 10:08
@vgvoleg vgvoleg added the SLO label Dec 3, 2026
@github-actions github-actions Bot removed the SLO label Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

🌋 SLO Test Results

🟡 6 workload(s) tested — 1 workload(s) exceeded warning thresholds

Commit: bf2d275 · View run

Workload Thresholds Duration Report
sync-topic-multiwriter 🟢 OK 10m 7s 📄 Report
sync-query 🟢 OK 10m 12s 📄 Report
sync-topic 🟢 OK 10m 7s 📄 Report
async-topic 🟢 OK 10m 3s 📄 Report
sync-table 🟡 Warning 10m 3s 📄 Report
async-query 🟢 OK 10m 3s 📄 Report

Threshold violations:

sync-table:

  • read_retry_attempts: ▲ 33.3% (≥ 20% warn)

Generated by ydb-slo-action

@vgvoleg
vgvoleg merged commit 631d6d4 into main Dec 3, 2026
40 checks passed
@vgvoleg
vgvoleg deleted the discovery-channel-shutdown-timeout branch December 3, 2026 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants