-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[FLINK-37676][cdc-common] Add caching mechanism to Selectors for improved performance #3994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joyCurry30
wants to merge
1
commit into
apache:master
Choose a base branch
from
joyCurry30:FLINK-37676
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,6 +20,10 @@ | |||||||||||||||||||
import org.apache.flink.cdc.common.event.TableId; | ||||||||||||||||||||
import org.apache.flink.cdc.common.utils.Predicates; | ||||||||||||||||||||
|
||||||||||||||||||||
import org.apache.flink.shaded.guava31.com.google.common.cache.Cache; | ||||||||||||||||||||
import org.apache.flink.shaded.guava31.com.google.common.cache.CacheBuilder; | ||||||||||||||||||||
|
||||||||||||||||||||
import java.time.Duration; | ||||||||||||||||||||
import java.util.ArrayList; | ||||||||||||||||||||
import java.util.Iterator; | ||||||||||||||||||||
import java.util.List; | ||||||||||||||||||||
|
@@ -29,8 +33,16 @@ | |||||||||||||||||||
/** Selectors for filtering tables. */ | ||||||||||||||||||||
public class Selectors { | ||||||||||||||||||||
|
||||||||||||||||||||
private static final Duration CACHE_EXPIRE_DURATION = Duration.ofHours(1); | ||||||||||||||||||||
|
||||||||||||||||||||
private List<Selector> selectors; | ||||||||||||||||||||
|
||||||||||||||||||||
private final Cache<TableId, Boolean> cache = | ||||||||||||||||||||
CacheBuilder.newBuilder() | ||||||||||||||||||||
.expireAfterAccess(CACHE_EXPIRE_DURATION) | ||||||||||||||||||||
.maximumSize(1024) | ||||||||||||||||||||
.build(); | ||||||||||||||||||||
|
||||||||||||||||||||
private Selectors() {} | ||||||||||||||||||||
|
||||||||||||||||||||
/** | ||||||||||||||||||||
|
@@ -71,8 +83,20 @@ public boolean isMatch(TableId tableId) { | |||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/** Match the {@link TableId} against the {@link Selector}s. * */ | ||||||||||||||||||||
/** Match the {@link TableId} against the {@link Selector}s. */ | ||||||||||||||||||||
public boolean isMatch(TableId tableId) { | ||||||||||||||||||||
Boolean cachedResult = cache.getIfPresent(tableId); | ||||||||||||||||||||
if (cachedResult != null) { | ||||||||||||||||||||
return cachedResult; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
boolean match = computeIsMatch(tableId); | ||||||||||||||||||||
cache.put(tableId, match); | ||||||||||||||||||||
return match; | ||||||||||||||||||||
Comment on lines
+88
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be simplified with
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/** Computes the match result if not present in the cache. */ | ||||||||||||||||||||
private boolean computeIsMatch(TableId tableId) { | ||||||||||||||||||||
for (Selector selector : selectors) { | ||||||||||||||||||||
if (selector.isMatch(tableId)) { | ||||||||||||||||||||
return true; | ||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...tests/src/test/java/org/apache/flink/cdc/pipeline/tests/benchmark/SelectorsBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* 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.pipeline.tests.benchmark; | ||
|
||
import org.apache.flink.cdc.common.event.TableId; | ||
import org.apache.flink.cdc.common.schema.Selectors; | ||
|
||
import org.apache.flink.shaded.guava31.com.google.common.collect.ImmutableList; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Threads; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Benchmark for table selector performance with and without cache. | ||
* | ||
* <pre> | ||
* Benchmark Mode Cnt Score Error Units | ||
* SelectorsBenchmark.testSelectorWithCache thrpt 20 1028.979 ± 218.663 ops/ms | ||
* SelectorsBenchmark.testSelectorWithoutCache thrpt 20 136.747 ± 11.872 ops/ms | ||
* </pre> | ||
*/ | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 10, time = 2, timeUnit = TimeUnit.SECONDS) | ||
@Fork(2) | ||
@Threads(2) | ||
public class SelectorsBenchmark { | ||
|
||
private Selectors selectors; | ||
private List<TableId> queryTableIds; | ||
|
||
@Setup(Level.Trial) | ||
public void setup() { | ||
selectors = | ||
new Selectors.SelectorsBuilder() | ||
.includeTables( | ||
"test_wms_inventory_[a-z]+.inventory_batch_detail," | ||
+ "test_wms_inventory_[a-z]+.inventory_batch_detail_record," | ||
+ "test_wms_inventory_[a-z]+.inventory_batch_input," | ||
+ "test_wms_inventory_[a-z]+.inventory_flow_volume_level," | ||
+ "test_wms_inventory_[a-z]+.inventory_snapshot," | ||
+ "test_wms_log_[a-z]+.log_common_log_[a-z]+") | ||
.build(); | ||
|
||
queryTableIds = | ||
ImmutableList.of( | ||
TableId.tableId( | ||
"test_wms_common_europe.occupy_strategy_exe_progress_order"), | ||
TableId.tableId("test_wms_common_europe.wave_strategy_rule_relation"), | ||
TableId.tableId("db.sc2.A1"), | ||
TableId.tableId("db2.sc2.A1"), | ||
TableId.tableId("test_wms_output_s.out_moment_storage_location_relation"), | ||
TableId.tableId("test_wms_output_a.out_moment_storage_location_relation")); | ||
|
||
// warm up cache | ||
for (TableId id : queryTableIds) { | ||
selectors.isMatch(id); | ||
} | ||
} | ||
|
||
/** | ||
* Benchmark to evaluate the performance of table selector with caching enabled. | ||
* | ||
* <p>This benchmark measures throughput when using a pre-built {@link Selectors} instance that | ||
* leverages internal caching mechanisms. This simulates a typical usage scenario where selector | ||
* rules are initialized once and reused across multiple queries. | ||
* | ||
* <p>Expected to perform significantly better than non-cached version due to avoidance of | ||
* repeated regex parsing and compilation. | ||
*/ | ||
@Benchmark | ||
public void testSelectorWithCache() { | ||
for (TableId id : queryTableIds) { | ||
selectors.isMatch(id); | ||
} | ||
} | ||
|
||
/** | ||
* Benchmark to evaluate the performance of table selector without using cache. | ||
* | ||
* <p>This benchmark constructs a new {@link Selectors} instance for each invocation, simulating | ||
* a cold-start or ad-hoc usage scenario. The overhead includes pattern parsing and matcher | ||
* construction, which significantly impacts throughput. | ||
* | ||
* <p>Useful for understanding worst-case performance and comparing against the cached version. | ||
*/ | ||
@Benchmark | ||
public void testSelectorWithoutCache() { | ||
Selectors freshSelectors = | ||
new Selectors.SelectorsBuilder() | ||
.includeTables( | ||
"test_wms_inventory_[a-z]+.inventory_batch_detail," | ||
+ "test_wms_inventory_[a-z]+.inventory_batch_detail_record," | ||
+ "test_wms_inventory_[a-z]+.inventory_batch_input," | ||
+ "test_wms_inventory_[a-z]+.inventory_flow_volume_level," | ||
+ "test_wms_inventory_[a-z]+.inventory_snapshot," | ||
+ "test_wms_log_[a-z]+.log_common_log_[a-z]+") | ||
.build(); | ||
for (TableId id : queryTableIds) { | ||
freshSelectors.isMatch(id); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
Options options = | ||
new OptionsBuilder() | ||
.include(SelectorsBenchmark.class.getSimpleName()) | ||
.detectJvmArgs() | ||
.build(); | ||
new Runner(options).run(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can use
LoadingCache
here instead of checking / updating caches manually.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using LoadingCache is not suitable here because the cache value computation (computeIsMatch) depends on the internal state of the Selectors instance (specifically the selectors list). LoadingCache requires a stateless or independently executable loading function. Forcing it here would introduce unnecessary complexity and potential bugs — the current manual approach is safer, clearer, and easier to maintain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC both
Selector
andSelectors
are meant to be effectivelyfinal
and should not have any mutable states. Any comments @GOODBOY008?