|
18 | 18 | */
|
19 | 19 |
|
20 | 20 | /*
|
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. |
22 | 22 | */
|
23 | 23 | package org.opensolaris.opengrok.history;
|
24 | 24 |
|
|
36 | 36 | import java.util.Set;
|
37 | 37 | import java.util.concurrent.CountDownLatch;
|
38 | 38 | import java.util.concurrent.ExecutorService;
|
| 39 | +import java.util.concurrent.Executors; |
| 40 | +import java.util.concurrent.ThreadFactory; |
39 | 41 | import java.util.concurrent.TimeUnit;
|
40 | 42 | import java.util.logging.Level;
|
41 | 43 | import java.util.logging.Logger;
|
@@ -764,34 +766,66 @@ public void invalidateRepositories(Collection<? extends RepositoryInfo> repos) {
|
764 | 766 | if (repos == null || repos.isEmpty()) {
|
765 | 767 | repositories.clear();
|
766 | 768 | } else {
|
767 |
| - Map<String, Repository> newrepos |
768 |
| - = new HashMap<>(repos.size()); |
| 769 | + Map<String, Repository> newrepos = |
| 770 | + Collections.synchronizedMap(new HashMap<>(repos.size())); |
769 | 771 | Statistics elapsed = new Statistics();
|
770 | 772 | boolean verbose = RuntimeEnvironment.getInstance().isVerbose();
|
| 773 | + |
771 | 774 | if (verbose) {
|
772 | 775 | LOGGER.log(Level.FINE, "invalidating repositories");
|
773 | 776 | }
|
| 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 | + |
774 | 795 | 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 | + } |
783 | 815 | }
|
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 | + }); |
793 | 817 | }
|
| 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 | + |
794 | 827 | repositories = newrepos;
|
| 828 | + |
795 | 829 | if (verbose) {
|
796 | 830 | elapsed.report(LOGGER, "done invalidating repositories");
|
797 | 831 | }
|
|
0 commit comments