Skip to content

Count requests dropped after -READONLY as ops - #593

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix/readonly-drop-accounting
Oct 14, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix/readonly-drop-accounting

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With --cluster-mode and --read-preference other than primary, a request that a replica rejects with -READONLY and that isn't retried is never counted as an op. A --requests run exits 0, but Totals Count (plus latency and bytes) comes out below the requested total. In OSS cluster this happens when a write script without a shebang (EVAL "...redis.call('set',...)") is marked --command-is-read: the replica runs it and the write inside fails with -READONLY. With -t 2 -c 2 --requests 500 (2000 requests), master reports Count 649–694.

Root cause

The -READONLY branch of cluster_client::handle_response() ends a dropped request in finalize_dropped_redirect(), which only logs the failed key and calls m_stats.inc_error(). MOVED/ASK also call update_redirect_stats(..., completed=true), which counts the op (#582). The READONLY branch returns without that, so client::handle_response()'s update_*_op() never runs. shard_connection::process_response() still calls inc_reqs_processed(), so the run ends on time and the #557/#559/#575 processed/generated accounting is consistent. Only the op stats are short.

Fix

Add cluster_client::finalize_dropped_readonly(). It calls finalize_dropped_redirect(), then records the op the same way client::handle_response() records any other error reply: update_{get,set,wait,arbitrary}_op() with latency from m_first_sent_time. Both READONLY drop sites use it. MOVED/ASK are unchanged. The dead request && guards in the READONLY branch are now assert(request != NULL). shard_connection::process_response() dereferences the popped request before it calls handle_response().

Tests

tests/test_read_preference_modes.py::test_read_preference_readonly_reply_counted runs the EVAL write script with --command-is-read --read-preference=secondary on a cluster with replicas (-t 1 -c 2 -n 100). It asserts that the replicas answered -READONLY (errorstat_READONLY > 0) and that Totals Count == 200. It uses --read-preference-fallback=primary to stay clear of a separate stall, #602.

Local RLTest (--env oss-cluster --shards-count 3 --use-slaves), 5 runs each:

Binary Result Totals Count
origin/master e4d14e8 5/5 FAIL 112, 80, 111, 78, 74 of 200
this branch 5/5 PASS 200

Manual repro, -t 2 -c 2 --requests 500 on a local 3+3 cluster: master Count 649 / 663 / 694, this branch 2000 / 2000 / 2000. Exit 0 in all runs.

tests/test_cluster_readonly_drop.py::test_readonly_refused_retry_counted covers the second drop site, where a live primary refuses the retry. The cluster test above only reaches the first site, where there is no live primary for the slot. A scripted RESP peer names itself the only primary and answers every SET with -READONLY. It runs --cluster-mode --ratio 1:0 --retry-on-error --max-retries 0 -n 20, so enqueue_retry() refuses. The test asserts:

  • Sets/Totals Count == 20 and the histogram count == 20
  • a loose latency floor (the peer delays each reply by 5 ms, and the test requires min and avg latency ≥ 2.5 ms)
  • Time-Serie Bytes RX/TX exactly equal to what the peer saw
  • one --failed-keys-file row per -READONLY

The cluster test also asserts Evals.Count == Totals.Count, Totals Bytes TX ≥ Count × the smallest possible request, and 0 < failed-keys rows ≤ replica errorstat_READONLY. That check is a bound, because a slot-map refresh can retire a replica connection before memtier reads its reply, and the request is then replayed on the primary. The test skips on Redis < 6.2, which has no INFO ERRORSTATS.

Mutation results with both tests:

Mutant Result
no fix (both sites reverted) killed by both tests
revert site 1 only killed (cluster test: Count 69–73/200)
revert site 2 only killed (scripted test: Count 0/20)
rt_arbitrary recorded via update_get_op killed (Evals.Count != Totals.Count)
bytes zeroed killed by both tests
latency zeroed killed (scripted test latency floor)
failed-keys logging dropped killed by both tests

Stability with the fix: the scripted test passed 15/15 and the cluster test 8/8.

Behavior change (release notes)

Read-preference runs (--read-preference != primary) that hit -READONLY now count those requests as ops. Totals and per-command Count, latency (histograms and percentiles), Bytes RX/TX, KB/sec and Ops/sec all change for such runs. Results from those runs are not comparable with 2.5.0–2.5.2. Runs that never see -READONLY are unaffected.

Fixes #592

Affected versions

2.5.0 through 2.5.2 and master. The branch was introduced by ee757e7 (#456, --read-preference); its first tag is 2.5.0. Confirmed by running on master e4d14e8, 2.5.2 and 2.5.0 (3/3 runs undercount on each). 2.5.1 is derived from history.

🤖 Generated with Claude Code


Note

Medium Risk
Touches cluster error/response accounting on the -READONLY path; behavior change for read-preference workloads that see READONLY, but logic mirrors existing error op recording.

Overview
Fixes under-counted benchmark stats when cluster mode drops a request after a -READONLY reply and does not retry it (e.g. read-preference to replicas, or --max-retries=0).

Previously those paths only used finalize_dropped_redirect(), which logs failed keys and bumps errors but does not record an op like MOVED/ASK’s redirect stats or normal error handling in client::handle_response(). Totals Count, latency, and byte metrics could fall short of --requests even though the run completed.

The PR adds finalize_dropped_readonly(), which keeps the redirect-style failure logging and then calls the appropriate update_{get,set,wait,arbitrary}_op() with latency from m_first_sent_time. Both -READONLY terminal drop sites in handle_response() use it. The READONLY branch also assert(request != NULL) after shard_connection always pops a request per reply.

Tests: a scripted RESP peer (test_cluster_readonly_drop.py) for refused primary retry, and a cluster integration test (test_read_preference_modes.py) for EVAL misclassified as read on replicas.

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

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

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

Thanks for this. The root cause matches what I see in handle_response(). The READONLY branch was the only terminal path that didn't count the op. The two tests also cover both drop sites, which the first commit didn't, and that matters.

One question:

finalize_dropped_readonly() copies the update_{get,set,wait,arbitrary}_op() switch from client::handle_response(). paulorsousa asked something similar on PR#320: "it would be nice for all errors to go through the same path. Does that sound feasible?" Could the switch move into a small protected helper on client, say record_op(timestamp, request, response), that both call? The two copies have already drifted a bit. The cluster copy skips the --scan-incremental-iteration cursor reset on error. And the default: assert(0) becomes a silent no-count under NDEBUG if a request type is ever added to one switch but not the other. That's the same class of undercount this PR fixes. I don't think it blocks the fix, so if you'd rather keep this diff minimal, a follow-up is fine.

Nitpicking: test_read_preference_readonly_reply_counted makes its directory with tempfile.mkdtemp() and never removes it. Not a big deal if the other tests in that file do the same.

The request && → assert(request != NULL) change looks right to me, since process_response() already dereferences the popped request before this point. The change to the numbers is a bugfix and the PR documents it honestly. No JSON keys or schema change, so I don't see a compat concern. The fix looks solid to me. I'd just like an answer on the shared-helper question before this merges.

(I'd file this as a comment, not a formal block.)

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Replying to the automated first-pass review, point by point. The new head is d440157, rebased onto master after #584, #594 and #601.

1. The finalize_dropped_readonly() switch duplicates client::handle_response().
I kept this as is for now. The switch in client::handle_response() does more than the update_*_op() calls. Its rt_arbitrary case also does per-key miss accounting from the parsed reply shape, and SCAN cursor tracking. A shared helper would have to split that case in two, and that is a larger refactor than this fix needs. The copy here stays limited to the four update_*_op() calls and uses the same latency reference (m_first_sent_time), so it records an error op the same way. If the maintainers prefer the helper, I can do it here or in a follow-up.

2. Only one of the two drop sites is covered.
Good catch, but the sites were the other way round. Mutation testing showed that the original EVAL/replica test reaches the first site (no live primary for the slot, so the slots are refreshed and the request is dropped). Reverting that call fails the test. It never reached the second site, where a live primary refuses the retry, and reverting that call passed. d440157 adds tests/test_cluster_readonly_drop.py for the second site. A scripted RESP peer names itself the only primary and answers every SET with -READONLY. It runs --cluster-mode --ratio 1:0 --retry-on-error --max-retries 0 -n 20, so enqueue_retry() refuses on the live primary. The test asserts:

  • Sets/Totals Count == 20 and the histogram count == 20
  • a loose latency floor (the peer delays each reply by 5 ms, and the test requires min and avg latency ≥ 2.5 ms)
  • Time-Serie Bytes RX/TX exactly equal to what the peer sent and received
  • one --failed-keys-file row per -READONLY

The cluster test now also asserts Evals.Count == Totals.Count, Totals Bytes TX ≥ Count × the smallest possible request, and 0 < failed-keys rows ≤ replica errorstat_READONLY. That last check is a bound and not an equality, because a slot-map refresh can retire a replica connection before memtier reads its reply, and the request is then replayed on the primary. It now skips on Redis < 6.2, which has no INFO ERRORSTATS.

Mutant results. Each column is the result with both tests together.

Mutant Result
revert site 1 killed (cluster test: Count 69–73/200)
revert site 2 killed (scripted test: Count 0/20)
rt_arbitrary recorded via update_get_op killed (Evals.Count != Totals.Count)
bytes zeroed killed (both tests)
latency zeroed killed (scripted test latency floor)
failed-keys logging dropped killed (both tests)

Stability with the fix: the scripted test passed 15/15 and the cluster test 8/8 after the bound change.

Nit: rt_wait is logged as "REDIRECT".
Agreed that the label reads oddly. As you say, it can't happen on this path, because a WAIT never gets -READONLY. Renaming the label would change what existing --failed-keys-file consumers see for MOVED/ASK drops, so I left it out of this fix.

I also replaced the dead request && guards in the READONLY branch with assert(request != NULL). shard_connection::process_response() dereferences the popped request before it calls handle_response().

🤖 Generated with Claude Code

fcostaoliveira and others added 2 commits October 14, 2026 04:38
In cluster mode with --read-preference other than primary, a request a
replica rejects with -READONLY and that is not retried ended in
finalize_dropped_redirect(), which only bumps the error counter. For
MOVED/ASK the caller also counts the op through update_redirect_stats(),
but the READONLY branch returns without it and never reaches
client::handle_response(). The request was still counted as processed,
so a --requests run ended on time, but Totals Count, latency and bytes
left out every -READONLY reply.

Add finalize_dropped_readonly(), which does what finalize_dropped_redirect()
does and also records the op the way client::handle_response() does for
any other error reply. Use it for both READONLY drop paths.

The regression test runs a write script marked --command-is-read under
--read-preference=secondary, so replicas answer -READONLY, and checks that
Totals Count equals the requested total.

Fixes #592

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The #592 regression test only reached the first finalize_dropped_readonly()
call (no live primary for the slot) and only checked Totals Count, so
reverting the second call, recording the op under Gets, or zeroing its bytes
or latency all passed.

Add test_cluster_readonly_drop.py: a scripted RESP peer names itself the
only primary and answers every SET with -READONLY. memtier re-routes the
request to that live primary, and --max-retries=0 makes enqueue_retry()
refuse, so every request is dropped through the second call. The test
checks Sets and Totals Count, the histogram count, a latency floor from a
5 ms reply delay, the exact bytes the peer saw, and one --failed-keys-file
row per -READONLY.

In the cluster test, also check that the ops land under Evals, that Totals
Bytes TX covers at least the smallest request per op, and that the dropped
requests are logged. A replica can count more -READONLY replies than memtier
reads, since a slot map refresh may retire the connection first and replay
the request on the primary, so that check is a bound. Skip on Redis older
than 6.2, which has no INFO ERRORSTATS.

The request && guards in the READONLY branch were dead: shard_connection
pops a request for every reply it hands to handle_response(). Assert it.

Refs #592

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.

Cluster --read-preference: requests dropped after -READONLY are not counted as ops (Count < --requests total)

1 participant