Skip to content

fix: apply locality filter in statsForSpan to prevent ApproximateTotalStats over-count - #173216

Open
waterWang wants to merge 1 commit into
cockroachdb:masterfrom
waterWang:fix/spanstats-locality-filter
Open

fix: apply locality filter in statsForSpan to prevent ApproximateTotalStats over-count#173216
waterWang wants to merge 1 commit into
cockroachdb:masterfrom
waterWang:fix/spanstats-locality-filter

Conversation

@waterWang

Copy link
Copy Markdown

Summary

SpanStats.ApproximateTotalStats is intended to approximate the post-replication (physical) MVCC size of a span — roughly logical_size × replication_factor. Instead, it scales with the number of nodes the span's ranges live on, not the replication factor. For a span that occupies more nodes than RF, ApproximateTotalStats over-reports by numNodesContacted / RF.

Root cause

In statsForSpan, every fanned-out node iterates all range descriptors overlapping the span and collects MVCC stats for every range — regardless of whether the node holds a replica of that range. The coordinator's collectSpanStatsResponses then sums each node's full logical TotalStats into ApproximateTotalStats:

res.SpanToStats[spanStr].ApproximateTotalStats.Add(spanStats.TotalStats)

Since each node returns the full logical size for all ranges, the sum equals numNodes × logical instead of RF × logical. The bug is masked for spans confined to exactly RF nodes (where numNodes == RF) and only appears once a span is wide enough to live on more than RF nodes.

Fix

Apply a locality filter in statsForSpan: only collect MVCC stats for ranges where the current node holds a replica. This makes each node's contribution its actual share of the span, so the coordinator's sum across nodes yields the true replicated (RF × logical) total.

The metadata (RangeCount, ReplicaCount, StoreIDs) is still recorded for every range so the coordinator retains complete span-wide information.

Fixes #173182

@waterWang
waterWang requested a review from a team as a code owner August 8, 2026 16:27
@waterWang
waterWang requested review from jasonlmfong and removed request for a team August 8, 2026 16:27
@blathers-crl

blathers-crl Bot commented Aug 8, 2026

Copy link
Copy Markdown

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

Before a member of our team reviews your PR, I have some potential action items for you:

  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Aug 8, 2026
@cockroachlabs-cla-agent

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

server: SpanStats ApproximateTotalStats over-counts, scaling with node count instead of replication factor

1 participant