drill-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] vrozov commented on a change in pull request #1333: DRILL-6410: Memory leak in Parquet Reader during cancellation
Date Wed, 18 Jul 2018 23:53:58 GMT
vrozov commented on a change in pull request #1333: DRILL-6410: Memory leak in Parquet Reader
during cancellation
URL: https://github.com/apache/drill/pull/1333#discussion_r203566491
 
 

 ##########
 File path: exec/java-exec/src/main/java/org/apache/drill/exec/util/ExecutableTasksLatch.java
 ##########
 @@ -0,0 +1,255 @@
+/*
+ * 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.drill.exec.util;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.locks.LockSupport;
+import java.util.function.BooleanSupplier;
+
+import org.apache.drill.exec.testing.CountDownLatchInjection;
+import org.apache.drill.exec.testing.NoOpControlsInjector;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+/**
+ * A wrapper class around {@linkplain ExecutorService an execution service} that allows a
thread that instantiated an
+ * instance of the class to wait for a submitted task to complete (either successfully or
unsuccessfully) or to wait for
+ * all submitted tasks to complete.
+ * @param <C> type of tasks to execute. <tt>C</tt> must extend {@linkplain
Callable}{@literal <Void>}
+ */
+public class ExecutableTasksLatch<C extends Callable<Void>> {
+  private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ExecutableTasksLatch.class);
+
+  /**
+   * An interface that class {@literal <C>} may optionally implement to receive a callback
when a submitted for execution
+   * task starts or finishes.
+   */
+  public interface Notifiable {
+    /**
+     * Notify a task that it is assigned to a thread for execution
+     */
+    void started();
+
+    /**
+     * A callback in case a task is considered to be successfully completed (not cancelled).
+     */
+    void finished();
+  }
+
+  private final ExecutorService executor;
+  private final Thread thread;
+  private final AtomicInteger count;
+  private final Queue<ExecutableTask<C>> executableTasks;
+  private final CountDownLatchInjection testCountDownLatch;
+
+  /**
+   * Constructs an instance of <tt>ExecutableTasksLatch</tt>. A thread where
construction is done becomes the waiting
+   * thread.
+   * @param executor instance of {@linkplain ExecutorService execution service} to wrap
+   * @param testCountDownLatch optional {@linkplain CountDownLatchInjection}
+   */
+  public ExecutableTasksLatch(ExecutorService executor, CountDownLatchInjection testCountDownLatch)
{
+    this.executor = executor;
+    thread = Thread.currentThread();
+    count = new AtomicInteger();
+    executableTasks = new LinkedList<>();
+    this.testCountDownLatch = testCountDownLatch == null ? NoOpControlsInjector.LATCH : testCountDownLatch;
+  }
+
+  /**
+   * Waits for an earliest submitted task to complete and removes task from a collection
of known tasks.
+   * @throws ExecutionException if task threw an Exception during execution
+   * @throws InterruptedException if wait is interrupted
+   */
+  public void take() throws ExecutionException, InterruptedException {
+    final ExecutableTask<C> task = executableTasks.peek();
+    Preconditions.checkState(task != null, "No tasks are scheduled for execution");
+    while (ExecutableTask.State.COMPLETING.compareTo(task.state.get()) > 0) {
+      if (Thread.interrupted()) {
+        throw new InterruptedException();
+      }
+      LockSupport.park();
+    }
+    executableTasks.remove();
+    while (ExecutableTask.State.COMPLETING.compareTo(task.state.get()) == 0) {
+      Thread.yield();
+    }
+
+    if (task.exception != null) {
+      throw task.exception;
+    }
+  }
+
+  /**
+   * @return immutable collection of submitted for execution tasks that are not yet taken
from
+   * the {@linkplain ExecutableTasksLatch}
+   */
+  public Collection<ExecutableTask<C>> getExecutableTasks() {
+    return ImmutableList.copyOf(executableTasks);
+  }
+
+  /**
+   * submits a task for execution by {@linkplain ExecutorService}
+   * @param callable task to execute
+   * @return newly created instance of {@linkplain ExecutableTask}
+   */
+  public ExecutableTask<C> execute(C callable) {
+    ExecutableTasksLatch.ExecutableTask<C> task = new ExecutableTasksLatch.ExecutableTask<>(callable,
this);
+    executor.execute(task);
+    count.getAndIncrement();
+    if (!executableTasks.add(task)) {
+      task.cancel(true);
+      throw new IllegalStateException();
+    }
+    return task;
+  }
+
+  /**
+   * Wait for completion of all tasks (cancelling them if cancel returns true)
+   * @param cancel BooleanSupplier that should return <tt>true</tt> when tasks
cancellation is requested
+   */
+  public void await(BooleanSupplier cancel) {
+    Preconditions.checkState(Thread.currentThread() == thread,
+        String.format("%s can be awaited only from the %s thread", getClass().getSimpleName(),
thread.getName()));
+    boolean cancelled = false;
+    while (count.get() > 0) {
+      if (!cancelled && cancel.getAsBoolean()) {
+        executableTasks.forEach(task -> task.cancel(true));
+        cancelled = true;
+      } else {
+        LockSupport.park();
+      }
+    }
+  }
+
+  /**
+   * Helper class to wrap {@linkplain Callable}{@literal <Void>} with cancellation
and waiting for completion support
+   *
+   */
+  public static final class ExecutableTask<C extends Callable<Void>> implements
Runnable {
 
 Review comment:
   Please see my comment for `ExecutableTasksLatch.await()`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message