]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6366 add stopable ExecutorService component
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 7 Apr 2015 08:53:34 +0000 (10:53 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 17 Apr 2015 13:15:00 +0000 (15:15 +0200)
server/sonar-server/src/main/java/org/sonar/server/util/AbstractStoppableExecutorService.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/util/StoppableExecutorService.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/util/AbstractStoppableExecutorServiceTest.java [new file with mode: 0644]

diff --git a/server/sonar-server/src/main/java/org/sonar/server/util/AbstractStoppableExecutorService.java b/server/sonar-server/src/main/java/org/sonar/server/util/AbstractStoppableExecutorService.java
new file mode 100644 (file)
index 0000000..278eb46
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.util;
+
+import org.sonar.api.utils.log.Loggers;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import static java.lang.String.format;
+
+/**
+ * Abstract implementation of StoppableExecutorService that implements the
+ * stop() method and delegates all methods to the provided ExecutorService instance.
+ */
+public abstract class AbstractStoppableExecutorService implements StoppableExecutorService {
+  private final ExecutorService delegate;
+
+  public AbstractStoppableExecutorService(ExecutorService delegate) {
+    this.delegate = delegate;
+  }
+
+  @Override
+  public void stop() {
+    delegate.shutdown(); // Disable new tasks from being submitted
+    try {
+      // Wait a while for existing tasks to terminate
+      if (!delegate.awaitTermination(5, TimeUnit.SECONDS)) {
+        delegate.shutdownNow(); // Cancel currently executing tasks
+        // Wait a while for tasks to respond to being cancelled
+        if (!delegate.awaitTermination(5, TimeUnit.SECONDS)) {
+          Loggers.get(getClass()).error(format("Pool %s did not terminate", getClass().getSimpleName()));
+        }
+      }
+    } catch (InterruptedException ie) {
+      Loggers.get(getClass()).error(format("Termination of pool %s failed", getClass().getSimpleName()), ie);
+      // (Re-)Cancel if current thread also interrupted
+      delegate.shutdownNow();
+    }
+  }
+
+  @Override
+  public void shutdown() {
+    delegate.shutdown();
+  }
+
+  @Override
+  public List<Runnable> shutdownNow() {
+    return delegate.shutdownNow();
+  }
+
+  @Override
+  public boolean isShutdown() {
+    return delegate.isShutdown();
+  }
+
+  @Override
+  public boolean isTerminated() {
+    return delegate.isTerminated();
+  }
+
+  @Override
+  public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
+    return delegate.awaitTermination(timeout, unit);
+  }
+
+  @Override
+  public <T> Future<T> submit(Callable<T> task) {
+    return delegate.submit(task);
+  }
+
+  @Override
+  public <T> Future<T> submit(Runnable task, T result) {
+    return delegate.submit(task, result);
+  }
+
+  @Override
+  public Future<?> submit(Runnable task) {
+    return delegate.submit(task);
+  }
+
+  @Override
+  public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
+    return delegate.invokeAll(tasks);
+  }
+
+  @Override
+  public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
+    long timeout,
+    TimeUnit unit) throws InterruptedException {
+    return delegate.invokeAll(tasks, timeout, unit);
+  }
+
+  @Override
+  public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
+    return delegate.invokeAny(tasks);
+  }
+
+  @Override
+  public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
+    long timeout,
+    TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
+    return delegate.invokeAny(tasks, timeout, unit);
+  }
+
+  @Override
+  public void execute(Runnable command) {
+    delegate.execute(command);
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/util/StoppableExecutorService.java b/server/sonar-server/src/main/java/org/sonar/server/util/StoppableExecutorService.java
new file mode 100644 (file)
index 0000000..2d3bad0
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.util;
+
+import java.util.concurrent.ExecutorService;
+
+/**
+ * ExecutorService that exposes a {@code stop} method which can be invoked by Pico container to shutdown properly
+ * the service.
+ */
+public interface StoppableExecutorService extends ExecutorService {
+
+  /**
+   * Stops the ExecutorService nicely (ie. first let a little time for jobs to end and then abort them)
+   */
+  void stop();
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/util/AbstractStoppableExecutorServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/util/AbstractStoppableExecutorServiceTest.java
new file mode 100644 (file)
index 0000000..6f7b827
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.util;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.Test;
+import org.mockito.InOrder;
+import org.mockito.Mockito;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class AbstractStoppableExecutorServiceTest {
+  private static final Callable<String> SOME_CALLABLE = new Callable<String>() {
+    @Override
+    public String call() throws Exception {
+      return null;
+    }
+  };
+  private static final Runnable SOME_RUNNABLE = new Runnable() {
+    @Override
+    public void run() {
+
+    }
+  };
+  private static final String SOME_STRING = "some string";
+  private static final long SOME_LONG = 100l;
+  private static final int TIMEOUT = 5;
+  private static final TimeUnit TIMEOUT_UNIT = TimeUnit.SECONDS;
+
+  private ExecutorService executorService = mock(ExecutorService.class);
+  private InOrder inOrder = Mockito.inOrder(executorService);
+  private AbstractStoppableExecutorService underTest = new AbstractStoppableExecutorService(executorService) {
+  };
+  public static final ImmutableList<Callable<String>> CALLABLES = ImmutableList.of(SOME_CALLABLE);
+
+  @Test
+  public void stop_calls_shutdown_and_verify_termination() throws InterruptedException {
+    when(executorService.awaitTermination(TIMEOUT, TIMEOUT_UNIT)).thenReturn(true);
+
+    underTest.stop();
+
+    inOrder.verify(executorService).shutdown();
+    inOrder.verify(executorService).awaitTermination(TIMEOUT, TIMEOUT_UNIT);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void stop_calls_shutdown_then_shutdownNow_if_not_terminated_and_check_termination_again() throws InterruptedException {
+    when(executorService.awaitTermination(TIMEOUT, TIMEOUT_UNIT)).thenReturn(false).thenReturn(true);
+
+    underTest.stop();
+
+    inOrder.verify(executorService).shutdown();
+    inOrder.verify(executorService).awaitTermination(TIMEOUT, TIMEOUT_UNIT);
+    inOrder.verify(executorService).shutdownNow();
+    inOrder.verify(executorService).awaitTermination(TIMEOUT, TIMEOUT_UNIT);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void stop_calls_shutdownnow_if_interrupted_exception_is_raised() throws InterruptedException {
+    when(executorService.awaitTermination(TIMEOUT, TIMEOUT_UNIT)).thenThrow(new InterruptedException());
+
+    underTest.stop();
+
+    inOrder.verify(executorService).shutdown();
+    inOrder.verify(executorService).awaitTermination(TIMEOUT, TIMEOUT_UNIT);
+    inOrder.verify(executorService).shutdownNow();
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void shutdown_delegates_to_executorService() {
+    underTest.shutdown();
+
+    inOrder.verify(executorService).shutdown();
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void shutdownNow_delegates_to_executorService() {
+    underTest.shutdownNow();
+
+    inOrder.verify(executorService).shutdownNow();
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void isShutdown_delegates_to_executorService() {
+    underTest.isShutdown();
+
+    inOrder.verify(executorService).isShutdown();
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void isTerminated_delegates_to_executorService() {
+    underTest.isTerminated();
+
+    inOrder.verify(executorService).isTerminated();
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void awaitTermination_delegates_to_executorService() throws InterruptedException {
+    underTest.awaitTermination(SOME_LONG, SECONDS);
+
+    inOrder.verify(executorService).awaitTermination(SOME_LONG, SECONDS);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void submit_callable_delegates_to_executorService() {
+    underTest.submit(SOME_CALLABLE);
+
+    inOrder.verify(executorService).submit(SOME_CALLABLE);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void submit_runnable_delegates_to_executorService() {
+    underTest.submit(SOME_RUNNABLE);
+
+    inOrder.verify(executorService).submit(SOME_RUNNABLE);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void submit_runnable_with_result_delegates_to_executorService() {
+    underTest.submit(SOME_RUNNABLE, SOME_STRING);
+
+    inOrder.verify(executorService).submit(SOME_RUNNABLE, SOME_STRING);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void invokeAll_delegates_to_executorService() throws InterruptedException {
+    underTest.invokeAll(CALLABLES);
+
+    inOrder.verify(executorService).invokeAll(CALLABLES);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void invokeAll1_delegates_to_executorService() throws InterruptedException {
+    underTest.invokeAll(CALLABLES, SOME_LONG, SECONDS);
+
+    inOrder.verify(executorService).invokeAll(CALLABLES, SOME_LONG, SECONDS);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void invokeAny_delegates_to_executorService() throws ExecutionException, InterruptedException {
+    underTest.invokeAny(CALLABLES);
+
+    inOrder.verify(executorService).invokeAny(CALLABLES);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void invokeAny1_delegates_to_executorService() throws InterruptedException, ExecutionException, TimeoutException {
+    underTest.invokeAny(CALLABLES, SOME_LONG, SECONDS);
+
+    inOrder.verify(executorService).invokeAny(CALLABLES, SOME_LONG, SECONDS);
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void execute_delegates_to_executorService() {
+    underTest.execute(SOME_RUNNABLE);
+
+    inOrder.verify(executorService).execute(SOME_RUNNABLE);
+    inOrder.verifyNoMoreInteractions();
+  }
+}