← Back to Pull requests

fix: Optimizes SpillingGrouper for high cardinality dimension(s) GroupBy with large memory footprint aggregators #19357

Closed
unknown closed this pull request without merging on May 07, 2026 17:58 UTC
View on GitHub
unknown unknown commented on Apr 21, 2026 00:24 UTC
Author

Optimizes SpillingGrouper for high cardinality dimension(s) GroupBy with large memory footprint aggregators

Description

tldr; Batch small spill files in SpillingGrouper to reduce file count

Problem

When aggregators like HLL sketches (or ThetaSketch) are used in groupBy queries, the BufferHashGrouper pre-allocates a large fixed-size buffer per group slot (e.g. ~64KB per slot for HLL with lgK=16). This causes the in-memory grouper to fill up quickly and spill frequently (as the number of unique grouping the BufferHashGrouper can store in memory is low). However, when each key has only been seen a few times (i.e. high cardinality dimension(s) GroupBy), the sketch serializes to a compact form of just a handful of bytes (HLL in List mode). The result is thousands of tiny spill files on disk — each one only a few KB despite the grouper buffer being full.

Large number of spill files can cause OOM when GroupBy merges spill files by opening all of them simultaneously. We previously prevent this by adding guardrail in https://github.com/apache/druid/pull/19141 which would fail the query. This PR fix the issue and will allow the query to succeed.

Fix

Instead of writing every grouper flush directly to its own spill file, small flushes (serialized size < 1MB) are batched in heap memory. When the accumulated pending bytes reach the 1MB threshold, all pending runs are merge-sorted and written as a single consolidated file. Large flushes (>= 1MB) bypass batching and go directly to disk as before.

The approach avoids holding the full serialized data in heap during the spill by streaming directly to a temp file first, then reading back only small files (< 1MB) into memory for batching. This prevents OOM risk when the serialized size happens to be close to the buffer size.

##### Key changed/added classes in this PR
All changes are in SpillingGrouper.java:

- spill(): Streams grouper contents to a temp file first. If the file is small (< MIN_SPILL_FILE_BYTES), reads it back into pendingSpillRuns and deletes the temp file. If large, keeps it on disk. Triggers flushPendingRunsToDisk() when accumulated pending bytes reach the threshold.
- flushPendingRunsToDisk() (new): Deserializes all pending in-memory runs, merge-sorts them (each run is individually sorted from grouper.iterator(true)), and writes the merged result as a single sorted spill file. Also writes a dictionary file for the accumulated dictionary entries.
- deserializeIterator() (new / refactor): Extracted the deserialization transform (deserialize aggregator values, Integer→Long coercion) that was previously inlined in iterator(). Now shared between iterator() and flushPendingRunsToDisk().
- reset() / close(): Clear pending state (pendingSpillRuns, pendingSpillBytes, pendingDictionaryEntries).
- iterator(): Calls flushPendingRunsToDisk() before iteration to flush any runs that didn't reach the threshold during the spill phase.

Memory overhead

Peak additional heap usage is bounded by ~2 × MIN_SPILL_FILE_BYTES (2MB): up to 1MB of accumulated pending byte arrays plus one file being read back. The grouper buffer itself is an off-heap direct ByteBuffer and is reset before the read-back.

Optimizations considered but not pursued



Combine duplicate keys during flush (combineByKey)



During flushPendingRunsToDisk(), we tried deserializing all pending runs, combining entries with duplicate keys by merging their aggregator values (using AggregatorFactory.combine()), then serializing the deduplicated result. The idea was to reduce the output file size and the final merge fan-in.

Flame graph analysis showed combineByKey itself consumed only 28 CPU samples — negligible overhead. However, wall-clock time regressed from 180s to 190s (+5.5%). The cause: even though the CPU cost of combine() itself was small, the synchronous deserialize-combine-reserialize pipeline added latency to the processing thread's critical path. The processing thread cannot ingest new rows while flushing, so any additional work during the flush directly extends the stall. In this workload, the keys had low duplication across the small runs, so combineByKey eliminated very few entries while paying the full deserialization cost for every entry.

Reducing merge fan-in with cascaded intermediate merges



Instead of writing all pending runs as a single merged file, we considered a cascaded merge approach — progressively merging spill files on disk (similar to an external merge sort with bounded fan-in) to keep the final iterator() merge across a small number of large files. This was not pursued because the batching approach already achieves the same goal more simply: by accumulating small runs in memory and writing them as one file, the number of files at iterator() time is naturally reduced by orders of magnitude (e.g., from ~2,000 to ~dozens). Cascaded disk merges would add I/O amplification (each entry written to disk multiple times) for marginal additional fan-in reduction.

