[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-15 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r335151691
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1071 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
 
 Review comment:
   not organic ? :-P
   
   maybe copy some part from 
https://github.com/apache/incubator-druid/issues/8577 also and add link to that 
.


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


With regards,
Apache Git Services

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



[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-15 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334687224
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1071 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334759043
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1071 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334687224
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1071 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334687224
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1071 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334687224
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1071 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334687224
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1071 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334646597
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1077 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334666223
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1071 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334664439
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1077 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334659806
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1071 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334646597
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1077 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334645042
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1077 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334645042
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1077 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-14 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334645042
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1077 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-11 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334213899
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1077 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-11 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334212480
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1077 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new 

[GitHub] [incubator-druid] himanshug commented on a change in pull request #8578: parallel broker merges on fork join pool

2019-10-11 Thread GitBox
himanshug commented on a change in pull request #8578: parallel broker merges 
on fork join pool
URL: https://github.com/apache/incubator-druid/pull/8578#discussion_r334212402
 
 

 ##
 File path: 
core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
 ##
 @@ -0,0 +1,1077 @@
+/*
+ * 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.java.util.common.guava;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BinaryOperator;
+
+/**
+ * Artisanal, locally-sourced, hand-crafted, gluten and GMO free, bespoke, 
small-batch parallel merge combinining sequence
+ */
+public class ParallelMergeCombiningSequence extends YieldingSequenceBase
+{
+  private static final Logger LOG = new 
Logger(ParallelMergeCombiningSequence.class);
+
+  private final ForkJoinPool workerPool;
+  private final List> baseSequences;
+  private final Ordering orderingFn;
+  private final BinaryOperator combineFn;
+  private final int queueSize;
+  private final boolean hasTimeout;
+  private final long timeoutAtNanos;
+  private final int queryPriority; // not currently used :(
+  private final int yieldAfter;
+  private final int batchSize;
+  private final int parallelism;
+  private final CancellationGizmo cancellationGizmo;
+
+  public ParallelMergeCombiningSequence(
+  ForkJoinPool workerPool,
+  List> baseSequences,
+  Ordering orderingFn,
+  BinaryOperator combineFn,
+  boolean hasTimeout,
+  long timeoutMillis,
+  int queryPriority,
+  int parallelism,
+  int yieldAfter,
+  int batchSize
+  )
+  {
+this.workerPool = workerPool;
+this.baseSequences = baseSequences;
+this.orderingFn = orderingFn;
+this.combineFn = combineFn;
+this.hasTimeout = hasTimeout;
+this.timeoutAtNanos = System.nanoTime() + 
TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS);
+this.queryPriority = queryPriority;
+this.parallelism = parallelism;
+this.yieldAfter = yieldAfter;
+this.batchSize = batchSize;
+this.queueSize = 4 * (yieldAfter / batchSize);
+this.cancellationGizmo = new CancellationGizmo();
+  }
+
+  @Override
+  public  Yielder toYielder(OutType initValue, 
YieldingAccumulator accumulator)
+  {
+if (baseSequences.isEmpty()) {
+  return Sequences.empty().toYielder(initValue, accumulator);
+}
+
+final BlockingQueue> outputQueue = new 
ArrayBlockingQueue<>(queueSize);
+MergeCombinePartitioningAction finalMergeAction = new 
MergeCombinePartitioningAction<>(
+baseSequences,
+orderingFn,
+combineFn,
+outputQueue,
+queueSize,
+parallelism,
+yieldAfter,
+batchSize,
+hasTimeout,
+timeoutAtNanos,
+cancellationGizmo
+);
+workerPool.execute(finalMergeAction);
+Sequence finalOutSequence = makeOutputSequenceForQueue(outputQueue, 
hasTimeout, timeoutAtNanos, cancellationGizmo);
+return finalOutSequence.toYielder(initValue, accumulator);
+  }
+
+  /**
+   * Create an output {@link Sequence} that wraps the output {@link 
BlockingQueue} of a
+   * {@link MergeCombinePartitioningAction}
+   */
+  static  Sequence makeOutputSequenceForQueue(
+  BlockingQueue> queue,
+  boolean hasTimeout,
+  long timeoutAtNanos,
+  CancellationGizmo cancellationGizmo
+  )
+  {
+return new