Skip to content

Commit e2d0542

Browse files
authored
Merge pull request #1375 from vladak/parallel_invalidate
parallelize invalidateRepositories()
2 parents b1d8975 + b2324f4 commit e2d0542

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

src/org/opensolaris/opengrok/history/HistoryGuru.java

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -36,6 +36,8 @@
3636
import java.util.Set;
3737
import java.util.concurrent.CountDownLatch;
3838
import java.util.concurrent.ExecutorService;
39+
import java.util.concurrent.Executors;
40+
import java.util.concurrent.ThreadFactory;
3941
import java.util.concurrent.TimeUnit;
4042
import java.util.logging.Level;
4143
import java.util.logging.Logger;
@@ -764,34 +766,66 @@ public void invalidateRepositories(Collection<? extends RepositoryInfo> repos) {
764766
if (repos == null || repos.isEmpty()) {
765767
repositories.clear();
766768
} else {
767-
Map<String, Repository> newrepos
768-
= new HashMap<>(repos.size());
769+
Map<String, Repository> newrepos =
770+
Collections.synchronizedMap(new HashMap<>(repos.size()));
769771
Statistics elapsed = new Statistics();
770772
boolean verbose = RuntimeEnvironment.getInstance().isVerbose();
773+
771774
if (verbose) {
772775
LOGGER.log(Level.FINE, "invalidating repositories");
773776
}
777+
778+
/*
779+
* getRepository() below does various checks of the repository
780+
* which involves executing commands and I/O so make the checks
781+
* run in parallel to speed up the process.
782+
*/
783+
final CountDownLatch latch = new CountDownLatch(repos.size());
784+
final ExecutorService executor = Executors.newFixedThreadPool(
785+
Runtime.getRuntime().availableProcessors(),
786+
new ThreadFactory() {
787+
@Override
788+
public Thread newThread(Runnable runnable) {
789+
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
790+
thread.setName("invalidate-repos-" + thread.getId());
791+
return thread;
792+
}
793+
});
794+
774795
for (RepositoryInfo i : repos) {
775-
try {
776-
Repository r = RepositoryFactory.getRepository(i);
777-
if (r == null) {
778-
LOGGER.log(Level.WARNING,
779-
"Failed to instanciate internal repository data for {0} in {1}",
780-
new Object[]{i.getType(), i.getDirectoryName()});
781-
} else {
782-
newrepos.put(r.getDirectoryName(), r);
796+
executor.submit(new Runnable() {
797+
@Override
798+
public void run() {
799+
try {
800+
Repository r = RepositoryFactory.getRepository(i);
801+
if (r == null) {
802+
LOGGER.log(Level.WARNING,
803+
"Failed to instantiate internal repository data for {0} in {1}",
804+
new Object[]{i.getType(), i.getDirectoryName()});
805+
} else {
806+
newrepos.put(r.getDirectoryName(), r);
807+
}
808+
} catch (Exception ex) {
809+
// We want to catch any exception since we are in thread.
810+
LOGGER.log(Level.WARNING, "Could not create " + i.getType()
811+
+ " for '" + i.getDirectoryName(), ex);
812+
} finally {
813+
latch.countDown();
814+
}
783815
}
784-
} catch (InstantiationException ex) {
785-
LOGGER.log(Level.WARNING, "Could not create " + i.getType()
786-
+ " for '" + i.getDirectoryName()
787-
+ "', could not instantiate the repository.", ex);
788-
} catch (IllegalAccessException iae) {
789-
LOGGER.log(Level.WARNING, "Could not create " + i.getType()
790-
+ " for '" + i.getDirectoryName()
791-
+ "', missing access rights.", iae);
792-
}
816+
});
793817
}
818+
819+
// Wait until all repositories are validated.
820+
try {
821+
latch.await();
822+
} catch (InterruptedException ex) {
823+
LOGGER.log(Level.SEVERE, "latch exception{0}", ex);
824+
}
825+
executor.shutdown();
826+
794827
repositories = newrepos;
828+
795829
if (verbose) {
796830
elapsed.report(LOGGER, "done invalidating repositories");
797831
}

src/org/opensolaris/opengrok/history/RepositoryFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
21-
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
20+
/*
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -82,9 +82,9 @@ public static List<Class<? extends Repository>> getRepositoryClasses() {
8282
* @param file File that might contain a repository
8383
* @return Correct repository for the given file
8484
* @throws java.lang.InstantiationException in case we cannot create the
85-
* repo object
86-
* @throws java.lang.IllegalAccessException in case no permissions to repo
87-
* file
85+
* repository object
86+
* @throws java.lang.IllegalAccessException in case no permissions
87+
* to repository file
8888
*/
8989
public static Repository getRepository(File file) throws InstantiationException, IllegalAccessException {
9090
RuntimeEnvironment env = RuntimeEnvironment.getInstance();

0 commit comments

Comments
 (0)