35
35
import java .util .List ;
36
36
import java .util .Map ;
37
37
import java .util .Objects ;
38
+ import java .util .Optional ;
38
39
import java .util .Set ;
39
40
import java .util .concurrent .ConcurrentHashMap ;
40
41
import java .util .concurrent .CountDownLatch ;
42
+ import java .util .concurrent .ExecutionException ;
41
43
import java .util .concurrent .ExecutorService ;
42
44
import java .util .concurrent .Executors ;
43
45
import java .util .concurrent .Future ;
@@ -944,7 +946,7 @@ public void storeHistory(File file, History history) {
944
946
}
945
947
}
946
948
947
- private void createHistoryCache (Repository repository , String sinceRevision ) {
949
+ private void createHistoryCache (Repository repository , String sinceRevision ) throws CacheException , HistoryException {
948
950
if (!repository .isHistoryEnabled ()) {
949
951
LOGGER .log (Level .INFO ,
950
952
"Skipping history cache creation for {0} and its subdirectories" , repository );
@@ -955,26 +957,19 @@ private void createHistoryCache(Repository repository, String sinceRevision) {
955
957
Statistics elapsed = new Statistics ();
956
958
957
959
LOGGER .log (Level .INFO , "Creating history cache for {0}" , repository );
958
-
959
- try {
960
- repository .createCache (historyCache , sinceRevision );
961
- } catch (Exception e ) {
962
- LOGGER .log (Level .WARNING ,
963
- String .format ("An error occurred while creating cache for %s" , repository ), e );
964
- }
965
-
960
+ repository .createCache (historyCache , sinceRevision );
966
961
elapsed .report (LOGGER , String .format ("Done history cache for %s" , repository ));
967
962
} else {
968
963
LOGGER .log (Level .WARNING ,
969
964
"Skipping creation of history cache for {0}: Missing SCM dependencies?" , repository );
970
965
}
971
966
}
972
967
973
- private void createHistoryCacheReal (Collection <Repository > repositories ) {
968
+ private Map < Repository , Optional < Exception >> createHistoryCacheReal (Collection <Repository > repositories ) {
974
969
if (repositories .isEmpty ()) {
975
970
LOGGER .log (Level .WARNING , "History cache is enabled however the list of repositories is empty. " +
976
971
"Either specify the repositories in configuration or let the indexer scan them." );
977
- return ;
972
+ return Collections . emptyMap () ;
978
973
}
979
974
980
975
Statistics elapsed = new Statistics ();
@@ -999,32 +994,37 @@ private void createHistoryCacheReal(Collection<Repository> repositories) {
999
994
}
1000
995
}
1001
996
1002
- LOGGER .log (Level .INFO , "Creating history cache for {0} repositories" ,
1003
- repos2process .size ());
1004
- final CountDownLatch latch = new CountDownLatch (repos2process .size ());
1005
- for (final Map .Entry <Repository , String > entry : repos2process .entrySet ()) {
1006
- executor .submit (() -> {
1007
- try {
1008
- createHistoryCache (entry .getKey (), entry .getValue ());
1009
- } catch (Exception ex ) {
1010
- // We want to catch any exception since we are in thread.
1011
- LOGGER .log (Level .WARNING , "createHistoryCacheReal() got exception" , ex );
1012
- } finally {
1013
- latch .countDown ();
1014
- }
1015
- });
997
+ LOGGER .log (Level .INFO , "Creating history cache for {0} repositories" , repos2process .size ());
998
+ Map <Repository , Future <Optional <Exception >>> futures = new HashMap <>();
999
+ try (Progress progress = new Progress (LOGGER , "repository invalidation" , repos2process .size ())) {
1000
+ for (final Map .Entry <Repository , String > entry : repos2process .entrySet ()) {
1001
+ futures .put (entry .getKey (), executor .submit (() -> {
1002
+ try {
1003
+ createHistoryCache (entry .getKey (), entry .getValue ());
1004
+ } catch (Exception ex ) { // We want to catch any exception since we are in thread.
1005
+ LOGGER .log (Level .WARNING ,
1006
+ String .format ("failed to create history cache for %s" , entry .getKey ()), ex );
1007
+ return Optional .of (ex );
1008
+ } finally {
1009
+ progress .increment ();
1010
+ }
1011
+ return Optional .empty ();
1012
+ }));
1013
+ }
1016
1014
}
1017
1015
1018
1016
/*
1019
1017
* Wait until the history of all repositories is done. This is necessary
1020
1018
* since the next phase of generating index will need the history to
1021
1019
* be ready as it is recorded in Lucene index.
1022
1020
*/
1023
- try {
1024
- latch .await ();
1025
- } catch (InterruptedException ex ) {
1026
- LOGGER .log (Level .SEVERE , "latch exception" , ex );
1027
- return ;
1021
+ Map <Repository , Optional <Exception >> results = new HashMap <>();
1022
+ for (Map .Entry <Repository , Future <Optional <Exception >>> entry : futures .entrySet ()) {
1023
+ try {
1024
+ results .put (entry .getKey (), entry .getValue ().get ());
1025
+ } catch (InterruptedException | ExecutionException ex ) {
1026
+ results .put (entry .getKey (), Optional .of (ex ));
1027
+ }
1028
1028
}
1029
1029
1030
1030
// The cache has been populated. Now, optimize how it is stored on
@@ -1036,6 +1036,8 @@ private void createHistoryCacheReal(Collection<Repository> repositories) {
1036
1036
}
1037
1037
elapsed .report (LOGGER , "Done history cache for all repositories" , "indexer.history.cache" );
1038
1038
setHistoryIndexDone ();
1039
+
1040
+ return results ;
1039
1041
}
1040
1042
1041
1043
/**
@@ -1044,12 +1046,13 @@ private void createHistoryCacheReal(Collection<Repository> repositories) {
1044
1046
* internal map, e.g. via {@code setRepositories()} or {@code addRepositories()}.
1045
1047
*
1046
1048
* @param repositories list of repository paths
1049
+ * @return map of repository to optional exception
1047
1050
*/
1048
- public void createHistoryCache (Collection <String > repositories ) {
1051
+ public Map < Repository , Optional < Exception >> createHistoryCache (Collection <String > repositories ) {
1049
1052
if (!useHistoryCache ()) {
1050
- return ;
1053
+ return Collections . emptyMap () ;
1051
1054
}
1052
- createHistoryCacheReal (getReposFromString (repositories ));
1055
+ return createHistoryCacheReal (getReposFromString (repositories ));
1053
1056
}
1054
1057
1055
1058
/**
@@ -1161,13 +1164,14 @@ public List<String> removeAnnotationCache(Collection<RepositoryInfo> repositorie
1161
1164
1162
1165
/**
1163
1166
* Create the history cache for all repositories.
1167
+ * @return map of repository to optional exception
1164
1168
*/
1165
- public void createHistoryCache () {
1169
+ public Map < Repository , Optional < Exception >> createHistoryCache () {
1166
1170
if (!useHistoryCache ()) {
1167
- return ;
1171
+ return Collections . emptyMap () ;
1168
1172
}
1169
1173
1170
- createHistoryCacheReal (repositories .values ());
1174
+ return createHistoryCacheReal (repositories .values ());
1171
1175
}
1172
1176
1173
1177
/**
0 commit comments