This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hudi.git
The following commit(s) were added to refs/heads/master by this push:
new f357fce418be perf(flink): reuse completed-instant write size totals
(#21092)
f357fce418be is described below
commit f357fce418be4f3bdfbbc0eb510d0231d2e2592b
Author: ViktorBystrov72 <[email protected]>
AuthorDate: Sat Dec 5 13:49:46 2026 +0000
perf(flink): reuse completed-instant write size totals (#21092)
* perf(flink): reuse completed-instant write size totals
* test(flink): fix delta commit lookup and verify profile cache reuse
* test(flink): explain the commit size cache reloads
---------
Co-authored-by: Viktor Bystrov
<[email protected]>
Co-authored-by: danny0405 <[email protected]>
---
.../sink/partitioner/profile/WriteProfile.java | 30 ++++++-----
.../hudi/sink/partitioner/TestBucketAssigner.java | 59 ++++++++++++++--------
2 files changed, 53 insertions(+), 36 deletions(-)
diff --git
a/hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/partitioner/profile/WriteProfile.java
b/hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/partitioner/profile/WriteProfile.java
index 304d8f5d7452..e7780bbc31c1 100644
---
a/hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/partitioner/profile/WriteProfile.java
+++
b/hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/partitioner/profile/WriteProfile.java
@@ -21,12 +21,12 @@ package org.apache.hudi.sink.partitioner.profile;
import org.apache.hudi.client.common.HoodieFlinkEngineContext;
import org.apache.hudi.common.config.HoodieStorageConfig;
import org.apache.hudi.common.model.HoodieBaseFile;
-import org.apache.hudi.common.model.HoodieCommitMetadata;
import org.apache.hudi.common.model.HoodieRecordLocation;
import org.apache.hudi.common.table.HoodieTableMetaClient;
import org.apache.hudi.common.table.timeline.HoodieInstant;
import org.apache.hudi.common.table.timeline.HoodieTimeline;
import org.apache.hudi.common.table.view.SyncableFileSystemView;
+import org.apache.hudi.common.util.collection.Pair;
import org.apache.hudi.config.HoodieWriteConfig;
import org.apache.hudi.sink.partitioner.BucketAssigner;
import org.apache.hudi.table.HoodieFlinkTable;
@@ -41,6 +41,7 @@ import org.apache.flink.core.fs.Path;
import org.apache.hadoop.conf.Configuration;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -103,9 +104,9 @@ public class WriteProfile {
protected SyncableFileSystemView fsView;
/**
- * Metadata cache to reduce IO of metadata files.
+ * Cache the bytes and records written per completed instant without
retaining its write stats.
*/
- private final Map<String, HoodieCommitMetadata> metadataCache;
+ private final Map<String, Pair<Long, Long>> commitSizeCache;
/**
* The engine context.
@@ -119,7 +120,7 @@ public class WriteProfile {
this.smallFilesMap = new HashMap<>();
this.metaClient = StreamerUtil.createMetaClient(
config.getBasePath(),
context.getStorageConf().unwrapAs(Configuration.class));
- this.metadataCache = new HashMap<>();
+ this.commitSizeCache = new HashMap<>();
this.fsView = getFileSystemView();
// profile the record statistics on construction
recordProfile();
@@ -151,16 +152,17 @@ public class WriteProfile {
Iterator<HoodieInstant> instants =
commitTimeline.getReverseOrderedInstants().iterator();
while (instants.hasNext()) {
HoodieInstant instant = instants.next();
- final HoodieCommitMetadata commitMetadata =
- this.metadataCache.computeIfAbsent(
+ Pair<Long, Long> totals =
+ this.commitSizeCache.computeIfAbsent(
instant.requestedTime(),
k ->
WriteProfiles.getCommitMetadataSafely(config.getTableName(), basePath, instant,
commitTimeline)
+ .map(metadata -> Pair.of(metadata.fetchTotalBytesWritten(),
metadata.fetchTotalRecordsWritten()))
.orElse(null));
- if (commitMetadata == null) {
+ if (totals == null) {
continue;
}
- long totalBytesWritten = commitMetadata.fetchTotalBytesWritten();
- long totalRecordsWritten = commitMetadata.fetchTotalRecordsWritten();
+ long totalBytesWritten = totals.getLeft();
+ long totalRecordsWritten = totals.getRight();
if (totalBytesWritten > fileSizeThreshold && totalRecordsWritten > 0) {
return (long) Math.ceil((fileSizeCalibrationRatio * totalBytesWritten)
/ totalRecordsWritten);
}
@@ -230,9 +232,9 @@ public class WriteProfile {
* Remove the overdue metadata from the cache
* whose instant does not belong to the given instants {@code instants}.
*/
- private void cleanMetadataCache(Stream<HoodieInstant> instants) {
+ private void cleanCommitSizeCache(Stream<HoodieInstant> instants) {
Set<String> timestampSet =
instants.map(HoodieInstant::requestedTime).collect(Collectors.toSet());
- this.metadataCache.keySet().retainAll(timestampSet);
+ this.commitSizeCache.keySet().retainAll(timestampSet);
}
private void recordProfile() {
@@ -262,13 +264,13 @@ public class WriteProfile {
oldFsView.close();
recordProfile();
-
cleanMetadataCache(this.metaClient.getCommitsTimeline().filterCompletedInstants().getInstantsAsStream());
+
cleanCommitSizeCache(this.metaClient.getCommitsTimeline().filterCompletedInstants().getInstantsAsStream());
this.smallFilesMap.clear();
this.reloadedCheckpointId = checkpointId;
}
@VisibleForTesting
- public Map<String, HoodieCommitMetadata> getMetadataCache() {
- return this.metadataCache;
+ public Set<String> getCachedCommitInstants() {
+ return Collections.unmodifiableSet(commitSizeCache.keySet());
}
}
diff --git
a/hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/sink/partitioner/TestBucketAssigner.java
b/hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/sink/partitioner/TestBucketAssigner.java
index ee7bccf6a11c..5569cdb62953 100644
---
a/hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/sink/partitioner/TestBucketAssigner.java
+++
b/hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/sink/partitioner/TestBucketAssigner.java
@@ -23,8 +23,10 @@ import org.apache.hudi.common.config.HoodieStorageConfig;
import org.apache.hudi.common.model.HoodieCommitMetadata;
import org.apache.hudi.common.model.HoodieRecordLocation;
import org.apache.hudi.common.model.HoodieTableType;
+import org.apache.hudi.common.model.HoodieWriteStat;
import org.apache.hudi.common.table.HoodieTableVersion;
import org.apache.hudi.common.table.timeline.HoodieTimeline;
+import org.apache.hudi.common.util.Option;
import org.apache.hudi.config.HoodieCompactionConfig;
import org.apache.hudi.config.HoodieWriteConfig;
import org.apache.hudi.configuration.FlinkOptions;
@@ -32,6 +34,7 @@ import org.apache.hudi.configuration.HadoopConfigurations;
import org.apache.hudi.hadoop.fs.HadoopFSUtils;
import org.apache.hudi.sink.partitioner.profile.DeltaWriteProfile;
import org.apache.hudi.sink.partitioner.profile.WriteProfile;
+import org.apache.hudi.sink.partitioner.profile.WriteProfiles;
import org.apache.hudi.table.action.commit.BucketInfo;
import org.apache.hudi.table.action.commit.BucketType;
import org.apache.hudi.table.action.commit.SmallFile;
@@ -45,6 +48,7 @@ import org.apache.flink.configuration.Configuration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
+import org.mockito.MockedStatic;
import java.io.File;
import java.io.IOException;
@@ -63,6 +67,11 @@ import static
org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
/**
* Test cases for {@link BucketAssigner}.
@@ -394,22 +403,31 @@ public class TestBucketAssigner {
}
@Test
- public void testWriteProfileMetadataCache() throws Exception {
+ public void testWriteProfileCommitSizeCache() throws Exception {
WriteProfile writeProfile = new WriteProfile(writeConfig, context);
- assertTrue(writeProfile.getMetadataCache().isEmpty(), "Empty table should
no have any instant metadata");
+ assertTrue(writeProfile.getCachedCommitInstants().isEmpty(), "An empty
table should have no cached instants");
// write 3 instants of data
for (int i = 0; i < 3; i++) {
TestData.writeData(TestData.DATA_SET_INSERT, conf);
}
- // the record profile triggers the metadata loading
- writeProfile.reload(1);
- assertThat("Metadata cache should have same number entries as timeline
instants",
- writeProfile.getMetadataCache().size(), is(3));
-
- writeProfile.getSmallFiles("par1");
- assertThat("The metadata should be reused",
- writeProfile.getMetadataCache().size(), is(3));
+ // Count accesses to a zero-valued stat alongside each small commit's
actual write stats.
+ HoodieWriteStat countingStat = mock(HoodieWriteStat.class);
+ try (MockedStatic<WriteProfiles> profiles =
mockStatic(WriteProfiles.class)) {
+ profiles.when(() -> WriteProfiles.getCommitMetadataSafely(any(), any(),
any(), any())).thenAnswer(call -> {
+ HoodieTimeline timeline = call.getArgument(3);
+ HoodieCommitMetadata metadata =
timeline.readCommitMetadata(call.getArgument(2));
+ metadata.addWriteStat("par1", countingStat);
+ return Option.of(metadata);
+ });
+
+ // The first reload loads each commit's metadata to profile record size.
The second reuses the cached totals.
+ writeProfile.reload(1);
+ assertEquals(3, writeProfile.getCachedCommitInstants().size());
+ writeProfile.reload(2);
+ verify(countingStat, times(3)).getTotalWriteBytes();
+ verify(countingStat, times(3)).getNumWrites();
+ }
}
@Test
@@ -436,9 +454,7 @@ public class TestBucketAssigner {
writeConfig = FlinkWriteClients.getHoodieClientConfig(conf);
WriteProfile writeProfile = new WriteProfile(writeConfig, context);
- String latestInstant = getLastCompleteInstant(writeProfile);
- HoodieCommitMetadata commitMetadata =
writeProfile.getMetadataCache().get(latestInstant);
- assertNotNull(commitMetadata);
+ HoodieCommitMetadata commitMetadata =
readLatestCommitMetadata(writeProfile);
long expectedAvgSize = (long) Math.ceil(
1.0 * commitMetadata.fetchTotalBytesWritten() /
commitMetadata.fetchTotalRecordsWritten());
@@ -487,9 +503,7 @@ public class TestBucketAssigner {
new FlinkTaskContextSupplier(null));
DeltaWriteProfile writeProfile = new DeltaWriteProfile(morWriteConfig,
morContext);
- String latestInstant = getLastCompleteInstant(writeProfile);
- HoodieCommitMetadata commitMetadata =
writeProfile.getMetadataCache().get(latestInstant);
- assertNotNull(commitMetadata);
+ HoodieCommitMetadata commitMetadata =
readLatestCommitMetadata(writeProfile);
long expectedAvgSize = (long) Math.ceil(
0.5 * commitMetadata.fetchTotalBytesWritten() /
commitMetadata.fetchTotalRecordsWritten());
@@ -519,9 +533,7 @@ public class TestBucketAssigner {
new FlinkTaskContextSupplier(null));
DeltaWriteProfile writeProfile = new DeltaWriteProfile(morWriteConfig,
morContext);
- String latestInstant = getLastCompleteInstant(writeProfile);
- HoodieCommitMetadata commitMetadata =
writeProfile.getMetadataCache().get(latestInstant);
- assertNotNull(commitMetadata);
+ HoodieCommitMetadata commitMetadata =
readLatestCommitMetadata(writeProfile);
long expectedAvgSize = (long) Math.ceil(
1.0 * commitMetadata.fetchTotalBytesWritten() /
commitMetadata.fetchTotalRecordsWritten());
@@ -550,9 +562,7 @@ public class TestBucketAssigner {
new FlinkTaskContextSupplier(null));
DeltaWriteProfile writeProfile = new DeltaWriteProfile(morWriteConfig,
morContext);
- String latestInstant = getLastCompleteInstant(writeProfile);
- HoodieCommitMetadata commitMetadata =
writeProfile.getMetadataCache().get(latestInstant);
- assertNotNull(commitMetadata);
+ HoodieCommitMetadata commitMetadata =
readLatestCommitMetadata(writeProfile);
long expectedAvgSize = (long) Math.ceil(
1.0 * commitMetadata.fetchTotalBytesWritten() /
commitMetadata.fetchTotalRecordsWritten());
@@ -590,6 +600,11 @@ public class TestBucketAssigner {
writeProfile.getRecordsPerBucket(),
is(morWriteConfig.getParquetMaxFileSize() / 256L));
}
+ private static HoodieCommitMetadata readLatestCommitMetadata(WriteProfile
profile) throws IOException {
+ HoodieTimeline timeline =
profile.getMetaClient().getCommitsTimeline().filterCompletedInstants();
+ return timeline.readCommitMetadata(timeline.lastInstant().get());
+ }
+
private static String getLastCompleteInstant(WriteProfile profile) {
return StreamerUtil.getLastCompletedInstant(profile.getMetaClient());
}