Potential future improvements

Reducing sketch initialization cost

Flame graph profiling shows that HllSketchMergeBufferAggregatorHelper.createNewUnion() accounts for 50.7% of the processing thread's CPU time — the dominant bottleneck. Every time a new group slot is initialized in the BufferHashGrouper, init() calls createNewUnion() which copies a pre-built empty Union image (~65KB for lgK=16) into the buffer via initializeEmptyUnion(), then wraps it with Union.writableWrap(). During hash table growth (adjustTableWhenFull()), every existing slot is relocated via relocate(), which calls createNewUnion() again. With frequent spills, the grouper repeatedly fills, spills, resets, and re-initializes — multiplying the init cost.

Possible approaches:

1. Variable-size buffer slots in BufferHashGrouper: Currently, ByteBufferHashTable uses fixed-width buckets (bucketSizeWithHash = HASH_SIZE + keySize + aggregators.spaceNeeded()), where spaceNeeded() is the maximum intermediate size. For HLL with lgK=16, this pre-allocates ~65KB per slot even when the sketch is in List mode (few distinct values, only a handful of bytes). If the hash table supported variable-width slots — starting small and growing on demand as sketches transition from List → Set → HLL mode — far more groups would fit in the buffer before spilling. This would dramatically reduce the number of spills and proportionally reduce createNewUnion() calls. However, this is a fundamental architectural change to ByteBufferHashTable, which relies on fixed-width contiguous buckets for O(1) offset calculation and linear probing.

2. Lazy sketch initialization: Instead of initializing a full Union object when a new bucket is created, defer the expensive createNewUnion() call until the first aggregation value is actually merged. For groups that are spilled before receiving many values, this could avoid the full initialization cost entirely. The BufferAggregator interface would need to support deferred init — e.g., storing a sentinel in the buffer and lazily initializing on first aggregate() call.

Benchmarks result


Before this PR:
Before Change
Benchmark                                                  (initialBuckets)  (numProcessingThreads)  (numSegments)  (queryGranularity)  (rowsPerSegment)  (schemaAndQuery)  (vectorize)  Mode  Cnt       Score       Error  Units
GroupByBenchmark.queryMultiQueryableIndexTTFR                            -1                       4              4                 all            100000           basic.A        force  avgt   15   42116.148 ±  2627.574  us/op
GroupByBenchmark.queryMultiQueryableIndexWithSerde                       -1                       4              4                 all            100000           basic.A        force  avgt   15  103143.571 ± 19105.285  us/op
GroupByBenchmark.queryMultiQueryableIndexWithSpilling                    -1                       4              4                 all            100000           basic.A        force  avgt   15  388720.771 ± 15675.278  us/op
GroupByBenchmark.queryMultiQueryableIndexWithSpillingTTFR                -1                       4              4                 all            100000           basic.A        force  avgt   15   95759.378 ±  1799.533  us/op
GroupByBenchmark.queryMultiQueryableIndexX                               -1                       4              4                 all            100000           basic.A        force  avgt   15   66064.295 ±  8078.106  us/op

New Benchmarks

bufferGrouperMaxSize=100 (spill size ~6 KB)
GroupByBenchmark.queryMultiQueryableIndexWithSmallSpilling                    -1                       4              4                 all            100000           basic.A        force  avgt   15  1422480.170 ± 73222.415  us/op
GroupByBenchmark.queryMultiQueryableIndexWithSmallSpillingTTFR                -1                       4              4                 all            100000           basic.A        force  avgt   15   938728.684 ± 54411.363  us/op

bufferGrouperMaxSize=70000 (spill size ~4 MB)
GroupByBenchmark.queryMultiQueryableIndexWithLargeSpilling                    -1                       4              4                 all           1000000           basic.A        force  avgt   15  5179514.738 ± 79378.483  us/op
GroupByBenchmark.queryMultiQueryableIndexWithLargeSpillingTTFR                -1                       4              4                 all           1000000           basic.A        force  avgt   15  1647320.687 ± 62434.948  us/op

After this PR:

