Skip to content

Commit af10791

Browse files
committed
parallelize projects index check
fixes #4244
1 parent 7b70c22 commit af10791

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

opengrok-web/src/main/java/org/opengrok/web/WebappListener.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434
import org.opengrok.indexer.analysis.AnalyzerGuru;
3535
import org.opengrok.indexer.authorization.AuthorizationFramework;
3636
import org.opengrok.indexer.configuration.CommandTimeoutType;
37+
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
3738
import org.opengrok.indexer.configuration.Project;
3839
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3940
import org.opengrok.indexer.index.IndexCheck;
4041
import org.opengrok.indexer.index.IndexDatabase;
4142
import org.opengrok.indexer.logger.LoggerFactory;
43+
import org.opengrok.indexer.util.Statistics;
4244
import org.opengrok.indexer.web.SearchHelper;
4345
import org.opengrok.web.api.ApiTaskManager;
4446
import org.opengrok.web.api.v1.controller.ConfigurationController;
@@ -51,6 +53,9 @@
5153
import java.time.Duration;
5254
import java.time.Instant;
5355
import java.util.Map;
56+
import java.util.concurrent.ExecutorService;
57+
import java.util.concurrent.Executors;
58+
import java.util.concurrent.TimeUnit;
5459
import java.util.logging.Level;
5560
import java.util.logging.Logger;
5661

@@ -146,18 +151,34 @@ private void checkIndex(RuntimeEnvironment env) {
146151
Path indexRoot = Path.of(env.getDataRootPath(), IndexDatabase.INDEX_DIR);
147152
if (indexRoot.toFile().exists()) {
148153
LOGGER.log(Level.FINE, "Checking indexes for all projects");
154+
Statistics statistics = new Statistics();
155+
ExecutorService executor = Executors.newFixedThreadPool(env.getRepositoryInvalidationParallelism(),
156+
new OpenGrokThreadFactory("webapp-index-check"));
149157
for (Map.Entry<String, Project> projectEntry : projects.entrySet()) {
150-
try {
151-
IndexCheck.checkDir(Path.of(indexRoot.toString(), projectEntry.getKey()),
152-
IndexCheck.IndexCheckMode.VERSION, projectEntry.getKey());
153-
} catch (Exception e) {
154-
LOGGER.log(Level.WARNING,
155-
String.format("Project %s index check failed, marking as not indexed",
156-
projectEntry.getKey()), e);
157-
projectEntry.getValue().setIndexed(false);
158+
executor.submit(() -> {
159+
try {
160+
IndexCheck.checkDir(Path.of(indexRoot.toString(), projectEntry.getKey()),
161+
IndexCheck.IndexCheckMode.VERSION, projectEntry.getKey());
162+
} catch (Exception e) {
163+
LOGGER.log(Level.WARNING,
164+
String.format("Project %s index check failed, marking as not indexed",
165+
projectEntry.getKey()), e);
166+
projectEntry.getValue().setIndexed(false);
167+
}
168+
});
169+
}
170+
executor.shutdown();
171+
try {
172+
// TODO: make the timeout tunable
173+
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
174+
LOGGER.log(Level.WARNING, "index check took more than 60 seconds");
175+
executor.shutdownNow();
158176
}
177+
} catch (InterruptedException e) {
178+
LOGGER.log(Level.WARNING, "failed to await termination of index check");
179+
executor.shutdownNow();
159180
}
160-
LOGGER.log(Level.FINE, "Index check for all projects done");
181+
statistics.report(LOGGER, Level.FINE, "Index check for all projects done");
161182
}
162183
} else {
163184
LOGGER.log(Level.FINE, "Checking index");

0 commit comments

Comments
 (0)