dlmarion commented on a change in pull request #1818: URL: https://github.com/apache/accumulo/pull/1818#discussion_r536897371
########## File path: core/src/main/java/org/apache/accumulo/core/util/ThreadPools.java ########## @@ -0,0 +1,368 @@ +/* + * 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.core.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.OptionalInt; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.accumulo.core.conf.AccumuloConfiguration; +import org.apache.accumulo.core.conf.Property; +import org.apache.accumulo.core.util.Threads.NamedRunnable; +import org.apache.htrace.wrappers.TraceCallable; +import org.apache.htrace.wrappers.TraceRunnable; + +public class ThreadPools { + + /** + * ThreadFactory that sets the name and optionally the priority on a newly created Thread. + */ + private static class NamedThreadFactory implements ThreadFactory { + + private static final String FORMAT = "%s-%s-%d"; + + private AtomicInteger threadNum = new AtomicInteger(1); + private String name; + private OptionalInt priority; + + private NamedThreadFactory(String name) { + this(name, OptionalInt.empty()); + } + + private NamedThreadFactory(String name, OptionalInt priority) { + this.name = name; + this.priority = priority; + } + + @Override + public Thread newThread(Runnable r) { + String threadName = null; + if (r instanceof NamedRunnable) { + NamedRunnable nr = (NamedRunnable) r; + threadName = String.format(FORMAT, name, nr.getName(), threadNum.getAndIncrement()); + } else { + threadName = + String.format(FORMAT, name, r.getClass().getSimpleName(), threadNum.getAndIncrement()); + } + return Threads.createThread(threadName, priority, r); + } + } + + /** + * ScheduledThreadPoolExecutor that traces executed tasks. + */ + public static class TracingScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor { + + private TracingScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) { + super(corePoolSize, threadFactory); + } + + @Override + public void execute(Runnable command) { + super.execute(new TraceRunnable(command)); + } + + @Override + public <T> Future<T> submit(Callable<T> task) { + return super.submit(new TraceCallable<T>(task)); + } + + @Override + public <T> Future<T> submit(Runnable task, T result) { + return super.submit(new TraceRunnable(task), result); + } + + @Override + public Future<?> submit(Runnable task) { + return super.submit(new TraceRunnable(task)); + } + + private <T> Collection<? extends Callable<T>> + wrapCollection(Collection<? extends Callable<T>> tasks) { + List<Callable<T>> result = new ArrayList<Callable<T>>(tasks.size()); + tasks.forEach(t -> result.add(new TraceCallable<T>(t))); + return result; + } + + @Override + public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) + throws InterruptedException { + return super.invokeAll(wrapCollection(tasks)); + } + + @Override + public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, + TimeUnit unit) throws InterruptedException { + return super.invokeAll(wrapCollection(tasks), timeout, unit); + } + + @Override + public <T> T invokeAny(Collection<? extends Callable<T>> tasks) + throws InterruptedException, ExecutionException { + return super.invokeAny(wrapCollection(tasks)); + } + + @Override + public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) + throws InterruptedException, ExecutionException, TimeoutException { + return super.invokeAny(wrapCollection(tasks), timeout, unit); + } + + @Override + public void shutdown() { + super.shutdown(); + } + + @Override + public List<Runnable> shutdownNow() { + return super.shutdownNow(); + } + + } + + /** + * ThreadPoolExecutor that traces executed tasks. + */ + public static class TracingThreadPoolExecutor extends ThreadPoolExecutor { + + private TracingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, + TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); + } + + @Override + public void execute(Runnable command) { + super.execute(new TraceRunnable(command)); + } + + @Override + public <T> Future<T> submit(Callable<T> task) { + return super.submit(new TraceCallable<T>(task)); + } + + @Override + public <T> Future<T> submit(Runnable task, T result) { + return super.submit(new TraceRunnable(task), result); + } + + @Override + public Future<?> submit(Runnable task) { + return super.submit(new TraceRunnable(task)); + } + + private <T> Collection<? extends Callable<T>> + wrapCollection(Collection<? extends Callable<T>> tasks) { + List<Callable<T>> result = new ArrayList<Callable<T>>(tasks.size()); + tasks.forEach(t -> result.add(new TraceCallable<T>(t))); + return result; + } + + @Override + public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) + throws InterruptedException { + return super.invokeAll(wrapCollection(tasks)); + } + + @Override + public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, + TimeUnit unit) throws InterruptedException { + return super.invokeAll(wrapCollection(tasks), timeout, unit); + } + + @Override + public <T> T invokeAny(Collection<? extends Callable<T>> tasks) + throws InterruptedException, ExecutionException { + return super.invokeAny(wrapCollection(tasks)); + } + + @Override + public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) + throws InterruptedException, ExecutionException, TimeoutException { + return super.invokeAny(wrapCollection(tasks), timeout, unit); + } + + @Override + public void shutdown() { + super.shutdown(); + } + + @Override + public List<Runnable> shutdownNow() { + return super.shutdownNow(); + } Review comment: Yeah, these were leftovers from an earlier implementation idea. ---------------------------------------------------------------- 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: [email protected]
