[flink] Reduce heap retained by sink undo recovery
Search before asking
- [x] I searched existing issues and found no matching report.
Fluss version
main (development)
Please describe the bug
During sink undo recovery, the executor retains write-completion futures until the scan finishes. It also keeps an in-memory set of processed primary keys for each bucket. A single bucket with enough distinct keys can exhaust the TaskManager heap, causing recovery to fail and repeat on restart.
Solution
Reduce the heap retained for write completion and exact primary-key deduplication. Recovery must still process the first valid change per key within each bucket’s fixed recovery range, propagate write failures, and return recovery offsets only after the writes have been acknowledged.
Are you willing to submit a PR?
- [x] I'm willing to submit a PR!
On 1.0.0, undo recovery holds a processedKeys entry and a write future for every distinct key in the undo range (checkpointed offset to log end). #5120 bounds the futures and releases each bucket's key set when its scan ends. As its description says, one bucket's key set still grows with every distinct key in its range, so a bucket whose range exceeds the TaskManager heap still never recovers. Please decide whether the actions below belong in #5120 or a follow-up PR.
Suggested actions (proposals, not tested). Happy to raise a PR for these if that would be useful.
- Bound the per-bucket key set.
UndoComputer.processRecorduses it to undo only the first change per key, so it can't simply be cleared mid-scan. A compact or spillable key set would keep that behaviour. - Stop failed attempts from widening the next range (#4955). Keep the first attempt's
logEndOffsetand reuse it on retry, so the attempt's own undo writes aren't scanned again. - Add an
UndoRecoveryExecutorTestcase where a single bucket's range has more keys than the memory budget.
Evidence (1.0.0, Flink 1.20.3, 1 bucket, 248 MiB TaskManager heap): a Flink SQL sink writes into an aggregation table agg_m and checkpoints at offset 5. A separate Java client upserts 1,500,000 new keys, flushing every 100,000. Then kill -9 the TaskManager. Every restore attempt runs out of heap, and no checkpoint completes:
RecoveryOffsetManager - Subtask 0: bucket=TableBucket{tableId=44, bucket=0}, baseline=5 (explicit=true), current=1500005
java.lang.OutOfMemoryError: Java heap space
- jmap peak, with
'client.writer.batch-size' = '256kb'on the sink to avoid #2967: 1,215,488ByteArrayWrapperand 1,215,563CompletableFuture, with 236.9 MiB of 248 MiB live. - With 300,000 keys, undo completes in 1.3 s.
- The
currentoffset in that log line grew from 1,500,005 to 2,380,417 across attempts because each failed attempt's undo writes land in the log. The table was left partly undone.
Thanks @mcgrj for the detailed reproducer. The heap dump helped pinpoint both sources of retention. I updated #5120 to store processed keys in a temporary local RocksDB and track write acknowledgements in callbacks. I also checked the single-bucket case locally under a constrained heap. The PR description includes that experiment; the regular executor tests cover first-change selection and failure propagation.
I’d keep the retry-range issue as a separate follow-up. #4955 advances the baseline after a successful recovery. If an attempt fails after appending undo writes, the next attempt can still see a later log end, as you observed. Reusing the original end offset across attempts would change the recovery protocol and deserves its own discussion.