]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10078 Logs ordering is messed up in case of exception during file indexing
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 29 Dec 2017 10:36:20 +0000 (11:36 +0100)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 29 Dec 2017 14:10:50 +0000 (15:10 +0100)
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java

index e0dc7eab38b3d150b26365f83fabf6ff2759bd8d..f41ab2797757df491e0358154f7b7d26c3a5e6d6 100644 (file)
@@ -100,7 +100,10 @@ public class FileIndexer {
 
   public void index() {
     int threads = Math.max(1, Runtime.getRuntime().availableProcessors() - 1);
-    this.executorService = Executors.newFixedThreadPool(threads, new ThreadFactoryBuilder().setNameFormat("FileIndexer-%d").build());
+    this.executorService = Executors.newFixedThreadPool(threads, new ThreadFactoryBuilder()
+      .setNameFormat("FileIndexer-%d")
+      .setDaemon(true)
+      .build());
 
     progressReport = new ProgressReport("Report about progress of file indexation", TimeUnit.SECONDS.toMillis(10));
     progressReport.start("Index files");
@@ -111,7 +114,7 @@ public class FileIndexer {
     indexFiles(moduleFileSystemInitializer.sources(), InputFile.Type.MAIN, progress);
     indexFiles(moduleFileSystemInitializer.tests(), InputFile.Type.TEST, progress);
 
-    waitForTasksToComplete();
+    waitForTasksToComplete(progressReport);
 
     progressReport.stop(progress.count() + " " + pluralizeFiles(progress.count()) + " indexed");
 
@@ -120,20 +123,32 @@ public class FileIndexer {
     }
   }
 
-  private void waitForTasksToComplete() {
+  private void waitForTasksToComplete(ProgressReport report) {
     executorService.shutdown();
     for (Future<Void> task : tasks) {
       try {
         task.get();
       } catch (ExecutionException e) {
         // Unwrap ExecutionException
+        stopAsap(report);
         throw e.getCause() instanceof RuntimeException ? (RuntimeException) e.getCause() : new IllegalStateException(e.getCause());
       } catch (InterruptedException e) {
+        stopAsap(report);
         throw new IllegalStateException(e);
       }
     }
   }
 
+  private void stopAsap(ProgressReport report) {
+    report.stop(null);
+    executorService.shutdownNow();
+    try {
+      executorService.awaitTermination(5, TimeUnit.SECONDS);
+    } catch (InterruptedException e1) {
+      // ignore, what's important is the original exception
+    }
+  }
+
   private static String pluralizeFiles(int count) {
     return count == 1 ? "file" : "files";
   }
index 82cc47a9b05719436c07532269e0a5df876f92f0..a0e282af132f225772afdecfc354fedc1343a964 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.scanner.util;
 
+import javax.annotation.Nullable;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 
@@ -28,7 +29,7 @@ public class ProgressReport implements Runnable {
   private final long period;
   private String message = "";
   private final Thread thread;
-  private String stopMessage = "";
+  private String stopMessage = null;
 
   public ProgressReport(String threadName, long period) {
     this.period = period;
@@ -47,7 +48,9 @@ public class ProgressReport implements Runnable {
         break;
       }
     }
-    log(stopMessage);
+    if (stopMessage != null) {
+      log(stopMessage);
+    }
   }
 
   public void start(String startMessage) {
@@ -59,7 +62,7 @@ public class ProgressReport implements Runnable {
     this.message = message;
   }
 
-  public void stop(String stopMessage) {
+  public void stop(@Nullable String stopMessage) {
     this.stopMessage = stopMessage;
     thread.interrupt();
     try {