]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10104 use fixed ThreadPool with timeout on core threads 2818/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Nov 2017 09:44:32 +0000 (10:44 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 24 Nov 2017 08:23:58 +0000 (09:23 +0100)
this strategy really allows to use multiple threads when queue is growing and little to no workers when its small

server/sonar-server/src/main/java/org/sonar/server/async/AsyncExecutionExecutorServiceImpl.java

index f329d52ec4029d72444950161273ee30f40e5ab5..fe3d4f83088a01c11bd48cc94bb5d9a899cda091 100644 (file)
@@ -26,29 +26,33 @@ import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.server.util.AbstractStoppableExecutorService;
 
-import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
 
 public class AsyncExecutionExecutorServiceImpl
   extends AbstractStoppableExecutorService<ThreadPoolExecutor>
   implements AsyncExecutionExecutorService, AsyncExecutionMonitoring {
   private static final Logger LOG = Loggers.get(AsyncExecutionExecutorServiceImpl.class);
 
-  private static final int MIN_THREAD_COUNT = 1;
   private static final int MAX_THREAD_COUNT = 10;
-  private static final int MAX_QUEUE_SIZE = Integer.MAX_VALUE;
-  private static final long KEEP_ALIVE_TIME_IN_MILLISECONDS = 0L;
+  private static final int UNLIMITED_QUEUE = Integer.MAX_VALUE;
+  private static final long KEEP_ALIVE_TIME_IN_MINUTES = 5L;
 
   public AsyncExecutionExecutorServiceImpl() {
-    super(
-      new ThreadPoolExecutor(
-        MIN_THREAD_COUNT, MAX_THREAD_COUNT,
-        KEEP_ALIVE_TIME_IN_MILLISECONDS, MILLISECONDS,
-        new LinkedBlockingQueue<>(MAX_QUEUE_SIZE),
-        new ThreadFactoryBuilder()
-          .setDaemon(false)
-          .setNameFormat("SQ_async-%d")
-          .setUncaughtExceptionHandler(((t, e) -> LOG.error("Thread " + t + " failed unexpectedly", e)))
-          .build()));
+    super(createDelegate());
+  }
+
+  private static ThreadPoolExecutor createDelegate() {
+    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
+      MAX_THREAD_COUNT, MAX_THREAD_COUNT,
+      KEEP_ALIVE_TIME_IN_MINUTES, MINUTES,
+      new LinkedBlockingQueue<>(UNLIMITED_QUEUE),
+      new ThreadFactoryBuilder()
+        .setDaemon(false)
+        .setNameFormat("SQ_async-%d")
+        .setUncaughtExceptionHandler(((t, e) -> LOG.error("Thread " + t + " failed unexpectedly", e)))
+        .build());
+    threadPoolExecutor.allowCoreThreadTimeOut(true);
+    return threadPoolExecutor;
   }
 
   @Override