Benchmark                                                  (initialBuckets)  (numProcessingThreads)  (numSegments)  (queryGranularity)  (rowsPerSegment)  (schemaAndQuery)  (vectorize)  Mode  Cnt       Score       Error  Units
GroupByBenchmark.queryMultiQueryableIndexTTFR                            -1                       4              4                 all            100000           basic.A        force  avgt   15   53740.436 ±  8723.658  us/op
GroupByBenchmark.queryMultiQueryableIndexWithSerde                       -1                       4              4                 all            100000           basic.A        force  avgt   15   99847.941 ±  5631.568  us/op
GroupByBenchmark.queryMultiQueryableIndexWithSpilling                    -1                       4              4                 all            100000           basic.A        force  avgt   15  372782.928 ± 31551.417  us/op
GroupByBenchmark.queryMultiQueryableIndexWithSpillingTTFR                -1                       4              4                 all            100000           basic.A        force  avgt   15  176957.932 ± 24544.399  us/op
GroupByBenchmark.queryMultiQueryableIndexX                               -1                       4              4                 all            100000           basic.A        force  avgt   15   75079.741 ± 23374.447  us/op


New Benchmarks

bufferGrouperMaxSize=100 (spill size ~6 KB)
GroupByBenchmark.queryMultiQueryableIndexWithSmallSpilling                    -1                       4              4                 all            100000           basic.A        force  avgt   15  843467.611 ± 81698.388  us/op
GroupByBenchmark.queryMultiQueryableIndexWithSmallSpillingTTFR                -1                       4              4                 all            100000           basic.A        force  avgt   15  618040.133 ± 64764.729  us/op

bufferGrouperMaxSize=70000 (spill size ~4 MB)
GroupByBenchmark.queryMultiQueryableIndexWithLargeSpilling                    -1                       4              4                 all           1000000           basic.A        force  avgt   15  4997633.779 ± 124961.464  us/op
GroupByBenchmark.queryMultiQueryableIndexWithLargeSpillingTTFR                -1                       4              4                 all           1000000           basic.A        force  avgt   15  2413843.500 ± 856006.565  us/op

The existing queryMultiQueryableIndexWithSpilling/queryMultiQueryableIndexWithSpillingTTFR uses bufferGrouperMaxSize=4000 which produce reasonable spills of ~200kb. I have also added new benchmarks with similar idea but producing spill files on extremes ends for size. queryMultiQueryableIndexWithSmallSpilling/queryMultiQueryableIndexWithSmallSpillingTTFR sets bufferGrouperMaxSize=100, producing spill size ~6 KB. This would result in more batching. queryMultiQueryableIndexWithLargeSpilling/queryMultiQueryableIndexWithLargeSpillingTTFR sets bufferGrouperMaxSize=70000, producing spill size ~4 MB. This would skip batching. These new benchmarks are not added to the PR since they are really the same as queryMultiQueryableIndexWithSpilling/queryMultiQueryableIndexWithSpillingTTFR just with different config values.

