Copilot commented on code in PR #4278: URL: https://github.com/apache/flink-cdc/pull/4278#discussion_r2882786119
########## flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/test/java/org/apache/flink/cdc/connectors/mysql/source/TableExclusionDuringSnapshotIT.java: ########## @@ -0,0 +1,283 @@ +/* + * 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.common.eventtime.WatermarkStrategy; +import org.apache.flink.cdc.connectors.mysql.debezium.DebeziumUtils; +import org.apache.flink.cdc.connectors.mysql.source.split.MySqlSnapshotSplit; +import org.apache.flink.cdc.connectors.mysql.source.utils.hooks.SnapshotPhaseHooks; +import org.apache.flink.cdc.connectors.mysql.testutils.UniqueDatabase; +import org.apache.flink.cdc.debezium.table.RowDataDebeziumDeserializeSchema; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.StateRecoveryOptions; +import org.apache.flink.core.execution.CheckpointingMode; Review Comment: `StreamExecutionEnvironment#enableCheckpointing(..., CheckpointingMode)` uses `org.apache.flink.streaming.api.CheckpointingMode` (as in other MySQL ITs). Importing `org.apache.flink.core.execution.CheckpointingMode` here is likely incorrect and may fail compilation / use the wrong type. ```suggestion import org.apache.flink.streaming.api.CheckpointingMode; ``` ########## flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/test/java/org/apache/flink/cdc/connectors/mysql/source/TableExclusionDuringSnapshotIT.java: ########## @@ -0,0 +1,283 @@ +/* + * 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.common.eventtime.WatermarkStrategy; +import org.apache.flink.cdc.connectors.mysql.debezium.DebeziumUtils; +import org.apache.flink.cdc.connectors.mysql.source.split.MySqlSnapshotSplit; +import org.apache.flink.cdc.connectors.mysql.source.utils.hooks.SnapshotPhaseHooks; +import org.apache.flink.cdc.connectors.mysql.testutils.UniqueDatabase; +import org.apache.flink.cdc.debezium.table.RowDataDebeziumDeserializeSchema; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.StateRecoveryOptions; +import org.apache.flink.core.execution.CheckpointingMode; +import org.apache.flink.core.execution.JobClient; +import org.apache.flink.core.execution.SavepointFormatType; +import org.apache.flink.runtime.checkpoint.CheckpointException; +import org.apache.flink.runtime.dispatcher.UnavailableDispatcherOperationException; +import org.apache.flink.streaming.api.datastream.DataStreamSource; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.conversion.RowRowConverter; +import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.types.utils.TypeConversions; +import org.apache.flink.util.CloseableIterator; +import org.apache.flink.util.ExceptionUtils; + +import io.debezium.connector.mysql.MySqlConnection; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.sql.SQLException; +import java.time.ZoneId; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; + +/** + * IT test for FLINK-38334: MySQL CDC source gets stuck in INITIAL_ASSIGNING state when a table is + * excluded from configuration after splits have been assigned but before they are finished. + * + * <p>NOTE: The test uses a JUnit timeout as a CI safeguard. The test logic is deterministic: it + * either receives a binlog record (proving streaming mode) or blocks forever (bug exists). + * + * <p>When the bug exists, the test will timeout because: + * + * <ol> + * <li>The savepoint captures table "a" splits in assignedSplits (assigned but not finished) + * <li>After restart with table "a" excluded, the reader skips table "a" splits + * <li>The enumerator waits for table "a" splits to be reported as finished (they never will be) + * <li>allSnapshotSplitsFinished() never returns true → job stuck in INITIAL_ASSIGNING + * </ol> + * + * <p>When the fix is applied, the test passes because excluded table splits are cleaned up on + * restore, allowing the job to transition to streaming mode. + */ +public class TableExclusionDuringSnapshotIT extends MySqlSourceTestBase { + private static final UniqueDatabase DATABASE = + new UniqueDatabase(MYSQL_CONTAINER, "table_exclusion_snapshot", "mysqluser", "mysqlpw"); + private static final DataType DATA_TYPE = DataTypes.ROW(DataTypes.FIELD("id", DataTypes.INT())); + private static final RowRowConverter ROW_CONVERTER = RowRowConverter.create(DATA_TYPE); + private static final RowDataDebeziumDeserializeSchema DESERIALIZER = + RowDataDebeziumDeserializeSchema.newBuilder() + .setPhysicalRowType((RowType) DATA_TYPE.getLogicalType()) + .setResultTypeInfo( + InternalTypeInfo.of(TypeConversions.fromDataToLogicalType(DATA_TYPE))) + .build(); + + @BeforeEach + void setUp() { + DATABASE.createAndInitialize(); + } + + // Latches for coordinating between test thread and snapshot hook + // These are static because the hook is serialized and deserialized + private static volatile CountDownLatch hookTriggeredLatch; + private static volatile CountDownLatch savepointTakenLatch; + + /** + * Tests that excluding a table from configuration during INITIAL_ASSIGNING phase doesn't cause + * the source to get stuck. + * + * <p>Scenario: + * + * <ol> + * <li>Start job capturing tables "a" and "b" with a blocking hook on table "a" + * <li>Take savepoint while table "a" is being snapshotted (splits assigned but not finished) + * <li>Restart with configuration excluding table "a" + * <li>Insert a new record into table "b" + * <li>Verify we receive the new record (proves job transitioned to streaming) + * </ol> + * + * <p>If the bug exists, the job will be stuck in INITIAL_ASSIGNING because the enumerator waits + * for table "a" splits to be reported as finished, but the reader skips them. + */ + @Test + @Timeout(120) + void testTableExclusionDuringInitialAssigning(@TempDir Path tempDir) throws Exception { + final String savepointDirectory = tempDir.toUri().toString(); + + executeSql("INSERT INTO a VALUES (1)"); + executeSql("INSERT INTO b VALUES (1)"); + executeSql("INSERT INTO b VALUES (2)"); + + // Phase 1: Take savepoint while table "a" splits are assigned but not finished + String savepointPath = + runJobAndSavepointDuringInitialAssigning(savepointDirectory, "a", "b"); + + // Phase 2: Restart with only table "b", verify streaming mode works + String binlogRecord = runJobFromSavepointAndVerifyStreaming(savepointPath, "b"); + Assertions.assertThat(binlogRecord).isEqualTo("+I[200]"); + } + + /** Starts a job and takes a savepoint while splits are assigned but not finished. */ + private String runJobAndSavepointDuringInitialAssigning( + String savepointDirectory, String... tableNames) throws Exception { + hookTriggeredLatch = new CountDownLatch(1); + savepointTakenLatch = new CountDownLatch(1); + + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + env.setParallelism(1); + env.enableCheckpointing(200, CheckpointingMode.EXACTLY_ONCE); + + MySqlSource<RowData> source = createSourceWithBlockingHook(tableNames); + DataStreamSource<RowData> stream = + env.fromSource(source, WatermarkStrategy.noWatermarks(), "MySQL CDC Source"); + stream.print(); + + JobClient jobClient = env.executeAsync("Snapshot phase"); + + hookTriggeredLatch.await(); + String savepointPath = triggerSavepointWithRetry(jobClient, savepointDirectory); + savepointTakenLatch.countDown(); + + jobClient.cancel().get(); + return savepointPath; + } + + /** Restarts job from savepoint and verifies it transitions to streaming mode. */ + private String runJobFromSavepointAndVerifyStreaming(String savepointPath, String... tableNames) + throws Exception { + Configuration configuration = new Configuration(); + configuration.set(StateRecoveryOptions.SAVEPOINT_PATH, savepointPath); + Review Comment: The restart config uses `StateRecoveryOptions.SAVEPOINT_PATH`, but other connector ITs in this module restore via `SavepointConfigOptions.SAVEPOINT_PATH`. If this option key isn’t honored for savepoint restore in the test runtime, the job will start from scratch and the test may pass without actually covering the restore scenario (false positive). Prefer the same savepoint restore option as the existing tests. -- 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]
