Skip to content

Treat a deadline-clamped reconnect as at the deadline when it fires - #873

Merged
fcostaoliveira merged 5 commits into
masterfrom
fix/cluster-retry-at-deadline
Oct 26, 2026
Merged

fcostaoliveira merged 5 commits into
masterfrom
fix/cluster-retry-at-deadline

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #852.

test_cluster_slots_never_started_client_fails_at_deadline fails intermittently in CI (run 40130526066). A cluster client whose CLUSTER SLOTS keeps failing never starts. With unlimited reconnects its last retry is clamped to fire exactly at the --test-time deadline (next_reconnect_delay()). When the timer fires, run_deadline_passed() checks the wall clock against the deadline with 1 ms of slack.

libevent on Linux times events with a coarse monotonic clock (1–4 ms ticks), so the timer can fire more than 1 ms early by the wall clock. The check then says the run isn't over, and the retry reconnects and gets a good slot map. The client ends normally at its first reply, so memtier exits 0 instead of failing the run for a client that never started. #812 fixed when the retry is scheduled; this is about when it fires.

Fix (shard_connection.cpp/.h, +16):

  • next_reconnect_delay() records the deadline it clamped a reconnect to.
  • When that reconnect timer fires, run_deadline_passed() treats that deadline as passed, whatever the wall clock says.

Unchanged:

Test: test_deadline_retry_fires_early() in tests/unit/reconnect_siblings_test.cpp. It arms a clamped retry 100 ms before the deadline and runs the timer handler at once, standing in for a timer that fires early. It checks that the client doesn't reconnect and that the run fails. A second case checks that a retry 2 s before the deadline still reconnects. With master's shard_connection files the new test fails both checks; with the fix it passes.

Results (ASan + UBSan, libevent 2.1.12, Ubuntu 24.04 container):

  • test_cluster_slots_bad_reply.py (cluster) passes 20/20 in each of 5 iterations.
  • Six reconnect-related test files pass 73/73 in cluster and in standalone mode.
  • make check passes, and clang-format is clean.

The end-to-end flake doesn't reproduce locally: this machine's coarse clock ticks at 1 ms, which the existing slack covers. That's why the regression test is a unit test.

🤖 Generated with Claude Code


Note

Medium Risk
Changes when timed benchmark runs stop reconnecting and fail vs succeed exit; scope is limited to unlimited reconnect + --test-time deadline clamping.

Overview
Fixes #852: when unlimited reconnects clamp the next retry to the --test-time deadline, libevent on Linux can fire that timer 1–4 ms early (coarse monotonic clock). run_deadline_passed() only allowed 1 ms slack on the wall clock, so the run was treated as not over, the client reconnected, completed setup, and exited 0 instead of failing a never-started cluster client.

shard_connection now records the deadline when next_reconnect_delay() shortens the backoff (m_reconnect_deadline), copies it to m_fired_reconnect_deadline when that reconnect timer fires, and run_deadline_passed() treats that fired deadline as passed regardless of wall time. Pending deadline state is cleared when reconnect timers are cancelled. Retries well before the deadline are unchanged.

Adds test_deadline_retry_fires_early() in reconnect_siblings_test.cpp to simulate an early-fired deadline-clamped retry and assert no reconnect + run failure, plus a control case that still reconnects before the deadline.

Reviewed by Cursor Bugbot for commit 73adaf2. Bugbot is set up for automated code reviews on this repo. Configure here.

