lvyanquan commented on code in PR #3957:
URL: https://github.com/apache/flink-cdc/pull/3957#discussion_r2014607752
##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-mysql/src/main/java/org/apache/flink/cdc/connectors/mysql/factory/MySqlDataSourceFactory.java:
##########
@@ -520,8 +526,32 @@ private String validateTableAndReturnDebeziumStyle(String
tables) {
+ SCAN_BINLOG_NEWLY_ADDED_TABLE_ENABLED
+ " was enabled.");
Review Comment:
What's more, could we replace `,` with `|` here?
##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-mysql/src/test/java/org/apache/flink/cdc/connectors/mysql/source/MySqlTablePatternMatchingTest.java:
##########
@@ -0,0 +1,421 @@
+/*
+ * 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;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.cdc.common.configuration.Configuration;
+import org.apache.flink.cdc.common.factories.Factory;
+import org.apache.flink.cdc.common.pipeline.PipelineOptions;
+import org.apache.flink.cdc.common.pipeline.SchemaChangeBehavior;
+import org.apache.flink.cdc.composer.PipelineExecution;
+import org.apache.flink.cdc.composer.definition.PipelineDef;
+import org.apache.flink.cdc.composer.definition.SinkDef;
+import org.apache.flink.cdc.composer.definition.SourceDef;
+import org.apache.flink.cdc.composer.flink.FlinkPipelineComposer;
+import org.apache.flink.cdc.connectors.mysql.factory.MySqlDataSourceFactory;
+import org.apache.flink.cdc.connectors.values.factory.ValuesDataFactory;
+import org.apache.flink.cdc.connectors.values.sink.ValuesDataSinkOptions;
+
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import javax.annotation.Nullable;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static
org.apache.flink.cdc.connectors.mysql.source.MySqlDataSourceOptions.HOSTNAME;
+import static
org.apache.flink.cdc.connectors.mysql.source.MySqlDataSourceOptions.PASSWORD;
+import static
org.apache.flink.cdc.connectors.mysql.source.MySqlDataSourceOptions.PORT;
+import static
org.apache.flink.cdc.connectors.mysql.source.MySqlDataSourceOptions.SCAN_BINLOG_NEWLY_ADDED_TABLE_ENABLED;
+import static
org.apache.flink.cdc.connectors.mysql.source.MySqlDataSourceOptions.TABLES;
+import static
org.apache.flink.cdc.connectors.mysql.source.MySqlDataSourceOptions.TABLES_EXCLUDE;
+import static
org.apache.flink.cdc.connectors.mysql.source.MySqlDataSourceOptions.USERNAME;
+import static
org.apache.flink.cdc.connectors.mysql.testutils.MySqSourceTestUtils.TEST_PASSWORD;
+import static
org.apache.flink.cdc.connectors.mysql.testutils.MySqSourceTestUtils.TEST_USER;
+import static
org.apache.flink.cdc.connectors.mysql.testutils.MySqSourceTestUtils.loopCheck;
+
+/** Test cases for matching MySQL source tables. */
+class MySqlTablePatternMatchingTest extends MySqlSourceTestBase {
+
+ private final PrintStream standardOut = System.out;
+
+ private static final List<Tuple2<String, String>> TEST_TABLES =
+ Arrays.asList(
+ Tuple2.of("db", "tbl1"),
+ Tuple2.of("db", "tbl2"),
+ Tuple2.of("db", "tbl3"),
+ Tuple2.of("db", "tbl4"),
+ Tuple2.of("db2", "tbl2"),
+ Tuple2.of("db3", "tbl3"),
+ Tuple2.of("db4", "tbl4"));
+
+ @BeforeAll
+ static void initializeDatabase() throws Exception {
+ initializeMySqlTables(TEST_TABLES);
+ }
+
+ @AfterAll
+ static void tearDownDatabase() throws Exception {
+ tearDownMySqlTables(TEST_TABLES);
+ }
+
+ @Test
+ void testWildcardMatching() throws Exception {
+ Assertions.assertThat(testGenericTableMatching("\\.*.\\.*", null,
false))
+ .containsExactlyInAnyOrder(
+ "db.tbl1",
+ "db.tbl2",
+ "db.tbl3",
+ "db.tbl4",
+ "db2.tbl2",
+ "db3.tbl3",
+ "db4.tbl4");
+
+ Assertions.assertThat(testGenericTableMatching("\\.*.\\.*", null,
true))
+ .containsExactlyInAnyOrder(".*\\..*");
+ }
+
+ @Test
+ void testWildcardMatchingDatabases() throws Exception {
+ Assertions.assertThat(testGenericTableMatching("\\.*.tbl[3-4]", null,
false))
+ .containsExactlyInAnyOrder("db.tbl3", "db.tbl4", "db3.tbl3",
"db4.tbl4");
+
+ Assertions.assertThat(testGenericTableMatching("\\.*.tbl[3-4]", null,
true))
+ .containsExactlyInAnyOrder(".*\\.tbl[3-4]");
+ }
+
+ @Test
+ void testWildcardMatchingTables() throws Exception {
+ Assertions.assertThat(testGenericTableMatching("db.\\.*", null, false))
+ .containsExactlyInAnyOrder("db.tbl1", "db.tbl2", "db.tbl3",
"db.tbl4");
+
+ Assertions.assertThat(testGenericTableMatching("db.\\.*", null, true))
+ .containsExactlyInAnyOrder("db\\..*");
+ }
+
+ @Test
+ void testWildcardMatchingPartialDatabases() throws Exception {
+ // `db.` matches `db2`, `db3`, `db4` but not `db`
+ Assertions.assertThat(testGenericTableMatching("db\\..\\.*", null,
false))
+ .containsExactlyInAnyOrder("db2.tbl2", "db3.tbl3", "db4.tbl4");
+
+ Assertions.assertThat(testGenericTableMatching("db\\..\\.*", null,
true))
+ .containsExactlyInAnyOrder("db.\\..*");
+ }
+
+ @Test
+ void testWildcardMatchingWithExclusion() throws Exception {
+ Assertions.assertThat(testGenericTableMatching("\\.*.\\.*", "db.tbl3",
false))
+ .containsExactlyInAnyOrder(
+ "db.tbl1", "db.tbl2", "db.tbl4", "db2.tbl2",
"db3.tbl3", "db4.tbl4");
+ }
+
+ @Test
+ void testWildcardMatchingDatabasesWithExclusion() throws Exception {
+ Assertions.assertThat(testGenericTableMatching("\\.*.tbl[3-4]",
"db.tbl[3-4]", false))
+ .containsExactlyInAnyOrder("db3.tbl3", "db4.tbl4");
+ }
+
+ @Test
+ void testWildcardMatchingTablesWithExclusion() throws Exception {
+ Assertions.assertThat(testGenericTableMatching("db.\\.*", "db.tbl4",
false))
+ .containsExactlyInAnyOrder("db.tbl1", "db.tbl2", "db.tbl3");
+ }
+
+ @Test
+ void testWildcardMatchingPartialDatabasesWithExclusion() throws Exception {
+ // `db.` matches `db2`, `db3`, `db4` but not `db`
+ Assertions.assertThat(testGenericTableMatching("db\\..\\.*",
"db3.\\.*", false))
+ .containsExactlyInAnyOrder("db2.tbl2", "db4.tbl4");
+ }
+
+ @Test
+ void testWildcardMatchingRealTables() throws Exception {
+ String[] expected =
+ new String[] {
+ "CreateTableEvent{tableId=db.tbl1, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl1, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db.tbl2, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl2, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db.tbl3, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl3, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db.tbl4, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl4, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db2.tbl2, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db2.tbl2, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db3.tbl3, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db3.tbl3, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db4.tbl4, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db4.tbl4, before=[], after=[17],
op=INSERT, meta=()}"
+ };
+
+ Assertions.assertThat(testRealWorldTableMatching("\\.*.\\.*", null,
false, expected.length))
+ .containsExactlyInAnyOrder(expected);
+
+ Assertions.assertThat(testRealWorldTableMatching("\\.*.\\.*", null,
true, expected.length))
+ .containsExactlyInAnyOrder(expected);
+ }
+
+ @Test
+ void testWildcardMatchingDatabasesRealTables() throws Exception {
+ String[] expected =
+ new String[] {
+ "CreateTableEvent{tableId=db.tbl3, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl3, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db.tbl4, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl4, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db3.tbl3, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db3.tbl3, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db4.tbl4, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db4.tbl4, before=[], after=[17],
op=INSERT, meta=()}"
+ };
+
+ Assertions.assertThat(
+ testRealWorldTableMatching("\\.*.tbl[3-4]", null,
false, expected.length))
+ .containsExactlyInAnyOrder(expected);
+
+ Assertions.assertThat(
+ testRealWorldTableMatching("\\.*.tbl[3-4]", null,
true, expected.length))
+ .containsExactlyInAnyOrder(expected);
+ }
+
+ @Test
+ void testWildcardMatchingTablesRealTables() throws Exception {
+ String[] expected =
+ new String[] {
+ "CreateTableEvent{tableId=db.tbl1, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl1, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db.tbl2, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl2, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db.tbl3, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl3, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db.tbl4, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db.tbl4, before=[], after=[17],
op=INSERT, meta=()}"
+ };
+
+ Assertions.assertThat(testRealWorldTableMatching("db.\\.*", null,
false, expected.length))
+ .containsExactlyInAnyOrder(expected);
+
+ Assertions.assertThat(testRealWorldTableMatching("db.\\.*", null,
true, expected.length))
+ .containsExactlyInAnyOrder(expected);
+ }
+
+ @Test
+ void testWildcardMatchingPartialDatabasesRealTables() throws Exception {
+ String[] expected =
+ new String[] {
+ "CreateTableEvent{tableId=db2.tbl2, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db2.tbl2, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db3.tbl3, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db3.tbl3, before=[], after=[17],
op=INSERT, meta=()}",
+ "CreateTableEvent{tableId=db4.tbl4, schema=columns={`id`
INT NOT NULL}, primaryKeys=id, options=()}",
+ "DataChangeEvent{tableId=db4.tbl4, before=[], after=[17],
op=INSERT, meta=()}"
+ };
+
+ // `db.` matches `db2`, `db3`, `db4` but not `db`
+ Assertions.assertThat(
+ testRealWorldTableMatching("db\\..\\.*", null, false,
expected.length))
+ .containsExactlyInAnyOrder(expected);
+
+ Assertions.assertThat(testRealWorldTableMatching("db\\..\\.*", null,
true, expected.length))
+ .containsExactlyInAnyOrder(expected);
+ }
+
+ private static void initializeMySqlTables(List<Tuple2<String, String>>
tableNames)
+ throws Exception {
+ tableNames.forEach(
+ tableName -> {
+ try (Connection connection =
+ DriverManager.getConnection(
+ MYSQL_CONTAINER.getJdbcUrl(),
+ TEST_USER,
+ TEST_PASSWORD);
+ Statement statement =
connection.createStatement()) {
+ statement.execute(
+ String.format("CREATE DATABASE IF NOT EXISTS
`%s`;", tableName.f0));
+ statement.execute(
+ String.format(
+ "CREATE TABLE IF NOT EXISTS `%s`.`%s`
(id INT PRIMARY KEY NOT NULL);",
+ tableName.f0, tableName.f1));
+
+ statement.execute(
+ String.format(
+ "INSERT INTO `%s`.`%s` VALUES (17);",
+ tableName.f0, tableName.f1));
+ } catch (SQLException e) {
+ throw new RuntimeException("Failed to initialize
databases and tables", e);
+ }
+ });
+ }
+
+ private static void tearDownMySqlTables(List<Tuple2<String, String>>
tableNames)
+ throws Exception {
+ tableNames.forEach(
+ tableName -> {
+ try (Connection connection =
+ DriverManager.getConnection(
+ MYSQL_CONTAINER.getJdbcUrl(),
+ TEST_USER,
+ TEST_PASSWORD);
+ Statement statement =
connection.createStatement()) {
+ statement.execute(
+ String.format("DROP DATABASE IF EXISTS `%s`;",
tableName.f0));
+ } catch (SQLException e) {
+ throw new RuntimeException("Failed to clean-up
databases", e);
+ }
+ });
+ }
+
+ private List<String> testGenericTableMatching(
+ String tablesConfig, @Nullable String tablesExclude, boolean
scanBinlogNewlyAddedTable)
+ throws Exception {
+ Map<String, String> options = new HashMap<>();
+ options.put(HOSTNAME.key(), MYSQL_CONTAINER.getHost());
+ options.put(PORT.key(),
String.valueOf(MYSQL_CONTAINER.getDatabasePort()));
+ options.put(USERNAME.key(), TEST_USER);
+ options.put(PASSWORD.key(), TEST_PASSWORD);
+ options.put(TABLES.key(), tablesConfig);
+ options.put(
+ SCAN_BINLOG_NEWLY_ADDED_TABLE_ENABLED.key(),
+ String.valueOf(scanBinlogNewlyAddedTable));
+ if (tablesExclude != null) {
+ options.put(TABLES_EXCLUDE.key(), tablesExclude);
+ }
+ Factory.Context context = new
MockContext(Configuration.fromMap(options));
+
+ MySqlDataSourceFactory factory = new MySqlDataSourceFactory();
+ MySqlDataSource dataSource = (MySqlDataSource)
factory.createDataSource(context);
+ return dataSource.getSourceConfig().getTableList();
+ }
+
+ class MockContext implements Factory.Context {
+
+ Configuration factoryConfiguration;
+
+ public MockContext(Configuration factoryConfiguration) {
+ this.factoryConfiguration = factoryConfiguration;
+ }
+
+ @Override
+ public Configuration getFactoryConfiguration() {
+ return factoryConfiguration;
+ }
+
+ @Override
+ public Configuration getPipelineConfiguration() {
+ return null;
+ }
+
+ @Override
+ public ClassLoader getClassLoader() {
+ return this.getClassLoader();
+ }
+ }
+
+ private List<String> testRealWorldTableMatching(
Review Comment:
getRealWorldMatchedTables
--
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]