hantmac commented on code in PR #9661:
URL: https://github.com/apache/seatunnel/pull/9661#discussion_r2290248410


##########
seatunnel-connectors-v2/connector-databend/src/main/java/org/apache/seatunnel/connectors/seatunnel/databend/sink/DatabendSinkAggregatedCommitter.java:
##########
@@ -0,0 +1,296 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.databend.sink;
+
+import org.apache.seatunnel.api.sink.SinkAggregatedCommitter;
+import org.apache.seatunnel.api.table.catalog.CatalogTable;
+import 
org.apache.seatunnel.connectors.seatunnel.databend.config.DatabendSinkConfig;
+import 
org.apache.seatunnel.connectors.seatunnel.databend.exception.DatabendConnectorErrorCode;
+import 
org.apache.seatunnel.connectors.seatunnel.databend.exception.DatabendConnectorException;
+import org.apache.seatunnel.connectors.seatunnel.databend.util.DatabendUtil;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Aggregated committer for Databend sink that handles CDC (Change Data 
Capture) operations. In CDC
+ * mode, this committer performs merge operations to apply changes to the 
target table. Merge
+ * operations are only performed when the accumulated record count reaches the 
configured batch
+ * size, which helps optimize performance by reducing the frequency of merge 
operations.
+ */
+@Slf4j
+public class DatabendSinkAggregatedCommitter
+        implements SinkAggregatedCommitter<
+                DatabendSinkCommitterInfo, DatabendSinkAggregatedCommitInfo> {
+
+    // Add a unique identifier for each instance
+    private static final AtomicLong INSTANCE_COUNTER = new AtomicLong(0);
+    private final long instanceId = INSTANCE_COUNTER.getAndIncrement();
+
+    private final DatabendSinkConfig databendSinkConfig;
+    private final String database;
+    private final String table;
+    private final String rawTableName;
+    private final String streamName;
+
+    private Connection connection;
+    private boolean isCdcMode;
+    // Store catalog table to access schema information
+    private CatalogTable catalogTable;
+
+    // Add a setter for catalogTable
+    public void setCatalogTable(CatalogTable catalogTable) {
+        this.catalogTable = catalogTable;
+    }
+
+    public DatabendSinkAggregatedCommitter(
+            DatabendSinkConfig databendSinkConfig,
+            String database,
+            String table,
+            String rawTableName,
+            String streamName) {
+        this.databendSinkConfig = databendSinkConfig;
+        this.database = database;
+        this.table = table;
+        this.rawTableName = rawTableName;
+        this.streamName = streamName;
+        this.isCdcMode = databendSinkConfig.isCdcMode();
+    }
+
+    @Override
+    public void init() {
+        try {
+            log.info("[Instance {}] Initializing 
DatabendSinkAggregatedCommitter", instanceId);
+            log.info("[Instance {}] DatabendSinkConfig: {}", instanceId, 
databendSinkConfig);
+            log.info("[Instance {}] Database: {}", instanceId, database);
+            log.info("[Instance {}] Table: {}", instanceId, table);
+            log.info("[Instance {}] Is CDC mode: {}", instanceId, isCdcMode);
+
+            this.connection = 
DatabendUtil.createConnection(databendSinkConfig);
+            log.info(
+                    "[Instance {}] Databend connection created successfully: 
{}",
+                    instanceId,
+                    connection);
+
+            // CDC infrastructure is now initialized in 
DatabendSink.setJobContext
+            // Just log that we're in CDC mode
+            if (isCdcMode) {
+                log.info("[Instance {}] Running in CDC mode", instanceId);
+            }
+        } catch (SQLException e) {
+            log.error(
+                    "[Instance {}] Failed to initialize 
DatabendSinkAggregatedCommitter: {}",
+                    instanceId,
+                    e.getMessage(),
+                    e);
+            throw new DatabendConnectorException(
+                    DatabendConnectorErrorCode.CONNECT_FAILED,
+                    "Failed to initialize DatabendSinkAggregatedCommitter: " + 
e.getMessage(),
+                    e);
+        } catch (Exception e) {
+            log.error(
+                    "[Instance {}] Unexpected error during initialization: {}",
+                    instanceId,
+                    e.getMessage(),
+                    e);
+            throw e;
+        }
+    }
+
+    private String getCurrentTimestamp() {
+        return 
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
+    }
+
+    @Override
+    public List<DatabendSinkAggregatedCommitInfo> commit(
+            List<DatabendSinkAggregatedCommitInfo> aggregatedCommitInfos) 
throws IOException {
+        // Perform final merge operation in CDC mode only when necessary
+        if (isCdcMode && shouldPerformMerge()) {

Review Comment:
   Perhaps by adding the shouldPerformMerge check, I can reduce some 
unnecessary merge into operations and alleviate the pressure on the target side?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to