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

JNSimba pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


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

commit bc56c5ec932351f57ac7e1ef315f9fb2975c8992
Author: wudi <[email protected]>
AuthorDate: Mon Nov 23 15:09:03 2026 +0800

    [improvement](cdc) Stream MySQL snapshot reads to reduce memory usage 
(#70255)
    
    ### Background
    
    Streaming jobs read MySQL snapshot splits through JDBC before continuing
    with binlog events. The shared MySQL reader builds the source
    configuration used by snapshot readers in both read and write paths.
    
    ### What problem does this PR solve?
    
    Issue Number: None
    Related PR: None
    PR Type: Enhancement
    
    Problem Summary:
    
    Improve memory efficiency when reading large MySQL snapshot splits or
    wide rows. With the default JDBC options, the existing positive fetch
    size allows the driver to buffer an entire split before rows reach the
    downstream queue. Enable row streaming so snapshot rows can reach the
    queue as they are read, allowing downstream backpressure to take effect
    during JDBC consumption. Most of the added code verifies the actual
    driver behavior and connection reuse.
    
    ### Details
    
    - **Before and after:** Previously, fetch size 1024 with the default
    JDBC options produced a fully buffered result set. Setting the shared
    MySQL source fetch size to `Integer.MIN_VALUE` enables row streaming
    with the existing forward-only, read-only snapshot statements.
    - **Configs and defaults:** Row streaming becomes the default for
    initial snapshots and snapshot-only reads. No new user setting is
    introduced. The explicit statement fetch size also selects row streaming
    when the JDBC URL supplies cursor-fetch, server-prepared-statement or
    default-fetch-size options.
    - **Impact on existing users:** Reduces driver-side buffering for large
    splits and wide rows in both read and write paths. Binlog consumption,
    offsets and checkpoint formats are unchanged. The inherited OceanBase
    reader shares this configuration path but was not tested against a live
    OceanBase instance.
    - **Behavior change:** Snapshot rows are consumed through the driver's
    streaming implementation. Memory retained in queues, polled batches,
    backfill and sink buffers still requires separate controls; this change
    does not establish a process-wide memory bound.
---
 .../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