Skip to content

Commit 07af13e

Browse files
committed
treat IOException thrown during index check as success
1 parent c6cd071 commit 07af13e

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexCheck.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ private IndexCheck() {
137137
* on whether projects are enabled in the configuration.
138138
* @return true on success, false on failure
139139
*/
140-
public static boolean check(@NotNull Configuration configuration, IndexCheckMode mode,
141-
Collection<String> projectNames) {
140+
public static boolean isOkay(@NotNull Configuration configuration, IndexCheckMode mode,
141+
Collection<String> projectNames) {
142142

143143
if (mode.equals(IndexCheckMode.NO_CHECK)) {
144144
LOGGER.log(Level.WARNING, "no index check mode selected");
@@ -151,15 +151,15 @@ public static boolean check(@NotNull Configuration configuration, IndexCheckMode
151151
if (!projectNames.isEmpty()) {
152152
// Assumes projects are enabled.
153153
for (String projectName : projectNames) {
154-
ret |= checkDirNoExceptions(Path.of(indexRoot.toString(), projectName), mode, projectName);
154+
ret |= checkDirNoExceptions(Path.of(indexRoot.toString(), projectName), mode);
155155
}
156156
} else {
157157
if (configuration.isProjectsEnabled()) {
158158
for (String projectName : configuration.getProjects().keySet()) {
159-
ret |= checkDirNoExceptions(Path.of(indexRoot.toString(), projectName), mode, projectName);
159+
ret |= checkDirNoExceptions(Path.of(indexRoot.toString(), projectName), mode);
160160
}
161161
} else {
162-
ret |= checkDirNoExceptions(indexRoot, mode, "");
162+
ret |= checkDirNoExceptions(indexRoot, mode);
163163
}
164164
}
165165

@@ -170,10 +170,13 @@ public static boolean check(@NotNull Configuration configuration, IndexCheckMode
170170
* @param indexPath directory with index
171171
* @return 0 on success, 1 on failure
172172
*/
173-
private static int checkDirNoExceptions(Path indexPath, IndexCheckMode mode, String projectName) {
173+
private static int checkDirNoExceptions(Path indexPath, IndexCheckMode mode) {
174174
try {
175175
LOGGER.log(Level.INFO, "Checking index in ''{0}''", indexPath);
176176
checkDir(indexPath, mode);
177+
} catch (IOException e) {
178+
LOGGER.log(Level.WARNING, String.format("Could not perform index check for directory '%s'", indexPath), e);
179+
return 0;
177180
} catch (Exception e) {
178181
LOGGER.log(Level.WARNING, String.format("Index check for directory '%s' failed", indexPath), e);
179182
return 1;
@@ -191,6 +194,7 @@ private static int checkDirNoExceptions(Path indexPath, IndexCheckMode mode, Str
191194
* @param mode index check mode
192195
* @throws IOException if the directory cannot be opened
193196
* @throws IndexVersionException if the version of the index does not match Lucene index version
197+
* @throws IndexDocumentException if there are duplicate documents in the index
194198
*/
195199
public static void checkDir(Path indexPath, IndexCheckMode mode)
196200
throws IndexVersionException, IndexDocumentException, IOException {

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public static void main(String[] argv) {
268268
System.exit(1);
269269
}
270270

271-
if (!IndexCheck.check(cfg, indexCheckMode, subFileArgs)) {
271+
if (!IndexCheck.isOkay(cfg, indexCheckMode, subFileArgs)) {
272272
System.err.printf("Index check failed%n");
273273
System.err.print("You might want to remove " +
274274
(!subFilePaths.isEmpty() ? "data for projects " + String.join(",", subFilePaths) :

opengrok-indexer/src/test/java/org/opengrok/indexer/index/IndexCheckTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ private void testIndexVersion(boolean projectsEnabled, List<String> subFiles) th
9999
null, null);
100100
Indexer.getInstance().doIndexerExecution(null, null);
101101

102-
IndexCheck.check(configuration, IndexCheck.IndexCheckMode.VERSION, subFiles);
102+
IndexCheck.isOkay(configuration, IndexCheck.IndexCheckMode.VERSION, subFiles);
103103
}
104104

105105
@Test
106106
void testIndexVersionNoIndex() {
107-
IndexCheck.check(configuration, IndexCheck.IndexCheckMode.VERSION, new ArrayList<>());
107+
IndexCheck.isOkay(configuration, IndexCheck.IndexCheckMode.VERSION, new ArrayList<>());
108108
}
109109

110110
@Test
@@ -138,7 +138,7 @@ void testIndexVersionOldIndex() throws Exception {
138138
configuration.setDataRoot(oldIndexDataDir.toString());
139139
env.setProjectsEnabled(false);
140140
configuration.setProjectsEnabled(false);
141-
assertFalse(IndexCheck.check(configuration, IndexCheck.IndexCheckMode.VERSION, new ArrayList<>()));
141+
assertFalse(IndexCheck.isOkay(configuration, IndexCheck.IndexCheckMode.VERSION, new ArrayList<>()));
142142

143143
assertThrows(IndexCheck.IndexVersionException.class, () ->
144144
IndexCheck.checkDir(indexPath, IndexCheck.IndexCheckMode.VERSION));
@@ -149,4 +149,19 @@ void testEmptyDir(@TempDir Path tempDir) throws Exception {
149149
assertEquals(0, tempDir.toFile().list().length);
150150
IndexCheck.checkDir(tempDir, IndexCheck.IndexCheckMode.VERSION);
151151
}
152+
153+
/**
154+
* Check that {@link IOException} thrown during index check is treated as success.
155+
*/
156+
@Test
157+
void testIndexCheckIOException() {
158+
configuration.setDataRoot("/nonexistent");
159+
configuration.setProjectsEnabled(false);
160+
161+
IndexCheck.IndexCheckMode mode = IndexCheck.IndexCheckMode.VERSION;
162+
assertThrows(IOException.class, () -> IndexCheck.checkDir(Path.of(configuration.getDataRoot()), mode));
163+
// Assumes that IndexCheck.checkDir() is called via IndexCheck.isOkay() and the latter method
164+
// infers the result from the call.
165+
assertTrue(IndexCheck.isOkay(configuration, mode, new ArrayList<>()));
166+
}
152167
}

0 commit comments

Comments
 (0)