diff --git a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/FileStoreLookupFunction.java b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/FileStoreLookupFunction.java index 762b0dd3eef1..09f3a57408fc 100644 --- a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/FileStoreLookupFunction.java +++ b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/FileStoreLookupFunction.java @@ -425,6 +425,10 @@ void tryRefresh() throws Exception { boolean partitionChanged = partitionLoader.checkRefresh(); List partitions = partitionLoader.partitions(); if (partitions.isEmpty()) { + if (partitionChanged) { + // Stop serving the previous selection until a new non-empty one is loaded. + partitionRefresher.clearPartitions(); + } // no data to be load, fast exit return; } diff --git a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/PartitionRefresher.java b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/PartitionRefresher.java index b10ad237803e..436451048428 100644 --- a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/PartitionRefresher.java +++ b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/PartitionRefresher.java @@ -36,6 +36,7 @@ import java.io.Closeable; import java.io.File; import java.io.IOException; +import java.util.Collections; import java.util.List; import java.util.UUID; import java.util.concurrent.ExecutorService; @@ -61,6 +62,9 @@ public class PartitionRefresher implements Closeable { /** Current partitions being used for lookup. Updated when partition refresh completes. */ private List currentPartitions; + /** Incremented when partitions are cleared, so async refreshes started earlier are dropped. */ + private long generation; + public PartitionRefresher( boolean partitionRefreshAsync, String tableName, @@ -88,6 +92,15 @@ public List currentPartitions() { return currentPartitions; } + /** + * Stop serving the current partitions because the partition selection became empty. Async + * refreshes started before this call are dropped when they complete. + */ + public void clearPartitions() { + this.currentPartitions = Collections.emptyList(); + this.generation++; + } + /** * Start partition refresh. Chooses sync or async mode based on configuration. * @@ -138,6 +151,7 @@ private void asyncPartitionRefresh( "Starting async partition refresh for table {}, new partitions detected.", tableName); + long refreshGeneration = generation; partitionRefreshExecutor.submit( () -> { File newPath = new File(tmpDirectory, "lookup-" + UUID.randomUUID()); @@ -164,7 +178,7 @@ private void asyncPartitionRefresh( } try { - publishRefreshResult(newTable, newPartitions, newPath); + publishRefreshResult(newTable, newPartitions, newPath, refreshGeneration); LOG.info("Async partition refresh completed for table {}.", tableName); } catch (IOException e) { LOG.error( @@ -177,10 +191,12 @@ private void asyncPartitionRefresh( } @VisibleForTesting - void publishRefreshResult(LookupTable lookupTable, List partitions, File refreshPath) + void publishRefreshResult( + LookupTable lookupTable, List partitions, File refreshPath, long generation) throws IOException { RefreshResult previous = - pendingRefresh.getAndSet(new RefreshResult(lookupTable, partitions, refreshPath)); + pendingRefresh.getAndSet( + new RefreshResult(lookupTable, partitions, refreshPath, generation)); if (previous != null) { previous.lookupTable.close(); } @@ -210,6 +226,11 @@ public LookupTable getNewLookupTable() throws Exception { if (refreshResult == null) { return null; } + if (refreshResult.generation != generation) { + // Built for a selection that was cleared afterwards. + refreshResult.lookupTable.close(); + return null; + } this.currentPartitions = refreshResult.partitions; this.path = refreshResult.path; @@ -244,11 +265,14 @@ private static final class RefreshResult { private final LookupTable lookupTable; private final List partitions; private final File path; + private final long generation; - private RefreshResult(LookupTable lookupTable, List partitions, File path) { + private RefreshResult( + LookupTable lookupTable, List partitions, File path, long generation) { this.lookupTable = lookupTable; this.partitions = partitions; this.path = path; + this.generation = generation; } } } diff --git a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/FileStoreLookupFunctionTest.java b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/FileStoreLookupFunctionTest.java index 17bce6aae8d6..99749e093b64 100644 --- a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/FileStoreLookupFunctionTest.java +++ b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/FileStoreLookupFunctionTest.java @@ -42,6 +42,7 @@ import org.apache.paimon.types.DataType; import org.apache.paimon.types.DataTypes; import org.apache.paimon.types.RowType; +import org.apache.paimon.utils.CommonTestUtils; import org.apache.paimon.utils.TraceableFileIO; import org.apache.flink.table.data.RowData; @@ -60,6 +61,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; import java.lang.reflect.Field; @@ -298,6 +300,55 @@ public void testLookupDynamicPartition() throws Exception { .isEqualTo(0); } + @ParameterizedTest + @CsvSource({"FULL, false", "FULL, true", "MEMORY, false", "MEMORY, true", "AUTO, false"}) + public void testLookupAfterDroppingAllDynamicPartitions( + FlinkConnectorOptions.LookupCacheMode cacheMode, boolean partitionRefreshAsync) + throws Exception { + Options options = new Options(); + options.set(LOOKUP_CACHE_MODE, cacheMode); + options.set( + FlinkConnectorOptions.LOOKUP_DYNAMIC_PARTITION_REFRESH_ASYNC, + partitionRefreshAsync); + options.set(FlinkConnectorOptions.LOOKUP_DYNAMIC_PARTITION_REFRESH_INTERVAL, Duration.ZERO); + table = createFileStoreTable(true, true, false, null).copy(options.toMap()); + try (StreamTableWrite writer = table.newStreamWriteBuilder().newWrite()) { + writer.write(GenericRow.of(1, 2, 3L)); + commit(writer.prepareCommit(true, 1)); + } + + lookupFunction = + new FileStoreLookupFunction(table, new int[] {0, 1, 2}, new int[] {1}, null, null); + lookupFunction.open(tempDir.toString()); + RowData key = new FlinkRowData(GenericRow.of(2)); + assertThat(lookupFunction.lookup(key)) + .extracting(row -> row.getLong(2)) + .containsExactly(3L); + + try (TableCommitImpl commit = table.newCommit(commitUser)) { + commit.truncatePartitions( + Collections.singletonList(Collections.singletonMap("pt", "1"))); + } + assertThat(table.newReadBuilder().newScan().topNPartitions(1, 1)).isEmpty(); + assertThat(lookupFunction.lookup(key)).isEmpty(); + assertThat(lookupFunction.lookup(key)).isEmpty(); + + // Reusing the same partition must load the new rows instead of the old cache. + try (StreamTableWrite writer = table.newStreamWriteBuilder().newWrite()) { + writer.write(GenericRow.of(1, 2, 4L)); + commit(writer.prepareCommit(true, 3)); + } + // An async refresh is pending here; the pre-truncate cache must stay hidden meanwhile. + assertThat(lookupFunction.lookup(key)).extracting(row -> row.getLong(2)).doesNotContain(3L); + CommonTestUtils.waitUtil( + () -> lookupFunction.lookup(key).stream().anyMatch(row -> row.getLong(2) == 4L), + Duration.ofSeconds(10), + Duration.ofMillis(10)); + assertThat(lookupFunction.lookup(key)) + .extracting(row -> row.getLong(2)) + .containsExactly(4L); + } + @Test public void testParseWrongTimePeriodsBlacklist() throws Exception { FileStoreTable table = createFileStoreTable(false, false, false, null); diff --git a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/PartitionRefresherTest.java b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/PartitionRefresherTest.java index 21b3e7dbde23..5a622d58bf54 100644 --- a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/PartitionRefresherTest.java +++ b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/PartitionRefresherTest.java @@ -49,7 +49,7 @@ void testRefreshResultKeepsGenerationConsistent() throws Exception { PartitionRefresher refresher = createRefresher(initialPartitions); try { - refresher.publishRefreshResult(refreshedTable, refreshedPartitions, refreshPath); + refresher.publishRefreshResult(refreshedTable, refreshedPartitions, refreshPath, 0); assertThat(refresher.getNewLookupTable()).isSameAs(refreshedTable); assertThat(refresher.currentPartitions()).isSameAs(refreshedPartitions); @@ -72,8 +72,8 @@ void testPublishingNewResultClosesUnconsumedTable() throws Exception { PartitionRefresher refresher = createRefresher(partitions(1)); try { - refresher.publishRefreshResult(tableB, partitionsB, pathB); - refresher.publishRefreshResult(tableC, partitionsC, pathC); + refresher.publishRefreshResult(tableB, partitionsB, pathB, 0); + refresher.publishRefreshResult(tableC, partitionsC, pathC, 0); assertThat(tableB.closed).isTrue(); assertThat(tableC.closed).isFalse();