Skip to content

Count requests dropped at reconnect under --retry-on-error as errors - #661

Merged
fcostaoliveira merged 4 commits into
masterfrom
fix-652-retry-drop-errors
Oct 16, 2026
Merged

fcostaoliveira merged 4 commits into
masterfrom
fix-652-retry-drop-errors

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Decision: with --retry-on-error, a request memtier sends and then gives up on (never resent, never answered) counts as an error and gets a --failed-keys-file row. client::disconnect_all() already does this for run-ended-before-resend since #620. Ops stay uncounted, as before. Without --retry-on-error, requests dropped on a disconnect are still only uncounted, as today.

Summary

With --retry-on-error, every request memtier sends and then gives up on without a reply is now logged to --failed-keys-file and counted as an error, wherever it is dropped. That covers the reconnect drain, the cluster hand-off, disconnect() under --transaction, and the end of a thread that lost its server. Before, only run-ended-before-resend was counted (#620). Every drop site goes through one helper, connections_manager::abandon_requests().

Problem

With --retry-on-error, a request dropped after its connection drops was logged to --failed-keys-file but never counted as an error. The run reported errors 0 (+0) next to hundreds of connection-dropped rows. A request abandoned when the run ends has been counted since #620, so whether a lost request reached the error total depended only on where it was dropped.

Root cause

The drop sites that run at reconnect or hand-off time called dec_reqs_generated() but never m_stats.inc_error():

  • shard_connection::drain_replay_queue_after_reconnect(): connection-dropped, no-captured-bytes-for-replay
  • cluster_client::hand_off_replay_requests(): transaction-not-replayed, no-slot-owner-for-replay, slot-owner-down-for-replay
  • shard_connection::disconnect() under --retry-on-error: transaction-not-replayed, plus requests with no captured bytes

Separately, when --max-reconnect-attempts ran out and the thread ended, the requests held for resend were freed with the connections, with no row and no error. That covered every connection on the thread, not only the one that ran out: on the same outage its sibling connections (other --clients on the thread) had already moved their in-flight requests into their own replay queues. With -t1 -c4 --pipeline 16 and the server gone, 64 requests vanished; counting only the connection that ran out still left 48 of them uncounted. Without --reconnect-on-error it was worse: the first EOF ends the thread before the siblings handle their own EOF, so their requests are still in flight, not even in a replay queue. With a real server, -t2 -c4 --pipeline=16 and SHUTDOWN, only 32 of 128 were counted at the previous head of this PR, and 0 on master.

Fix

  • connections_manager::abandon_requests(count) uncounts the requests (dec_reqs_generated(), as before) and counts each one as an error. run_stats::inc_error() now takes an optional count. Every drop site above uses it.
  • shard_connection::abandon_replay_requests(reason) logs and abandons everything still held for resend on a connection. Two places use it:
    • disconnect_all(), for run-ended-before-resend (same rows and counts as before);
    • client_group::run(), once the thread's event loop returns, for every connection of every client on the thread. Under --retry-on-error it first calls disconnect() on each connection that still has requests in flight, which moves them to its replay queue. It skips that on Ctrl+C: in-flight requests are dropped there as before.
      • The connection that ended the thread (either "Maximum reconnection attempts exceeded, ending this thread" path, in attempt_reconnect() or handle_reconnect_timer_event()) records its reason, and its requests are logged with it: max-reconnect-attempts-exceeded, or connection-lost-no-reconnect when --reconnect-on-error is off. The timer path is only armed from the --reconnect-on-error branch of attempt_reconnect(), so it always uses the first reason.
      • Every other connection's requests are logged as thread-ended-before-resend. This covers the siblings above and any other early loop break.
      • One stderr warning per thread gives the total, like run-ended-before-resend does.
      • A run that ended normally has already emptied the queues in disconnect_all(), and has nothing in flight, so nothing is counted twice.
      • Side effect: requests still waiting in a retry queue when Ctrl+C stops the loop are now logged and counted the same way. Before, they were freed silently.
  • No double counting: a request that is moved into the replay queue and later resent is not counted. It is counted once, at the place it is finally dropped.
  • memtier_errors_total's HELP text, docs/REALTIME_METRICS.md and the m_errors comment now say errors include requests given up on without a reply.
  • Failed-keys rows are unchanged, except for the new reasons max-reconnect-attempts-exceeded, connection-lost-no-reconnect and thread-ended-before-resend, plus not-replayable for the disconnect() fallbacks for requests with no captured bytes (only when malloc fails during capture). Those fallbacks had no row before.

Overlap with #669: open PR #669 adds drop_pending_requests() / abandon_unanswered_requests(). Whichever of the two PRs lands second should route its drops through abandon_requests(). This PR does not depend on #669.

Not given up on (unchanged, on purpose)

  • Max attempts reached while peers are still in setup (read-preference != primary, "leaving connection dead and continuing"), and a READONLY failure without --reconnect-on-error. Both call disconnect(), which keeps the held requests. They are not abandoned at that point. A CLUSTER SLOTS refresh either reconnects the connection and resends them, or retires it and hands them off through hand_off_replay_requests(), which counts what it can't place. Anything still held when the run ends is counted by disconnect_all() as run-ended-before-resend.
  • cluster_client::disconnect(), which deletes non-main connections. It is only reached through --reconnect-interval, and verify_cluster_option() rejects that option in cluster mode. In standalone, client::disconnect() keeps the single connection and its replay queue, which is drained after the reconnect.
  • slot-owner-down-for-replay now counts like the other hand-off reasons. It is not covered by a test, because it needs a node that dropped out of a previous map, gave up reconnecting, and is then named again as the slot owner.

Test evidence

Before = origin/master code. Mutant = this branch with the disconnect() --retry-on-error --transaction branch reverted to plain dec_reqs_generated.

test before mutant after
test_dropped_replays_are_counted_as_errors (new; CLIENT KILL SKIPME yes, connection-dropped) 5/5 FAIL (0 == 144, 0 == 176) n/a 8/8 PASS
test_transaction_requests_are_not_retried (now asserts errors == failed-keys rows) 3/3 FAIL (21 == 53, 16 == 48, 22 == 55) 3/3 FAIL (15 == 47, 14 == 54, 15 == 55) 6/6 PASS (3 standalone, 3 on a 3-shard cluster)
test_requests_dropped_at_hand_off_count_as_errors (new; scripted two-node cluster in test_cluster_failed_keys.py, no-slot-owner-for-replay) 5/5 FAIL (0 == 7 or 0 == 8) n/a 5/5 PASS
test_replays_abandoned_when_reconnects_run_out_are_counted (new; max-reconnect-attempts-exceeded) 5/5 FAIL (no rows) n/a 5/5 PASS
test_replays_held_by_sibling_clients_are_counted_when_thread_ends (new; -t1 -c4 --pipeline 16, --reconnect-on-error --max-reconnect-attempts=1, every connection closed, reconnect refused) master 3/3 FAIL (0 == 64); 5/5 FAIL (16 == 64) on the commit before 965f106 n/a 10/10 PASS
test_in_flight_requests_of_sibling_clients_are_counted_when_thread_ends (new; same, but the peer closes only the first connection) master 3/3 FAIL (0 == 32); 965f106 3/3 FAIL (8 == 32) n/a 10/10 PASS
test_requests_lost_without_reconnect_are_counted_when_thread_ends (new; -c4 --pipeline 8 --retry-on-error, no --reconnect-on-error, connection-lost-no-reconnect) master 3/3 FAIL (0 == 32); 965f106 3/3 FAIL (8 == 32) n/a 10/10 PASS

Real server, -t2 -c4 --pipeline=16 --retry-on-error, killed with SHUTDOWN NOSAVE from a --command: 965f106 logged and counted 32 rows; this head logs 128 rows (32 connection-lost-no-reconnect, 96 thread-ended-before-resend) with errors 128.

test_dropped_replays_are_counted_as_errors now also accepts run-ended-before-resend rows: a kill in the last ms of --test-time can leave held requests for disconnect_all(). It still asserts errors == rows.

How the hand-off test works:

  1. B owns 0-8191 and closes its socket when the first SET arrives, with up to 8 writes in flight.
  2. The next map leaves B out and 0-8191 unowned, so the held requests are dropped as no-slot-owner-for-replay.
  3. A then answers one SET with -MOVED, and memtier refreshes to a map where A owns every slot. All 50 requests complete.

Full files with this branch:

file env result
tests/test_retry_on_error.py --env oss 22/23 pass (rebased on 6eedafb). test_transaction_drops_not_logged_without_retry failed once with "no CLIENT KILL found a memtier connection", a timing miss in its kill loop, and passed 4/4 on rerun. That test does not use --retry-on-error, and it also passes 4/4 on 965f106.
tests/test_retry_on_error.py --env oss-cluster --shards-count 3 19/19 pass (before the rebase)
tests/test_cluster_failed_keys.py 7/7 pass (rebased on 6eedafb)

Fixes #652

Affected versions

2.4.0 through 2.5.2 (latest) and master.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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

Thanks for this. I went through the drop sites and the new client_group::run() sweep. Routing everything through abandon_requests() is a nice cleanup, and it makes the error total agree with the --failed-keys-file rows no matter where a request was dropped. The before/mutant/after table is the kind of fails-then-passes evidence this repo has accepted before (PR#364 is an example). I also checked that pop_replay_request() takes from both the replay and retry queues, so a sibling with only backed-off retries and nothing in flight is still swept even though the get_pending_resp() > 0 gate skips its disconnect().

One question, which you already half-flagged in the description: on Ctrl+C the sweep now counts requests sitting in a retry/replay queue as errors (thread-ended-before-resend), but requests still in flight are skipped because of the !interrupted gate. So the error total of an interrupted run depends on where each request happened to be when the user hit Ctrl+C, which is the same kind of inconsistency this PR fixes elsewhere. Did you consider skipping the sweep entirely when interrupted, or at least giving those rows their own reason, so a Ctrl+C'd run doesn't show errors that are really just "stopped early"? I may be missing a reason to prefer the current split.

Nitpicking: the comment above the sweep is fairly long for what the code does. Leaving it as is would be fine too.

The core change looks solid to me. Since it changes what errors / memtier_errors_total mean under --retry-on-error, it's probably worth a release-notes line, and someone who owns the retry/cluster hand-off code should take a look too, especially because of the overlap with #669.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Addressed in 965f106, which is also rebased onto current master (969c4de).

a. Flakiness in test_dropped_replays_are_counted_as_errors. Agreed, the timing can happen. A kill in the last ms of --test-time can leave held requests for disconnect_all(), which logs them as run-ended-before-resend. The test now accepts those rows as well as connection-dropped, and still asserts errors == rows, which is the invariant it is really about.

b. A later CLUSTER SLOTS hand-off after event_base_loopbreak(). I don't think one can happen. The hand-off only runs from a libevent callback: a CLUSTER SLOTS reply, or a timer. event_base_loopbreak() stops the loop when the current callback returns, so no other callback runs on that base, and the thread then destroys its clients. Looking at this path again turned up a real gap next to it, though. Only the connection that ran out had its queue abandoned. The other connections on the same thread had already moved their in-flight requests into their own replay queues on the same outage, and those were freed silently. With -t1 -c4 --pipeline 16 that was 16 rows out of 64. client_group::run() now abandons everything still held on every connection of the thread once the loop returns (thread-ended-before-resend). The second terminal path, in handle_reconnect_timer_event(), now abandons its own queue too; before, it abandoned nothing. The new test test_replays_held_by_sibling_clients_are_counted_when_thread_ends failed 5/5 on the previous head (16 == 64) and passes 20/20 now.

c. The "Account it like any other drop" comment. Reworded to "It cannot be resent: abandon it." That fallback, and the in-flight one above it, now also log a not-replayable row, so they match their error count.

Also in this push: the reason is connection-lost-no-reconnect when --reconnect-on-error is off, there is a stderr summary for the requests abandoned when reconnects run out, and the memtier_errors_total HELP text and docs now include requests given up on without a reply.

fcostaoliveira and others added 4 commits October 16, 2026 09:29
With --retry-on-error, a request memtier gives up on after its connection
drops is logged to --failed-keys-file but was never counted as an error,
so the run reported "errors 0". Since #620 a request abandoned when the
run ends is counted, so whether a lost request reached the error total
depended only on where it was dropped.

Add connections_manager::abandon_requests(), which uncounts the requests
from the generated total (as the drop sites already did) and counts each
one as an error, and use it at every place a request held for resend is
dropped for good:

- drain_replay_queue_after_reconnect(): "connection-dropped" (max
  retries reached) and "no-captured-bytes-for-replay"
- cluster_client::hand_off_replay_requests(): "transaction-not-replayed",
  "no-slot-owner-for-replay", "slot-owner-down-for-replay"
- shard_connection::disconnect() under --retry-on-error:
  "transaction-not-replayed" and requests with no captured bytes
- client::disconnect_all(): "run-ended-before-resend" (unchanged count)

A request moved to the replay queue and later resent is not counted; it is
counted only where it is finally dropped. Requests dropped on a disconnect
without --retry-on-error are still only uncounted, as before. The
failed-keys rows are unchanged.

Fixes #652

Co-Authored-By: Claude Opus 5.5 <[email protected]>
When --max-reconnect-attempts is exhausted the thread ends, and the
requests held for resend on that connection were freed with it: no
failed-keys row and no error. Log them as "max-reconnect-attempts-exceeded"
and count them as errors, through a new
shard_connection::abandon_replay_requests(), which disconnect_all() now
uses too for "run-ended-before-resend".

The other connections that disconnect() and keep their held requests
(max attempts reached while peers are mid-setup, READONLY failure without
--reconnect-on-error) are not giving up: a CLUSTER SLOTS refresh reconnects
them and resends, retires them and hands the requests off, or the run end
counts them in disconnect_all().

Tests:
- test_transaction_requests_are_not_retried: the error total equals the
  failed-keys rows, so the --transaction drop in disconnect() is covered.
- test_requests_dropped_at_hand_off_count_as_errors: a scripted two-node
  cluster drops a connection with writes in flight and retires it with
  their slot unowned ("no-slot-owner-for-replay").
- test_replays_abandoned_when_reconnects_run_out_are_counted.

Refs #652

Co-Authored-By: Claude Opus 5.5 <[email protected]>
When one connection runs out of --max-reconnect-attempts it breaks the
thread's event loop. Only that connection's replay queue was abandoned:
the other connections on the same thread had already moved their
in-flight requests to their own replay queues on the same outage, and
those were freed with the connections, without a --failed-keys-file row
or an error. With -c4 --pipeline 16 only 16 of the 64 lost requests were
counted.

client_group::run() now abandons, once the loop returns, every request
still held for resend on any connection of the thread
("thread-ended-before-resend"); a normal run end has already emptied
them. The connection that ran out logs its own requests as
"max-reconnect-attempts-exceeded", or "connection-lost-no-reconnect"
without --reconnect-on-error, with a stderr summary; the
handle_reconnect_timer_event() terminal path now does the same instead
of nothing. disconnect()'s no-captured-bytes fallbacks log a
"not-replayable" row too. The memtier_errors_total HELP text and the
m_errors comment now include requests given up on without a reply.

Fixes #652

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Without --reconnect-on-error, the first connection that loses its server
ends the whole thread before the other connections on it handle their
own EOF. Their requests are still in flight, not in a replay queue, so
the client_group::run() sweep missed them: with -t2 -c4 --pipeline=16
and a SHUTDOWN, 32 of the 128 lost requests were counted. The same
happens with --reconnect-on-error when only one connection drops.

Under --retry-on-error, the sweep now disconnects every connection that
still has requests in flight, which moves them to its replay queue,
before abandoning the queue. On Ctrl+C in-flight requests are still
dropped as before.

The connection that ends the thread now only records its reason; the
sweep logs its requests with it, so one stderr warning covers the whole
thread instead of one per connection plus one for the thread.

Fixes #652

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

Copy link
Copy Markdown
Collaborator Author

Thanks. 63a3295 addresses the points from your 08:22 edit and is rebased onto master 6eedafb.

a. Description out of date. Updated. It lists every reason this PR adds: max-reconnect-attempts-exceeded, connection-lost-no-reconnect, thread-ended-before-resend and not-replayable.

b. Sibling test failing before the fix. Confirmed. It fails 3/3 on master (0 == 64) and 5/5 on the commit before 965f106 (16 == 64). It passes 10/10 at this head. Both results are now in the evidence table.

c. connection-lost-no-reconnect untested. Added test_requests_lost_without_reconnect_are_counted_when_thread_ends: -c4 --pipeline 8 --retry-on-error, no --reconnect-on-error, and the peer closes every connection after its pipeline. Writing it showed the previous head was still short. Without --reconnect-on-error, the first EOF ends the thread before the siblings handle their own EOF. Their requests were still in flight, not in a replay queue, so the sweep missed them: 8 of 32 rows, and 32 of 128 with a real server and SHUTDOWN at -t2 -c4 --pipeline=16. Under --retry-on-error, the client_group::run() sweep now calls disconnect() first on every connection that still has requests in flight, but not on Ctrl+C. A second new test covers the --reconnect-on-error variant, where only one connection drops. Both fail 3/3 on master (0 == 32) and on 965f106 (8 == 32), and pass 10/10 now. I did not add a test for not-replayable: it needs a malloc failure.

d. Timer-path reason. Confirmed. handle_reconnect_timer_event() runs only from the timer that attempt_reconnect() arms in its --reconnect-on-error branch, so max-reconnect-attempts-exceeded is always the right reason there. There is now a comment saying so.

e. Double warnings. Before, the per-connection line and the thread-level line counted disjoint sets, so nothing was double-counted, but it was two lines for one outage. They are now one line. The connection that ends the thread only records its reason, and the sweep logs its requests with that reason. The thread gets one "N request(s) held for resend when the thread ended ..." warning. disconnect_all() keeps its own run-ended-before-resend warning, which covers a different case: the run reached its end.

Earlier question: a CLUSTER SLOTS hand-off after event_base_loopbreak(). No. A hand-off only runs from a libevent callback, and none runs on that base once the loop breaks. The only code after the loop is the client_group::run() sweep, which abandons what is left, and then the thread tears the clients down.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

On the 09:51 point about Ctrl+C:

The asymmetry is intentional. A request in a retry or replay queue at Ctrl+C has already been sent at least once and been given up on by the server or the connection. memtier was holding it only to resend it, and the interrupt is what stops that resend, so it is logged and counted, as run-ended-before-resend already is at a normal run end (#620). A request still in flight on a healthy connection at Ctrl+C has not failed. The user stopped the run before its reply arrived. Counting it as an error would turn every Ctrl+C into a burst of errors proportional to pipeline depth, so it is dropped as before.

The exit code and the reported window are unchanged. The PR body's fix section states the Ctrl+C semantics. I've kept the reason thread-ended-before-resend rather than adding an interrupt-specific one, because the CSV row already shows the run was interrupted (the JSON's Interrupted: true), and a separate reason would split one category in two for no extra information. If you'd prefer a distinct interrupted-before-resend, it's a one-line change.

The sweep comment is long because it documents those two cases. I'll shorten it if you want.

@fcostaoliveira
fcostaoliveira merged commit 577e799 into master Oct 16, 2026
129 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
…ts()

#661 added connections_manager::abandon_requests() as the single path for
"uncount and count as an error". Use it in drop_pending_requests() instead
of dec_reqs_generated() plus a separate inc_error() loop, so every
give-up site counts the same way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

--retry-on-error: requests dropped at reconnect (connection-dropped, slot-owner-down-for-replay, ...) are not counted as errors, unlike end-of-run drops

1 participant