← Back to Pull requests

feat: Add mergeBuffer/maxSpillProximity metric for groupBy spill diagnosis #19627

Closed
unknown closed this pull request without merging on Aug 25, 2026 20:14 UTC
View on GitHub
unknown unknown commented on Jun 24, 2026 18:23 UTC
Author

Description

When a groupBy query runs, ConcurrentGrouper divides the single acquired merge buffer into druid.processing.numThreads equal slices (sliceSize = capacity / numThreads) and gives one slice to each processing thread. A query spills to disk as soon as its fullest single slice fills — at roughly sizeBytes / numThreads, which can be far below the configured druid.processing.buffer.sizeBytes.

The existing metrics do not let an operator see this. mergeBuffer/maxBytesUsed is a per-query sum across slices, further discounted by the hash-table load factor, so it never approaches sizeBytes even while queries are actively spilling — making it impossible to compare against druid.processing.buffer.sizeBytes or to reason about spill pressure.

Concretely, an operator with sizeBytes = 125 MiB and numThreads = 240 (slices ≈ 546 KiB) saw groupBy/spilledQueries climbing while mergeBuffer/maxBytesUsed sat around ~60 MB, which looks contradictory until you account for slicing.

Change

This PR adds mergeBuffer/maxSpillProximity, a dimensionless gauge in [0.0, 1.0] that reports how close a groupBy query came to spilling to disk:

maxSpillProximity = MAX over queries of
                      MAX over that query's slices of
                        peak( size / terminalRegrowthThreshold )     // clamped to [0, 1]

  • It is computed per-slice and maxed (never summed), because the fullest slice is what actually triggers a spill.

  • It is bucket-count based, measured on the exact condition a BufferHashGrouper spills on: it can no longer allocate a new bucket. The denominator is the terminal-level regrowthThreshold — the bucket-count ceiling of the largest table the slice can grow to before it must spill. Because the table grows by doubling and size only grows (preserved across rehash), the ratio is monotonic and rises smoothly to exactly 1.0 at the real spill point (a bucket allocation is rejected, or the table is parked at its terminal threshold). Dividing by the current level's threshold instead produces a sawtooth — it resets to ~0.5 on every doubling and climbs back to ~1.0 before the next — which would pin the lifetime max near (N-1)/N for any query that grows even once, regardless of remaining headroom.

  • Being bucket-count based, the value is independent of bucket width, offset-list overhead, and integer truncation in the arena-size calculation.

  • Limit push-down queries read 0.0. A LIMIT query without an aggregator-dependent sort uses LimitedBufferHashGrouper, whose two-sub-buffer AlternatingByteBufferHashTable heap-trims to the top-limit entries and never disk-spills via that path. Its sub-buffer refills to the threshold on every swap, so it is deliberately excluded from fill-proximity recording (it would otherwise saturate near (T-1)/T ≈ 1.0 and read as a perpetual near-spill). Only a genuine bucket rejection on that table would report 1.0.
  • Operators can read mergeBuffer/maxSpillProximity alongside groupBy/spilledQueries: a value near 1.0 means slices are saturating, and the fix is to widen each slice by raising druid.processing.buffer.sizeBytes or lowering druid.processing.numThreads.

    Changed files


  • ByteBufferHashTable — track the lifetime-max size / terminalRegrowthThreshold ratio (getMaxSpillProximity()), pinned to 1.0 at the real spill trigger and preserved across reset(). computeSpillRegrowthThreshold() derives the fixed terminal denominator by replaying the arena geometry (no allocation, memoized); a recordsFillProximity() hook lets heap-trim tables opt out of fill recording.

  • LimitedBufferHashGrouper — its AlternatingByteBufferHashTable overrides recordsFillProximity() to false so heap-trim swaps are not misreported as spills; a genuine bucket rejection still pins 1.0.

  • AbstractBufferHashGrouper — expose getMaxSpillProximity() (0.0 when the grouper was never initialized).

  • GroupByStatsProvider — keep the per-slice max getSpillProximity() (clamped to [0,1], NaN ignored), aggregated as a max across queries.

  • SpillingGrouper — report each slice's peak proximity in close() (only when initialized).

  • GroupByStatsMonitor — emit mergeBuffer/maxSpillProximity.

  • docs/operations/metrics.md — document the new metric and clarify the slicing semantics of mergeBuffer/bytesUsed and mergeBuffer/maxBytesUsed.
  • <hr>

    This PR has:

  • been self-reviewed.

  • added documentation for new or modified features or behaviors.

  • a release note entry in the PR description. (New metric mergeBuffer/maxSpillProximity; no behavior or config changes.)

  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.

  • added integration tests. (N/A — metric is covered by unit tests.)

  • been tested in a test Druid cluster.
  • Diff: 627 lines (+470 / -157) across 7 files Reviewer: abhishekrb19 Review duration: 89390m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 627 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.385
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #19627"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.3% (MEDIUM) · Higher = larger blast radius
    50.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.9% · Safe to merge
    19.9%
    Repository