ctubbsii commented on a change in pull request #1818: URL: https://github.com/apache/accumulo/pull/1818#discussion_r536526096
########## 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; Review comment: Let's move this to a subpackage of util. Util is pretty bloated, and in a subpackage, we can break out all the inner classes. Inner classes make it hard to follow the code. package-private classes would be better than private inner classes. ########## 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: There's a few of these methods that don't do anything other than call the superclass implementation. These overriding methods can be removed, so the caller of these methods can call the superclass' implementation directly. ########## 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(); + } + + } + + public static class CloseableThreadPoolExecutor implements AutoCloseable { + + private final ThreadPoolExecutor tpe; + + public CloseableThreadPoolExecutor(ThreadPoolExecutor tpe) { + this.tpe = tpe; + } + + @Override + public void close() throws Exception { + this.tpe.shutdownNow(); + } + + } Review comment: Rather than create this new type, you can just have a different cleanup method in CleanerUtil that calls shutdownNow instead of close on the argument. It should be a much smaller change... and fewer types is good. I can help with this part, if you're not sure what I mean. ########## 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(); + } + + } + + public static class CloseableThreadPoolExecutor implements AutoCloseable { + + private final ThreadPoolExecutor tpe; + + public CloseableThreadPoolExecutor(ThreadPoolExecutor tpe) { + this.tpe = tpe; + } + + @Override + public void close() throws Exception { + this.tpe.shutdownNow(); + } + + } + + // the number of seconds before we allow a thread to terminate with non-use. + public static final long DEFAULT_TIMEOUT_MILLISECS = 180000L; + + /** + * Get a thread pool based on a thread pool related property + * + * @param conf + * accumulo configuration + * @param p + * thread pool related property + * @return ExecutorService impl + * @throws RuntimeException + * if property is not handled + */ + public static ExecutorService getExecutorService(AccumuloConfiguration conf, Property p) { Review comment: This method, and how you based it on the property might be my favorite piece of code this year. :smiley_cat: This was a great way to bring all the various implementations into one centralized place. ---------------------------------------------------------------- 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]
