shenh062326 commented on a change in pull request #1868:
URL: https://github.com/apache/hudi/pull/1868#discussion_r464775753



##########
File path: 
hudi-client/src/main/java/org/apache/hudi/table/action/commit/UpsertPartitioner.java
##########
@@ -270,20 +273,24 @@ public int getPartition(Object key) {
       return updateLocationToBucket.get(location.getFileId());
     } else {
       String partitionPath = keyLocation._1().getPartitionPath();
-      List<InsertBucket> targetBuckets = 
partitionPathToInsertBuckets.get(partitionPath);
+      List<InsertBucketCumulativeWeightPair> targetBuckets = 
partitionPathToInsertBucketInfos.get(partitionPath);
       // pick the target bucket to use based on the weights.
-      double totalWeight = 0.0;
       final long totalInserts = Math.max(1, 
profile.getWorkloadStat(partitionPath).getNumInserts());
       final long hashOfKey = NumericUtils.getMessageDigestHash("MD5", 
keyLocation._1().getRecordKey());
       final double r = 1.0 * Math.floorMod(hashOfKey, totalInserts) / 
totalInserts;
-      for (InsertBucket insertBucket : targetBuckets) {
-        totalWeight += insertBucket.weight;
-        if (r <= totalWeight) {
-          return insertBucket.bucketNumber;
-        }
+
+      int index = Collections.binarySearch(targetBuckets, new 
InsertBucketCumulativeWeightPair(new InsertBucket(), r));
+
+      if (index >= 0) {

Review comment:
       Below add a testcase for Collections.binarySearch.
   ```
     @Test
     public void test() {
       List<Double> cumulativeWeighs = new ArrayList<>();
       cumulativeWeighs.add(0.1);
       cumulativeWeighs.add(0.5);
       cumulativeWeighs.add(1.0);
   
       assertEquals (0, Collections.binarySearch(cumulativeWeighs, 0.1));
       assertEquals (1, Collections.binarySearch(cumulativeWeighs, 0.5));
       assertEquals (2, Collections.binarySearch(cumulativeWeighs, 1.0));
   
       assertEquals (-1, Collections.binarySearch(cumulativeWeighs, 0.01));
       assertEquals (-2, Collections.binarySearch(cumulativeWeighs, 0.2));
       assertEquals (-3, Collections.binarySearch(cumulativeWeighs, 0.6));
       assertEquals (-4, Collections.binarySearch(cumulativeWeighs, 1.1));
     }
   ```
   
   If the the search key is bigger than all the elements in the list, it will 
return (-(insertion point) - 1)= -3 -1 = -4 .
   So I have to add the following judge:
   ```
         if ((-1 * index - 1) < targetBuckets.size()) {
           return targetBuckets.get((-1 * index - 1)).getKey().bucketNumber;
         }
   ```




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

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


Reply via email to