Repository: aurora Updated Branches: refs/heads/master bf319ffef -> 64c00f105
Use guava replacement for ExecutorServiceShutdown. Reviewed at https://reviews.apache.org/r/38038/ Project: http://git-wip-us.apache.org/repos/asf/aurora/repo Commit: http://git-wip-us.apache.org/repos/asf/aurora/commit/64c00f10 Tree: http://git-wip-us.apache.org/repos/asf/aurora/tree/64c00f10 Diff: http://git-wip-us.apache.org/repos/asf/aurora/diff/64c00f10 Branch: refs/heads/master Commit: 64c00f105a1eab1e458c0edfc8b062251dc8452d Parents: bf319ff Author: Bill Farner <[email protected]> Authored: Tue Sep 1 20:54:11 2015 -0700 Committer: Bill Farner <[email protected]> Committed: Tue Sep 1 20:54:11 2015 -0700 ---------------------------------------------------------------------- .../concurrent/ExecutorServiceShutdown.java | 71 -------------------- .../scheduler/storage/log/LogStorage.java | 15 ++++- .../aurora/scheduler/app/SchedulerIT.java | 6 +- .../aurora/scheduler/app/local/FakeMaster.java | 6 +- .../storage/AbstractTaskStoreTest.java | 9 ++- .../storage/mem/StorageTransactionTest.java | 12 +--- 6 files changed, 23 insertions(+), 96 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aurora/blob/64c00f10/commons/src/main/java/org/apache/aurora/common/util/concurrent/ExecutorServiceShutdown.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/util/concurrent/ExecutorServiceShutdown.java b/commons/src/main/java/org/apache/aurora/common/util/concurrent/ExecutorServiceShutdown.java deleted file mode 100644 index 7aad9ef..0000000 --- a/commons/src/main/java/org/apache/aurora/common/util/concurrent/ExecutorServiceShutdown.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Licensed 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.aurora.common.util.concurrent; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.logging.Logger; - -import com.google.common.base.Preconditions; - -import org.apache.aurora.common.base.Command; -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; - -/** - * An implementation of the graceful shutdown sequence recommended by {@link ExecutorService}. - * - * @author John Sirois - */ -public class ExecutorServiceShutdown implements Command { - private static final Logger LOG = Logger.getLogger(ExecutorServiceShutdown.class.getName()); - - private final ExecutorService executor; - private final Amount<Long, Time> gracePeriod; - - /** - * Creates a new {@code ExecutorServiceShutdown} command that will try to gracefully shut down the - * given {@code executor} when executed. If the supplied grace period is less than or equal to - * zero the executor service will be asked to shut down but no waiting will be done after these - * requests. - * - * @param executor The executor service this command should shut down when executed. - * @param gracePeriod The maximum time to wait after a shutdown request before continuing to the - * next shutdown phase. - */ - public ExecutorServiceShutdown(ExecutorService executor, Amount<Long, Time> gracePeriod) { - this.executor = Preconditions.checkNotNull(executor); - this.gracePeriod = Preconditions.checkNotNull(gracePeriod); - } - - @Override - public void execute() { - executor.shutdown(); // Disable new tasks from being submitted. - try { - // Wait a while for existing tasks to terminate. - if (!executor.awaitTermination(gracePeriod.as(Time.MILLISECONDS), TimeUnit.MILLISECONDS)) { - executor.shutdownNow(); // Cancel currently executing tasks. - // Wait a while for tasks to respond to being cancelled. - if (!executor.awaitTermination(gracePeriod.as(Time.MILLISECONDS), TimeUnit.MILLISECONDS)) { - LOG.warning("Pool did not terminate"); - } - } - } catch (InterruptedException ie) { - // (Re-)Cancel if current thread also interrupted. - executor.shutdownNow(); - // Preserve interrupt status. - Thread.currentThread().interrupt(); - } - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/64c00f10/src/main/java/org/apache/aurora/scheduler/storage/log/LogStorage.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/aurora/scheduler/storage/log/LogStorage.java b/src/main/java/org/apache/aurora/scheduler/storage/log/LogStorage.java index ad78432..adbf459 100644 --- a/src/main/java/org/apache/aurora/scheduler/storage/log/LogStorage.java +++ b/src/main/java/org/apache/aurora/scheduler/storage/log/LogStorage.java @@ -27,15 +27,16 @@ import javax.inject.Inject; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; +import com.google.common.util.concurrent.MoreExecutors; import org.apache.aurora.codec.ThriftBinaryCodec.CodingException; import org.apache.aurora.common.application.ShutdownRegistry; import org.apache.aurora.common.base.Closure; +import org.apache.aurora.common.base.Command; import org.apache.aurora.common.inject.TimedInterceptor.Timed; import org.apache.aurora.common.quantity.Amount; import org.apache.aurora.common.quantity.Time; import org.apache.aurora.common.stats.SlidingStats; -import org.apache.aurora.common.util.concurrent.ExecutorServiceShutdown; import org.apache.aurora.gen.HostAttributes; import org.apache.aurora.gen.JobUpdate; import org.apache.aurora.gen.storage.LogEntry; @@ -157,8 +158,16 @@ public class LogStorage implements NonVolatileStorage, DistributedSnapshotStore ScheduledExecutorSchedulingService(ShutdownRegistry shutdownRegistry, Amount<Long, Time> shutdownGracePeriod) { scheduledExecutor = AsyncUtil.singleThreadLoggingScheduledExecutor("LogStorage-%d", LOG); - shutdownRegistry.addAction( - new ExecutorServiceShutdown(scheduledExecutor, shutdownGracePeriod)); + shutdownRegistry.addAction(new Command() { + + @Override + public void execute() throws RuntimeException { + MoreExecutors.shutdownAndAwaitTermination( + scheduledExecutor, + shutdownGracePeriod.getValue(), + shutdownGracePeriod.getUnit().getTimeUnit()); + } + }); } @Override http://git-wip-us.apache.org/repos/asf/aurora/blob/64c00f10/src/test/java/org/apache/aurora/scheduler/app/SchedulerIT.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/aurora/scheduler/app/SchedulerIT.java b/src/test/java/org/apache/aurora/scheduler/app/SchedulerIT.java index 1a25924..b2ec13f 100644 --- a/src/test/java/org/apache/aurora/scheduler/app/SchedulerIT.java +++ b/src/test/java/org/apache/aurora/scheduler/app/SchedulerIT.java @@ -35,6 +35,7 @@ import com.google.common.collect.Iterables; import com.google.common.hash.Hashing; import com.google.common.io.Files; import com.google.common.util.concurrent.Atomics; +import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.inject.AbstractModule; import com.google.inject.Guice; @@ -51,10 +52,8 @@ import org.apache.aurora.common.base.ExceptionalCommand; import org.apache.aurora.common.net.pool.DynamicHostSet.HostChangeMonitor; import org.apache.aurora.common.quantity.Amount; import org.apache.aurora.common.quantity.Data; -import org.apache.aurora.common.quantity.Time; import org.apache.aurora.common.stats.Stats; import org.apache.aurora.common.thrift.ServiceInstance; -import org.apache.aurora.common.util.concurrent.ExecutorServiceShutdown; import org.apache.aurora.common.zookeeper.ServerSet; import org.apache.aurora.common.zookeeper.ServerSetImpl; import org.apache.aurora.common.zookeeper.ZooKeeperClient; @@ -105,7 +104,6 @@ import org.junit.Before; import org.junit.Test; import static org.apache.aurora.common.testing.easymock.EasyMockTest.createCapture; - import static org.apache.mesos.Protos.FrameworkInfo; import static org.easymock.EasyMock.capture; import static org.easymock.EasyMock.createControl; @@ -244,7 +242,7 @@ public class SchedulerIT extends BaseZooKeeperTest { @Override public void tearDown() throws Exception { lifecycle.shutdown(); - new ExecutorServiceShutdown(executor, Amount.of(10L, Time.SECONDS)).execute(); + MoreExecutors.shutdownAndAwaitTermination(executor, 10, TimeUnit.SECONDS); } }); } http://git-wip-us.apache.org/repos/asf/aurora/blob/64c00f10/src/test/java/org/apache/aurora/scheduler/app/local/FakeMaster.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/aurora/scheduler/app/local/FakeMaster.java b/src/test/java/org/apache/aurora/scheduler/app/local/FakeMaster.java index 07ea19a..f8940ed 100644 --- a/src/test/java/org/apache/aurora/scheduler/app/local/FakeMaster.java +++ b/src/test/java/org/apache/aurora/scheduler/app/local/FakeMaster.java @@ -32,11 +32,9 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import com.google.common.eventbus.EventBus; import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; -import org.apache.aurora.common.util.concurrent.ExecutorServiceShutdown; import org.apache.aurora.scheduler.app.local.simulator.Events.Started; import org.apache.aurora.scheduler.mesos.DriverFactory; import org.apache.mesos.Protos; @@ -160,7 +158,7 @@ public class FakeMaster implements SchedulerDriver, DriverFactory { @Override public Status stop() { stopped.countDown(); - new ExecutorServiceShutdown(executor, Amount.of(1L, Time.SECONDS)).execute(); + MoreExecutors.shutdownAndAwaitTermination(executor, 1, TimeUnit.SECONDS); return Status.DRIVER_RUNNING; } http://git-wip-us.apache.org/repos/asf/aurora/blob/64c00f10/src/test/java/org/apache/aurora/scheduler/storage/AbstractTaskStoreTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/aurora/scheduler/storage/AbstractTaskStoreTest.java b/src/test/java/org/apache/aurora/scheduler/storage/AbstractTaskStoreTest.java index 2c03238..295974a 100644 --- a/src/test/java/org/apache/aurora/scheduler/storage/AbstractTaskStoreTest.java +++ b/src/test/java/org/apache/aurora/scheduler/storage/AbstractTaskStoreTest.java @@ -20,21 +20,20 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; +import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Module; -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; import org.apache.aurora.common.testing.TearDownTestCase; -import org.apache.aurora.common.util.concurrent.ExecutorServiceShutdown; import org.apache.aurora.gen.Attribute; import org.apache.aurora.gen.Container; import org.apache.aurora.gen.ExecutorConfig; @@ -597,7 +596,7 @@ public abstract class AbstractTaskStoreTest extends TearDownTestCase { read.await(); } finally { - new ExecutorServiceShutdown(executor, Amount.of(1L, Time.SECONDS)).execute(); + MoreExecutors.shutdownAndAwaitTermination(executor, 1, TimeUnit.SECONDS); } } @@ -612,7 +611,7 @@ public abstract class AbstractTaskStoreTest extends TearDownTestCase { ExecutorService executor = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setNameFormat("AsyncRead-%d").setDaemon(true).build()); - addTearDown(() -> new ExecutorServiceShutdown(executor, Amount.of(1L, Time.SECONDS)).execute()); + addTearDown(() -> MoreExecutors.shutdownAndAwaitTermination(executor, 1, TimeUnit.SECONDS)); saveTasks(TASK_A); storage.write(new Storage.MutateWork.NoResult<Exception>() { http://git-wip-us.apache.org/repos/asf/aurora/blob/64c00f10/src/test/java/org/apache/aurora/scheduler/storage/mem/StorageTransactionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/aurora/scheduler/storage/mem/StorageTransactionTest.java b/src/test/java/org/apache/aurora/scheduler/storage/mem/StorageTransactionTest.java index 947a50f..d40e75a 100644 --- a/src/test/java/org/apache/aurora/scheduler/storage/mem/StorageTransactionTest.java +++ b/src/test/java/org/apache/aurora/scheduler/storage/mem/StorageTransactionTest.java @@ -19,16 +19,15 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.ThreadFactoryBuilder; -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; import org.apache.aurora.common.testing.TearDownTestCase; -import org.apache.aurora.common.util.concurrent.ExecutorServiceShutdown; import org.apache.aurora.gen.ResourceAggregate; import org.apache.aurora.scheduler.base.Query; import org.apache.aurora.scheduler.base.TaskTestUtil; @@ -60,12 +59,7 @@ public class StorageTransactionTest extends TearDownTestCase { public void setUp() { executor = Executors.newCachedThreadPool( new ThreadFactoryBuilder().setNameFormat("SlowRead-%d").setDaemon(true).build()); - addTearDown(new TearDown() { - @Override - public void tearDown() { - new ExecutorServiceShutdown(executor, Amount.of(1L, Time.SECONDS)).execute(); - } - }); + addTearDown(() -> MoreExecutors.shutdownAndAwaitTermination(executor, 1, TimeUnit.SECONDS)); storage = DbUtil.createStorage(); }
