Skip to content

Commit 552e80c

Browse files
ahornaceVladimir Kotal
authored andcommitted
Avoid repository invalidation with --checkIndex
fixes #3645
1 parent 3312d53 commit 552e80c

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
import org.apache.lucene.store.LockFactory;
3535
import org.apache.lucene.store.NativeFSLockFactory;
3636
import org.apache.lucene.util.Version;
37-
import org.opengrok.indexer.configuration.RuntimeEnvironment;
37+
import org.jetbrains.annotations.NotNull;
38+
import org.opengrok.indexer.configuration.Configuration;
3839
import org.opengrok.indexer.logger.LoggerFactory;
3940

4041
/**
@@ -52,8 +53,8 @@ public static class IndexVersionException extends Exception {
5253

5354
private static final long serialVersionUID = 5693446916108385595L;
5455

55-
private int luceneIndexVersion = -1;
56-
private int indexVersion = -1;
56+
private final int luceneIndexVersion;
57+
private final int indexVersion;
5758

5859
public IndexVersionException(String s, int luceneIndexVersion, int indexVersion) {
5960
super(s);
@@ -75,12 +76,12 @@ private IndexCheck() {
7576

7677
/**
7778
* Check if version of index(es) matches major Lucene version.
79+
* @param configuration configuration based on which to perform the check
7880
* @param subFilesList list of paths. If non-empty, only projects matching these paths will be checked.
7981
* @return true on success, false on failure
8082
*/
81-
public static boolean check(List<String> subFilesList) {
82-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
83-
File indexRoot = new File(env.getDataRootPath(), IndexDatabase.INDEX_DIR);
83+
public static boolean check(@NotNull Configuration configuration, List<String> subFilesList) {
84+
File indexRoot = new File(configuration.getDataRoot(), IndexDatabase.INDEX_DIR);
8485
LOGGER.log(Level.FINE, "Checking for Lucene index version mismatch in {0}",
8586
indexRoot);
8687
int ret = 0;
@@ -94,8 +95,8 @@ public static boolean check(List<String> subFilesList) {
9495
ret |= checkDirNoExceptions(new File(indexRoot, projectName));
9596
}
9697
} else {
97-
if (env.isProjectsEnabled()) {
98-
for (String projectName : env.getProjects().keySet()) {
98+
if (configuration.isProjectsEnabled()) {
99+
for (String projectName : configuration.getProjects().keySet()) {
99100
LOGGER.log(Level.FINER,
100101
"Checking Lucene index version in project {0}",
101102
projectName);
@@ -115,7 +116,7 @@ private static int checkDirNoExceptions(File dir) {
115116
try {
116117
checkDir(dir);
117118
} catch (Exception e) {
118-
LOGGER.log(Level.WARNING, "Index check for directory " + dir.toString() + " failed", e);
119+
LOGGER.log(Level.WARNING, "Index check for directory " + dir + " failed", e);
119120
return 1;
120121
}
121122

@@ -141,7 +142,7 @@ public static void checkDir(File dir) throws IndexVersionException, IOException
141142

142143
if (segVersion != Version.LATEST.major) {
143144
throw new IndexVersionException(
144-
String.format("Directory %s has index version discrepancy", dir.toString()),
145+
String.format("Directory %s has index version discrepancy", dir),
145146
Version.LATEST.major, segVersion);
146147
}
147148
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,6 @@ public static void main(String[] argv) {
257257
}
258258
}
259259

260-
// Set updated configuration in RuntimeEnvironment.
261-
env.setConfiguration(cfg, subFilesList, CommandTimeoutType.INDEXER);
262-
263260
// Check version of index(es) versus current Lucene version and exit
264261
// with return code upon failure.
265262
if (checkIndex) {
@@ -268,7 +265,7 @@ public static void main(String[] argv) {
268265
System.exit(1);
269266
}
270267

271-
if (!IndexCheck.check(subFilesList)) {
268+
if (!IndexCheck.check(cfg, subFilesList)) {
272269
System.err.printf("Index check failed%n");
273270
System.err.print("You might want to remove " +
274271
(subFilesList.size() > 0 ?
@@ -280,6 +277,9 @@ public static void main(String[] argv) {
280277
System.exit(0);
281278
}
282279

280+
// Set updated configuration in RuntimeEnvironment.
281+
env.setConfiguration(cfg, subFilesList, CommandTimeoutType.INDEXER);
282+
283283
// Let repository types to add items to ignoredNames.
284284
// This changes env so is called after the setConfiguration()
285285
// call above.

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.junit.jupiter.api.BeforeAll;
4242
import org.junit.jupiter.api.BeforeEach;
4343
import org.junit.jupiter.api.Test;
44+
import org.opengrok.indexer.configuration.Configuration;
4445
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4546
import org.opengrok.indexer.history.RepositoryFactory;
4647
import org.opengrok.indexer.util.FileUtilities;
@@ -51,11 +52,12 @@
5152
* Verify index check.
5253
* @author Vladimír Kotal
5354
*/
54-
public class IndexCheckTest {
55+
class IndexCheckTest {
5556

5657
private TestRepository repository;
5758
private final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
5859
private Path oldIndexDataDir;
60+
private Configuration configuration;
5961

6062
@BeforeAll
6163
public static void setUpClass() {
@@ -68,6 +70,9 @@ public void setUp() throws IOException {
6870
repository = new TestRepository();
6971
repository.create(IndexerTest.class.getResourceAsStream("/org/opengrok/indexer/history/repositories.zip"));
7072
oldIndexDataDir = null;
73+
configuration = new Configuration();
74+
configuration.setDataRoot(env.getDataRootPath());
75+
configuration.setSourceRoot(env.getSourceRootPath());
7176
}
7277

7378
@AfterEach
@@ -84,36 +89,38 @@ public void tearDown() throws IOException {
8489
*/
8590
private void testIndexVersion(boolean projectsEnabled, List<String> subFiles) throws Exception {
8691
env.setHistoryEnabled(false);
92+
configuration.setHistoryEnabled(false);
8793
env.setProjectsEnabled(projectsEnabled);
94+
configuration.setProjectsEnabled(projectsEnabled);
8895
Indexer.getInstance().prepareIndexer(env, true, true,
8996
false, null, null);
9097
Indexer.getInstance().doIndexerExecution(true, null, null);
9198

92-
IndexCheck.check(subFiles);
99+
IndexCheck.check(configuration, subFiles);
93100
}
94101

95102
@Test
96-
public void testIndexVersionNoIndex() throws Exception {
97-
IndexCheck.check(new ArrayList<>());
103+
void testIndexVersionNoIndex() {
104+
IndexCheck.check(configuration, new ArrayList<>());
98105
}
99106

100107
@Test
101-
public void testIndexVersionProjects() throws Exception {
108+
void testIndexVersionProjects() throws Exception {
102109
testIndexVersion(true, new ArrayList<>());
103110
}
104111

105112
@Test
106-
public void testIndexVersionSelectedProjects() throws Exception {
113+
void testIndexVersionSelectedProjects() throws Exception {
107114
testIndexVersion(true, Arrays.asList("mercurial", "git"));
108115
}
109116

110117
@Test
111-
public void testIndexVersionNoProjects() throws Exception {
118+
void testIndexVersionNoProjects() throws Exception {
112119
testIndexVersion(false, new ArrayList<>());
113120
}
114121

115122
@Test
116-
public void testIndexVersionOldIndex() throws Exception {
123+
void testIndexVersionOldIndex() throws Exception {
117124
oldIndexDataDir = Files.createTempDirectory("data");
118125
Path indexPath = oldIndexDataDir.resolve("index");
119126
Files.createDirectory(indexPath);
@@ -125,8 +132,10 @@ public void testIndexVersionOldIndex() throws Exception {
125132
assertTrue(archive.isFile(), "archive exists");
126133
FileUtilities.extractArchive(archive, indexDir);
127134
env.setDataRoot(oldIndexDataDir.toString());
135+
configuration.setDataRoot(oldIndexDataDir.toString());
128136
env.setProjectsEnabled(false);
129-
assertFalse(IndexCheck.check(new ArrayList<>()));
137+
configuration.setProjectsEnabled(false);
138+
assertFalse(IndexCheck.check(configuration, new ArrayList<>()));
130139

131140
assertThrows(IndexCheck.IndexVersionException.class, () -> IndexCheck.checkDir(indexDir));
132141
}

0 commit comments

Comments
 (0)