Repository navigation
Retry cluster shard connections that fail to dial synchronously - #574
Conversation
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this. I went through the fix and the four call sites in One question, on the behaviour change you already flagged. Replicas are dialed from CLUSTER SLOTS whatever Nitpicking: the The core fix looks solid. I'd just like the replica/default-config question answered before this merges. |
When a CLUSTER SLOTS refresh dials a shard connection and the dial fails before reaching the event loop (a getaddrinfo error, or EMFILE or ENETUNREACH from socket()/connect()), connect_shard_connection() only returned false and every caller ignored it. No connection event reports such a failure and no reconnect timer was pending, since a refresh only dials connections without one. The connection was left dead with nothing to retry it: it was redialed with no backoff on every refresh that a key routed to it triggered, ignoring --max-reconnect-attempts, or never, for a replica, which triggers no refresh. Handle the failure like a connection error with attempt_reconnect(), so the connection is retried with backoff through a slot map refresh and counts against --max-reconnect-attempts. Fixes #570. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Assert that the announce IP is reverted in the test's finally block, so a topology left poisoned fails here rather than in a later cluster test. Count every synchronous dial failure through the "Connection failed" lines that attempt_reconnect() logs, whichever step failed, and require the exhaustion message to name that context. The "connect failed" count, which only covers bufferevent_socket_connect(), stays as a second bound. Co-Authored-By: Claude Opus 5.5 <[email protected]>
7cc08aa to
23974ef
Compare
|
Thanks for the review. I rebased onto current master (f33599f, with #571–#573) without conflicts. The fix commit is now 0a99e87 and is unchanged apart from the rebase. The test changes are in 23974ef. 1. EMFILE without 2. Connection-error stats: agreed. Synchronous dial failures now go through 3. 4. Counting only I ran the test locally on a 3-shard OSS cluster and it passes. 🤖 Generated with Claude Code |
hand_off_replay_requests() runs inside a CLUSTER SLOTS reply handler, possibly the one of the connection that now owns the slot. Resending from there pushed onto that connection's pipeline mid-process_response(), before its m_cluster_slots was setup_done. adopt_replay_request() now only queues the request and calls schedule_fill(); fill_pipeline() resends it once the connection is connected and set up. A connection that isn't connected yet still resends it when it connects. Drop and uncount, with a --failed-keys-file line, a request that: - is held under --transaction, as MOVED/ASK do, since resending it on another connection would split its MULTI/EXEC block or land inside one that connection has open; - targets a primary that is disconnected with no reconnect pending. The refresh dials every primary without a pending timer, and since #574 a failed dial schedules one, so this is a primary that attempt_reconnect() gave up on (no --reconnect-on-error, or --max-reconnect-attempts reached). adopt_replay_request() asserts the replay queue exists instead of silently dropping the request. Co-Authored-By: Claude Opus 5.5 <[email protected]>
hand_off_replay_requests() runs inside a CLUSTER SLOTS reply handler, possibly the one of the connection that now owns the slot. Resending from there pushed onto that connection's pipeline mid-process_response(), before its m_cluster_slots was setup_done. adopt_replay_request() now only queues the request and calls schedule_fill(); fill_pipeline() resends it once the connection is connected and set up. A connection that isn't connected yet still resends it when it connects. Drop and uncount, with a --failed-keys-file line, a request that: - is held under --transaction, as MOVED/ASK do, since resending it on another connection would split its MULTI/EXEC block or land inside one that connection has open; - targets a primary that is disconnected with no reconnect pending. The refresh dials every primary without a pending timer, and since #574 a failed dial schedules one, so this is a primary that attempt_reconnect() gave up on (no --reconnect-on-error, or --max-reconnect-attempts reached). adopt_replay_request() asserts the replay queue exists instead of silently dropping the request. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Problem
In cluster mode, when a CLUSTER SLOTS refresh dials a shard connection and the dial fails synchronously, nothing schedules a retry. Examples are a
getaddrinfoerror, orEMFILEorENETUNREACHfromsocket()orconnect().connect_shard_connection()returnsfalse, and all four call sites inhandle_cluster_slots()ignore it. No connection event reports the failure, and no reconnect timer is pending, because the refresh only dials connections that have none (!is_reconnect_pending()).What happens next depends on the connection's role:
--max-reconnect-attemptsis never consulted. On master, with one primary announcing224.0.0.1(TCP connect fails withENETUNREACHright away) and--reconnect-on-error --max-reconnect-attempts=3 --test-time=3, memtier made 10,642 failed dials and about 10.6k CLUSTER SLOTS refreshes in 3 s and exited 0.Root cause
The issue's diagnosis is confirmed, with one addition: the bug is not only in
shard_connection::connect(). Thegetaddrinfofailure path inconnect_shard_connection()returns beforeconnect()is even called.The comment in
shard_connection::connect()("Cancel it only now, so a failed attempt above still leaves it to retry") only holds for asetup_socket()failure. A failingbufferevent_socket_connect()callsdisconnect(), which frees the timer. Either way, since #560 no timer is pending when a refresh dials a connection, so nothing is left to retry it.Fix
connect_shard_connection()now callssc->attempt_reconnect("Connection failed"), the same handling an asynchronous connection error gets. The connection then retries with backoff throughreconnect()and a slot map refresh (the Reconnect only the dropped cluster connection, and only once #560 path), and each attempt counts against--max-reconnect-attempts.getaddrinfo,setup_socket()(socket/fcntl, e.g.EMFILE), andbufferevent_socket_connect().attempt_reconnect()is now public.shard_connection::connect()is corrected.Timers and attempt counting:
attempt_reconnect()won't arm a timer whilem_reconnectingis set. It also callsdisconnect()first, which frees any timer.reconnect()returns 1 (refresh requested), and that path does not incrementm_reconnect_attempts.Behaviour change: without
--reconnect-on-error, a synchronous dial failure now ends the worker thread ("Maximum reconnection attempts (0) exceeded ..."). Before, the connection stayed dead or kept being redialed. This matches what an asynchronous failure such asECONNREFUSEDon the same connection already does. The--read-preferenceexception inattempt_reconnect()(leave the connection dead while peers are still in setup) still applies.This also applies to a transient resource failure such as
EMFILE: without--reconnect-on-errorthe thread ends rather than running on with one shard's connection silently dead, which would skew the results. The non-cluster client already treats a failed dial as fatal (prepare: failed to connect, test aborted). With--reconnect-on-error, the failure is retried with backoff.Stats change:
attempt_reconnect()callsupdate_connection_error(), so synchronous dial failures are now counted in the connection-error stats and output. Before, they were not counted at all.Test evidence
New test:
test_reconnections.py::test_cluster_shard_dial_failure_honors_max_reconnect_attempts. It runs on cluster only, and setscluster-announce-ip 224.0.0.1on a non-seed primary, so dialing that primary fails synchronously withENETUNREACH. It then runs memtier with--reconnect-on-error --max-reconnect-attempts=2and checks that:max+1. It counts the "Connection failed" lines thatattempt_reconnect()logs, so the bound holds whichever step fails (getaddrinfo,setup_socket()orbufferevent_socket_connect()). The "connect failed" lines frombufferevent_socket_connect()are bounded too;It restores the announce IP in a
finallyblock and asserts that the change propagates, so a poisoned topology fails this test rather than a later one.Manual runs with this PR:
--max-reconnect-attempts=3gives four failed dials, each at a 1 s backoff ("attempting reconnection 1/3 … 3/3"). Then the attempts are exhausted and the thread ends.--reconnect-backoff-factor=2the backoff goes 2 s, 4 s, 8 s.cluster-announce-ipmid-run. The next refresh connected the shard and traffic resumed: about 49.5k SETs on each of the three primaries in 8 s.Regression check:
test_reconnections.pyandtest_read_preference_failover.pyon a local 3-shard OSS cluster: 12/12 passed.Note: this test relies on TCP connect to a multicast address failing synchronously. Linux returns
ENETUNREACH. The cluster suites only run on Linux CI.Fixes #570
Affected versions: 2.4.0–2.5.2 and master (code history; widened by #560)
🤖 Generated with Claude Code
Note
Medium Risk
Touches cluster connection setup and reconnect accounting; behavior changes when synchronous dials fail without
--reconnect-on-error(worker may exit instead of leaving a dead connection). Core reconnect path is shared with existing async errors.Overview
Fixes cluster mode when a shard dial fails before the event loop (DNS/
getaddrinfo,EMFILE,ENETUNREACH, etc.) duringCLUSTER SLOTShandling. Those failures used to return fromconnect_shard_connection()with no reconnect timer, so refreshes could redial on every slot-map update without honoring--max-reconnect-attemptsor backoff (#570).connect_shard_connection()now callsattempt_reconnect("Connection failed")on any non-success path, matching async connection errors: backoff viareconnect(), attempts counted against--max-reconnect-attempts, and refresh-driven redials skipped while a timer is pending.attempt_reconnectis public onshard_connection(moved out of private). Comments inshard_connection::connect()clarify when the reconnect timer is cleared vs left for the caller to schedule retry.Adds cluster integration test
test_cluster_shard_dial_failure_honors_max_reconnect_attempts(unreachablecluster-announce-ip) to assert bounded dials and exhausted-attempts exit.Reviewed by Cursor Bugbot for commit 23974ef. Bugbot is set up for automated code reviews on this repo. Configure here.