You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2020/03/02 19:15:26 UTC

[GitHub] [druid] ccaominh opened a new pull request #9448: Fix superbatch merge last partition boundaries

ccaominh opened a new pull request #9448: Fix superbatch merge last partition boundaries
URL: https://github.com/apache/druid/pull/9448
 
 
   ### Description
   
   A bug in the computation for the last parallel merge partition could cause an `IndexOutOfBoundsException` or precondition failure due to an empty partition.
   
   <hr>
   
   This PR has:
   - [x] been self-reviewed.
   - [x] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
   - [x] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
   - [x] added unit tests or modified existing tests to cover new code paths.
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] ccaominh merged pull request #9448: Fix superbatch merge last partition boundaries

Posted by GitBox <gi...@apache.org>.
ccaominh merged pull request #9448: Fix superbatch merge last partition boundaries
URL: https://github.com/apache/druid/pull/9448
 
 
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on a change in pull request #9448: Fix superbatch merge last partition boundaries

Posted by GitBox <gi...@apache.org>.
jihoonson commented on a change in pull request #9448: Fix superbatch merge last partition boundaries
URL: https://github.com/apache/druid/pull/9448#discussion_r387316905
 
 

 ##########
 File path: indexing-service/src/main/java/org/apache/druid/indexing/common/task/batch/parallel/ParallelIndexSupervisorTask.java
 ##########
 @@ -760,29 +761,39 @@ private PartitionBoundaries determineRangePartition(Collection<StringDistributio
     // See PartitionStat in GeneratedPartitionsReport.
     final List<Pair<Interval, Integer>> partitions = new ArrayList<>(partitionToLocations.keySet());
     Collections.shuffle(partitions, ThreadLocalRandom.current());
-    final int numPartitionsPerTask = (int) Math.round(partitions.size() / (double) numMergeTasks);
 
     final List<M> assignedPartitionLocations = new ArrayList<>(numMergeTasks);
-    for (int i = 0; i < numMergeTasks - 1; i++) {
+    for (int i = 0; i < numMergeTasks; i++) {
+      Pair<Integer, Integer> partitionBoundaries = getPartitionBoundaries(i, partitions.size(), numMergeTasks);
       final List<L> assignedToSameTask = partitions
-          .subList(i * numPartitionsPerTask, (i + 1) * numPartitionsPerTask)
+          .subList(partitionBoundaries.lhs, partitionBoundaries.rhs)
           .stream()
           .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
           .collect(Collectors.toList());
       assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
     }
 
-    // The last task is assigned all remaining partitions.
-    final List<L> assignedToSameTask = partitions
-        .subList((numMergeTasks - 1) * numPartitionsPerTask, partitions.size())
-        .stream()
-        .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
-        .collect(Collectors.toList());
-    assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
-
     return assignedPartitionLocations;
   }
 
+  /**
+   * Partition items into as evenly-sized splits as possible.
+   *
+   * @param index  index of partition
+   * @param total  number of items to partitions
+   * @param splits number of desired partitions
+   *
+   * @return partition range: [lhs, rhs)
+   */
+  private static Pair<Integer, Integer> getPartitionBoundaries(int index, int total, int splits)
+  {
+    int chunk = total / splits;
+    int remainder = total % splits;
+    int start = index * chunk + (index < remainder ? index : remainder);
 
 Review comment:
   I think `index * (chunk + index < remainder ? 1 : 0)` is more clear.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on a change in pull request #9448: Fix superbatch merge last partition boundaries

Posted by GitBox <gi...@apache.org>.
jihoonson commented on a change in pull request #9448: Fix superbatch merge last partition boundaries
URL: https://github.com/apache/druid/pull/9448#discussion_r387318051
 
 

 ##########
 File path: indexing-service/src/main/java/org/apache/druid/indexing/common/task/batch/parallel/ParallelIndexSupervisorTask.java
 ##########
 @@ -760,29 +761,39 @@ private PartitionBoundaries determineRangePartition(Collection<StringDistributio
     // See PartitionStat in GeneratedPartitionsReport.
     final List<Pair<Interval, Integer>> partitions = new ArrayList<>(partitionToLocations.keySet());
     Collections.shuffle(partitions, ThreadLocalRandom.current());
-    final int numPartitionsPerTask = (int) Math.round(partitions.size() / (double) numMergeTasks);
 
     final List<M> assignedPartitionLocations = new ArrayList<>(numMergeTasks);
-    for (int i = 0; i < numMergeTasks - 1; i++) {
+    for (int i = 0; i < numMergeTasks; i++) {
+      Pair<Integer, Integer> partitionBoundaries = getPartitionBoundaries(i, partitions.size(), numMergeTasks);
       final List<L> assignedToSameTask = partitions
-          .subList(i * numPartitionsPerTask, (i + 1) * numPartitionsPerTask)
+          .subList(partitionBoundaries.lhs, partitionBoundaries.rhs)
           .stream()
           .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
           .collect(Collectors.toList());
       assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
     }
 
-    // The last task is assigned all remaining partitions.
-    final List<L> assignedToSameTask = partitions
-        .subList((numMergeTasks - 1) * numPartitionsPerTask, partitions.size())
-        .stream()
-        .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
-        .collect(Collectors.toList());
-    assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
-
     return assignedPartitionLocations;
   }
 
+  /**
+   * Partition items into as evenly-sized splits as possible.
+   *
+   * @param index  index of partition
+   * @param total  number of items to partitions
+   * @param splits number of desired partitions
+   *
+   * @return partition range: [lhs, rhs)
+   */
+  private static Pair<Integer, Integer> getPartitionBoundaries(int index, int total, int splits)
+  {
+    int chunk = total / splits;
+    int remainder = total % splits;
+    int start = index * chunk + (index < remainder ? index : remainder);
 
 Review comment:
   I think the comment would be nice.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] maytasm3 commented on a change in pull request #9448: Fix superbatch merge last partition boundaries

Posted by GitBox <gi...@apache.org>.
maytasm3 commented on a change in pull request #9448: Fix superbatch merge last partition boundaries
URL: https://github.com/apache/druid/pull/9448#discussion_r386626730
 
 

 ##########
 File path: indexing-service/src/main/java/org/apache/druid/indexing/common/task/batch/parallel/ParallelIndexSupervisorTask.java
 ##########
 @@ -760,29 +761,39 @@ private PartitionBoundaries determineRangePartition(Collection<StringDistributio
     // See PartitionStat in GeneratedPartitionsReport.
     final List<Pair<Interval, Integer>> partitions = new ArrayList<>(partitionToLocations.keySet());
     Collections.shuffle(partitions, ThreadLocalRandom.current());
-    final int numPartitionsPerTask = (int) Math.round(partitions.size() / (double) numMergeTasks);
 
     final List<M> assignedPartitionLocations = new ArrayList<>(numMergeTasks);
-    for (int i = 0; i < numMergeTasks - 1; i++) {
+    for (int i = 0; i < numMergeTasks; i++) {
+      Pair<Integer, Integer> partitionBoundaries = getPartitionBoundaries(i, partitions.size(), numMergeTasks);
       final List<L> assignedToSameTask = partitions
-          .subList(i * numPartitionsPerTask, (i + 1) * numPartitionsPerTask)
+          .subList(partitionBoundaries.lhs, partitionBoundaries.rhs)
           .stream()
           .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
           .collect(Collectors.toList());
       assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
     }
 
-    // The last task is assigned all remaining partitions.
-    final List<L> assignedToSameTask = partitions
-        .subList((numMergeTasks - 1) * numPartitionsPerTask, partitions.size())
-        .stream()
-        .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
-        .collect(Collectors.toList());
-    assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
-
     return assignedPartitionLocations;
   }
 
+  /**
+   * Partition items into as evenly-sized splits as possible.
+   *
+   * @param index  index of partition
+   * @param total  number of items to partitions
+   * @param splits number of desired partitions
+   *
+   * @return partition range: [lhs, rhs)
+   */
+  private static Pair<Integer, Integer> getPartitionBoundaries(int index, int total, int splits)
+  {
+    int chunk = total / splits;
+    int remainder = total % splits;
+    int start = index * chunk + (index < remainder ? index : remainder);
 
 Review comment:
   Can you add comment on  purpose of `(index < remainder ? index : remainder)` ?
   i.e. splitting the `remainder` across the first `remainder` tasks etc.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] maytasm3 commented on a change in pull request #9448: Fix superbatch merge last partition boundaries

Posted by GitBox <gi...@apache.org>.
maytasm3 commented on a change in pull request #9448: Fix superbatch merge last partition boundaries
URL: https://github.com/apache/druid/pull/9448#discussion_r386628214
 
 

 ##########
 File path: indexing-service/src/test/java/org/apache/druid/indexing/common/task/batch/parallel/ParallelIndexSupervisorTaskTest.java
 ##########
 @@ -0,0 +1,121 @@
+/*
+ * 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.druid.indexing.common.task.batch.parallel;
+
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.Pair;
+import org.joda.time.Interval;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+@RunWith(Enclosed.class)
+public class ParallelIndexSupervisorTaskTest
+{
+  @RunWith(Parameterized.class)
+  public static class CreateMergeIoConfigsTest
+  {
+    private static final Function<List<HashPartitionLocation>, PartialHashSegmentMergeIOConfig>
+        CREATE_PARTIAL_SEGMENT_MERGE_IO_CONFIG = PartialHashSegmentMergeIOConfig::new;
+
+    @Parameterized.Parameters(name = "count = {0}")
+    public static Iterable<? extends Object> data()
+    {
+      // different scenarios for last (index = 10 - 1 = 9) partition:
+      return Arrays.asList(
+          20,  // even partitions per task: round(20 / 10) * (10 - 1) = 2 * 9 = 18 < 20
+          24,  // round down:               round(24 / 10) * (10 - 1) = 2 * 9 = 18 < 24
+          25,  // round up to greater:      round(25 / 10) * (10 - 1) = 3 * 9 = 27 > 25 (index out of bounds)
+          27   // round up to equal:        round(27 / 10) * (10 - 1) = 3 * 9 = 27 == 27 (empty partition)
+      );
+    }
+
+    @Parameterized.Parameter
+    public int count;
+
+    @Test
+    public void handlesLastPartitionCorrectly()
 
 Review comment:
   Some ideas for other assertions.
   1) Verify that the split is evenly distributed. i.e. we should have 10 tasks with 2 each (for 20), 4 tasks with 3 and 6 tasks with 2 (for 24), etc.
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] maytasm3 commented on a change in pull request #9448: Fix superbatch merge last partition boundaries

Posted by GitBox <gi...@apache.org>.
maytasm3 commented on a change in pull request #9448: Fix superbatch merge last partition boundaries
URL: https://github.com/apache/druid/pull/9448#discussion_r386626841
 
 

 ##########
 File path: indexing-service/src/main/java/org/apache/druid/indexing/common/task/batch/parallel/ParallelIndexSupervisorTask.java
 ##########
 @@ -760,29 +761,39 @@ private PartitionBoundaries determineRangePartition(Collection<StringDistributio
     // See PartitionStat in GeneratedPartitionsReport.
     final List<Pair<Interval, Integer>> partitions = new ArrayList<>(partitionToLocations.keySet());
     Collections.shuffle(partitions, ThreadLocalRandom.current());
-    final int numPartitionsPerTask = (int) Math.round(partitions.size() / (double) numMergeTasks);
 
     final List<M> assignedPartitionLocations = new ArrayList<>(numMergeTasks);
-    for (int i = 0; i < numMergeTasks - 1; i++) {
+    for (int i = 0; i < numMergeTasks; i++) {
+      Pair<Integer, Integer> partitionBoundaries = getPartitionBoundaries(i, partitions.size(), numMergeTasks);
       final List<L> assignedToSameTask = partitions
-          .subList(i * numPartitionsPerTask, (i + 1) * numPartitionsPerTask)
+          .subList(partitionBoundaries.lhs, partitionBoundaries.rhs)
           .stream()
           .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
           .collect(Collectors.toList());
       assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
     }
 
-    // The last task is assigned all remaining partitions.
-    final List<L> assignedToSameTask = partitions
-        .subList((numMergeTasks - 1) * numPartitionsPerTask, partitions.size())
-        .stream()
-        .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
-        .collect(Collectors.toList());
-    assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
-
     return assignedPartitionLocations;
   }
 
+  /**
+   * Partition items into as evenly-sized splits as possible.
+   *
+   * @param index  index of partition
+   * @param total  number of items to partitions
+   * @param splits number of desired partitions
+   *
+   * @return partition range: [lhs, rhs)
+   */
+  private static Pair<Integer, Integer> getPartitionBoundaries(int index, int total, int splits)
+  {
+    int chunk = total / splits;
+    int remainder = total % splits;
+    int start = index * chunk + (index < remainder ? index : remainder);
+    int stop = start + chunk + (index < remainder ? 1 : 0);
 
 Review comment:
   same here for `(index < remainder ? 1 : 0)`

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on a change in pull request #9448: Fix superbatch merge last partition boundaries

Posted by GitBox <gi...@apache.org>.
jihoonson commented on a change in pull request #9448: Fix superbatch merge last partition boundaries
URL: https://github.com/apache/druid/pull/9448#discussion_r387314304
 
 

 ##########
 File path: indexing-service/src/main/java/org/apache/druid/indexing/common/task/batch/parallel/ParallelIndexSupervisorTask.java
 ##########
 @@ -760,29 +761,39 @@ private PartitionBoundaries determineRangePartition(Collection<StringDistributio
     // See PartitionStat in GeneratedPartitionsReport.
     final List<Pair<Interval, Integer>> partitions = new ArrayList<>(partitionToLocations.keySet());
     Collections.shuffle(partitions, ThreadLocalRandom.current());
-    final int numPartitionsPerTask = (int) Math.round(partitions.size() / (double) numMergeTasks);
 
     final List<M> assignedPartitionLocations = new ArrayList<>(numMergeTasks);
-    for (int i = 0; i < numMergeTasks - 1; i++) {
+    for (int i = 0; i < numMergeTasks; i++) {
+      Pair<Integer, Integer> partitionBoundaries = getPartitionBoundaries(i, partitions.size(), numMergeTasks);
       final List<L> assignedToSameTask = partitions
-          .subList(i * numPartitionsPerTask, (i + 1) * numPartitionsPerTask)
+          .subList(partitionBoundaries.lhs, partitionBoundaries.rhs)
           .stream()
           .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
           .collect(Collectors.toList());
       assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
     }
 
-    // The last task is assigned all remaining partitions.
-    final List<L> assignedToSameTask = partitions
-        .subList((numMergeTasks - 1) * numPartitionsPerTask, partitions.size())
-        .stream()
-        .flatMap(intervalAndPartitionId -> partitionToLocations.get(intervalAndPartitionId).stream())
-        .collect(Collectors.toList());
-    assignedPartitionLocations.add(createPartialSegmentMergeIOConfig.apply(assignedToSameTask));
-
     return assignedPartitionLocations;
   }
 
+  /**
+   * Partition items into as evenly-sized splits as possible.
+   *
+   * @param index  index of partition
+   * @param total  number of items to partitions
 
 Review comment:
   `to partition`?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org