Skip to content

perf(flink): reduce allocations when merging write statuses - #21008

Queued
VirtualSelect wants to merge 3 commits into
apache:masterfrom
VirtualSelect:fix/flink-coordinator-write-statuses-20991
Queued

VirtualSelect wants to merge 3 commits into
apache:masterfrom
VirtualSelect:fix/flink-coordinator-write-statuses-20991

Conversation

@VirtualSelect

@VirtualSelect VirtualSelect commented Dec 1, 2026 •

Copy link
Copy Markdown

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

  • Read through the contributor's guide
  • Context is provided above
  • Added focused regression tests

This PR was prepared with assistance from OpenAI ChatGPT.

@github-actions github-actions Bot added the size:M PR with lines of changes in (100, 300] label Dec 1, 2026
this.writeStatusesByFile = null;
}

private void writeObject(ObjectOutputStream out) throws IOException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@VirtualSelect VirtualSelect changed the title perf(flink): accumulate coordinator write statuses incrementally perf(flink): reduce allocations when merging write statuses Dec 1, 2026
@codecov-commenter

codecov-commenter commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.03%. Comparing base (7b02733) to head (1d28aef).
⚠️ Report is 9 commits behind head on master.

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     
Components Coverage Δ
hudi-common 84.40% <ø> (ø)
hudi-client 84.26% <ø> (-0.01%) ⬇️
hudi-flink 86.35% <100.00%> (+<0.01%) ⬆️
hudi-spark-datasource 74.58% <ø> (-0.04%) ⬇️
hudi-utilities 79.27% <ø> (-0.04%) ⬇️
hudi-cli 70.92% <ø> (ø)
hudi-hadoop 71.26% <ø> (ø)
hudi-sync 77.37% <ø> (+0.06%) ⬆️
hudi-io 81.66% <ø> (ø)
hudi-timeline-service 82.52% <ø> (ø)
hudi-cloud 81.01% <ø> (ø)
hudi-kafka-connect 58.40% <ø> (ø)
Flag Coverage Δ
common-and-other-modules 44.69% <100.00%> (+<0.01%) ⬆️
flink-integration-tests 40.88% <100.00%> (+0.01%) ⬆️
hadoop-mr-java-client 45.20% <ø> (-0.03%) ⬇️
integration-tests 14.49% <ø> (+<0.01%) ⬆️
spark-client-hadoop-common 38.16% <ø> (+<0.01%) ⬆️
spark-java-tests 52.85% <ø> (-0.03%) ⬇️
spark-scala-tests 53.35% <ø> (+<0.01%) ⬆️
utilities 42.32% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...org/apache/hudi/sink/event/WriteMetadataEvent.java 93.75% <100.00%> (-0.46%) ⬇️

... and 17 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines +183 to +194
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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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.

@VirtualSelect

Copy link
Copy Markdown
Author

Thanks. c838e2f2 applies the sequential toMap form while retaining the same file key and old-to-new merge order. All four focused Flink 1.20.1 tests passed; the prior head and new head also read each other’s serialized checkpoint fixtures. In a local allocation-only probe of 256 distinct-file flushes, the upstream grouping path allocated about 12.69 MB, the prior PR head 3.71 MB, and this revision 3.80 MB. This retains the reduction versus upstream (about 70% in this fixture); it is not a runtime or latency claim.

@VirtualSelect

Copy link
Copy Markdown
Author

@hudi-bot run azure

Build 17781 on c838e2f2 stopped before tests: the Spark lane's Maven install timed out fetching org.apache.maven.shared:maven-shared-utils:3.1.0 from Central. Retrying the same head without code changes.

@hudi-bot

hudi-bot commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

CI report:

Bot commands @hudi-bot supports the following commands:
  • @hudi-bot run azure re-run the last Azure build

@danny0405
danny0405 added this pull request to the merge queue Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M PR with lines of changes in (100, 300]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants