|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package io.debezium.connector.mysql; |
| 19 | + |
| 20 | +import io.debezium.config.Configuration; |
| 21 | +import io.debezium.connector.mysql.MySqlConnectorConfig.GtidNewChannelPosition; |
| 22 | +import io.debezium.jdbc.JdbcConfiguration; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.lang.reflect.Field; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * Integration test for {@link MySqlStreamingChangeEventSource#filterGtidSet} to ensure the LATEST |
| 33 | + * mode fix (FLINK-39149) cannot regress. This test directly invokes the production method via |
| 34 | + * reflection, bypassing the heavy constructor dependencies. |
| 35 | + */ |
| 36 | +class FilterGtidSetTest { |
| 37 | + |
| 38 | + /** |
| 39 | + * Verifies that filterGtidSet() in LATEST mode fixes non-contiguous checkpoint GTIDs for old |
| 40 | + * channels and uses server's full GTID for new channels. |
| 41 | + * |
| 42 | + * <p>This is the core FLINK-39149 bug scenario: checkpoint has "aaa-111:5000-8000" (gap |
| 43 | + * 1-4999), and without the fix, MySQL would replay transactions 1-4999. |
| 44 | + */ |
| 45 | + @Test |
| 46 | + void testFilterGtidSetLatestModeFixesNonContiguousGtid() throws Exception { |
| 47 | + MySqlStreamingChangeEventSource source = createSourceWithConfig("latest", null); |
| 48 | + |
| 49 | + MySqlOffsetContext offsetContext = createOffsetContext(source, "aaa-111:5000-8000"); |
| 50 | + GtidSet availableServerGtidSet = new GtidSet("aaa-111:1-10000,bbb-222:1-3000"); |
| 51 | + GtidSet purgedServerGtid = new GtidSet(""); |
| 52 | + |
| 53 | + GtidSet result = |
| 54 | + source.filterGtidSet(offsetContext, availableServerGtidSet, purgedServerGtid); |
| 55 | + |
| 56 | + // Old channel aaa-111: gap should be filled from :1 to 8000 |
| 57 | + assertThat(result.toString()).contains("aaa-111:1-8000"); |
| 58 | + // New channel bbb-222: should use server's full GTID |
| 59 | + assertThat(result.toString()).contains("bbb-222:1-3000"); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Verifies that filterGtidSet() in LATEST mode with gtidSourceFilter excludes filtered UUIDs. |
| 64 | + */ |
| 65 | + @Test |
| 66 | + void testFilterGtidSetLatestModeWithSourceFilter() throws Exception { |
| 67 | + MySqlStreamingChangeEventSource source = createSourceWithConfig("latest", "ccc-333"); |
| 68 | + |
| 69 | + MySqlOffsetContext offsetContext = |
| 70 | + createOffsetContext(source, "aaa-111:5000-8000,bbb-222:1-2000"); |
| 71 | + GtidSet availableServerGtidSet = |
| 72 | + new GtidSet("aaa-111:1-10000,bbb-222:1-3000,ccc-333:1-5000"); |
| 73 | + GtidSet purgedServerGtid = new GtidSet(""); |
| 74 | + |
| 75 | + GtidSet result = |
| 76 | + source.filterGtidSet(offsetContext, availableServerGtidSet, purgedServerGtid); |
| 77 | + |
| 78 | + assertThat(result.toString()).contains("aaa-111:1-8000"); |
| 79 | + assertThat(result.toString()).contains("bbb-222:1-2000"); |
| 80 | + // ccc-333 should be excluded |
| 81 | + assertThat(result.toString()).doesNotContain("ccc-333"); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Verifies that filterGtidSet() in EARLIEST mode still works correctly (no regression from |
| 86 | + * LATEST mode fix). |
| 87 | + */ |
| 88 | + @Test |
| 89 | + void testFilterGtidSetEarliestModeNotAffected() throws Exception { |
| 90 | + MySqlStreamingChangeEventSource source = createSourceWithConfig("earliest", null); |
| 91 | + |
| 92 | + MySqlOffsetContext offsetContext = createOffsetContext(source, "aaa-111:5000-8000"); |
| 93 | + GtidSet availableServerGtidSet = new GtidSet("aaa-111:1-10000,bbb-222:1-3000"); |
| 94 | + GtidSet purgedServerGtid = new GtidSet(""); |
| 95 | + |
| 96 | + GtidSet result = |
| 97 | + source.filterGtidSet(offsetContext, availableServerGtidSet, purgedServerGtid); |
| 98 | + |
| 99 | + // EARLIEST mode: old channel should be fixed |
| 100 | + assertThat(result.toString()).contains("aaa-111:1-8000"); |
| 101 | + // EARLIEST mode does NOT add new channels (different from LATEST) |
| 102 | + assertThat(result.forServerWithId("bbb-222")).isNull(); |
| 103 | + } |
| 104 | + |
| 105 | + /** Verifies that filterGtidSet() returns null when offsetContext has no GTID. */ |
| 106 | + @Test |
| 107 | + void testFilterGtidSetReturnsNullWhenNoGtid() throws Exception { |
| 108 | + MySqlStreamingChangeEventSource source = createSourceWithConfig("latest", null); |
| 109 | + |
| 110 | + MySqlOffsetContext offsetContext = createOffsetContext(source, null); |
| 111 | + GtidSet availableServerGtidSet = new GtidSet("aaa-111:1-10000"); |
| 112 | + GtidSet purgedServerGtid = new GtidSet(""); |
| 113 | + |
| 114 | + GtidSet result = |
| 115 | + source.filterGtidSet(offsetContext, availableServerGtidSet, purgedServerGtid); |
| 116 | + |
| 117 | + assertThat(result).isNull(); |
| 118 | + } |
| 119 | + |
| 120 | + // ---- Helper methods ---- |
| 121 | + |
| 122 | + /** |
| 123 | + * Creates a MySqlStreamingChangeEventSource via Unsafe (bypassing constructor) and injects the |
| 124 | + * connectorConfig field via reflection. |
| 125 | + */ |
| 126 | + @SuppressWarnings("restriction") |
| 127 | + private static MySqlStreamingChangeEventSource createSourceWithConfig( |
| 128 | + String gtidNewChannelPosition, String gtidSourceExcludes) throws Exception { |
| 129 | + // Build Debezium Configuration |
| 130 | + JdbcConfiguration.Builder builder = |
| 131 | + JdbcConfiguration.create().with("database.server.name", "test_server"); |
| 132 | + if (gtidNewChannelPosition != null) { |
| 133 | + builder = builder.with("gtid.new.channel.position", gtidNewChannelPosition); |
| 134 | + } |
| 135 | + if (gtidSourceExcludes != null) { |
| 136 | + builder = builder.with("gtid.source.excludes", gtidSourceExcludes); |
| 137 | + } |
| 138 | + Configuration dezConf = builder.build(); |
| 139 | + MySqlConnectorConfig connectorConfig = new MySqlConnectorConfig(dezConf); |
| 140 | + |
| 141 | + // Verify config is as expected |
| 142 | + if ("latest".equalsIgnoreCase(gtidNewChannelPosition)) { |
| 143 | + assertThat(connectorConfig.gtidNewChannelPosition()) |
| 144 | + .isEqualTo(GtidNewChannelPosition.LATEST); |
| 145 | + } else if ("earliest".equalsIgnoreCase(gtidNewChannelPosition)) { |
| 146 | + assertThat(connectorConfig.gtidNewChannelPosition()) |
| 147 | + .isEqualTo(GtidNewChannelPosition.EARLIEST); |
| 148 | + } |
| 149 | + |
| 150 | + // Create instance via Unsafe to bypass heavy constructor |
| 151 | + sun.misc.Unsafe unsafe = getUnsafe(); |
| 152 | + MySqlStreamingChangeEventSource source = |
| 153 | + (MySqlStreamingChangeEventSource) |
| 154 | + unsafe.allocateInstance(MySqlStreamingChangeEventSource.class); |
| 155 | + |
| 156 | + // Inject connectorConfig via reflection |
| 157 | + Field configField = |
| 158 | + MySqlStreamingChangeEventSource.class.getDeclaredField("connectorConfig"); |
| 159 | + configField.setAccessible(true); |
| 160 | + configField.set(source, connectorConfig); |
| 161 | + |
| 162 | + return source; |
| 163 | + } |
| 164 | + |
| 165 | + private static MySqlOffsetContext createOffsetContext( |
| 166 | + MySqlStreamingChangeEventSource source, String gtidSetStr) throws Exception { |
| 167 | + Field configField = |
| 168 | + MySqlStreamingChangeEventSource.class.getDeclaredField("connectorConfig"); |
| 169 | + configField.setAccessible(true); |
| 170 | + MySqlConnectorConfig config = (MySqlConnectorConfig) configField.get(source); |
| 171 | + |
| 172 | + if (gtidSetStr == null) { |
| 173 | + // Return an offset context without GTID (gtidSet() returns null) |
| 174 | + Map<String, Object> offsetMap = new HashMap<>(); |
| 175 | + offsetMap.put("file", "mysql-bin.000001"); |
| 176 | + offsetMap.put("pos", 4L); |
| 177 | + return new MySqlOffsetContext.Loader(config).load(offsetMap); |
| 178 | + } |
| 179 | + |
| 180 | + Map<String, Object> offsetMap = new HashMap<>(); |
| 181 | + offsetMap.put("file", "mysql-bin.000001"); |
| 182 | + offsetMap.put("pos", 4L); |
| 183 | + offsetMap.put("gtids", gtidSetStr); |
| 184 | + return new MySqlOffsetContext.Loader(config).load(offsetMap); |
| 185 | + } |
| 186 | + |
| 187 | + @SuppressWarnings("restriction") |
| 188 | + private static sun.misc.Unsafe getUnsafe() throws Exception { |
| 189 | + Field unsafeField = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
| 190 | + unsafeField.setAccessible(true); |
| 191 | + return (sun.misc.Unsafe) unsafeField.get(null); |
| 192 | + } |
| 193 | +} |
0 commit comments