Skip to content

handle empty data directory in index check #3981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
*/
package org.opengrok.indexer.index;
Expand All @@ -28,6 +28,8 @@
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.lucene.index.IndexNotFoundException;
import org.apache.lucene.index.SegmentInfos;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
Expand Down Expand Up @@ -135,8 +137,13 @@ public static void checkDir(File dir) throws IndexVersionException, IOException
int segVersion;

try (Directory indexDirectory = FSDirectory.open(dir.toPath(), lockFactory)) {
SegmentInfos segInfos = SegmentInfos.readLatestCommit(indexDirectory);
segVersion = segInfos.getIndexCreatedVersionMajor();
try {
SegmentInfos segInfos = SegmentInfos.readLatestCommit(indexDirectory);
segVersion = segInfos.getIndexCreatedVersionMajor();
} catch (IndexNotFoundException e) {
LOGGER.log(Level.FINE, "no index found in ''{0}''", indexDirectory);
return;
}
}

if (segVersion != Version.LATEST.major) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2019, Chris Fraire <[email protected]>.
*/
package org.opengrok.indexer.index;
Expand All @@ -33,6 +33,7 @@
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -42,6 +43,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.opengrok.indexer.configuration.Configuration;
import org.opengrok.indexer.configuration.RuntimeEnvironment;
import org.opengrok.indexer.history.RepositoryFactory;
Expand Down Expand Up @@ -140,4 +142,10 @@ void testIndexVersionOldIndex() throws Exception {

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

@Test
void testEmptyDir(@TempDir Path tempDir) throws Exception {
assertEquals(0, tempDir.toFile().list().length);
IndexCheck.checkDir(tempDir.toFile());
}
}