Skip to content

ring: count each key once when it reaches quorum in DoBatch - #7975

Merged
alanprot merged 5 commits into
cortexproject:masterfrom
alanprot:dobatch-quorum-once
Nov 27, 2026
Merged

alanprot merged 5 commits into
cortexproject:masterfrom
alanprot:dobatch-quorum-once

Conversation

@alanprot

Copy link
Copy Markdown
Member

What this PR does:

batchTracker.record decrements rpcsPending on every success at or past a key's quorum, not only on the success that reaches it. With RF 3, a fully replicated key is counted twice. In rare cases, this lets DoBatch return success before every key has reached quorum, so an error from a key that failed on all its replicas is not returned.

Example with two keys and RF 3 (quorum 2). Key A succeeds on all 3 replicas, and key B fails on all 3:

Recorded A successes rpcsPending
start 0 2
a1 ok 1 2
a2 ok 2 (quorum) 1
a3 ok 3 0: DoBatch returns nil
b1, b2, b3 fail (not reported)

This only happens when the failures are recorded after the successes. If B's failure is recorded first, the error is returned as expected.

This change counts each key once, on the success that reaches its quorum, using the same shape as grafana/dskit#430, which fixed this in dskit's copy of DoBatch. Successes past quorum still skip the "not enough remaining instances" check. minSuccess is also clamped to at least 1, so a replication set with MaxErrors >= len(Instances) still completes on the first success. No in-tree replication strategy produces such a set.

Since DoBatch now waits until every key reaches quorum, a push takes as long as its slowest series needs to reach quorum, and returns that series' error if it misses quorum.

Tests:

  • pkg/ring: scripted completion orders (4xx/5xx, keys at exactly quorum, error-family rules, MaxErrors = 0, a replica filtered out, MaxErrors >= len(Instances)), every result and completion order over small layouts, and concurrent runs over a real ring that also check DoBatch never answers success before every key has quorum.
  • pkg/distributor: Push fails when one series is rejected by all its ingesters, including when the ingesters store the rest of the RPC and fail it with 400.
Notes for reviewers
  • Whole-RPC attribution. An ingester that rejects one series stores the rest and fails the whole Push, and DoBatch counts that failure against every series in it. If two ingesters each reject a different series, a series sent to both counts as failed on 2 of 3 although all three stored it, and the push returns 400. Before this change it returned 200 or 400 depending on response order; now it is always 400. TestDistributor_Push_RejectionsOnTwoIngestersFailASeriesStoredOnAll pins this. Per-series attribution is out of scope.
  • Latency. Virtual-time simulation over a real ring, 2,000 keys, lognormal RPC latency with a 2 ms median: with 30 ingesters P99 goes from 3.1 to 7.8 ms; with 1% of RPCs stalling 100 ms, from 3.3 to 101.8 ms. The old numbers are those of the early return this PR removes. BenchmarkBatch* (no latency) is unchanged.
  • Stream push. With -distributor.use-stream-push, PushStreamConnection sends to streamPushChan without checking the context, so an RPC can outlive -distributor.remote-timeout. A push now waits for quorum on every series, so two stuck replicas of one series hold it open. Not changed here.
  • Unchanged, pre-existing: two instance IDs sharing one Addr are counted as two replicas; zero keys, or an empty replication set, wait until the context deadline; the first key to fail decides the error family of the whole request.
  • Alertmanager callers pass a single key, so their behaviour does not change.

Which issue(s) this PR fixes:
None filed.

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags

batchTracker.record decremented rpcsPending on every success at or past
a key's quorum. With RF 3 a fully replicated key counted twice, so in
rare cases DoBatch returned success before every key reached quorum,
and an error from a key that failed on all its replicas was not
returned.

Count a key only on the success that reaches its quorum, as dskit does
(grafana/dskit#430). Successes past quorum still skip the
remaining-instances check. Require at least one success per key, so a
replication set with MaxErrors >= len(Instances) cannot leave the key
uncounted.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Scripted tests fix the order in which DoBatch records each instance's
outcome: fully replicated keys next to keys that miss quorum (4xx and
5xx, several orders), keys at exactly quorum, error family selection,
a replica filtered out, MaxErrors 0 and MaxErrors >= len(Instances),
failed RPCs counting for every key in them, and outcomes recorded after
the context is canceled.

TestDoBatch_EveryCompletionOrder checks every result and completion
order over small layouts. Randomized tests, one over a real ring, check
concurrent completion against the quorum of each key, and that DoBatch
never answers success before every key has quorum among the RPCs that
have returned.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Push fails when a series is rejected by all its ingesters while the
other series replicate fully. mockIngester can now reject chosen series
the way a real ingester does: store the others and fail the whole RPC
with 400. Tests pin how such a failure counts against every series in
the RPC.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
@alanprot
alanprot requested a review from a team as a code owner November 27, 2026 20:06
@alanprot
alanprot requested a review from friedrichg November 27, 2026 20:06
Signed-off-by: Alan Protasio <alanprot@gmail.com>
@alanprot
alanprot merged commit fcc660d into cortexproject:master Nov 27, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants