Skip to content

Ingester: keep only each tenant's ownership share - #7961

Merged
alanprot merged 18 commits into
cortexproject:masterfrom
alanprot:ownership-own-zone
Nov 24, 2026
Merged

alanprot merged 18 commits into
cortexproject:masterfrom
alanprot:ownership-own-zone

Conversation

@alanprot

@alanprot alanprot commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

What this PR does:

Follow-up to #7954. With -ingester.use-ring-ownership-for-limits, each ingester now keeps only the tenants' ownership shares, and recomputes a share only when the tenant's subring changes. In #7954 the refresher read a ring that cached every tenant's subring, the cache was dropped on any READONLY or topology change, and the delegate updated that ring inside the Lifecycler's heartbeat callback.

How it works

flowchart TD
    subgraph delegate["On every heartbeat, in the Lifecycler's callback"]
        hb["Ring desc about to be written"] --> cmp{"Instances, zones, tokens<br/>or READONLY changed?"}
        cmp -->|no| keep["Keep the old copy"]
        cmp -->|yes| copy["Keep a new copy"]
    end
    subgraph refresh["Every 10 s, in the refresh service"]
        same{"Copy changed since<br/>the last refresh?"}
        same -->|no| reuseAll["Reuse the shares"]
        same -->|yes| build["Build a ring from the copy"]
        build --> sub["For each tenant: build its subring<br/>and fingerprint its members"]
        sub --> fp{"Fingerprint changed,<br/>or a token held twice?"}
        fp -->|no| reuse["Reuse the share"]
        fp -->|yes| zo["Compute the share<br/>with ZonalOwnership"]
    end
    copy -.-> build
    reuseAll --> lim["Limiter: local limit = global limit × share"]
    reuse --> lim
    zo --> lim
Loading
  • Change detection. On each heartbeat the delegate compares the ring with the copy it keeps: the instances, and each one's zone, tokens and READONLY flag. Only those can move a shuffle-shard pick or a share, so heartbeats, addresses and other state changes keep the copy. The comparison allocates nothing.
  • One ring per refresh. When the copy changed, the refresh builds a ring from it, so a heartbeat cannot change what a refresh reads. The refresh runs in its own service instead of on updateLoop, where a multi-second refresh delayed the ingestion-rate tick.
  • A fingerprint per tenant decides whether its share is recomputed: the sum of xxhash hashes of each subring member's zone, READONLY flag and tokens, kept with this ingester's own hash when it is a member, since the sum does not say which member it is. While a token is held twice, an instance outside a subring can change its ownership, so every tenant is recomputed after each change. cortex_ingester_ring_ownership_computations_total counts the computations.
  • ZonalOwnership() without an operation (pkg/ring, unreleased): every instance owns its tokens' ranges except READONLY instances, and a token shared across zones counts in the zone Get sends its keys to.

Cost

3 zones × 1,000 instances × 128 tokens, 1,000 tenants:

master (#7954) this PR
heartbeat callback: no change / READONLY change / join or leave 2.6 ms / 2.6 ms / 215 ms 0.6 ms / 1.2 ms / 0.9 ms
refresh after heartbeats only (shard 30 / 300) 4.3 / 26.7 ms under 1 ms
refresh after a READONLY change (shard 30 / 300) 1.2 s / 18 s 0.8 s / 11.6 s
retained heap (shard 30 / 300) 73.5 / 431 MB 33 MB

After a change, most of the refresh is building every tenant's subring to check its fingerprint. Ownership is computed for 5 to 13 of 1,000 tenants at shard 30, and 81 to 117 at shard 300.

Known limitations

  • -distributor.extend-writes=true (the default). Shares no longer follow extend-writes: while an instance is JOINING, LEAVING or PENDING, the instance that takes its writes has a smaller share than it receives. On 3 zones × 30 instances, a tenant is throttled there at 83% of its global limit (whole ring) or 75% (shard 30), against 100% with Ingester: convert global limits to local using actual ring ownership #7954 and 82% / 68% with the equal-share conversion.
  • A change in an excluded zone triggers a full check, which recomputes nothing.
  • Pre-existing, in Ring.Get: a shard member's token shared with an instance outside the shard costs those keys a replica, and credits the member with keys it does not receive. The fix belongs in a separate PR.

Tests

  • Unit tests for change detection, fingerprint reuse, this ingester swapping tokens with a neighbour, shared tokens, changes during a refresh, excluded zones, cancellation and the delegate's lock, checking shares against ones computed on a separate ring; randomized runs of 300 ring changes, with and without shared tokens.
  • The refresh as the ingester runs it, on an ingester whose lifecycler heartbeats the ring: it publishes the tenant's share to the limiter, and a refresh stopped by the service's context publishes nothing.
  • 31 mutations of the new code are caught. Removing the refresh service from starting() is caught only by the Docker test below.
  • TestIngesterRingOwnershipLimits (9 ingesters, Docker): 3991 and 3979 of a 4000 limit stored, as with Ingester: convert global limits to local using actual ring ownership #7954.
  • go test -race on pkg/ring, pkg/ingester and pkg/cortex; golangci-lint clean.

Which issue(s) this PR fixes:
None filed. Follow-up to #7954.

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

@alanprot alanprot changed the title Ingester: compute ring ownership shares from the ingester's own zone Ingester: keep only each tenant's ownership share Nov 21, 2026
@alanprot
alanprot force-pushed the ownership-own-zone branch 3 times, most recently from 130fc87 to e041da2 Compare November 21, 2026 05:58
@alanprot
alanprot force-pushed the ownership-own-zone branch 2 times, most recently from f0b924d to 6298160 Compare November 23, 2026 17:23
ZonalOwnership took the write operation and left out the tokens of every
instance it extends the replica set past. Its only caller, added with it
in cortexproject#7954 and unreleased, now asks for one rule, so drop the parameter: every
instance owns the ranges of its tokens except READONLY instances, whose
ranges go to the next instance in their zone. The cache becomes one map,
cleared when tokens change or an instance becomes or stops being READONLY,
which RingCompare reports as EqualButReadOnly. A heartbeat or any other
state change no longer clears it.

Add HasDuplicateTokens, read from the ring's existing token lists. A token
maps to one instance ring-wide, so when instances of two zones share one,
one zone's ownership and shuffle-shard picks depend on an instance of the
other.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
@alanprot
alanprot force-pushed the ownership-own-zone branch 2 times, most recently from 8856dc6 to de4dde3 Compare November 23, 2026 17:54
The refresher computed every tenant's share on each 10s refresh by building
a shuffle-shard subring per tenant, and the ring it read from cached every
subring. A READONLY or topology change in any zone clears that cache, and
the next refresh rebuilt and recomputed every tenant: over a second per
1,000 tenants with shard size 30 at 3,000 ingesters, plus the cached
subrings, 43 to 401 MB.

Keep one share per tenant, with a fingerprint of the subring it was
computed from: each member's zone, tokens and whether it is READONLY. The
heartbeat delegate bumps a generation when an instance joins or leaves,
changes zone or tokens, or becomes or stops being READONLY, in any zone:
the only changes that can move a shuffle-shard pick or zonal ownership.
After a bump, the refresher builds each tenant's subring and computes
ownership only if its fingerprint changed. Heartbeats and other state
changes cost nothing. While two zones share a token, an instance outside
a subring can change its ownership, so every tenant is recomputed after a
bump. The ring is built with SubringCacheDisabled, since a subring is built
only to check or compute one share.

The delegate updates the ring and its record under one lock: heartbeat
callbacks can overlap, and otherwise the ring could hold one desc while the
record held another.

Shares now count every instance's tokens except READONLY instances'. With
-distributor.extend-writes=true, the default, the instances that take a
JOINING, LEAVING or PENDING neighbour's writes keep a smaller share than
they receive while the neighbour stays in that state. The ingester no
longer needs the distributor's extend-writes setting.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Two cases kept a wrong share after the ring changed back:

- While a token is shared across zones, a subring's ownership depends on an
  instance outside it. When the sharing ended, the subring's fingerprint was
  as before, so the share computed during the sharing was reused.
- Whole-ring tenants read the live ring. The whole-ring fingerprint was taken
  once per refresh while each tenant's share was read later, so a heartbeat in
  between could store a share from one ring under the fingerprint of another,
  and a ring that returned to the old fingerprint reused it.

Mark a share computed under shared tokens, or from a whole ring whose
fingerprint moved during the computation, as unsure, and recompute it after
the next change. Whole-ring tenants now share one share per refresh along
with the fingerprint.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
No change in behaviour. The refresher keeps the ring generation once rather
than per tenant, since every refresh visits every tenant, and the loop now
reads as three cases: nothing changed, the tenant's subring is as before,
or the share is computed. Computing the whole ring's fingerprint and share
once per refresh moves into wholeRingMemo, and publishing the shares into
publish.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
The delegate and the subring fingerprint hashed tokens with FNV-1a fed one
32-bit token at a time. Its low bits depend only on the tokens' low bits,
so the lowest bit is just their parity, and collisions are easy to build.

Hash each instance's zone (length-prefixed), READONLY flag and tokens with
xxhash, which the module already uses, through one instanceHasher shared by
the delegate and the fingerprint. The delegate now keeps one hash per
instance. Members are still summed, so identical members do not cancel.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
The refresh ran on updateLoop, the loop that also ticks the ingestion rate
every second. After a ring change at large shard sizes a refresh takes
seconds, and EwmaRate.Tick divides what it counted by one interval, so the
next tick inflated the rate and could trip
-ingester.instance-limits.max-ingestion-rate.

Run it in a timer service of its own, started with the ingester's other
subservices. On shutdown a refresh stops between tenants and leaves the
shares as they were.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
@alanprot
alanprot marked this pull request as ready for review November 23, 2026 21:17
@alanprot
alanprot requested a review from a team as a code owner November 23, 2026 21:17
@alanprot
alanprot requested a review from friedrichg November 23, 2026 21:17
A token maps to one instance ring-wide. When instances in two zones
share it, Get routes the token to the instance the token map holds, and
the other zone's keys before it go to that zone's next token.
ZonalOwnership credited that other zone's range to the instance in the
wrong zone, so that zone's shares summed to less than 1 and its next
instance was given too small a share.

Count a zone's token only if the token map holds it for an instance in
that zone. On a shuffle shard nothing changes: a token held by an
instance outside the shard was already skipped.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
The refresher read the ring the delegate updated on every heartbeat, so a
heartbeat could change it in the middle of a refresh. HasDuplicateTokens
was read once, at the start of a refresh: a heartbeat could share a token
after that read and before a tenant's subring was built, and another could
unshare it before the next refresh. The share, computed from the ring with
the token shared, was reused by the next refresh, because the instance
sharing the token was outside the subring and the fingerprint had not
changed. It stayed wrong until the tenant's subring changed.

Have the delegate keep a copy of the desc at each change that can move a
share, with a count of the changes, and have the refresher build a ring
from that copy whenever the count moved. A refresh reads one ring that
nothing else updates, so the whole-ring fingerprint no longer needs to be
read again after its share is computed.

A heartbeat without such a change now only hashes the instances: at 3,000
instances with 128 tokens each, 0.9 ms instead of 3 ms, and 1.6 ms instead
of 213 ms when an instance joins or leaves, since the delegate no longer
rebuilds a ring inside the Lifecycler's heartbeat callback. The refresher
builds the ring instead, in about 0.2 s, once per refresh after a change.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
To tell whether a heartbeat changed anything a share depends on, the
ownership delegate hashed every instance on each heartbeat, and kept the
hashes and a count of the changes. It already keeps a copy of the desc at
each change, so compare the new desc with that copy instead: the
instances, and each one's zone, tokens and READONLY flag. Addresses,
timestamps and other states are ignored, as before.

Each change replaces the copy with a new one, so the refresher tells a
change from the copy itself, and the count goes too.

At 3,000 instances with 128 tokens each, a heartbeat without such a change
takes 0.6 ms instead of 0.8 ms and allocates nothing instead of 111 KB; one
with a change takes 0.9 to 1.2 ms instead of 1.6 ms.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Only tests called refresh(userIDs), which passed context.Background() to
refreshContext. Rename refreshContext to refresh, so the refresher has one
method, and have the tests pass the context.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
A shuffle shard does not skip a READONLY instance: it keeps it and picks
one more instance in that zone. Say so in the comments that explain why
only READONLY, among the states, can move a pick.

In the CHANGELOG entry, a heartbeat still costs a comparison of the ring,
so say it causes no recomputation rather than nothing; every tenant is
recomputed while any token is held twice, within a zone too; and name the
new cortex_ingester_ring_ownership_computations_total metric.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
A subring's fingerprint sums its members' hashes, so it does not say which
member is this ingester. If this ingester and a neighbour in its zone
swapped their tokens, the fingerprint stayed the same and the old share
was reused, although this ingester now owns the neighbour's ranges.

Store this ingester's own hash with each share, or 0 when it is not in the
tenant's subring, and recompute when it differs. A change of this
ingester's instance then recomputes only the tenants whose subring it is
in; its share elsewhere stays 0.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
No test ran the refresh as the ingester wires it: a refresh that ignored
the service's context, or published nothing to the limiter, would have
passed. Move the service's iteration into Ingester.refreshOwnership and
test it on an ingester whose lifecycler heartbeats the ring: it publishes
its tenant's share to the limiter, and a refresh stopped by the context
publishes nothing. The ingester's services are not started, so the test
does not refresh at the same time as the service.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
-ingester.use-ring-ownership-for-limits is not released yet, so describe
it once, as it now behaves, under both pull requests.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Since shares are computed from a ring no heartbeat changes, the flag is
set only from HasDuplicateTokens: it records that a token was held twice
when the share was computed. Name it after that, so the reuse check reads
as no token held twice, then or now.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
@alanprot
alanprot merged commit b210a4c into cortexproject:master Nov 24, 2026
40 checks passed
@alanprot
alanprot deleted the ownership-own-zone branch November 24, 2026 16:24
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