fcostaoliveira and others added 2 commits October 25, 2026 07:57
A never-started cluster client whose CLUSTER SLOTS retry is due at the
--test-time deadline must fail the run (#667). next_reconnect_delay()
clamps that retry to the deadline, and handle_reconnect_timer_event()
then asks run_is_over(), which compares the wall clock with the
deadline, allowing 1 ms for the timer firing early.

On Linux libevent times events with CLOCK_MONOTONIC_COARSE, whose tick
is 1-4 ms, and maps it to wall time through an offset sampled once per
5 s. The clamped timer can therefore fire more than 1 ms before the
wall-clock deadline. run_is_over() then says the run is not over, the
retry reconnects, the slot map is now good, and the client completes
setup and ends normally at its first reply: memtier exits 0 (#852).

Remember the deadline a reconnect timer was clamped to, and once that
timer has fired, report that deadline as passed. A retry armed before
the deadline with its normal backoff is unchanged, and a deadline that
moved later since (the client started) is still judged by the clock.

The unit test arms a clamped retry 100 ms before the deadline and fires
its handler at once, staging the early timer: without the fix it
reconnects and the run does not fail.

Fixes #852

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…test's gap

m_reconnect_deadline is documented as zero when no clamped timer is
pending; clear it once the timer has fired and the deadline was copied.
The unit test's deadline was 100 ms away, which a stalled sanitizer
runner could overrun before the handler runs; 500 ms keeps the 1 s
backoff clamped with more room.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks, this reads well. The root-cause write-up is clear: the timer fires early because of libevent's coarse clock, so it's about when the retry fires, not when it was scheduled (#812). The unit test fails both checks on master's shard_connection files and stages the early fire directly, which seems like the right call given the flake doesn't reproduce on a 1 ms-tick machine. A couple of small things:

  • next_reconnect_delay() now has a side effect: it resets or sets m_reconnect_deadline. Before, the existing unit tests could call it as a pure query, and the new second case does that too while a retry is pending. It's harmless there because that delay isn't clamped. Did you consider returning the clamped deadline through an out-param and storing it only at the two arming sites, after event_new succeeds? That would also remove the two extra clears on the event_new failure paths. Not blocking, just wondering if it's the smaller shape.
  • Just checking: the sticky m_fired_reconnect_deadline relies on "a run's deadline only moves later". Going from benchmark_start_time + T to the client's own get_start_time() + T once replies arrive should only ever move it later. Is there any path, e.g. the start-time handling after Keep a client's start time on reconnect once it has sent workload #865, where the stats start time could come before benchmark_start_time? If not, fine as is.
  • Did you consider simply widening the 1 ms slack? I guess the commit message already answers it: the monotonic-to-wall offset is only resampled every 5 s, so there's no firm bound to pick, and recording the clamped deadline avoids guessing one. I'm mentioning it only so the reasoning is on record in the review.

The fix looks solid to me. I'd just like the second point confirmed before merge.

(would file this as a comment, not a formal block)

connect() and disconnect() free a pending reconnect timer; clear the
deadline it was clamped to there as well, so m_reconnect_deadline is
zero whenever no clamped timer is pending, as its comment says.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks.

  • Stale m_reconnect_deadline. Fixed in two steps. 899a50b clears it once the timer fires, right after it's copied to m_fired_reconnect_deadline. 67c4a78 clears it next to the two event_free(m_reconnect_timer) calls in connect() and disconnect(). So it's now zero whenever no clamped timer is pending, as the comment says. The timer handler copies the deadline before it calls connect(), so the new clear in connect() can't lose it.
  • Can the deadline move earlier than a fired one? No. run_deadline() is the benchmark start (or this client's start, once it has started) plus --test-time. A client's start time is never before the benchmark start, and once set it only stays or moves later (Keep a client's start time on reconnect once it has sent workload #865/End a --test-time client that never had a reply at its deadline #869). So the deadline only moves later. A later deadline fails the !timercmp(&m_fired_reconnect_deadline, &deadline, <) guard, and the wall clock decides again. The review on 17d26e6 checked every caller of run_deadline_passed() / run_is_over() for this.

Sanitized make check passes, test_cluster_slots_bad_reply.py + test_reconnections.py (cluster) pass 52/52, and clang-format is clean.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Both done in 74bfa4e: the two event_new() failure branches (attempt_reconnect and the retry path in handle_reconnect_timer_event) now clear m_reconnect_deadline too, so it's zero whenever no clamped timer is pending. The header comment now says m_fired_reconnect_deadline is never cleared on purpose: a run's deadline only moves later, and a later deadline goes back to the clock. make check passes and clang-format is clean.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. On the three points:

  • Can the deadline move earlier? run_deadline() is benchmark_start_time + T until the client has answered requests, then get_stats()->get_start_time() + T. client::set_start_time() (client.cpp:374) sets that start time either to benchmark_start_time (staircase mode) or to gettimeofday() at the client's first reply, which comes after the benchmark started. Both Keep a client's start time on reconnect once it has sent workload #865 call sites (shard_connection.cpp:1865, :2625) go through it. So the deadline only stays or moves later. The one exception is a wall clock that steps backwards between the benchmark start and the first reply (an NTP step). That could also make the fired deadline later than the new one. run_deadline_passed() would then report "passed" up to the size of the step early, which is the same exposure every wall-clock deadline check here already has.
  • Out-param instead of the side effect. I considered it, and kept the current shape. next_reconnect_delay() already owns the clamp decision, and setting m_reconnect_deadline there keeps the "clamped" fact next to the computation. Both arming sites, and the tests that call it as a query, behave correctly: the query in the second unit case isn't clamped. Returning it through an out-param would move the same two lines to both callers. If a maintainer prefers that shape, it's a mechanical change.
  • Widening the slack. Right, that's the reasoning. The monotonic-to-wall offset libevent uses is only resampled periodically, so there's no firm bound to widen to. Recording the clamped deadline avoids picking a number.

Keep both new unit tests in reconnect_siblings_test.cpp (#894's
connection-rejection test and this PR's deadline-retry test).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira merged commit 181f451 into master Oct 26, 2026
58 of 59 checks passed
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.

Flaky: never-started cluster client can start at the --test-time deadline (test_cluster_slots_never_started_client_fails_at_deadline)

1 participant