Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ void tryRefresh() throws Exception {
boolean partitionChanged = partitionLoader.checkRefresh();
List<BinaryRow> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -61,6 +62,9 @@ public class PartitionRefresher implements Closeable {
/** Current partitions being used for lookup. Updated when partition refresh completes. */
private List<BinaryRow> currentPartitions;

/** Incremented when partitions are cleared, so async refreshes started earlier are dropped. */
private long generation;

public PartitionRefresher(
boolean partitionRefreshAsync,
String tableName,
Expand Down Expand Up @@ -88,6 +92,15 @@ public List<BinaryRow> 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.
*
Expand Down Expand Up @@ -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());
Expand All @@ -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(
Expand All @@ -177,10 +191,12 @@ private void asyncPartitionRefresh(
}

@VisibleForTesting
void publishRefreshResult(LookupTable lookupTable, List<BinaryRow> partitions, File refreshPath)
void publishRefreshResult(
LookupTable lookupTable, List<BinaryRow> 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();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -244,11 +265,14 @@ private static final class RefreshResult {
private final LookupTable lookupTable;
private final List<BinaryRow> partitions;
private final File path;
private final long generation;

private RefreshResult(LookupTable lookupTable, List<BinaryRow> partitions, File path) {
private RefreshResult(
LookupTable lookupTable, List<BinaryRow> partitions, File path, long generation) {
this.lookupTable = lookupTable;
this.partitions = partitions;
this.path = path;
this.generation = generation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
Loading