From: Duarte Meneses Date: Fri, 29 Dec 2017 10:36:20 +0000 (+0100) Subject: SONAR-10078 Logs ordering is messed up in case of exception during file indexing X-Git-Tag: 7.0-RC1~85 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=49f29073c935c97e52d4f7d2a8e02e79391e3ff2;p=sonarqube.git SONAR-10078 Logs ordering is messed up in case of exception during file indexing --- diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java index e0dc7eab38b..f41ab279775 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java @@ -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 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"; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java index 82cc47a9b05..a0e282af132 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java @@ -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 {