Skip to content

[Flink] Spill buffered GroupByKey values to disk in SortedBatchCombineOperator - #41909

Draft
pkuzmickas wants to merge 1 commit into
apache:masterfrom
pkuzmickas:flink-spill-sorted-gbk
Draft

pkuzmickas wants to merge 1 commit into
apache:masterfrom
pkuzmickas:flink-spill-sorted-gbk

Conversation

@pkuzmickas

Copy link
Copy Markdown
Contributor

Addresses #40607. Follow-up to #40611.

SortedBatchCombineOperator.BufferingCombiner collects all values for the current GroupByKey key into an unbounded on-heap ArrayList before emitting the key. For pipelines with large keys (for example sorted-bucket writes grouping millions of records per bucket key), holding every deserialized value on heap across concurrent task slots causes heavy GC pressure or OutOfMemoryError: Java heap space, especially when a chained downstream operator also buffers per-key state on heap.

What changes

  • Update SortedBatchCombineOperator.BufferingCombiner to accept the value Coder:
    • Small keys (< 256 elements) stay in a plain ArrayList with no serialization overhead.
    • Larger keys compact into a bounded in-memory encoded byte buffer (16 MiB default) returned as a re-iterable EncodedIterable.
    • Keys whose encoded bytes reach the threshold spill to a local temporary file under Flink's IOManager spilling directory (falling back to the default temp directory when unavailable) and return a re-iterable SpilledIterable backed by buffered file reads.
  • Add open(@Nullable File spillDirectory) and close() lifecycle hooks on KeyCombiner and invoke them from SortedBatchCombineOperator#open and #close. Track live spill files with a ReferenceQueue so consumed keys are deleted before task completion, and deterministically close any open streams and delete remaining spill files on close().
  • Wire accumulatorCoder into BufferingCombiner in FlinkStreamingAggregationsTranslators#batchGroupByKey.
  • Add tests in SortedBatchCombineOperatorTest covering in-memory, compact-encoded, and disk-spilled keys, re-iteration, partial iteration, and spill cleanup on normal close and abort.

🤖 Generated with Xirp Agent

…eOperator

Co-Authored-By: Xirp Agent (gemini-4-neon) <xirp-agent@users.noreply.ghe.spotify.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant