Skip to content

Commit ab2dbb4

Browse files
ghislainpiotsonartech
authored andcommitted
SONARPY-3143 Switch from ForkJoinPool to Executor (#352)
GitOrigin-RevId: 42ee9076a294a99636a7799ddb355fdd1e5a0b31
1 parent dcbd65e commit ab2dbb4

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

python-commons/src/main/java/org/sonar/plugins/python/Scanner.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import com.sonar.sslr.api.RecognitionException;
2020
import java.io.IOException;
2121
import java.util.List;
22-
import java.util.concurrent.ForkJoinPool;
22+
import java.util.concurrent.CompletableFuture;
23+
import java.util.concurrent.CompletionException;
24+
import java.util.concurrent.Executors;
2325
import java.util.concurrent.atomic.AtomicInteger;
2426
import java.util.stream.Stream;
2527
import org.slf4j.Logger;
@@ -57,21 +59,32 @@ protected void processFiles(List<PythonInputFile> files, SensorContext context,
5759
getFilesStream(files).forEach(file -> processFile(context, file, progressReport, numScannedWithoutParsing));
5860
return;
5961
}
60-
var pool = new ForkJoinPool(numberOfThreads);
62+
var executor = Executors.newWorkStealingPool(numberOfThreads);
6163
try {
62-
pool.submit(() -> getFilesStream(files).forEach(file -> processFile(context, file, progressReport, numScannedWithoutParsing)))
63-
.join();
64+
var allTasks = CompletableFuture.allOf(
65+
files.stream()
66+
.map(file -> CompletableFuture.runAsync(() -> processFile(context, file, progressReport, numScannedWithoutParsing), executor))
67+
.toArray(CompletableFuture[]::new)
68+
);
69+
allTasks.join();
70+
} catch (CompletionException e) {
71+
var cause = e.getCause();
72+
if (cause instanceof RuntimeException runtimeException) {
73+
throw runtimeException;
74+
} else if (cause instanceof Error error) {
75+
throw error;
76+
} else {
77+
throw e;
78+
}
6479
} finally {
65-
pool.shutdown();
80+
executor.shutdown();
6681
}
6782
}
6883

6984
protected abstract void logStart(int numThreads);
7085

7186
protected Stream<PythonInputFile> getFilesStream(List<PythonInputFile> files) {
72-
return getNumberOfThreads(context) == 1
73-
? files.stream()
74-
: files.parallelStream();
87+
return files.stream();
7588
}
7689

7790
private void processFile(SensorContext context, PythonInputFile file, MultiFileProgressReport progressReport, AtomicInteger numScannedWithoutParsing) {

python-commons/src/test/java/org/sonar/plugins/python/PythonSensorTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,10 +693,7 @@ void test_exception_should_fail_analysis_if_configured_so() throws IOException {
693693

694694
assertThatThrownBy(() -> sensor.execute(context))
695695
.isInstanceOf(IllegalStateException.class)
696-
.hasCauseInstanceOf(IllegalStateException.class)
697-
.extracting(Throwable::getCause)
698-
.extracting(Throwable::getCause)
699-
.isInstanceOf(FileNotFoundException.class);
696+
.hasCauseInstanceOf(FileNotFoundException.class);
700697
}
701698

702699
@Test

0 commit comments

Comments
 (0)