You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "hawk9821 (via GitHub)" <gi...@apache.org> on 2025/07/20 05:41:37 UTC

[PR] [Fix][Connectors-v2] fix dynamic bucket for paimon sink [seatunnel]

hawk9821 opened a new pull request, #9595:
URL: https://github.com/apache/seatunnel/pull/9595

   <!--
   
   Thank you for contributing to SeaTunnel! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   ## Contribution Checklist
     - Make sure that the pull request corresponds to a [GITHUB issue](https://github.com/apache/seatunnel/issues).
     - Name the pull request in the form "[Feature] [component] Title of the pull request", where *Feature* can be replaced by `Hotfix`, `Bug`, etc.
     - Minor fixes should be named following this pattern: `[hotfix] [docs] Fix typo in README.md doc`.
   -->
   
   ### Purpose of this pull request
   Refactor Paimon's dynamic bucketing mechanism to address potential data duplication issues that may arise after modifying the parallelism level. 
   
   <!-- Describe the purpose of this pull request. For example: This pull request adds checkstyle plugin.-->
   
   
   ### Does this PR introduce _any_ user-facing change?
   
   
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released SeaTunnel versions or within the unreleased branches such as dev.
   If no, write 'No'.
   If you are adding/modifying connector documents, please follow our new specifications: https://github.com/apache/seatunnel/issues/4544.
   -->
   
   
   ### How was this patch tested?
   
   PaimonSinkDynamicBucketIT#testWriteForDifferentParallelism
   
   
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If you are adding E2E test cases, maybe refer to https://github.com/apache/seatunnel/blob/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-mysql-e2e/src/test/resources/mysqlcdc_to_mysql.conf, here is a good example.
   -->
   
   
   ### Check list
   
   * [ ] If any new Jar binary package adding in your PR, please add License Notice according
     [New License Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/contribution/new-license.md)
   * [ ] If necessary, please update the documentation to describe the new feature. https://github.com/apache/seatunnel/tree/dev/docs
   * [ ] If you are contributing the connector code, please check that the following files are updated:
     1. Update [plugin-mapping.properties](https://github.com/apache/seatunnel/blob/dev/plugin-mapping.properties) and add new connector information in it
     2. Update the pom file of [seatunnel-dist](https://github.com/apache/seatunnel/blob/dev/seatunnel-dist/pom.xml)
     3. Add ci label in [label-scope-conf](https://github.com/apache/seatunnel/blob/dev/.github/workflows/labeler/label-scope-conf.yml)
     4. Add e2e testcase in [seatunnel-e2e](https://github.com/apache/seatunnel/tree/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/)
     5. Update connector [plugin_config](https://github.com/apache/seatunnel/blob/dev/config/plugin_config)


-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connectors-v2] fix dynamic bucket for paimon sink [seatunnel]

Posted by "hawk9821 (via GitHub)" <gi...@apache.org>.
hawk9821 commented on code in PR #9595:
URL: https://github.com/apache/seatunnel/pull/9595#discussion_r2218430176


##########
seatunnel-connectors-v2/connector-paimon/src/main/java/org/apache/seatunnel/connectors/seatunnel/paimon/sink/PaimonSink.java:
##########
@@ -43,11 +43,14 @@
 
 import org.apache.paimon.table.Table;
 
+import lombok.extern.slf4j.Slf4j;
+
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Optional;
 
+@Slf4j

Review Comment:
   removed



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connectors-v2] fix dynamic bucket for paimon sink [seatunnel]

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #9595:
URL: https://github.com/apache/seatunnel/pull/9595#discussion_r2234481395


##########
docs/en/connector-v2/sink/Paimon.md:
##########
@@ -448,6 +448,10 @@ sink {
 
 Single dynamic bucket table with write props of paimon,operates on the primary key table and bucket is -1.
 
+> Notes:
+> - Currently only the ordinary dynamic bucket mode is supported (the primary key must include all partition fields).
+> - When running in a cluster environment, `parallelism` must be set to `1`; otherwise, data duplication may occur.

Review Comment:
   only when source is cdc?



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connectors-v2] fix dynamic bucket for paimon sink [seatunnel]

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #9595:
URL: https://github.com/apache/seatunnel/pull/9595#discussion_r2218086536


##########
seatunnel-connectors-v2/connector-paimon/src/main/java/org/apache/seatunnel/connectors/seatunnel/paimon/sink/PaimonSink.java:
##########
@@ -43,11 +43,14 @@
 
 import org.apache.paimon.table.Table;
 
+import lombok.extern.slf4j.Slf4j;
+
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Optional;
 
+@Slf4j

Review Comment:
   useless?



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connectors-v2] fix dynamic bucket for paimon sink [seatunnel]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #9595:
URL: https://github.com/apache/seatunnel/pull/9595#discussion_r2218124481


##########
seatunnel-connectors-v2/connector-paimon/src/main/java/org/apache/seatunnel/connectors/seatunnel/paimon/sink/PaimonSinkWriter.java:
##########
@@ -122,31 +130,39 @@ public PaimonSinkWriter(
         this.paimonTablePath = catalogTable.getTablePath();
         this.paimonCatalog = PaimonCatalog.loadPaimonCatalog(readonlyConfig);
         this.paimonCatalog.open();
-        this.paimonFileStoretable = (FileStoreTable) paimonFileStoretable;
+        this.paimonFileStoretable = (FileStoreTable) paimonTable;
         CoreOptions.ChangelogProducer changelogProducer =
                 this.paimonFileStoretable.coreOptions().changelogProducer();
         if (Objects.nonNull(paimonSinkConfig.getChangelogProducer())
                 && changelogProducer != paimonSinkConfig.getChangelogProducer()) {
             log.warn(
                     "configured the props named 'changelog-producer' which is not compatible with the options in table , so it will use the table's 'changelog-producer'");
         }
+        this.rowAssignerChannelComputer =
+                new RowAssignerChannelComputer(
+                        paimonFileStoretable.schema(), context.getNumberOfParallelSubtasks());
+        rowAssignerChannelComputer.setup(context.getNumberOfParallelSubtasks());
+        this.parallelism = context.getNumberOfParallelSubtasks();
         this.paimonSinkConfig = paimonSinkConfig;
         this.sinkPaimonTableSchema = this.paimonFileStoretable.schema();
         this.newTableWrite();
         BucketMode bucketMode = this.paimonFileStoretable.bucketMode();
-        this.dynamicBucket =
-                BucketMode.DYNAMIC == bucketMode || BucketMode.GLOBAL_DYNAMIC == bucketMode;
-        int bucket = ((FileStoreTable) paimonFileStoretable).coreOptions().bucket();
+        // https://paimon.apache.org/docs/master/primary-key-table/data-distribution/#dynamic-bucket
+        // When you need cross partition upsert (primary keys not contain all partition fields),
+        // Dynamic Bucket mode directly maintains the mapping of keys to partition and bucket, uses
+        // local disks, and initializes indexes by reading all existing keys in the table when
+        // starting job. For tables with a large amount of data, there will be a significant loss in
+        // performance. Moreover, initialization takes a long time. This mode is not supported at
+        // this time.
+        if (BucketMode.GLOBAL_DYNAMIC == bucketMode) {
+            throw new UnsupportedOperationException(
+                    "Cross Partitions Upsert Dynamic Bucket Mode is not sopported.");

Review Comment:
   There is a spelling error in the error message. 'sopported' should be 'supported'.
   ```suggestion
                       "Cross Partitions Upsert Dynamic Bucket Mode is not supported.");
   ```



##########
seatunnel-e2e/seatunnel-connector-v2-e2e/connector-paimon-e2e/src/test/java/org/apache/seatunnel/e2e/connector/paimon/PaimonSinkDynamicBucketIT.java:
##########
@@ -144,6 +213,95 @@ private void deleteTable() {
         }
     }
 
+    @TestTemplate
+    public void testWriteForDifferentParallelism(TestContainer container)
+            throws IOException, InterruptedException, SQLException {
+        // parallelism = 3
+        Container.ExecResult textWriteResult1 =
+                container.executeJob("/mysql_jdbc_to_dynamic_bucket_paimon_case1.conf");
+        Assertions.assertEquals(0, textWriteResult1.getExitCode());
+        try (Connection jdbcConnection = bucketDatabase.getJdbcConnection();
+                Statement statement = jdbcConnection.createStatement()) {
+            statement.executeUpdate(
+                    "update bucket.test_dynamic_bucket set version = '2' where id <= 102");
+            statement.executeUpdate(
+                    "update bucket.test_dynamic_bucket set version = '3' where id = 105");
+            statement.executeUpdate(
+                    "update bucket.test_dynamic_bucket set version = '4' where id = 109");
+        }
+        // parallelism = 1
+        Container.ExecResult textWriteResult2 =
+                container.executeJob("/mysql_jdbc_to_dynamic_bucket_paimon_case2.conf");
+        Assertions.assertEquals(0, textWriteResult2.getExitCode());
+        List<String> parallelism_1 = vertifyData(container);
+
+        // parallelism = 2
+        Container.ExecResult textWriteResult3 =
+                container.executeJob("/mysql_jdbc_to_dynamic_bucket_paimon_case3.conf");
+        Assertions.assertEquals(0, textWriteResult3.getExitCode());
+
+        List<String> parallelism_2 = vertifyData(container);
+        Assertions.assertEquals(parallelism_1, parallelism_2);
+    }
+
+    private List<String> vertifyData(TestContainer container) {

Review Comment:
   Method name has a spelling error. 'vertifyData' should be 'verifyData'.
   ```suggestion
           List<String> parallelism_1 = verifyData(container);
   
           // parallelism = 2
           Container.ExecResult textWriteResult3 =
                   container.executeJob("/mysql_jdbc_to_dynamic_bucket_paimon_case3.conf");
           Assertions.assertEquals(0, textWriteResult3.getExitCode());
   
           List<String> parallelism_2 = verifyData(container);
           Assertions.assertEquals(parallelism_1, parallelism_2);
       }
   
       private List<String> verifyData(TestContainer container) {
   ```



##########
seatunnel-connectors-v2/connector-paimon/src/main/java/org/apache/seatunnel/connectors/seatunnel/paimon/sink/PaimonSinkWriter.java:
##########
@@ -199,8 +215,22 @@ public void write(SeaTunnelRow element) throws IOException {
             PaimonSecurityContext.runSecured(
                     () -> {
                         if (dynamicBucket) {
-                            int bucket = bucketAssigner.assign(rowData);
-                            tableWrite.write(rowData, bucket);
+                            // The result of calculating the remainder of the parallelism using the
+                            // hash code of the primary key must be consistent with the task
+                            // sequence number.
+                            PaimonBucketAssigner bucketAssigner =
+                                    PaimonBucketAssignerFactory.getBucketAssigner(
+                                            paimonTablePath,
+                                            paimonFileStoretable,
+                                            parallelism,
+                                            rowAssignerChannelComputer.channel(rowData));
+                            // When multiple threads call assigner.assign() simultaneously, they can
+                            // corrupt the internal hash map structure, leading to the
+                            // ArrayIndexOutOfBoundsException during rehashing operations
+                            synchronized (bucketAssigner) {

Review Comment:
   Synchronizing on the bucketAssigner instance may cause performance bottlenecks. Consider using a more granular locking mechanism or concurrent data structures to reduce contention.



##########
seatunnel-connectors-v2/connector-paimon/src/main/java/org/apache/seatunnel/connectors/seatunnel/paimon/sink/bucket/PaimonBucketAssignerFactory.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.paimon.sink.bucket;
+
+import org.apache.seatunnel.api.table.catalog.TablePath;
+
+import org.apache.paimon.table.Table;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class PaimonBucketAssignerFactory {
+    private static final ConcurrentHashMap<TablePath, Map<Integer, PaimonBucketAssigner>>

Review Comment:
   The static ConcurrentHashMap may grow unbounded and cause memory leaks if tables are frequently created/dropped. Consider implementing a cleanup mechanism or using WeakReferences.



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connectors-v2] fix dynamic bucket for paimon sink [seatunnel]

Posted by "corgy-w (via GitHub)" <gi...@apache.org>.
corgy-w merged PR #9595:
URL: https://github.com/apache/seatunnel/pull/9595


-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connectors-v2] fix dynamic bucket for paimon sink [seatunnel]

Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #9595:
URL: https://github.com/apache/seatunnel/pull/9595#discussion_r2234479610


##########
seatunnel-connectors-v2/connector-paimon/src/main/java/org/apache/seatunnel/connectors/seatunnel/paimon/sink/bucket/PaimonBucketAssignerFactory.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.paimon.sink.bucket;
+
+import org.apache.seatunnel.api.table.catalog.TablePath;
+
+import org.apache.paimon.table.Table;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class PaimonBucketAssignerFactory implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+    private final ConcurrentHashMap<TablePath, Map<Integer, PaimonBucketAssigner>>
+            BUCKET_ASSIGNER_MAP = new ConcurrentHashMap<>();

Review Comment:
   ```suggestion
               bucketAssignerMap = new ConcurrentHashMap<>();
   ```



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org