the-other-tim-brown commented on code in PR #13976:
URL: https://github.com/apache/hudi/pull/13976#discussion_r2391585279


##########
hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/client/StreamingMetadataWriteHandler.java:
##########
@@ -87,12 +97,24 @@ public void commitToMetadataTable(HoodieTable table,
   private HoodieData<WriteStatus> 
streamWriteToMetadataTable(HoodieData<WriteStatus> dataTableWriteStatuses,
                                                              
HoodieTableMetadataWriter metadataWriter,
                                                              HoodieTable table,
-                                                             String 
instantTime) {
-    HoodieData<WriteStatus> allWriteStatus = dataTableWriteStatuses;
+                                                             String 
instantTime,
+                                                             boolean 
enforceCoalesceWithRepartition,
+                                                             int 
coalesceDividentForDataTableWrites) {
     HoodieData<WriteStatus> mdtWriteStatuses = 
metadataWriter.streamWriteToMetadataPartitions(dataTableWriteStatuses, 
instantTime);
-    allWriteStatus = allWriteStatus.union(mdtWriteStatuses);
-    allWriteStatus.persist("MEMORY_AND_DISK_SER", table.getContext(), 
HoodieData.HoodieDataCacheKey.of(table.getMetaClient().getBasePath().toString(),
 instantTime));
-    return allWriteStatus;
+    mdtWriteStatuses.persist("MEMORY_AND_DISK_SER", table.getContext(), 
HoodieData.HoodieDataCacheKey.of(table.getMetaClient().getBasePath().toString(),
 instantTime));
+    HoodieData<WriteStatus> coalescedDataWriteStatuses;
+    int coalesceParallelism = Math.max(1, 
dataTableWriteStatuses.getNumPartitions() / coalesceDividentForDataTableWrites);
+    if (enforceCoalesceWithRepartition) {
+      // with bulk insert and NONE sort mode, simple coalesce on datatable 
write statuses also impact record key generation stages.
+      // and hence we are adding a partitioner to cut the chain so that 
coalesce(1) here does not impact record key generation stages.
+      coalescedDataWriteStatuses = 
HoodieJavaRDD.of(HoodieJavaRDD.getJavaRDD(dataTableWriteStatuses)
+          .mapToPair((PairFunction<WriteStatus, Boolean, WriteStatus>) 
writeStatus -> new Tuple2(true, writeStatus))

Review Comment:
   The method is `public int getPartition(Object key)` which implies that the 
key is used. The key here is `true` due to `new Tuple2(true, writeStatus)`. If 
you set a breakpoint in the `CoalescingPartitioner` you will see this.



##########
hudi-client/hudi-spark-client/src/test/java/org/apache/hudi/client/TestCoalescingPartitioner.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.hudi.client;
+
+import org.apache.hudi.common.data.HoodieData;
+import org.apache.hudi.data.HoodieJavaRDD;
+import org.apache.hudi.testutils.HoodieClientTestBase;
+
+import org.apache.spark.api.java.JavaRDD;
+import org.apache.spark.api.java.function.Function;
+import org.apache.spark.api.java.function.PairFunction;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import scala.Tuple2;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class TestCoalescingPartitioner extends HoodieClientTestBase {
+
+  @Test
+  public void simpleCoalescingPartitionerTest() {
+    int numPartitions = 100;
+    HoodieData<Integer> rddData = HoodieJavaRDD.of(jsc.parallelize(
+        IntStream.rangeClosed(0, 100).boxed().collect(Collectors.toList()), 
numPartitions));
+
+    // 100 keys spread across 10 partitions.
+    CoalescingPartitioner coalescingPartitioner = new 
CoalescingPartitioner(10);
+    assertEquals(10, coalescingPartitioner.numPartitions());
+    rddData.collectAsList().forEach(entry -> {
+      assertEquals(entry.hashCode() % 10, 
coalescingPartitioner.getPartition(entry));
+    });
+
+    // 1 partition
+    CoalescingPartitioner coalescingPartitioner1 = new 
CoalescingPartitioner(1);
+    assertEquals(1, coalescingPartitioner1.numPartitions());
+    rddData.collectAsList().forEach(entry -> {
+      assertEquals(0, coalescingPartitioner1.getPartition(entry));
+    });
+
+    // empty rdd
+    rddData = HoodieJavaRDD.of(jsc.emptyRDD());
+    CoalescingPartitioner coalescingPartitioner2 = new 
CoalescingPartitioner(1);
+    assertEquals(1, coalescingPartitioner2.numPartitions());
+    rddData.collectAsList().forEach(entry -> {
+      // since there is only one partition, any getPartition will return just 
the same partition index
+      assertEquals(0, coalescingPartitioner2.getPartition(entry));
+    });
+  }
+
+  private static Stream<Arguments> coalesceTestArgs() {
+    return Arrays.stream(new Object[][] {
+        {100, 1},
+        {1, 1},
+        {1000, 10},
+        {100, 7},
+        {10, 2}
+    }).map(Arguments::of);
+  }
+
+  @ParameterizedTest
+  @MethodSource("coalesceTestArgs")
+  public void testCoalescingPartitionerWithRDD(int inputNumPartitions, int 
targetPartitions) {
+    List<Integer> inputData = IntStream.rangeClosed(0, 
inputNumPartitions).boxed().collect(Collectors.toList());
+    JavaRDD<Integer> data = jsc.parallelize(inputData, inputNumPartitions);
+    JavaRDD<Integer> coalescedData = data.mapToPair(new 
PairFunc()).partitionBy(new CoalescingPartitioner(targetPartitions)).map(new 
MapFunc());
+
+    assertEquals(targetPartitions, coalescedData.getNumPartitions());

Review Comment:
   We should check for a relatively even spread amongst the partitions. I wrote 
some local code to validate that the partitioner is unfortunately writing all 
data to the same spark partition:
   
   ```
   List<Pair<Integer, Integer>> results = 
coalescedData.mapPartitionsWithIndex((i, rows) -> {
         int count = 0;
         while (rows.hasNext()) {
           rows.next();
           count++;
         }
         return Collections.singletonList(Pair.of(i, count)).iterator();
       }, true).collect();
   
   ```



-- 
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