Non-spilling benchmarks (unchanged code path):

  ┌───────────────────────────────────┬────────────┬──────────┬───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────┐   
  │             Benchmark             │   Before   │  After   │ Delta │                                                  Notes                                                   │
  ├───────────────────────────────────┼────────────┼──────────┼───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────┤   
  │ queryMultiQueryableIndexX         │ 66,064 ±8K │ 67,746   │ +3%   │ No spilling. Within error bars. Noise.                                                                   │
  │                                   │            │ ±8K      │       │                                                                                                          │
  ├───────────────────────────────────┼────────────┼──────────┼───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────┤   
  │ queryMultiQueryableIndexWithSerde │ 103,144    │ 87,389   │ -15%  │ No spilling. Looks like an improvement but the "before" had huge error bars (±19K). The "after" run just │   
  │                                   │ ±19K       │ ±2K      │       │  had less variance. Not attributable to the change.                                                      │   
  ├───────────────────────────────────┼────────────┼──────────┼───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────┤   
  │ queryMultiQueryableIndexTTFR      │ 42,116     │ 41,499   │ -1%   │ No spilling. Identical within error bars.                                                                │
  │                                   │ ±2.6K      │ ±3K      │       │                                                                                                          │   
  └───────────────────────────────────┴────────────┴──────────┴───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────┘
                                                                                                                                                                                     
  No real change, as expected — these don't hit the spill path.                                                                                                                      
  
  ---                                                                                                                                                                                
  Default spilling (bufferGrouperMaxSize=4000, spill size ~250KB — batching applies):
                                                                                                                                                                                     
  ┌──────────────────┬──────────┬──────────┬───────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  │    Benchmark     │  Before  │  After   │ Delta │                                                            Notes                                                            │   
  ├──────────────────┼──────────┼──────────┼───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
  │ WithSpilling     │ 388,721  │ 343,351  │ -12%  │ Improvement. Each spill (~250KB) is under 1MB, so batching kicks in — many small spills are accumulated in memory and       │
  │                  │ ±16K     │ ±56K     │       │ merged into fewer disk files, reducing file I/O. Error bars overlap though, so the magnitude is uncertain.                  │
  ├──────────────────┼──────────┼──────────┼───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤   
  │                  │          │          │       │ TTFR regression. Before: spill files are written individually during spill(), so they're ready to iterate immediately.      │
  │ WithSpillingTTFR │ 95,759   │ 170,717  │ +78%  │ After: small spills are held in memory, and iterator() calls flushPendingRunsToDisk() to merge-sort and write them before   │   
  │                  │ ±1.8K    │ ±24K     │       │ returning the first row. That extra flush delays the first row. Doesn't affect total query time since GroupBy materializes  │
  │                  │          │          │       │ all rows before returning.                                                                                                  │   
  └──────────────────┴──────────┴──────────┴───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

  ---
  Small spilling (bufferGrouperMaxSize=100, spill size ~6KB — batching's sweet spot):

  ┌───────────────────────┬─────────────┬───────────┬───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  │       Benchmark       │   Before    │   After   │ Delta │                                                       Notes                                                        │
  ├───────────────────────┼─────────────┼───────────┼───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
  │ WithSmallSpilling     │ 1,422,480   │ 843,468   │ -41%  │ Large win. Without batching: thousands of tiny ~6KB files created on disk. With batching: accumulated in memory,   │
  │                       │ ±73K        │ ±82K      │       │ merged into a handful of files. Eliminates massive filesystem overhead (open/write/close/seek per file).           │
  ├───────────────────────┼─────────────┼───────────┼───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤   
  │                       │ 938,729     │ 618,040   │       │ Also faster. Even with the flushPendingRunsToDisk() cost in iterator(), the total work is far less than writing    │
  │ WithSmallSpillingTTFR │ ±54K        │ ±65K      │ -34%  │ thousands of individual files during spill(). The batching saves so much I/O that TTFR improves too despite the    │   
  │                       │             │           │       │ deferred flush.                                                                                                    │
  └───────────────────────┴─────────────┴───────────┴───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘   
                                                         
  This is the target scenario. Aggregators like ThetaSketch that pre-allocate large buffers but serialize small produce exactly this pattern.                                        
  
  ---                                                                                                                                                                                
  Large spilling (bufferGrouperMaxSize=70000, spill size ~4MB — bypasses batching):

  ┌───────────────────────┬────────────┬────────────┬───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  │       Benchmark       │   Before   │   After    │ Delta │                                                       Notes                                                       │
  ├───────────────────────┼────────────┼────────────┼───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤    
  │ WithLargeSpilling     │ 5,179,515  │ 4,997,634  │ -4%   │ Within error bars. Each spill is ~4MB (>1MB threshold), so it goes directly to disk — the batching path is        │
  │                       │ ±79K       │ ±125K      │       │ skipped entirely. No change expected.                                                                             │    
  ├───────────────────────┼────────────┼────────────┼───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤    
  │                       │ 1,647,321  │ 2,103,661  │       │ TTFR regression. Large spills bypass batching, but iterator() still calls flushPendingRunsToDisk() (which is a    │
  │ WithLargeSpillingTTFR │ ±62K       │ ±96K       │ +28%  │ no-op since pendingSpillRuns is empty). This shouldn't cause a 28% regression for a no-op call. More likely       │    
  │                       │            │            │       │ benchmark variance — the ±96K error bars are significant. Could also be environmental noise between runs.         │
  └───────────────────────┴────────────┴────────────┴───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘    
         


This PR has:

  • been self-reviewed.

  • - [ ] using the concurrency checklist (Remove this item if the PR doesn't have any relation to concurrency.)
  • added documentation for new or modified features or behaviors.

  • a release note entry in the PR description.

  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.

  • added or updated version, license, or notice information in licenses.yaml

  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.

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

  • added integration tests.

  • been tested in a test Druid cluster.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: maytasm Review duration: 24094m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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%
    56%
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #19357"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 22.5% · Safe to merge
    22.5%
    Repository