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 2022/04/12 04:24:28 UTC

[GitHub] [druid] kfaraz commented on a diff in pull request #12417: Use binary search to improve DimensionRangeShardSpec lookup

kfaraz commented on code in PR #12417:
URL: https://github.com/apache/druid/pull/12417#discussion_r847946933


##########
core/src/main/java/org/apache/druid/timeline/partition/DimensionRangeShardSpec.java:
##########
@@ -279,25 +253,14 @@ public <T> PartitionChunk<T> createChunk(T obj)
     }
   }
 
-  private boolean isInChunk(InputRow inputRow)
-  {
-    return isInChunk(dimensions, start, end, inputRow);
-  }
-
   public static boolean isInChunk(

Review Comment:
   You could probably remove this method. I don't think it is needed anymore.



##########
core/src/main/java/org/apache/druid/timeline/partition/BaseDimensionRangeShardSpec.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.timeline.partition;
+
+import com.google.common.collect.Ordering;
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.StringTuple;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.guava.Comparators;
+
+import javax.annotation.Nullable;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+
+public abstract class BaseDimensionRangeShardSpec implements ShardSpec
+{
+  protected final List<String> dimensions;
+  @Nullable
+  protected final StringTuple start;
+  @Nullable
+  protected final StringTuple end;
+
+  protected BaseDimensionRangeShardSpec(
+      List<String> dimensions,
+      @Nullable StringTuple start,
+      @Nullable StringTuple end
+  )
+  {
+    this.dimensions = dimensions;
+    this.start = start;
+    this.end = end;
+  }
+
+  @Override
+  public ShardSpecLookup getLookup(final List<? extends ShardSpec> shardSpecs)
+  {
+    return createLookup(dimensions, shardSpecs);
+  }
+
+  private static ShardSpecLookup createLookup(List<String> dimensions, List<? extends ShardSpec> shardSpecs)
+  {
+    BaseDimensionRangeShardSpec[] rangeShardSpecs = new BaseDimensionRangeShardSpec[shardSpecs.size()];
+    for (int i = 0; i < shardSpecs.size(); i++) {
+      rangeShardSpecs[i] = (BaseDimensionRangeShardSpec) shardSpecs.get(i);
+    }
+    final Comparator<StringTuple> startComparator = Comparators.naturalNullsFirst();
+    final Comparator<StringTuple> endComparator = Ordering.natural().nullsLast();
+    final Comparator<BaseDimensionRangeShardSpec> shardSpecComparator = new Comparator<BaseDimensionRangeShardSpec>()
+    {
+      @Override
+      public int compare(BaseDimensionRangeShardSpec o1, BaseDimensionRangeShardSpec o2)
+      {
+        int startComparison = startComparator.compare(o1.start, o2.start);
+        if (startComparison != 0) {
+          return startComparison;
+        }
+        return endComparator.compare(o1.end, o2.end);
+      }
+    };

Review Comment:
   You can try chaining the comparator. See below for an example.
   
   https://github.com/apache/druid/blob/master/indexing-service/src/main/java/org/apache/druid/indexing/common/task/batch/parallel/ParallelIndexSupervisorTask.java#L932-L937



##########
core/src/main/java/org/apache/druid/timeline/partition/BaseDimensionRangeShardSpec.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.timeline.partition;
+
+import com.google.common.collect.Ordering;
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.StringTuple;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.guava.Comparators;
+
+import javax.annotation.Nullable;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+
+public abstract class BaseDimensionRangeShardSpec implements ShardSpec
+{
+  protected final List<String> dimensions;
+  @Nullable
+  protected final StringTuple start;
+  @Nullable
+  protected final StringTuple end;
+
+  protected BaseDimensionRangeShardSpec(
+      List<String> dimensions,
+      @Nullable StringTuple start,
+      @Nullable StringTuple end
+  )
+  {
+    this.dimensions = dimensions;
+    this.start = start;
+    this.end = end;
+  }
+
+  @Override
+  public ShardSpecLookup getLookup(final List<? extends ShardSpec> shardSpecs)
+  {
+    return createLookup(dimensions, shardSpecs);
+  }
+
+  private static ShardSpecLookup createLookup(List<String> dimensions, List<? extends ShardSpec> shardSpecs)
+  {
+    BaseDimensionRangeShardSpec[] rangeShardSpecs = new BaseDimensionRangeShardSpec[shardSpecs.size()];
+    for (int i = 0; i < shardSpecs.size(); i++) {
+      rangeShardSpecs[i] = (BaseDimensionRangeShardSpec) shardSpecs.get(i);
+    }

Review Comment:
   Nit: You could use `shardSpecs.toArray()` for cleaner code.



##########
core/src/main/java/org/apache/druid/timeline/partition/BaseDimensionRangeShardSpec.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.timeline.partition;
+
+import com.google.common.collect.Ordering;
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.StringTuple;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.guava.Comparators;
+
+import javax.annotation.Nullable;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+
+public abstract class BaseDimensionRangeShardSpec implements ShardSpec
+{
+  protected final List<String> dimensions;
+  @Nullable
+  protected final StringTuple start;
+  @Nullable
+  protected final StringTuple end;
+
+  protected BaseDimensionRangeShardSpec(
+      List<String> dimensions,
+      @Nullable StringTuple start,
+      @Nullable StringTuple end
+  )
+  {
+    this.dimensions = dimensions;
+    this.start = start;
+    this.end = end;
+  }
+
+  @Override
+  public ShardSpecLookup getLookup(final List<? extends ShardSpec> shardSpecs)
+  {
+    return createLookup(dimensions, shardSpecs);
+  }
+
+  private static ShardSpecLookup createLookup(List<String> dimensions, List<? extends ShardSpec> shardSpecs)
+  {
+    BaseDimensionRangeShardSpec[] rangeShardSpecs = new BaseDimensionRangeShardSpec[shardSpecs.size()];
+    for (int i = 0; i < shardSpecs.size(); i++) {
+      rangeShardSpecs[i] = (BaseDimensionRangeShardSpec) shardSpecs.get(i);
+    }
+    final Comparator<StringTuple> startComparator = Comparators.naturalNullsFirst();
+    final Comparator<StringTuple> endComparator = Ordering.natural().nullsLast();
+    final Comparator<BaseDimensionRangeShardSpec> shardSpecComparator = new Comparator<BaseDimensionRangeShardSpec>()
+    {
+      @Override
+      public int compare(BaseDimensionRangeShardSpec o1, BaseDimensionRangeShardSpec o2)
+      {
+        int startComparison = startComparator.compare(o1.start, o2.start);
+        if (startComparison != 0) {
+          return startComparison;
+        }
+        return endComparator.compare(o1.end, o2.end);
+      }
+    };
+    Arrays.sort(rangeShardSpecs, shardSpecComparator);
+
+    return (long timestamp, InputRow row) -> {
+      StringTuple inputRowTuple = getInputRowTuple(dimensions, row);
+      int startIndex = 0;
+      int endIndex = shardSpecs.size() - 1;
+      while (startIndex <= endIndex) {
+        int mid = (startIndex + endIndex) >>> 1;
+        BaseDimensionRangeShardSpec rangeShardSpec = rangeShardSpecs[mid];
+        if (startComparator.compare(inputRowTuple, rangeShardSpec.start) < 0) {
+          endIndex = mid - 1;
+        } else if (endComparator.compare(inputRowTuple, rangeShardSpec.end) < 0) {
+          return rangeShardSpec;
+        } else {
+          startIndex = mid + 1;
+        }
+      }
+      throw new ISE("row[%s] doesn't fit in any shard[%s]", row, shardSpecs);
+    };
+  }
+
+  protected static StringTuple getInputRowTuple(List<String> dimensions, InputRow inputRow)
+  {
+    final String[] inputDimensionValues = new String[dimensions.size()];
+    for (int i = 0; i < dimensions.size(); ++i) {
+      List<String> values = inputRow.getDimension(dimensions.get(i));

Review Comment:
   Nit: please add the comment originally present in this method.
   `// Get the values of this dimension, treat multiple values as null`



##########
core/src/main/java/org/apache/druid/timeline/partition/BaseDimensionRangeShardSpec.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.timeline.partition;
+
+import com.google.common.collect.Ordering;
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.StringTuple;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.guava.Comparators;
+
+import javax.annotation.Nullable;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+
+public abstract class BaseDimensionRangeShardSpec implements ShardSpec
+{
+  protected final List<String> dimensions;
+  @Nullable
+  protected final StringTuple start;
+  @Nullable
+  protected final StringTuple end;
+
+  protected BaseDimensionRangeShardSpec(
+      List<String> dimensions,
+      @Nullable StringTuple start,
+      @Nullable StringTuple end
+  )
+  {
+    this.dimensions = dimensions;
+    this.start = start;
+    this.end = end;
+  }
+
+  @Override
+  public ShardSpecLookup getLookup(final List<? extends ShardSpec> shardSpecs)
+  {
+    return createLookup(dimensions, shardSpecs);
+  }
+
+  private static ShardSpecLookup createLookup(List<String> dimensions, List<? extends ShardSpec> shardSpecs)
+  {
+    BaseDimensionRangeShardSpec[] rangeShardSpecs = new BaseDimensionRangeShardSpec[shardSpecs.size()];
+    for (int i = 0; i < shardSpecs.size(); i++) {
+      rangeShardSpecs[i] = (BaseDimensionRangeShardSpec) shardSpecs.get(i);
+    }
+    final Comparator<StringTuple> startComparator = Comparators.naturalNullsFirst();
+    final Comparator<StringTuple> endComparator = Ordering.natural().nullsLast();
+    final Comparator<BaseDimensionRangeShardSpec> shardSpecComparator = new Comparator<BaseDimensionRangeShardSpec>()
+    {
+      @Override
+      public int compare(BaseDimensionRangeShardSpec o1, BaseDimensionRangeShardSpec o2)
+      {
+        int startComparison = startComparator.compare(o1.start, o2.start);
+        if (startComparison != 0) {
+          return startComparison;
+        }
+        return endComparator.compare(o1.end, o2.end);
+      }
+    };
+    Arrays.sort(rangeShardSpecs, shardSpecComparator);
+
+    return (long timestamp, InputRow row) -> {
+      StringTuple inputRowTuple = getInputRowTuple(dimensions, row);
+      int startIndex = 0;
+      int endIndex = shardSpecs.size() - 1;
+      while (startIndex <= endIndex) {
+        int mid = (startIndex + endIndex) >>> 1;
+        BaseDimensionRangeShardSpec rangeShardSpec = rangeShardSpecs[mid];
+        if (startComparator.compare(inputRowTuple, rangeShardSpec.start) < 0) {
+          endIndex = mid - 1;
+        } else if (endComparator.compare(inputRowTuple, rangeShardSpec.end) < 0) {
+          return rangeShardSpec;
+        } else {
+          startIndex = mid + 1;
+        }
+      }

Review Comment:
   You could probably simplify this using `Arrays.binarySearch(array, key, comparator)`



-- 
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@druid.apache.org

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


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