You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "kfaraz (via GitHub)" <gi...@apache.org> on 2023/05/04 05:49:50 UTC

[GitHub] [druid] kfaraz commented on a diff in pull request #13840: add sampling factor for DeterminePartitionsJob

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


##########
indexing-hadoop/src/main/java/org/apache/druid/indexer/DeterminePartitionsJobSampler.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.indexer;
+
+import com.google.common.hash.HashFunction;
+import com.google.common.hash.Hashing;
+
+import java.util.concurrent.ThreadLocalRandom;
+
+public class DeterminePartitionsJobSampler
+{
+  private static final HashFunction HASH_FUNCTION = Hashing.murmur3_32();
+
+  private final int samplingFactor;
+
+  private final int sampledTargetPartitionSize;
+
+  private final int sampledMaxRowsPerSegment;
+
+  public DeterminePartitionsJobSampler(int samplingFactor, int targetPartitionSize, int maxRowsPerSegment)
+  {
+    this.samplingFactor = Math.max(samplingFactor, 1);
+    this.sampledTargetPartitionSize = targetPartitionSize / this.samplingFactor;
+    this.sampledMaxRowsPerSegment = maxRowsPerSegment / this.samplingFactor;
+  }
+
+  /**
+   * If input rows is duplicate, we can use hash and mod to do sample. As we hash on whole group key,
+   * there will not likely data skew if the hash function is balanced enough.
+   */
+  boolean shouldEmitRow(byte[] groupKeyBytes)
+  {
+    return samplingFactor == 1 || HASH_FUNCTION.hashBytes(groupKeyBytes).asInt() % samplingFactor == 0;

Review Comment:
   Could you please elaborate on how this helps tackle duplicates? 
   
   IIUC, this would only emit group keys whose hash module comes out to be 0. The other group keys would never be emitted. For a group key that would be emitted, you would still emit duplicates.



##########
indexing-hadoop/src/main/java/org/apache/druid/indexer/HadoopTuningConfig.java:
##########
@@ -130,7 +142,8 @@ public HadoopTuningConfig(
       final @JsonProperty("logParseExceptions") @Nullable Boolean logParseExceptions,
       final @JsonProperty("maxParseExceptions") @Nullable Integer maxParseExceptions,
       final @JsonProperty("useYarnRMJobStatusFallback") @Nullable Boolean useYarnRMJobStatusFallback,
-      final @JsonProperty("awaitSegmentAvailabilityTimeoutMillis") @Nullable Long awaitSegmentAvailabilityTimeoutMillis
+      final @JsonProperty("awaitSegmentAvailabilityTimeoutMillis") @Nullable Long awaitSegmentAvailabilityTimeoutMillis,
+      final @JsonProperty("samplingFactor") int samplingFactor

Review Comment:
   Maybe rename this config to `determinePartitionsSamplingFactor` to avoid ambiguity.



##########
indexing-hadoop/src/main/java/org/apache/druid/indexer/DeterminePartitionsJobSampler.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.indexer;
+
+import com.google.common.hash.HashFunction;
+import com.google.common.hash.Hashing;
+
+import java.util.concurrent.ThreadLocalRandom;
+
+public class DeterminePartitionsJobSampler
+{
+  private static final HashFunction HASH_FUNCTION = Hashing.murmur3_32();
+
+  private final int samplingFactor;
+
+  private final int sampledTargetPartitionSize;
+
+  private final int sampledMaxRowsPerSegment;
+
+  public DeterminePartitionsJobSampler(int samplingFactor, int targetPartitionSize, int maxRowsPerSegment)
+  {
+    this.samplingFactor = Math.max(samplingFactor, 1);
+    this.sampledTargetPartitionSize = targetPartitionSize / this.samplingFactor;
+    this.sampledMaxRowsPerSegment = maxRowsPerSegment / this.samplingFactor;
+  }
+
+  /**
+   * If input rows is duplicate, we can use hash and mod to do sample. As we hash on whole group key,
+   * there will not likely data skew if the hash function is balanced enough.
+   */
+  boolean shouldEmitRow(byte[] groupKeyBytes)
+  {
+    return samplingFactor == 1 || HASH_FUNCTION.hashBytes(groupKeyBytes).asInt() % samplingFactor == 0;

Review Comment:
   For de-duplication, you can look at `DedupInputRowFilter` in `PartialDimensionDistributionTask`.



##########
indexing-hadoop/src/main/java/org/apache/druid/indexer/HadoopTuningConfig.java:
##########
@@ -130,7 +142,8 @@ public HadoopTuningConfig(
       final @JsonProperty("logParseExceptions") @Nullable Boolean logParseExceptions,
       final @JsonProperty("maxParseExceptions") @Nullable Integer maxParseExceptions,
       final @JsonProperty("useYarnRMJobStatusFallback") @Nullable Boolean useYarnRMJobStatusFallback,
-      final @JsonProperty("awaitSegmentAvailabilityTimeoutMillis") @Nullable Long awaitSegmentAvailabilityTimeoutMillis
+      final @JsonProperty("awaitSegmentAvailabilityTimeoutMillis") @Nullable Long awaitSegmentAvailabilityTimeoutMillis,
+      final @JsonProperty("samplingFactor") int samplingFactor

Review Comment:
   Also, I think the property should be nullable, if not specified, it would default to 1.



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