1818package org .apache .flink .cdc .pipeline .tests ;
1919
2020import org .apache .flink .cdc .common .test .utils .TestUtils ;
21+ import org .apache .flink .cdc .common .utils .Preconditions ;
2122import org .apache .flink .cdc .connectors .mysql .testutils .MySqlContainer ;
2223import org .apache .flink .cdc .connectors .mysql .testutils .MySqlVersion ;
2324import org .apache .flink .cdc .connectors .mysql .testutils .UniqueDatabase ;
3637import java .nio .file .Path ;
3738import java .sql .Connection ;
3839import java .sql .DriverManager ;
40+ import java .sql .ResultSet ;
3941import java .sql .SQLException ;
4042import java .sql .Statement ;
4143import java .time .Duration ;
@@ -333,16 +335,34 @@ public void testSchemaChangeEvents() throws Exception {
333335 }
334336
335337 @ Test
336- public void testDroppingTable () throws Exception {
337- Thread .sleep (5000 );
338- LOG .info ("Sleep 5 seconds to distinguish initial DDL events with dropping table events..." );
339- long ddlTimestamp = System .currentTimeMillis ();
340- Thread .sleep (5000 );
341- LOG .info ("Going to drop tables after timestamp {}" , ddlTimestamp );
338+ public void testDanglingDropTableEventInBinlog () throws Exception {
339+ // Create a new table for later deletion
340+ try (Connection connection = mysqlInventoryDatabase .getJdbcConnection ();
341+ Statement statement = connection .createStatement ()) {
342+ statement .execute ("CREATE TABLE live_fast(ID INT PRIMARY KEY);" );
343+ }
344+
345+ String logFileName = null ;
346+ Long logPosition = null ;
347+
348+ try (Connection connection = mysqlInventoryDatabase .getJdbcConnection ();
349+ Statement statement = connection .createStatement ()) {
350+ ResultSet rs = statement .executeQuery ("SHOW BINARY LOGS;" );
351+ while (rs .next ()) {
352+ logFileName = rs .getString ("Log_name" );
353+ logPosition = rs .getLong ("File_size" );
354+ }
355+ }
356+
357+ // We start reading binlog from the tail of current position and file to avoid reading
358+ // previous events. The next DDL event (DROP TABLE) will push binlog position forward.
359+ Preconditions .checkNotNull (logFileName , "Log file name must not be null" );
360+ Preconditions .checkNotNull (logPosition , "Log position name must not be null" );
361+ LOG .info ("Trying to restore from {} @ {}..." , logFileName , logPosition );
342362
343363 try (Connection connection = mysqlInventoryDatabase .getJdbcConnection ();
344364 Statement statement = connection .createStatement ()) {
345- statement .execute ("DROP TABLE products ;" );
365+ statement .execute ("DROP TABLE live_fast ;" );
346366 }
347367
348368 String pipelineJob =
@@ -356,8 +376,9 @@ public void testDroppingTable() throws Exception {
356376 + " tables: %s.\\ .*\n "
357377 + " server-id: 5400-5404\n "
358378 + " server-time-zone: UTC\n "
359- + " scan.startup.mode: timestamp\n "
360- + " scan.startup.timestamp-millis: %d\n "
379+ + " scan.startup.mode: specific-offset\n "
380+ + " scan.startup.specific-offset.file: %s\n "
381+ + " scan.startup.specific-offset.pos: %d\n "
361382 + " scan.binlog.newly-added-table.enabled: true\n "
362383 + "\n "
363384 + "sink:\n "
@@ -370,7 +391,8 @@ public void testDroppingTable() throws Exception {
370391 MYSQL_TEST_USER ,
371392 MYSQL_TEST_PASSWORD ,
372393 mysqlInventoryDatabase .getDatabaseName (),
373- ddlTimestamp ,
394+ logFileName ,
395+ logPosition ,
374396 parallelism );
375397 Path mysqlCdcJar = TestUtils .getResource ("mysql-cdc-pipeline-connector.jar" );
376398 Path valuesCdcJar = TestUtils .getResource ("values-cdc-pipeline-connector.jar" );
@@ -380,13 +402,13 @@ public void testDroppingTable() throws Exception {
380402 LOG .info ("Pipeline job is running" );
381403 waitUntilSpecificEvent (
382404 String .format (
383- "Table %s.products received SchemaChangeEvent DropTableEvent{tableId=%s.products } and start to be blocked." ,
405+ "Table %s.live_fast received SchemaChangeEvent DropTableEvent{tableId=%s.live_fast } and start to be blocked." ,
384406 mysqlInventoryDatabase .getDatabaseName (),
385407 mysqlInventoryDatabase .getDatabaseName ()));
386408
387409 waitUntilSpecificEvent (
388410 String .format (
389- "Schema change event DropTableEvent{tableId=%s.products } has been handled in another subTask already." ,
411+ "Schema change event DropTableEvent{tableId=%s.live_fast } has been handled in another subTask already." ,
390412 mysqlInventoryDatabase .getDatabaseName ()));
391413 }
392414
0 commit comments