|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.cdcclient.itcase; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +import org.apache.doris.cdcclient.common.Env; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.databind.JsonNode; |
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.testcontainers.containers.MySQLContainer; |
| 30 | +import org.testcontainers.junit.jupiter.Container; |
| 31 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 32 | +import org.testcontainers.utility.DockerImageName; |
| 33 | + |
| 34 | +import java.sql.Connection; |
| 35 | +import java.sql.DriverManager; |
| 36 | +import java.sql.ResultSet; |
| 37 | +import java.sql.Statement; |
| 38 | +import java.time.Duration; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.List; |
| 41 | +import java.util.concurrent.atomic.AtomicLong; |
| 42 | + |
| 43 | +/** |
| 44 | + * GTID counterpart of {@link MySqlStartupSpecificOffsetITCase}: given a recorded GTID set as the |
| 45 | + * specific-offset (no binlog file/position), the job must replay from exactly that point forward — |
| 46 | + * proving the GTID-only resume offset is honored end-to-end, not just preserved in config. |
| 47 | + */ |
| 48 | +@Testcontainers |
| 49 | +class MySqlStartupGtidOffsetITCase { |
| 50 | + |
| 51 | + private static final String ROOT_USER = "root"; |
| 52 | + private static final String ROOT_PASSWORD = "123456"; |
| 53 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 54 | + private static final AtomicLong JOB_ID_SEQ = new AtomicLong(980_000); |
| 55 | + |
| 56 | + @Container |
| 57 | + static final MySQLContainer<?> MYSQL = |
| 58 | + new MySQLContainer<>(DockerImageName.parse("mysql:8.0")) |
| 59 | + .withCommand("--gtid-mode=ON", "--enforce-gtid-consistency=ON") |
| 60 | + .withDatabaseName("cdc_test") |
| 61 | + .withUsername("cdc") |
| 62 | + .withPassword("123456") |
| 63 | + .withEnv("MYSQL_ROOT_PASSWORD", ROOT_PASSWORD); |
| 64 | + |
| 65 | + private String jobId; |
| 66 | + private String database; |
| 67 | + |
| 68 | + @BeforeEach |
| 69 | + void setUp() throws Exception { |
| 70 | + jobId = String.valueOf(JOB_ID_SEQ.incrementAndGet()); |
| 71 | + database = "gtidoff_db_" + jobId; |
| 72 | + try (Connection conn = rootConnection(""); |
| 73 | + Statement st = conn.createStatement()) { |
| 74 | + st.execute("CREATE DATABASE " + database); |
| 75 | + st.execute("USE " + database); |
| 76 | + st.execute("CREATE TABLE t_user (id INT PRIMARY KEY, name VARCHAR(50))"); |
| 77 | + st.execute("INSERT INTO t_user VALUES (1,'alice'), (2,'bob')"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @AfterEach |
| 82 | + void tearDown() throws Exception { |
| 83 | + Env.getCurrentEnv().close(jobId); |
| 84 | + try (Connection conn = rootConnection(""); |
| 85 | + Statement st = conn.createStatement()) { |
| 86 | + st.execute("DROP DATABASE IF EXISTS " + database); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void specificGtidOffsetReplaysFromRecordedPositionForward() throws Exception { |
| 92 | + // Record the GTID set right after the pre-existing rows; resume must start past it. |
| 93 | + String offset = String.format("{\"gtids\":\"%s\"}", currentGtidExecuted()); |
| 94 | + |
| 95 | + // This change happens after the recorded GTID but before the job starts — it must be read. |
| 96 | + insert("INSERT INTO t_user VALUES (3,'carol')"); |
| 97 | + |
| 98 | + try (MockDorisServer mock = new MockDorisServer(); |
| 99 | + CdcClientWriteHarness harness = |
| 100 | + CdcClientWriteHarness.mysql( |
| 101 | + jobId, |
| 102 | + MYSQL.getHost(), |
| 103 | + MYSQL.getMappedPort(MySQLContainer.MYSQL_PORT), |
| 104 | + ROOT_USER, |
| 105 | + ROOT_PASSWORD, |
| 106 | + database, |
| 107 | + "t_user", |
| 108 | + offset, |
| 109 | + "doris_target_db", |
| 110 | + mock)) { |
| 111 | + |
| 112 | + // Resume positions by GTID, so row 3 (already in the binlog) is read straight away. |
| 113 | + List<Integer> first = ids(harness.readBinlogFromStartupMode(1, Duration.ofSeconds(90))); |
| 114 | + assertThat(first).containsExactly(3); |
| 115 | + |
| 116 | + insert("INSERT INTO t_user VALUES (4,'dave')"); |
| 117 | + List<Integer> second = ids(harness.continueBinlog(1, Duration.ofSeconds(90))); |
| 118 | + assertThat(second).containsExactly(4); |
| 119 | + |
| 120 | + // Replays from the recorded GTID forward: rows 3 and 4 only, never 1 or 2. |
| 121 | + List<Integer> all = ids(harness.loadedRecords()); |
| 122 | + assertThat(all).doesNotContain(1, 2); |
| 123 | + assertThat(all).containsExactlyInAnyOrder(3, 4); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private String currentGtidExecuted() throws Exception { |
| 128 | + try (Connection conn = rootConnection(database); |
| 129 | + Statement st = conn.createStatement(); |
| 130 | + ResultSet rs = st.executeQuery("SELECT @@gtid_executed")) { |
| 131 | + if (!rs.next()) { |
| 132 | + throw new IllegalStateException("@@gtid_executed returned no row"); |
| 133 | + } |
| 134 | + // Single server uuid fits on one line; strip newlines defensively. |
| 135 | + return rs.getString(1).replace("\n", "").trim(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private List<Integer> ids(List<String> records) throws Exception { |
| 140 | + List<Integer> result = new ArrayList<>(); |
| 141 | + for (String record : records) { |
| 142 | + JsonNode node = MAPPER.readTree(record); |
| 143 | + result.add(node.get("id").asInt()); |
| 144 | + } |
| 145 | + return result; |
| 146 | + } |
| 147 | + |
| 148 | + private void insert(String sql) throws Exception { |
| 149 | + try (Connection conn = rootConnection(database); |
| 150 | + Statement st = conn.createStatement()) { |
| 151 | + st.execute(sql); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private Connection rootConnection(String db) throws Exception { |
| 156 | + String url = |
| 157 | + "jdbc:mysql://" |
| 158 | + + MYSQL.getHost() |
| 159 | + + ":" |
| 160 | + + MYSQL.getMappedPort(MySQLContainer.MYSQL_PORT) |
| 161 | + + "/" |
| 162 | + + db; |
| 163 | + return DriverManager.getConnection(url, ROOT_USER, ROOT_PASSWORD); |
| 164 | + } |
| 165 | +} |
0 commit comments