Repository navigation
perf(flink): reduce allocations when merging write statuses - #21008
VirtualSelect wants to merge 3 commits into
Conversation
| this.writeStatusesByFile = null; | ||
| } | ||
|
|
||
| private void writeObject(ObjectOutputStream out) throws IOException { |
There was a problem hiding this comment.
This writeObject changes the serializer for the writer’s write-metadata-state. I compared the base and PR serializer snapshots using TypeInformation.of(WriteMetadataEvent.class), the same call as AbstractStreamWriteFunction.initializeState. On Flink 2.0.0, 2.1.1, and 2.2.1, the base used PojoSerializer, the PR used KryoSerializer, and Flink reported INCOMPATIBLE. An existing job can therefore fail to restore after upgrading. The SerializedValue test exercises event transport. Could we keep the existing event serialization and cover the writer-state snapshot in the upgrade test?
There was a problem hiding this comment.
Thanks for catching the distinction between event transport and writer state. In 1d28aef I removed the custom serialization and persistent index and kept the optimization inside the shared merge helper. I reproduced the old PR head’s incompatibility with base-written writer state on Flink 2.0.0, 2.1.1, and 2.2.1; the new head reports compatible-as-is and reads the original bytes on all three. The writer-state regression fails on the old head and passes on the new head on Flink 2.2.1. I removed the closing keyword so #20991 can remain open for the scaling work.
| return writeStatus.getStat().getPartitionPath() + writeStatus.getStat().getFileId(); | ||
| } | ||
| })) | ||
| .collect(Collectors.groupingBy(WriteMetadataEvent::writeStatusKey)) |
There was a problem hiding this comment.
Could we keep this optimization inside mergeWriteStatuses and use it from both merge paths? The full replacement for the method body would be
Map<String, WriteStatus> byFile = Stream.concat(curStatuses.stream(), newStatuses.stream())
.collect(Collectors.toMap(WriteMetadataEvent::writeStatusKey, Function.identity(), WriteStatusMerger::merge));
return new ArrayList<>(byFile.values());The sequential stream visits existing statuses first, so the newer status stays on the right. This removes the per-file lists and lets us publish the result after the whole flush merges successfully. In a local 256-flush, distinct-file fixture, merge allocations fell from 12.8 MB to 3.9 MB. The persistent index saved about 4.6 ms over the base across that entire fixture. I’d keep the quadratic-scaling issue open and defer the index until a representative job shows coordinator queueing or checkpoint latency worth addressing.
Limit the optimization to per-merge allocation and add writer-state serializer coverage. Assisted-by: OpenAI ChatGPT
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #21008 +/- ##
============================================
- Coverage 81.03% 81.03% -0.01%
- Complexity 37160 37162 +2
============================================
Files 2606 2606
Lines 150587 150596 +9
Branches 18861 18863 +2
============================================
- Hits 122035 122030 -5
- Misses 20058 20068 +10
- Partials 8494 8498 +4
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
| Map<String, WriteStatus> byFile = new HashMap<>(); | ||
| // Visit existing statuses first so the incoming status stays on the right of a merge. | ||
| curStatuses.forEach(status -> byFile.merge(writeStatusKey(status), status, WriteStatusMerger::merge)); | ||
| newStatuses.forEach(status -> byFile.merge(writeStatusKey(status), status, WriteStatusMerger::merge)); | ||
| return new ArrayList<>(byFile.values()); | ||
| } | ||
|
|
||
| private static String writeStatusKey(WriteStatus status) { | ||
| if (status.getStat() instanceof HoodieDeltaWriteStat) { | ||
| return status.getStat().getPartitionPath() + status.getStat().getPath(); | ||
| } | ||
| return status.getStat().getPartitionPath() + status.getStat().getFileId(); |
There was a problem hiding this comment.
Could we express the merge in one place and drop writeStatusKey? The sequential stream visits curStatuses first, so when a key repeats, WriteStatusMerger.merge still receives the newer status on the right. Only the file-specific part of the key differs between base and delta writes.
| Map<String, WriteStatus> byFile = new HashMap<>(); | |
| // Visit existing statuses first so the incoming status stays on the right of a merge. | |
| curStatuses.forEach(status -> byFile.merge(writeStatusKey(status), status, WriteStatusMerger::merge)); | |
| newStatuses.forEach(status -> byFile.merge(writeStatusKey(status), status, WriteStatusMerger::merge)); | |
| return new ArrayList<>(byFile.values()); | |
| } | |
| private static String writeStatusKey(WriteStatus status) { | |
| if (status.getStat() instanceof HoodieDeltaWriteStat) { | |
| return status.getStat().getPartitionPath() + status.getStat().getPath(); | |
| } | |
| return status.getStat().getPartitionPath() + status.getStat().getFileId(); | |
| Map<String, WriteStatus> byFile = Stream.concat(curStatuses.stream(), newStatuses.stream()) | |
| .collect(Collectors.toMap(status -> { | |
| HoodieWriteStat stat = status.getStat(); | |
| String fileKey = stat instanceof HoodieDeltaWriteStat ? stat.getPath() : stat.getFileId(); | |
| return stat.getPartitionPath() + fileKey; | |
| }, Function.identity(), WriteStatusMerger::merge)); | |
| return new ArrayList<>(byFile.values()); |
This also drops the HashMap import and needs imports for HoodieWriteStat, Function, Collectors, and Stream.
Assisted-by: OpenAI ChatGPT
|
Thanks. |
|
@hudi-bot run azure Build 17781 on |
Describe the issue this Pull Request addresses
Related to #20991. This reduces allocations in each merge; it does not address quadratic cumulative scanning, so #20991 should remain open.
Summary and Changelog
Use one map per merge instead of concatenating the lists and allocating a list per file. Visit existing statuses before incoming statuses and preserve the existing COW partition/file-ID and MOR partition/path keys. Both normal and rescale merges use the helper. This revision adds no fields or custom serialization methods and makes no changes to EventBuffers.
Impact
Removes intermediate allocations but still scans existing statuses on each flush. No throughput, latency, or scaling claim.
Risk Level
The initial revision changed the writer-state serializer. Separate processes using the writer state's type information reproduced PojoSerializer to KryoSerializer and incompatible snapshots against the base on Flink 2.0.0, 2.1.1, and 2.2.1. This revision restores PojoSerializer on all three, reports compatible-as-is, and reads the exact state bytes written by the base on each version. The new regression fails on the initial revision and passes on the update under Flink 2.2.1. The focused event tests pass 4/4 on Flink 1.20.1, including coordinator checkpoint, rescale, and event transport coverage. This is serializer-level verification; no distributed savepoint upgrade was run.
Documentation Update
None.
Contributor's checklist
This PR was prepared with assistance from OpenAI ChatGPT.