accumulo-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] jschmidt10 commented on a change in pull request #402: ACCUMULO-4615: Updated get status for thread safety and with a per-task timeout
Date Thu, 12 Apr 2018 11:44:28 GMT
jschmidt10 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_r181051851
 
 

 ##########
 File path: server/master/src/main/java/org/apache/accumulo/master/TimeoutTaskExecutor.java
 ##########
 @@ -0,0 +1,249 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.concurrent.NotThreadSafe;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * 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.
+ *
+ * This class uses an underlying fixed thread pool to schedule the submitted tasks. Once
a task is submitted, the desired end time for the task is recorded and
+ * used to determine the timeout for the task's associated {@link Future}.
+ *
+ * The timeout will not be exact as the start time is recorded prior to submitting the {@link
Callable}. This may result in an effective timeout that is
+ * slightly smaller than expected. The timeout used during initialization should be adjusted
accordingly.
+ *
+ * The {@link TimeoutTaskExecutor} itself is not a thread-safe class. Only a single thread
should submit tasks and complete them.
+ *
+ * @param <T> The return type for the corresponding Callable.
+ * @param <C> The type of Callable submitted to this executor.
+ */
+@NotThreadSafe
+public class TimeoutTaskExecutor<T, C extends Callable<T>> implements AutoCloseable
{
+
+  private final static Logger log = LoggerFactory.getLogger(TimeoutTaskExecutor.class);
+
+  private final long timeoutInNanos;
+  private final ExecutorService executorService;
+  private final BlockingQueue<WrappedTask> startedTasks;
+  private final List<WrappedTask> wrappedTasks;
+
+  private SuccessCallback<T,C> successCallback;
+  private ExceptionCallback<C> exceptionCallback;
+  private TimeoutCallback<C> timeoutCallback;
+
+  /**
+   * Constructs a new TimeoutTaskExecutor that will use the given number of worker threads
and timeout. Takes an expected number of Callables to initialize the
+   * underlying data structures appropriately.
+   * <p>
+   * If the expectedNumCallables is sized too small, this executor will block on calls to
submit() once the internal queue is full.
+   *
+   * @param numThreads           The number of threads to use.
+   * @param timeoutInMillis      The timeout for each task in milliseconds.
+   * @param expectedNumCallables The expected number of callables you will schedule. Note
this is used for an underlying BlockingQueue. If sized too small this will cause blocking
+   *                             when calling submit().
+   * @throws IllegalArgumentException If numThreads is less than 1 or expectedNumCallables
is negative.
+   */
+  public TimeoutTaskExecutor(int numThreads, long timeoutInMillis, int expectedNumCallables)
{
+    Preconditions.checkArgument(numThreads >= 1, "Number of threads must be at least 1.");
+    Preconditions.checkArgument(expectedNumCallables >= 0, "The expected number of callables
must be non-negative.");
+
+    this.executorService = Executors.newFixedThreadPool(numThreads);
+    this.startedTasks = new ArrayBlockingQueue<>(expectedNumCallables);
+    this.timeoutInNanos = TimeUnit.MILLISECONDS.toNanos(timeoutInMillis);
+    this.wrappedTasks = new ArrayList<>(expectedNumCallables);
+  }
+
+  /**
+   * Submits a new task to the executor.
+   *
+   * @param callable Task to run
+   */
+  public void submit(C callable) {
 
 Review comment:
   Agreed. Will fix.

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