From notifications-return-42710-archive-asf-public=cust-asf.ponee.io@accumulo.apache.org Tue Mar 27 19:54:14 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 6791418064E for ; Tue, 27 Mar 2018 19:54:13 +0200 (CEST) Received: (qmail 19482 invoked by uid 500); 27 Mar 2018 17:54:12 -0000 Mailing-List: contact notifications-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jira@apache.org Delivered-To: mailing list notifications@accumulo.apache.org Received: (qmail 19471 invoked by uid 99); 27 Mar 2018 17:54:12 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Mar 2018 17:54:12 +0000 From: GitBox To: notifications@accumulo.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 Message-ID: <152217325191.25025.11323077868641496750.gitbox@gitbox.apache.org> Date: Tue, 27 Mar 2018 17:54:11 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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_r177515479 ########## File path: server/master/src/main/java/org/apache/accumulo/master/TimeoutTaskExecutor.java ########## @@ -0,0 +1,235 @@ +/* + * 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.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +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; + +import javax.annotation.concurrent.NotThreadSafe; + +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. + * + * 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 The return type for the corresponding Callable. + * @param The type of Callable submitted to this executor. + */ +@NotThreadSafe +public class TimeoutTaskExecutor> implements AutoCloseable { + + private final static Logger log = LoggerFactory.getLogger(TimeoutTaskExecutor.class); + + private final long timeout; + private final ExecutorService executorService; + private final BlockingQueue startedTasks; + private final List wrappedTasks; + + private SuccessCallback successCallback; + private ExceptionCallback exceptionCallback; + private TimeoutCallback 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. + *

+ * 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 timeout The timeout for each task. + * @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 timeout, 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.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); + wt.future = executorService.submit(wt); + 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 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 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 timeoutCallback) { + this.timeoutCallback = Objects.requireNonNull(timeoutCallback, "Must provide a non-null timeoutCallback."); + } + + /** + * Completes all the current tasks by dispatching to the appropriate 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); + + while (hasUnfinishedTasks()) { + completeTask(startedTasks.take()); + } + + wrappedTasks.clear(); + } + + private boolean hasUnfinishedTasks() { + for (WrappedTask wt : wrappedTasks) { + if (!wt.hasCompleted) { + return true; + } + } + return false; + } + + private void completeTask(WrappedTask wt) throws InterruptedException { + try { + long waitTime = wt.endTime - System.currentTimeMillis(); Review comment: Will update to nanoTime(). Thanks for the info guys. Learned something new :) ---------------------------------------------------------------- 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