Indexing diagnostics: workaround concurrent modification of statistics.

GitOrigin-RevId: f0007c29fec330e315eb3f85fba750725fbb73a4
This commit is contained in:
Sergey Patrikeev
2021-02-18 22:38:10 +00:00
committed by intellij-monorepo-bot
parent bd31ae3093
commit e506aacce9
2 changed files with 15 additions and 2 deletions
@@ -330,6 +330,7 @@ public final class PushedFilePropertiesUpdaterImpl extends PushedFilePropertiesU
invokeConcurrentlyIfPossible(tasks);
}
// TODO: this method may return earlier than all spawned threads have completed.
public static void invokeConcurrentlyIfPossible(@NotNull List<? extends Runnable> tasks) {
if (tasks.isEmpty()) return;
if (tasks.size() == 1 || ApplicationManager.getApplication().isWriteAccessAllowed()) {
@@ -47,6 +47,7 @@ import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
public final class UnindexedFilesUpdater extends DumbModeTask {
@@ -332,6 +333,11 @@ public final class UnindexedFilesUpdater extends DumbModeTask {
ConcurrentTasksProgressManager concurrentTasksProgressManager = new ConcurrentTasksProgressManager(indicator, providers.size());
// Workaround for concurrent modification of the [projectIndexingHistory].
// PushedFilePropertiesUpdaterImpl.invokeConcurrentlyIfPossible may finish earlier than all its spawned tasks have completed.
// And some scanning statistics may be tried to be added to the [projectIndexingHistory],
// leading to ConcurrentModificationException in the statistics' processor.
AtomicBoolean allTasksFinished = new AtomicBoolean();
List<Runnable> tasks = ContainerUtil.map(providers, provider -> {
SubTaskProgressIndicator subTaskIndicator = concurrentTasksProgressManager.createSubTaskIndicator(1);
List<VirtualFile> files = new ArrayList<>();
@@ -374,14 +380,20 @@ public final class UnindexedFilesUpdater extends DumbModeTask {
finally {
scanningStatistics.setNumberOfSkippedFiles(thisProviderDeduplicateFilter.getNumberOfSkippedFiles());
synchronized (projectIndexingHistory) {
projectIndexingHistory.addScanningStatistics(scanningStatistics);
if (!allTasksFinished.get()) {
projectIndexingHistory.addScanningStatistics(scanningStatistics);
}
}
subTaskIndicator.finished();
}
};
});
LOG.info("Scanning: use " + getNumberOfScanningThreads() + " scanning threads");
PushedFilePropertiesUpdaterImpl.invokeConcurrentlyIfPossible(tasks);
try {
PushedFilePropertiesUpdaterImpl.invokeConcurrentlyIfPossible(tasks);
} finally {
allTasksFinished.set(true);
}
return providerToFiles;
}