Repository navigation
perf(flink): reduce allocations when merging write statuses #21008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
|
|
||
| import org.apache.hudi.client.WriteStatus; | ||
| import org.apache.hudi.common.model.HoodieDeltaWriteStat; | ||
| import org.apache.hudi.common.model.HoodieWriteStat; | ||
| import org.apache.hudi.common.util.ValidationUtils; | ||
| import org.apache.hudi.util.WriteStatusMerger; | ||
|
|
||
|
|
@@ -28,9 +29,13 @@ | |
| import lombok.Setter; | ||
| import org.apache.flink.runtime.operators.coordination.OperatorEvent; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.ObjectOutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
|
|
@@ -46,6 +51,8 @@ public class WriteMetadataEvent implements OperatorEvent { | |
| public static final String BOOTSTRAP_INSTANT = ""; | ||
|
|
||
| private List<WriteStatus> writeStatuses; | ||
| // Coordinator-only index. Checkpoints continue to store the original list representation. | ||
| private transient Map<String, WriteStatus> writeStatusesByFile; | ||
| private int taskID; | ||
| private long checkpointId; | ||
| private String instantTime; | ||
|
|
@@ -118,7 +125,13 @@ public void mergeWith(WriteMetadataEvent other) { | |
| this.instantTime = other.instantTime; | ||
| // true if one of the event lastBatch is true | ||
| this.lastBatch |= other.lastBatch; | ||
| this.writeStatuses = mergeWriteStatuses(this.writeStatuses, other.writeStatuses); | ||
| List<WriteStatus> incoming = other.getWriteStatuses(); | ||
| if (writeStatusesByFile == null) { | ||
| writeStatusesByFile = new HashMap<>(); | ||
| writeStatuses.forEach(this::accumulateWriteStatus); | ||
| writeStatuses = null; | ||
| } | ||
| incoming.forEach(this::accumulateWriteStatus); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -132,10 +145,32 @@ public WriteMetadataEvent mergeWithRescale(WriteMetadataEvent other) { | |
| this.instantTime = other.instantTime; | ||
| // true if one of the event lastBatch is true | ||
| this.lastBatch |= other.lastBatch; | ||
| this.writeStatuses = mergeWriteStatuses(this.writeStatuses, other.writeStatuses); | ||
| this.writeStatuses = mergeWriteStatuses(getWriteStatuses(), other.getWriteStatuses()); | ||
| return this; | ||
| } | ||
|
|
||
| public List<WriteStatus> getWriteStatuses() { | ||
| if (writeStatusesByFile != null) { | ||
| writeStatuses = new ArrayList<>(writeStatusesByFile.values()); | ||
| writeStatusesByFile = null; | ||
| } | ||
| return writeStatuses; | ||
| } | ||
|
|
||
| public void setWriteStatuses(List<WriteStatus> writeStatuses) { | ||
| this.writeStatuses = writeStatuses; | ||
| this.writeStatusesByFile = null; | ||
| } | ||
|
|
||
| private void writeObject(ObjectOutputStream out) throws IOException { | ||
| getWriteStatuses(); | ||
| out.defaultWriteObject(); | ||
| } | ||
|
|
||
| private void accumulateWriteStatus(WriteStatus status) { | ||
| writeStatusesByFile.merge(writeStatusKey(status), status, WriteStatusMerger::merge); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether the event is ready to commit. | ||
| */ | ||
|
|
@@ -146,7 +181,7 @@ public boolean isReady(String currentInstant) { | |
| @Override | ||
| public String toString() { | ||
| return "WriteMetadataEvent{" | ||
| + "writeStatusesSize=" + writeStatuses.size() | ||
| + "writeStatusesSize=" + (writeStatusesByFile == null ? writeStatuses.size() : writeStatusesByFile.size()) | ||
| + ", taskID=" + taskID | ||
| + ", checkpointId=" + checkpointId | ||
| + ", instantTime='" + instantTime + '\'' | ||
|
|
@@ -186,18 +221,17 @@ private static List<WriteStatus> mergeWriteStatuses(List<WriteStatus> curStatuse | |
| merged.addAll(newStatuses); | ||
| return merged | ||
| .stream() | ||
| .collect(Collectors.groupingBy(writeStatus -> { | ||
| if (writeStatus.getStat() instanceof HoodieDeltaWriteStat) { | ||
| return writeStatus.getStat().getPartitionPath() + writeStatus.getStat().getPath(); | ||
| } else { | ||
| return writeStatus.getStat().getPartitionPath() + writeStatus.getStat().getFileId(); | ||
| } | ||
| })) | ||
| .collect(Collectors.groupingBy(WriteMetadataEvent::writeStatusKey)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we keep this optimization inside 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. |
||
| .values().stream() | ||
| .map(duplicates -> duplicates.stream().reduce(WriteStatusMerger::merge).get()) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static String writeStatusKey(WriteStatus status) { | ||
| HoodieWriteStat stat = status.getStat(); | ||
| return stat.getPartitionPath() + (stat instanceof HoodieDeltaWriteStat ? stat.getPath() : stat.getFileId()); | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Builder | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
writeObjectchanges the serializer for the writer’swrite-metadata-state. I compared the base and PR serializer snapshots usingTypeInformation.of(WriteMetadataEvent.class), the same call asAbstractStreamWriteFunction.initializeState. On Flink 2.0.0, 2.1.1, and 2.2.1, the base usedPojoSerializer, the PR usedKryoSerializer, and Flink reportedINCOMPATIBLE. An existing job can therefore fail to restore after upgrading. TheSerializedValuetest 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.
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.