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 @@ -22,6 +22,10 @@
import org.apache.flink.cdc.connectors.mysql.table.StartupOptions;
import org.apache.flink.table.catalog.ObjectPath;

import org.apache.flink.shaded.guava31.com.google.common.cache.CacheBuilder;
import org.apache.flink.shaded.guava31.com.google.common.cache.CacheLoader;
import org.apache.flink.shaded.guava31.com.google.common.cache.LoadingCache;

import io.debezium.config.Configuration;
import io.debezium.connector.mysql.MySqlConnectorConfig;
import io.debezium.relational.RelationalTableFilters;
Expand All @@ -42,6 +46,8 @@
/** A MySql Source configuration which is used by {@link MySqlSource}. */
public class MySqlSourceConfig implements Serializable {
private static final long serialVersionUID = 1L;
private static final Duration TABLE_FILTER_CACHE_EXPIRE_DURATION = Duration.ofHours(1);
private static final long TABLE_FILTER_CACHE_MAXIMUM_SIZE = 1024;

private final String hostname;
private final int port;
Expand Down Expand Up @@ -146,11 +152,7 @@ public class MySqlSourceConfig implements Serializable {
Tables.TableFilter tableFilter = dbzMySqlConfig.getTableFilters().dataCollectionFilter();
dbzMySqlConfig
.getTableFilters()
.setDataCollectionFilters(
(TableId tableId) ->
tableFilter.isIncluded(tableId)
&& (excludeTableFilter == null
|| !excludeTableFilter.isMatch(tableId)));
.setDataCollectionFilters(createCachedTableFilter(tableFilter, excludeTableFilter));
this.jdbcProperties = jdbcProperties;
this.chunkKeyColumns = chunkKeyColumns;
this.skipSnapshotBackfill = skipSnapshotBackfill;
Expand Down Expand Up @@ -284,6 +286,31 @@ public Predicate<TableId> getTableFilter() {
return tableId -> tableFilters.dataCollectionFilter().isIncluded(tableId);
}

static Tables.TableFilter createCachedTableFilter(
Tables.TableFilter tableFilter, @Nullable Selectors excludeTableFilter) {
LoadingCache<TableId, Boolean> tableFilterCache =
CacheBuilder.newBuilder()
.expireAfterAccess(TABLE_FILTER_CACHE_EXPIRE_DURATION)
.maximumSize(TABLE_FILTER_CACHE_MAXIMUM_SIZE)
.build(
new CacheLoader<TableId, Boolean>() {
@Override
public Boolean load(TableId tableId) {
return isTableIncluded(
tableFilter, excludeTableFilter, tableId);
}
});
return tableFilterCache::getUnchecked;
}
Comment on lines +289 to +304

@taoran92 taoran92 Jun 17, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I replaced the unbounded ConcurrentHashMap with a bounded LoadingCache (max 1024 entries, expire after 1h access), so hot TableIds are still cached while long-running jobs won't retain unlimited dynamic table keys.

BTW, I kept the cache bounds as internal constants instead of adding new connector options. This cache is only an internal optimization: eviction may cause the table filter to be evaluated again, but it does not affect correctness. Exposing these values would expand the public connector API without a clear user-facing semantic need.

The default maximum size follows the existing selector cache convention in the codebase, and we can still expose/tune it later if real workloads show the defaults are insufficient.


private static boolean isTableIncluded(
Tables.TableFilter tableFilter,
@Nullable Selectors excludeTableFilter,
TableId tableId) {
return tableFilter.isIncluded(tableId)
&& (excludeTableFilter == null || !excludeTableFilter.isMatch(tableId));
}

public Properties getJdbcProperties() {
return jdbcProperties;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.flink.cdc.connectors.mysql.source.config;

import io.debezium.relational.TableId;
import io.debezium.relational.Tables;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;

import static org.assertj.core.api.Assertions.assertThat;

/** Tests for {@link MySqlSourceConfig}. */
class MySqlSourceConfigTest {

@Test
void testCachesTableFilterResults() {
AtomicInteger filterInvocationCount = new AtomicInteger();
Tables.TableFilter cachedTableFilter =
MySqlSourceConfig.createCachedTableFilter(
tableId -> {
filterInvocationCount.incrementAndGet();
return tableId.table().startsWith("orders");
},
null);

TableId includedTable = new TableId("test_db", null, "orders_1");
TableId sameIncludedTable = new TableId("test_db", null, "orders_1");
TableId unmatchedTable = new TableId("test_db", null, "customers");
TableId sameUnmatchedTable = new TableId("test_db", null, "customers");

assertThat(cachedTableFilter.isIncluded(includedTable)).isTrue();
assertThat(cachedTableFilter.isIncluded(includedTable)).isTrue();
assertThat(cachedTableFilter.isIncluded(sameIncludedTable)).isTrue();
assertThat(cachedTableFilter.isIncluded(unmatchedTable)).isFalse();
assertThat(cachedTableFilter.isIncluded(unmatchedTable)).isFalse();
assertThat(cachedTableFilter.isIncluded(sameUnmatchedTable)).isFalse();
assertThat(filterInvocationCount).hasValue(2);
Comment thread
taoran92 marked this conversation as resolved.
}

@Test
void testTableFilterWithExcludeTableList() {
MySqlSourceConfig config =
new MySqlSourceConfigFactory()
.hostname("localhost")
.username("user")
.password("password")
.databaseList("test_db")
.tableList("test_db\\.orders_.*")
.excludeTableList("test_db.orders_skip")
Comment thread
taoran92 marked this conversation as resolved.
.createConfig(0);

Predicate<TableId> tableFilter = config.getTableFilter();
TableId includedTable = new TableId("test_db", null, "orders_1");
TableId excludedTable = new TableId("test_db", null, "orders_skip");
TableId unmatchedTable = new TableId("test_db", null, "customers");

assertThat(tableFilter.test(includedTable)).isTrue();
assertThat(tableFilter.test(excludedTable)).isFalse();
assertThat(tableFilter.test(unmatchedTable)).isFalse();
}
}
Loading