Skip to content

Commit fe7f812

Browse files
authored
tidy IndexCheck and improve its testing (#4442)
convert IndexCheck from static class also fold the project paralell check into the class, use Mockito for testing and rework exception propagation fixes #4412
1 parent f8a4c2e commit fe7f812

File tree

8 files changed

+445
-206
lines changed

8 files changed

+445
-206
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Project.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,9 @@ public static Project getProject(String path) {
570570
/**
571571
* Get the project for a specific file.
572572
*
573-
* @param file the file to lookup
574-
* @return the project that this file belongs to (or {@code null} if the file doesn't belong to a project)
573+
* @param file file under source root
574+
* @return the project that this file belongs to (or {@code null} if the file doesn't belong to a project,
575+
* or it is a symbolic link that is not allowed)
575576
*/
576577
@Nullable
577578
public static Project getProject(File file) {

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

Lines changed: 147 additions & 95 deletions
Large diffs are not rendered by default.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.index;
24+
25+
import java.nio.file.Path;
26+
import java.util.Collections;
27+
import java.util.HashSet;
28+
import java.util.Set;
29+
30+
/**
31+
* Common exception for all check modes.
32+
*/
33+
public class IndexCheckException extends Exception {
34+
private static final long serialVersionUID = 5693446916108385595L;
35+
36+
private final Set<Path> failedPaths = new HashSet<>();
37+
38+
public IndexCheckException(String s, Path path) {
39+
super(s);
40+
failedPaths.add(path);
41+
}
42+
43+
public IndexCheckException(String s, Set<Path> paths) {
44+
super(s);
45+
failedPaths.addAll(paths);
46+
}
47+
48+
public Set<Path> getFailedPaths() {
49+
return Collections.unmodifiableSet(failedPaths);
50+
}
51+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.index;
24+
25+
import java.nio.file.Path;
26+
import java.util.Map;
27+
28+
/**
29+
* Exception thrown when index contains duplicate live documents.
30+
*/
31+
public class IndexDocumentException extends IndexCheckException {
32+
private static final long serialVersionUID = 5693446916108385595L;
33+
34+
private final Map<String, Integer> fileMap;
35+
36+
public IndexDocumentException(String s, Path path) {
37+
super(s, path);
38+
this.fileMap = null;
39+
}
40+
41+
public IndexDocumentException(String s, Path path, Map<String, Integer> fileMap) {
42+
super(s, path);
43+
this.fileMap = fileMap;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return getMessage() + ": " + (fileMap == null ? "" : fileMap);
49+
}
50+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.index;
24+
25+
import java.nio.file.Path;
26+
27+
/**
28+
* Exception thrown when index version does not match Lucene version.
29+
*/
30+
public class IndexVersionException extends IndexCheckException {
31+
32+
private static final long serialVersionUID = 5693446916108385595L;
33+
34+
private final int luceneIndexVersion;
35+
private final int indexVersion;
36+
37+
public IndexVersionException(String s, Path path, int luceneIndexVersion, int indexVersion) {
38+
super(s, path);
39+
this.indexVersion = indexVersion;
40+
this.luceneIndexVersion = luceneIndexVersion;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return getMessage() + ": " + String.format("Lucene version = %d", luceneIndexVersion) + ", " +
46+
String.format("index version = %d", indexVersion);
47+
}
48+
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,30 +265,27 @@ public static void main(String[] argv) {
265265
env.setConfiguration(cfg, subFilePaths, CommandTimeoutType.INDEXER);
266266

267267
// Let repository types to add items to ignoredNames.
268-
// This changes env so is called after the setConfiguration()
269-
// call above.
268+
// This changes env so is called after the setConfiguration() call above.
270269
RepositoryFactory.initializeIgnoredNames(env);
271270

272-
// Check index(es). Exit with distinct return code upon failure.
271+
// Check index(es) and exit. Use distinct return code upon failure.
273272
if (indexCheckMode.ordinal() > IndexCheck.IndexCheckMode.NO_CHECK.ordinal()) {
274273
if (cfg.getDataRoot() == null || cfg.getDataRoot().isEmpty()) {
275274
System.err.println("Empty data root in configuration");
276275
System.exit(1);
277276
}
278277

279-
try {
280-
if (!IndexCheck.isOkay(cfg, indexCheckMode, subFileArgs)) {
281-
System.err.printf("Index check failed%n");
282-
System.err.print("You might want to remove " +
283-
(!subFilePaths.isEmpty() ? "data for projects " + String.join(",", subFilePaths) :
284-
"all data") + " under the data root and reindex\n");
285-
System.exit(1);
286-
}
278+
try (IndexCheck indexCheck = new IndexCheck(cfg, subFileArgs)) {
279+
indexCheck.check(indexCheckMode);
287280
} catch (IOException e) {
288281
// Use separate return code for cases where the index could not be read.
289282
// This avoids problems with wiping out the index based on the check.
290283
LOGGER.log(Level.WARNING, String.format("Could not perform index check for '%s'", subFileArgs), e);
291284
System.exit(2);
285+
} catch (IndexCheckException e) {
286+
System.err.printf("Index check failed%n");
287+
System.err.print("You might want to remove " + e.getFailedPaths());
288+
System.exit(1);
292289
}
293290

294291
System.exit(0);

0 commit comments

Comments
 (0)