This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new 41cb133f302 branch-4.2: [improvement](cdc) Stream MySQL snapshot reads 
to reduce memory usage #70255 (#70870)
41cb133f302 is described below

commit 41cb133f302db34f9d6190abd8ba67ca647b5908
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Dec 2 11:05:49 2026 +0800

    branch-4.2: [improvement](cdc) Stream MySQL snapshot reads to reduce memory 
usage #70255 (#70870)
    
    Cherry-picked from #70255
    
    Co-authored-by: wudi <[email protected]>
---
 .../source/reader/mysql/MySqlSourceReader.java     |   2 +
 .../itcase/MySqlSnapshotStreamingITCase.java       | 151 +++++++++++++++++++++
 .../source/reader/mysql/MySqlSourceReaderTest.java |   6 +
 3 files changed, 159 insertions(+)

diff --git 
a/fs_brokers/cdc_client/src/main/java/org/apache/doris/cdcclient/source/reader/mysql/MySqlSourceReader.java
 
b/fs_brokers/cdc_client/src/main/java/org/apache/doris/cdcclient/source/reader/mysql/MySqlSourceReader.java
index ed8acebd373..f373d5713d2 100644
--- 
a/fs_brokers/cdc_client/src/main/java/org/apache/doris/cdcclient/source/reader/mysql/MySqlSourceReader.java
+++ 
b/fs_brokers/cdc_client/src/main/java/org/apache/doris/cdcclient/source/reader/mysql/MySqlSourceReader.java
@@ -913,6 +913,8 @@ public class MySqlSourceReader extends 
AbstractCdcSourceReader {
     private MySqlSourceConfig generateMySqlConfig(
             Map<String, String> cdcConfig, String jobId, int subtaskId) {
         MySqlSourceConfigFactory configFactory = new 
MySqlSourceConfigFactory();
+        // Stream snapshot rows instead of buffering the entire split in 
Connector/J.
+        configFactory.fetchSize(Integer.MIN_VALUE);
         ConnectionUrl cu =
                 ConnectionUrl.getConnectionUrlInstance(
                         cdcConfig.get(DataSourceConfigKeys.JDBC_URL), null);
diff --git 
a/fs_brokers/cdc_client/src/test/java/org/apache/doris/cdcclient/itcase/MySqlSnapshotStreamingITCase.java
 
b/fs_brokers/cdc_client/src/test/java/org/apache/doris/cdcclient/itcase/MySqlSnapshotStreamingITCase.java
new file mode 100644
index 00000000000..da7e3d64f5c
--- /dev/null
+++ 
b/fs_brokers/cdc_client/src/test/java/org/apache/doris/cdcclient/itcase/MySqlSnapshotStreamingITCase.java
@@ -0,0 +1,151 @@
+// 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.doris.cdcclient.itcase;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.doris.cdcclient.source.reader.mysql.MySqlSourceReader;
+import org.apache.doris.job.cdc.DataSourceConfigKeys;
+
+import com.mysql.cj.jdbc.result.ResultSetImpl;
+import com.mysql.cj.protocol.a.result.ResultsetRowsCursor;
+import com.mysql.cj.protocol.a.result.ResultsetRowsStatic;
+import com.mysql.cj.protocol.a.result.ResultsetRowsStreaming;
+
+import org.apache.flink.cdc.connectors.mysql.debezium.DebeziumUtils;
+import org.apache.flink.cdc.connectors.mysql.source.config.MySqlSourceConfig;
+import org.apache.flink.cdc.connectors.mysql.source.utils.StatementUtils;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.testcontainers.containers.MySQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import java.lang.reflect.Method;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.Map;
+
+import io.debezium.connector.mysql.MySqlConnection;
+
+@Testcontainers
+class MySqlSnapshotStreamingITCase {
+    @Container
+    static final MySQLContainer<?> MYSQL =
+            new MySQLContainer<>("mysql:8.0")
+                    .withDatabaseName("cdc_test")
+                    .withUsername("cdc")
+                    .withPassword("123456");
+
+    @ParameterizedTest
+    @ValueSource(strings = {"", "useCursorFetch=true", 
"useServerPrepStmts=true", "defaultFetchSize=4096"})
+    void snapshotStreamsRowsAndReleasesConnection(String jdbcOptions) throws 
Exception {
+        String jdbcUrl = MYSQL.getJdbcUrl() + "?" + jdbcOptions;
+        try (Connection connection =
+                        DriverManager.getConnection(
+                                jdbcUrl, MYSQL.getUsername(), 
MYSQL.getPassword());
+                Statement statement = connection.createStatement()) {
+            statement.execute("DROP TABLE IF EXISTS t_snapshot_streaming");
+            statement.execute(
+                    "CREATE TABLE t_snapshot_streaming (id INT PRIMARY KEY, 
payload LONGBLOB)");
+            for (int id = 0; id < 16; id++) {
+                statement.execute(
+                        "INSERT INTO t_snapshot_streaming VALUES ("
+                                + id
+                                + ", REPEAT('x', 262144))");
+            }
+        }
+
+        Method method =
+                MySqlSourceReader.class.getDeclaredMethod(
+                        "generateMySqlConfig", Map.class, String.class, 
int.class);
+        method.setAccessible(true);
+        MySqlSourceConfig config =
+                (MySqlSourceConfig)
+                        method.invoke(
+                                new MySqlSourceReader(),
+                                Map.of(
+                                        DataSourceConfigKeys.JDBC_URL,
+                                        jdbcUrl,
+                                        DataSourceConfigKeys.USER,
+                                        MYSQL.getUsername(),
+                                        DataSourceConfigKeys.PASSWORD,
+                                        MYSQL.getPassword(),
+                                        DataSourceConfigKeys.DATABASE,
+                                        MYSQL.getDatabaseName(),
+                                        DataSourceConfigKeys.TABLE,
+                                        "t_snapshot_streaming",
+                                        DataSourceConfigKeys.OFFSET,
+                                        "initial"),
+                                "snapshot-streaming",
+                                0);
+        assertThat(config.getFetchSize()).isEqualTo(Integer.MIN_VALUE);
+
+        try (MySqlConnection jdbc = 
DebeziumUtils.createMySqlConnection(config)) {
+            // Use the same statement builder as snapshot splits, including an 
unbounded split.
+            for (int fetchSize : new int[] {1024, config.getFetchSize()}) {
+                for (boolean closeEarly : new boolean[] {true, false}) {
+                    try (PreparedStatement statement =
+                                    StatementUtils.readTableSplitDataStatement(
+                                            jdbc,
+                                            "SELECT * FROM 
cdc_test.t_snapshot_streaming ORDER BY"
+                                                + " id",
+                                            true,
+                                            true,
+                                            null,
+                                            null,
+                                            1,
+                                            fetchSize);
+                            ResultSet rows = statement.executeQuery()) {
+                        
assertThat(statement.getFetchSize()).isEqualTo(fetchSize);
+                        
assertThat(rows.getType()).isEqualTo(ResultSet.TYPE_FORWARD_ONLY);
+                        
assertThat(rows.getConcurrency()).isEqualTo(ResultSet.CONCUR_READ_ONLY);
+                        var driverRows = 
rows.unwrap(ResultSetImpl.class).getRows();
+                        if (fetchSize == Integer.MIN_VALUE) {
+                            
assertThat(driverRows).isInstanceOf(ResultsetRowsStreaming.class);
+                        } else if (jdbcOptions.equals("useCursorFetch=true")) {
+                            
assertThat(driverRows).isInstanceOf(ResultsetRowsCursor.class);
+                        } else {
+                            
assertThat(driverRows).isInstanceOf(ResultsetRowsStatic.class);
+                            // All rows are already buffered before the first 
ResultSet.next().
+                            assertThat(driverRows.size()).isEqualTo(16);
+                        }
+                        int count = 0;
+                        while (rows.next()) {
+                            assertThat(rows.getInt(1)).isEqualTo(count);
+                            assertThat(rows.getBytes(2)).hasSize(262144);
+                            count++;
+                            if (closeEarly) {
+                                break;
+                            }
+                        }
+                        assertThat(count).isEqualTo(closeEarly ? 1 : 16);
+                    }
+                    try (Statement statement = 
jdbc.connection().createStatement();
+                            ResultSet result = statement.executeQuery("SELECT 
1")) {
+                        assertThat(result.next()).isTrue();
+                        assertThat(result.getInt(1)).isEqualTo(1);
+                    }
+                }
+            }
+        }
+    }
+}
diff --git 
a/fs_brokers/cdc_client/src/test/java/org/apache/doris/cdcclient/source/reader/mysql/MySqlSourceReaderTest.java
 
b/fs_brokers/cdc_client/src/test/java/org/apache/doris/cdcclient/source/reader/mysql/MySqlSourceReaderTest.java
index 013ab9d2836..89de5fd4aac 100644
--- 
a/fs_brokers/cdc_client/src/test/java/org/apache/doris/cdcclient/source/reader/mysql/MySqlSourceReaderTest.java
+++ 
b/fs_brokers/cdc_client/src/test/java/org/apache/doris/cdcclient/source/reader/mysql/MySqlSourceReaderTest.java
@@ -160,6 +160,12 @@ public class MySqlSourceReaderTest {
         assertTrue(config.isIncludeSchemaChanges());
     }
 
+    @Test
+    void snapshotConfigEnablesStreamingResults() throws Exception {
+        assertEquals(Integer.MIN_VALUE, 
sourceConfig("initial").getFetchSize());
+        assertEquals(Integer.MIN_VALUE, 
sourceConfig("snapshot").getFetchSize());
+    }
+
     @Test
     void mysqlConfigCanDisableSchemaChanges() throws Exception {
         Map<String, String> overrides = new HashMap<>();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to