ruanhang1993 commented on code in PR #4422: URL: https://github.com/apache/flink-cdc/pull/4422#discussion_r3763132332
########## flink-cdc-connect/flink-cdc-source-connectors/flink-connector-debezium/src/main/java/io/debezium/relational/CachedTableFilter.java: ########## @@ -0,0 +1,67 @@ +/* + * 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 io.debezium.relational; + +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.relational.Tables.TableFilter; + +/** A bounded cache for table filter results. */ +public class CachedTableFilter implements TableFilter { + + private static final long TABLE_FILTER_CACHE_MAXIMUM_SIZE = 1024; + + private final TableFilter rawTableFilter; + private final LoadingCache<TableId, Boolean> tableFilterCache; + + private CachedTableFilter(TableFilter rawTableFilter) { + this.rawTableFilter = rawTableFilter; + this.tableFilterCache = + CacheBuilder.newBuilder() + .maximumSize(TABLE_FILTER_CACHE_MAXIMUM_SIZE) + .build( + new CacheLoader<TableId, Boolean>() { + @Override + public Boolean load(TableId tableId) { + return rawTableFilter.isIncluded(tableId); + } + }); + } + + /** Wraps the given filter in a cache, or returns it unchanged if it is already cached. */ + public static CachedTableFilter from(TableFilter tableFilter) { Review Comment: checkNotNull(tableFilter) ########## flink-cdc-connect/flink-cdc-source-connectors/flink-connector-debezium/src/main/java/io/debezium/relational/CachedTableFilter.java: ########## @@ -0,0 +1,67 @@ +/* + * 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 io.debezium.relational; + +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.relational.Tables.TableFilter; + +/** A bounded cache for table filter results. */ +public class CachedTableFilter implements TableFilter { + + private static final long TABLE_FILTER_CACHE_MAXIMUM_SIZE = 1024; + + private final TableFilter rawTableFilter; + private final LoadingCache<TableId, Boolean> tableFilterCache; + + private CachedTableFilter(TableFilter rawTableFilter) { + this.rawTableFilter = rawTableFilter; + this.tableFilterCache = + CacheBuilder.newBuilder() + .maximumSize(TABLE_FILTER_CACHE_MAXIMUM_SIZE) + .build( + new CacheLoader<TableId, Boolean>() { + @Override + public Boolean load(TableId tableId) { + return rawTableFilter.isIncluded(tableId); + } + }); + } + + /** Wraps the given filter in a cache, or returns it unchanged if it is already cached. */ + public static CachedTableFilter from(TableFilter tableFilter) { + if (tableFilter instanceof CachedTableFilter) { + return (CachedTableFilter) tableFilter; + } + return new CachedTableFilter(tableFilter); + } + + /** Returns a new cached filter that also requires the additional filter to match. */ + public CachedTableFilter withAdditionalFilter(TableFilter additionalFilter) { + TableFilter rawFilter = rawTableFilter; Review Comment: checkNotNull(additionalFilter) ########## flink-cdc-connect/flink-cdc-source-connectors/flink-connector-debezium/src/main/java/io/debezium/relational/RelationalTableFilters.java: ########## @@ -92,7 +94,7 @@ public RelationalTableFilters( ? tablePredicate.and(systemTablesFilter::isIncluded) : tablePredicate; - this.tableFilter = finalTablePredicate::test; + this.tableFilter = CachedTableFilter.from(finalTablePredicate::test); Review Comment: `tableFilter::isIncluded` at lines 117–120 is a bound method reference. It captures the current tableFilter instance during construction; it does not dynamically read `this.tableFilter` on each invocation. When `database.history.store.only.captured.tables.ddl=true` and MySQL later installs the filter containing excludeTableList, the object graph becomes: ``` schemaSnapshotFilter -> initial CachedTableFilter (Cache A) -> raw Debezium filter dataCollectionFilter -> final CachedTableFilter (Cache B) -> raw Debezium filter + MySQL exclude filter ``` We should preserve an explicit reference to the uncached initial filter and use that for schemaSnapshotFilter. ```java TableFilter initialTableFilter = finalTablePredicate::test; this.tableFilter = CachedTableFilter.from(initialTableFilter); // ... this.schemaSnapshotFilter = config.getBoolean(DatabaseHistory.STORE_ONLY_CAPTURED_TABLES_DDL) ? eligibleSchemaPredicate.and(initialTableFilter::isIncluded)::test : eligibleSchemaPredicate::test; ``` ########## flink-cdc-connect/flink-cdc-source-connectors/flink-connector-debezium/src/main/java/io/debezium/relational/CachedTableFilter.java: ########## @@ -0,0 +1,67 @@ +/* + * 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 io.debezium.relational; + +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.relational.Tables.TableFilter; + +/** A bounded cache for table filter results. */ +public class CachedTableFilter implements TableFilter { + + private static final long TABLE_FILTER_CACHE_MAXIMUM_SIZE = 1024; + + private final TableFilter rawTableFilter; + private final LoadingCache<TableId, Boolean> tableFilterCache; + + private CachedTableFilter(TableFilter rawTableFilter) { + this.rawTableFilter = rawTableFilter; + this.tableFilterCache = + CacheBuilder.newBuilder() + .maximumSize(TABLE_FILTER_CACHE_MAXIMUM_SIZE) Review Comment: The cache size does not affect filtering correctness, but it directly affects the performance improvement. I am not sure whether we should use a larger size and add TTL for this cache. @leonardBang @lvyanquan @taoran92 Do you have any suggestions on this? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
