keith-turner commented on a change in pull request #402: ACCUMULO-4615: Updated 
get status for thread safety and with a per-task timeout
URL: https://github.com/apache/accumulo/pull/402#discussion_r175576157
 
 

 ##########
 File path: 
server/master/src/main/java/org/apache/accumulo/master/TimeoutTaskExecutor.java
 ##########
 @@ -0,0 +1,198 @@
+/*
+ * 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.accumulo.master;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * Runs one or more tasks with a timeout per task (instead of a timeout for 
the entire pool). Uses callbacks to invoke functions on successful, timed out, 
or
+ * tasks that error.
+ *
+ * @param <T>
+ *          The return type for the corresponding Callable.
+ * @param <C>
+ *          The type of Callable submitted to this executor.
+ */
+public class TimeoutTaskExecutor<T,C extends Callable<T>> implements 
AutoCloseable {
+
+  private final static Logger log = 
LoggerFactory.getLogger(TimeoutTaskExecutor.class);
+
+  private final long timeout;
+  private final ExecutorService executorService;
+  private final List<WrappedTask> wrappedTasks;
+
+  private SuccessCallback<T,C> successCallback;
+  private ExceptionCallback<C> exceptionCallback;
+  private TimeoutCallback<C> timeoutCallback;
+
+  /**
+   * Constructs a new TimeoutTaskExecutor using the given executor to schedule 
tasks with a max timeout. Takes an expected number of Callables to initialize 
the
+   * underlying task collection more appropriately.
+   *
+   * @param executorService
+   *          The executor to schedule tasks with.
+   * @param timeout
+   *          The timeout for each task.
+   * @param expectedNumCallables
+   *          The expected number of callables you will schedule (for sizing 
optimization).
+   */
+  public TimeoutTaskExecutor(ExecutorService executorService, long timeout, 
int expectedNumCallables) {
+    this.executorService = executorService;
+    this.timeout = timeout;
+    this.wrappedTasks = new ArrayList<>(expectedNumCallables);
+  }
+
+  /**
+   * Submits a new task to the executor.
+   *
+   * @param callable
+   *          Task to run
+   */
+  public void submit(C callable) {
+    WrappedTask wt = new WrappedTask(callable, 
executorService.submit(callable));
+    wrappedTasks.add(wt);
+  }
+
+  /**
+   * Registers the callback to use on successful tasks.
+   *
+   * @param successCallback
+   *          The callback function to invoke on success.
+   * @throws NullPointerException
+   *           when a null successCallback is passed in
+   */
+  public void onSuccess(SuccessCallback<T,C> successCallback) {
+    this.successCallback = Objects.requireNonNull(successCallback, "Must 
provide a non-null successCallback.");
+  }
+
+  /**
+   * Registers the callback to use on tasks that throw exceptions.
+   *
+   * @param exceptionCallback
+   *          The callback function to invoke on exceptions.
+   * @throws NullPointerException
+   *           when a null exceptionCallback is passed in
+   */
+  public void onException(ExceptionCallback<C> exceptionCallback) {
+    this.exceptionCallback = Objects.requireNonNull(exceptionCallback, "Must 
provide a non-null exceptionCallback.");
+  }
+
+  /**
+   * Registers the callback to use on tasks that time out.
+   *
+   * @param timeoutCallback
+   *          The callback function to invoke on timeouts.
+   * @throws NullPointerException
+   *           when a null timeoutCallback is passed in
+   */
+  public void onTimeout(TimeoutCallback<C> timeoutCallback) {
+    this.timeoutCallback = Objects.requireNonNull(timeoutCallback, "Must 
provide a non-null timeoutCallback.");
+  }
+
+  /**
+   * Completes all the current tasks by dispatching to the appropriated 
callback.
+   *
+   * @throws IllegalStateException
+   *           If all of the callbacks were not registered before calling this 
method.
+   * @throws InterruptedException
+   *           If interrupted while awaiting callable results.
+   */
+  public void complete() throws InterruptedException {
+    Preconditions.checkState(successCallback != null, "Must set a success 
callback before completing " + this);
+    Preconditions.checkState(exceptionCallback != null, "Must set an exception 
callback before completing " + this);
+    Preconditions.checkState(timeoutCallback != null, "Must set a timeout 
callback before completing " + this);
+
+    for (WrappedTask wt : wrappedTasks) {
+      try {
+        successCallback.accept(wt.callable, wt.future.get(timeout, 
TimeUnit.MILLISECONDS));
+      } catch (InterruptedException e) {
+        throw e;
+      } catch (TimeoutException e) {
+        wt.future.cancel(true);
+        timeoutCallback.accept(wt.callable);
 
 Review comment:
   Its possible a future that is queued to execute, but has not yet executed 
could be canceled.  And if this happens 3 times for a tserver, the master will 
try to halt it.

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


With regards,
Apache Git Services

Reply via email to