From a8745bd7475c274d2d9db841a62bc165e8e2d9a4 Mon Sep 17 00:00:00 2001 From: YueZhang <69956021+zhangyue19921010@users.noreply.github.com> Date: Mon, 23 Nov 2026 12:34:00 +0800 Subject: [PATCH 1/2] [flink][cdc] Skip dropped tables when restoring source splits --- .../flink/pipeline/cdc/source/CDCSource.java | 20 +- .../enumerator/CDCSourceEnumerator.java | 6 +- .../source/reader/CDCSourceSplitReader.java | 19 +- .../cdc/source/CDCSourceRestoreTest.java | 230 ++++++++++++++++++ .../cdc/source/CDCSourceTableManagerTest.java | 34 +++ .../reader/CDCSourceSplitReaderTest.java | 5 + 6 files changed, 307 insertions(+), 7 deletions(-) create mode 100644 paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceRestoreTest.java diff --git a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSource.java b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSource.java index bcb2edf09548..45530e02ece1 100644 --- a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSource.java +++ b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSource.java @@ -206,7 +206,16 @@ public List generateSchemaChangeEventList( convertPaimonSchemaToFlinkCDCSchema(getTableSchema(identifier, schemaId))); } + public boolean isTableAvailable(Identifier identifier) { + return getTable(identifier, true) != null; + } + private FileStoreTable getTable(Identifier identifier) { + return getTable(identifier, false); + } + + @Nullable + private FileStoreTable getTable(Identifier identifier, boolean ignoreUnavailable) { if (tableMap.containsKey(identifier)) { return tableMap.get(identifier); } @@ -215,11 +224,18 @@ private FileStoreTable getTable(Identifier identifier) { try { table = catalog.getTable(identifier); } catch (Catalog.TableNotExistException e) { + if (ignoreUnavailable) { + return null; + } throw new RuntimeException(e); } - // As the table should have just been checked by the enumerator, it is supposed to exist - // and be a FileStoreTable. + if (ignoreUnavailable && !(table instanceof FileStoreTable)) { + return null; + } + + // Cache only available tables so a newly discovered table can be loaded after an + // earlier lookup found it missing. tableMap.put(identifier, (FileStoreTable) table); return (FileStoreTable) table; } diff --git a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/enumerator/CDCSourceEnumerator.java b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/enumerator/CDCSourceEnumerator.java index 5e62aeda291a..fc6b4a09a132 100644 --- a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/enumerator/CDCSourceEnumerator.java +++ b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/enumerator/CDCSourceEnumerator.java @@ -153,7 +153,11 @@ public CDCSourceEnumerator( tableProgress.schemaId()); } - addSplits(checkpoint.getSplits()); + for (TableAwareFileStoreSourceSplit split : checkpoint.getSplits()) { + if (tableStatusMap.containsKey(split.getIdentifier())) { + addSplit(split); + } + } } } diff --git a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReader.java b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReader.java index 960665bf365e..dddaf38e09d0 100644 --- a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReader.java +++ b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReader.java @@ -101,7 +101,9 @@ public RecordsWithSplitIds> fetch() throws IOEx return new FileStoreSourceSplitReader.EmptyRecordsWithSplitIds<>(); } - checkSplitOrStartNext(); + if (!checkSplitOrStartNext()) { + return finishSplit(); + } // poll from the pool first, pool size is 1, the underlying implementation does not allow // multiple batches to be read at the same time @@ -202,9 +204,9 @@ public void close() throws Exception { } } - private void checkSplitOrStartNext() throws IOException { + private boolean checkSplitOrStartNext() throws IOException { if (currentReader != null) { - return; + return true; } final TableAwareFileStoreSourceSplit nextSplit = splits.poll(); @@ -222,12 +224,20 @@ private void checkSplitOrStartNext() throws IOException { } Identifier identifier = nextSplit.getIdentifier(); + currentSplitId = nextSplit.splitId(); + // Reader state can contain splits for a table removed while the job was stopped. + if (!tableManager.isTableAvailable(identifier)) { + LOG.info( + "Skipping split {} because table {} no longer exists or is not a file store table.", + currentSplitId, + identifier); + return false; + } TableSchema tableSchema = tableManager.getTableSchema(identifier, nextSplit.getSchemaId()); List schemaChangeEvents = tableManager.generateSchemaChangeEventList( identifier, nextSplit.getLastSchemaId(), nextSplit.getSchemaId()); currentTableReaderInfo = new TableReaderInfo(identifier, tableSchema); - currentSplitId = nextSplit.splitId(); currentReader = createLazyRecordReader(nextSplit.split()); currentDataRowsRead = nextSplit.recordsToSkip(); currentSchemaChangeEvents.clear(); @@ -240,6 +250,7 @@ private void checkSplitOrStartNext() throws IOException { if (currentDataRowsRead > 0) { seek(currentDataRowsRead); } + return true; } private long schemaChangeEventsToSkip( diff --git a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceRestoreTest.java b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceRestoreTest.java new file mode 100644 index 000000000000..8abd7a170cea --- /dev/null +++ b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceRestoreTest.java @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.paimon.flink.pipeline.cdc.source; + +import org.apache.paimon.CoreOptions; +import org.apache.paimon.catalog.Catalog; +import org.apache.paimon.catalog.CatalogContext; +import org.apache.paimon.catalog.CatalogFactory; +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.data.GenericRow; +import org.apache.paimon.disk.IOManager; +import org.apache.paimon.flink.pipeline.cdc.CDCOptions; +import org.apache.paimon.flink.pipeline.cdc.source.enumerator.CDCCheckpoint; +import org.apache.paimon.flink.pipeline.cdc.source.reader.CDCSourceSplitReader; +import org.apache.paimon.flink.source.metrics.FileStoreSourceReaderMetrics; +import org.apache.paimon.options.Options; +import org.apache.paimon.schema.Schema; +import org.apache.paimon.table.FileStoreTable; +import org.apache.paimon.table.sink.BatchTableCommit; +import org.apache.paimon.table.sink.BatchTableWrite; +import org.apache.paimon.table.sink.BatchWriteBuilder; +import org.apache.paimon.types.DataTypes; + +import org.apache.flink.api.connector.source.SplitEnumerator; +import org.apache.flink.cdc.common.configuration.Configuration; +import org.apache.flink.cdc.common.event.DataChangeEvent; +import org.apache.flink.cdc.common.event.Event; +import org.apache.flink.cdc.common.event.TableId; +import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds; +import org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition; +import org.apache.flink.connector.file.src.reader.BulkFormat.RecordIterator; +import org.apache.flink.connector.file.src.util.RecordAndPosition; +import org.apache.flink.connector.testutils.source.reader.TestingSplitEnumeratorContext; +import org.apache.flink.core.io.SimpleVersionedSerializer; +import org.apache.flink.metrics.groups.SourceReaderMetricGroup; +import org.apache.flink.metrics.groups.UnregisteredMetricsGroup; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.apache.paimon.flink.pipeline.cdc.CDCOptions.toCDCOption; +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for restoring the CDC source after removing tables while the job is stopped. */ +public class CDCSourceRestoreTest { + + private static final String DATABASE = "default"; + private static final Identifier FIRST = Identifier.create(DATABASE, "first"); + private static final Identifier SECOND = Identifier.create(DATABASE, "second"); + + @TempDir private Path tempDir; + + private Catalog catalog; + private CDCSource source; + + @BeforeEach + void beforeEach() throws Exception { + Options options = new Options(); + options.setString("warehouse", tempDir.resolve("warehouse").toUri().toString()); + CatalogContext catalogContext = CatalogContext.create(options); + catalog = CatalogFactory.createCatalog(catalogContext); + catalog.createDatabase(DATABASE, true); + Schema schema = + Schema.newBuilder() + .column("v", DataTypes.INT()) + .option(CoreOptions.BUCKET.key(), "1") + .option(CoreOptions.BUCKET_KEY.key(), "v") + .build(); + int value = 1; + for (Identifier identifier : Arrays.asList(FIRST, SECOND)) { + catalog.createTable(identifier, schema, false); + FileStoreTable table = (FileStoreTable) catalog.getTable(identifier); + BatchWriteBuilder builder = table.newBatchWriteBuilder(); + try (BatchTableWrite write = builder.newWrite(); + BatchTableCommit commit = builder.newCommit()) { + write.write(GenericRow.of(value++)); + commit.commit(write.prepareCommit()); + } + } + Configuration cdcConfig = new Configuration(); + cdcConfig.set(toCDCOption(CDCOptions.DATABASE), DATABASE); + source = + new CDCSource( + catalogContext, + cdcConfig, + new org.apache.flink.configuration.Configuration()); + } + + @AfterEach + void afterEach() throws Exception { + catalog.close(); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void testRestorePendingSplitsAfterDroppingTables(boolean dropAll) throws Exception { + CDCCheckpoint checkpoint = createCheckpoint(); + catalog.dropTable(FIRST, false); + if (dropAll) { + catalog.dropTable(SECOND, false); + } + TestingSplitEnumeratorContext context = newContext(); + try (SplitEnumerator restored = + source.restoreEnumerator(context, checkpoint)) { + CDCCheckpoint restoredCheckpoint = restored.snapshotState(2L); + assertThat(restoredCheckpoint.getTableProgressMap()).doesNotContainKey(FIRST); + if (dropAll) { + assertThat(restoredCheckpoint.getSplits()).isEmpty(); + assertThat(restoredCheckpoint.getTableProgressMap()).isEmpty(); + } else { + assertThat(restoredCheckpoint.getSplits()) + .extracting(TableAwareFileStoreSourceSplit::getIdentifier) + .containsExactly(SECOND); + restored.handleSplitRequest(0, "test-host"); + assertThat(context.getSplitAssignments().get(0).getAssignedSplits()) + .containsExactlyElementsOf(restoredCheckpoint.getSplits()); + } + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void testRestoreAssignedSplitsAfterDroppingTable(boolean dropAll) throws Exception { + CDCCheckpoint checkpoint = createCheckpoint(); + TestingSplitEnumeratorContext context = newContext(); + List assigned; + try (SplitEnumerator enumerator = + source.restoreEnumerator(context, checkpoint)) { + enumerator.handleSplitRequest(0, "test-host"); + assigned = context.getSplitAssignments().get(0).getAssignedSplits(); + } + assertThat(assigned).hasSize(2); + SimpleVersionedSerializer serializer = + source.getSplitSerializer(); + List restoredSplits = new ArrayList<>(); + for (TableAwareFileStoreSourceSplit split : assigned) { + restoredSplits.add( + serializer.deserialize(serializer.getVersion(), serializer.serialize(split))); + } + TableAwareFileStoreSourceSplit dropped = restoredSplits.get(0); + TableAwareFileStoreSourceSplit retained = restoredSplits.get(1); + catalog.dropTable(dropped.getIdentifier(), false); + if (dropAll) { + catalog.dropTable(retained.getIdentifier(), false); + } + SourceReaderMetricGroup metricGroup = + UnregisteredMetricsGroup.createSourceReaderMetricGroup(); + try (IOManager ioManager = IOManager.create(tempDir.resolve("spill").toString()); + CDCSourceSplitReader reader = + new CDCSourceSplitReader( + new FileStoreSourceReaderMetrics(metricGroup), + new CDCSource.TableManager(catalog, ioManager, metricGroup))) { + reader.handleSplitsChanges(new SplitsAddition<>(restoredSplits)); + RecordsWithSplitIds> skipped = reader.fetch(); + assertThat(skipped.finishedSplits()).containsExactly(dropped.splitId()); + assertThat(skipped.nextSplit()).isNull(); + skipped.recycle(); + + RecordsWithSplitIds> records = reader.fetch(); + if (dropAll) { + assertThat(records.finishedSplits()).containsExactly(retained.splitId()); + assertThat(records.nextSplit()).isNull(); + records.recycle(); + return; + } + assertThat(records.nextSplit()).isEqualTo(retained.splitId()); + RecordIterator iterator = records.nextRecordFromSplit(); + List changes = new ArrayList<>(); + RecordAndPosition record; + while ((record = iterator.next()) != null) { + if (record.getRecord() instanceof DataChangeEvent) { + changes.add((DataChangeEvent) record.getRecord()); + } + } + records.recycle(); + assertThat(changes).hasSize(1); + assertThat(changes.get(0).tableId()) + .isEqualTo(TableId.tableId(DATABASE, retained.getIdentifier().getObjectName())); + assertThat(changes.get(0).after().getInt(0)) + .isEqualTo(retained.getIdentifier().equals(FIRST) ? 1 : 2); + } + } + + private CDCCheckpoint createCheckpoint() throws Exception { + TestingSplitEnumeratorContext context = newContext(); + CDCCheckpoint checkpoint; + try (SplitEnumerator enumerator = + source.createEnumerator(context)) { + enumerator.start(); + context.triggerAllActions(); + context.triggerAllActions(); + checkpoint = enumerator.snapshotState(1L); + } + assertThat(checkpoint.getSplits()).hasSize(2); + assertThat(checkpoint.getTableProgressMap()).containsOnlyKeys(FIRST, SECOND); + SimpleVersionedSerializer serializer = + source.getEnumeratorCheckpointSerializer(); + return serializer.deserialize(serializer.getVersion(), serializer.serialize(checkpoint)); + } + + private TestingSplitEnumeratorContext newContext() { + TestingSplitEnumeratorContext context = + new TestingSplitEnumeratorContext<>(1); + context.registerReader(0, "test-host"); + return context; + } +} diff --git a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceTableManagerTest.java b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceTableManagerTest.java index 6312bcefd3f0..5aa5ea0edc49 100644 --- a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceTableManagerTest.java +++ b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceTableManagerTest.java @@ -46,6 +46,7 @@ import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** Tests for {@link CDCSource.TableManager}. */ public class CDCSourceTableManagerTest { @@ -83,6 +84,39 @@ public void afterEach() throws Exception { catalog.close(); } + @Test + public void testMissingTableIsNotCached() throws Exception { + CDCSource.TableManager tableManager = createTableManager(1, 1, 1); + catalog.dropTable(IDENTIFIER, false); + assertThat(tableManager.isTableAvailable(IDENTIFIER)).isFalse(); + assertThat(tableManager.isTableAvailable(IDENTIFIER)).isFalse(); + + catalog.createTable( + IDENTIFIER, Schema.newBuilder().column("v0", DataTypes.BIGINT()).build(), false); + assertThat(tableManager.isTableAvailable(IDENTIFIER)).isTrue(); + assertThat(tableManager.getTableSchema(IDENTIFIER, 0L).id()).isZero(); + } + + @Test + public void testAvailabilityPropagatesCatalogErrors() { + Catalog failingCatalog = + new DelegateCatalog(catalog) { + @Override + public CatalogLoader catalogLoader() { + return wrapped.catalogLoader(); + } + + @Override + public Table getTable(Identifier identifier) { + throw new IllegalStateException("catalog unavailable"); + } + }; + CDCSource.TableManager tableManager = createTableManager(failingCatalog, 1, 1, 1); + assertThatThrownBy(() -> tableManager.isTableAvailable(IDENTIFIER)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("catalog unavailable"); + } + @Test public void testSchemaCacheIsBoundedAndReloadable() throws Exception { CDCSource.TableManager tableManager = createTableManager(1, 1, 1); diff --git a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReaderTest.java b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReaderTest.java index 27ae504f21ec..c9acfa068c5f 100644 --- a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReaderTest.java +++ b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReaderTest.java @@ -945,6 +945,11 @@ public TestTableManager(TableRead tableRead, List schemaChang this.schemaChangeEvents = schemaChangeEvents; } + @Override + public boolean isTableAvailable(Identifier identifier) { + return true; + } + @Override public @Nullable TableSchema getTableSchema( Identifier identifier, @Nullable Long schemaId) { From 5a573a93fefe85944d327d60d3f78fa1555fc4c4 Mon Sep 17 00:00:00 2001 From: YueZhang <69956021+zhangyue19921010@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:33:03 -0500 Subject: [PATCH 2/2] [flink][cdc] Check for dropped tables only when loading an uncached split schema --- .../flink/pipeline/cdc/source/CDCSource.java | 28 +++++++++++++------ .../source/reader/CDCSourceSplitReader.java | 5 ++-- .../cdc/source/CDCSourceTableManagerTest.java | 27 ++++++++++++++---- .../reader/CDCSourceSplitReaderTest.java | 5 ++-- 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSource.java b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSource.java index 45530e02ece1..47cce8fcfebf 100644 --- a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSource.java +++ b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSource.java @@ -173,12 +173,30 @@ protected TableManager( return null; } + return getTableSchema(identifier, schemaId, false); + } + + /** + * Returns null if the table no longer exists or is not a file store table. The catalog is + * only consulted when the schema is not cached. + */ + public @Nullable TableSchema getTableSchemaIfAvailable( + Identifier identifier, long schemaId) { + return getTableSchema(identifier, schemaId, true); + } + + private @Nullable TableSchema getTableSchema( + Identifier identifier, long schemaId, boolean ignoreUnavailable) { Tuple2 cacheKey = Tuple2.of(identifier, schemaId); if (tableSchemaMap.containsKey(cacheKey)) { return tableSchemaMap.get(cacheKey); } - TableSchema tableSchema = loadTableSchema(identifier, schemaId); + FileStoreTable table = getTable(identifier, ignoreUnavailable); + if (table == null) { + return null; + } + TableSchema tableSchema = table.schemaManager().schema(schemaId); tableSchemaMap.put(cacheKey, tableSchema); return tableSchema; } @@ -206,10 +224,6 @@ public List generateSchemaChangeEventList( convertPaimonSchemaToFlinkCDCSchema(getTableSchema(identifier, schemaId))); } - public boolean isTableAvailable(Identifier identifier) { - return getTable(identifier, true) != null; - } - private FileStoreTable getTable(Identifier identifier) { return getTable(identifier, false); } @@ -240,10 +254,6 @@ private FileStoreTable getTable(Identifier identifier, boolean ignoreUnavailable return (FileStoreTable) table; } - private TableSchema loadTableSchema(Identifier identifier, long schemaId) { - return getTable(identifier).schemaManager().schema(schemaId); - } - private TableRead loadTableRead(Identifier identifier, TableSchema schema) { FileStoreTable table = getTable(identifier).copy(schema); return table.newReadBuilder() diff --git a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReader.java b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReader.java index dddaf38e09d0..3ef03978c9e4 100644 --- a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReader.java +++ b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReader.java @@ -226,14 +226,15 @@ private boolean checkSplitOrStartNext() throws IOException { Identifier identifier = nextSplit.getIdentifier(); currentSplitId = nextSplit.splitId(); // Reader state can contain splits for a table removed while the job was stopped. - if (!tableManager.isTableAvailable(identifier)) { + TableSchema tableSchema = + tableManager.getTableSchemaIfAvailable(identifier, nextSplit.getSchemaId()); + if (tableSchema == null) { LOG.info( "Skipping split {} because table {} no longer exists or is not a file store table.", currentSplitId, identifier); return false; } - TableSchema tableSchema = tableManager.getTableSchema(identifier, nextSplit.getSchemaId()); List schemaChangeEvents = tableManager.generateSchemaChangeEventList( identifier, nextSplit.getLastSchemaId(), nextSplit.getSchemaId()); diff --git a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceTableManagerTest.java b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceTableManagerTest.java index 5aa5ea0edc49..db5f73d0454a 100644 --- a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceTableManagerTest.java +++ b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/CDCSourceTableManagerTest.java @@ -88,13 +88,30 @@ public void afterEach() throws Exception { public void testMissingTableIsNotCached() throws Exception { CDCSource.TableManager tableManager = createTableManager(1, 1, 1); catalog.dropTable(IDENTIFIER, false); - assertThat(tableManager.isTableAvailable(IDENTIFIER)).isFalse(); - assertThat(tableManager.isTableAvailable(IDENTIFIER)).isFalse(); + assertThat(tableManager.getTableSchemaIfAvailable(IDENTIFIER, 0L)).isNull(); + assertThat(tableManager.getTableSchemaIfAvailable(IDENTIFIER, 0L)).isNull(); catalog.createTable( IDENTIFIER, Schema.newBuilder().column("v0", DataTypes.BIGINT()).build(), false); - assertThat(tableManager.isTableAvailable(IDENTIFIER)).isTrue(); - assertThat(tableManager.getTableSchema(IDENTIFIER, 0L).id()).isZero(); + assertThat(tableManager.getTableSchemaIfAvailable(IDENTIFIER, 0L).id()).isZero(); + } + + @Test + public void testCachedSchemaSkipsAvailabilityLookup() throws Exception { + Identifier anotherIdentifier = Identifier.create(DATABASE, "another_table"); + catalog.createTable( + anotherIdentifier, + Schema.newBuilder().column("v0", DataTypes.BIGINT()).build(), + false); + CountingCatalog countingCatalog = new CountingCatalog(catalog); + // The table cache is smaller than the schema cache, as with the default sizes. + CDCSource.TableManager tableManager = createTableManager(countingCatalog, 1, 2, 1); + + tableManager.getTableSchemaIfAvailable(IDENTIFIER, 0L); + tableManager.getTableSchemaIfAvailable(anotherIdentifier, 0L); + assertThat(tableManager.getTableSchemaIfAvailable(IDENTIFIER, 0L)).isNotNull(); + + assertThat(countingCatalog.getTableCount(IDENTIFIER)).isEqualTo(1); } @Test @@ -112,7 +129,7 @@ public Table getTable(Identifier identifier) { } }; CDCSource.TableManager tableManager = createTableManager(failingCatalog, 1, 1, 1); - assertThatThrownBy(() -> tableManager.isTableAvailable(IDENTIFIER)) + assertThatThrownBy(() -> tableManager.getTableSchemaIfAvailable(IDENTIFIER, 0L)) .isInstanceOf(IllegalStateException.class) .hasMessage("catalog unavailable"); } diff --git a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReaderTest.java b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReaderTest.java index c9acfa068c5f..215e47646cf3 100644 --- a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReaderTest.java +++ b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/pipeline/cdc/source/reader/CDCSourceSplitReaderTest.java @@ -946,8 +946,9 @@ public TestTableManager(TableRead tableRead, List schemaChang } @Override - public boolean isTableAvailable(Identifier identifier) { - return true; + public @Nullable TableSchema getTableSchemaIfAvailable( + Identifier identifier, long schemaId) { + return getTableSchema(identifier, schemaId); } @Override