|
18 | 18 | */
|
19 | 19 |
|
20 | 20 | /*
|
21 |
| - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 21 | + * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. |
22 | 22 | * Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
|
23 | 23 | */
|
24 | 24 | package org.opengrok.indexer.index;
|
25 | 25 |
|
26 | 26 | import java.io.File;
|
| 27 | +import java.io.IOException; |
27 | 28 | import java.util.List;
|
28 | 29 | import java.util.logging.Level;
|
29 | 30 | import java.util.logging.Logger;
|
30 |
| -import org.apache.lucene.index.IndexNotFoundException; |
31 | 31 | import org.apache.lucene.index.SegmentInfos;
|
32 | 32 | import org.apache.lucene.store.Directory;
|
33 | 33 | import org.apache.lucene.store.FSDirectory;
|
|
38 | 38 | import org.opengrok.indexer.logger.LoggerFactory;
|
39 | 39 |
|
40 | 40 | /**
|
41 |
| - * Index version checker. |
| 41 | + * Index checker. |
42 | 42 | *
|
43 |
| - * @author Vladimir Kotal |
| 43 | + * @author Vladimír Kotal |
44 | 44 | */
|
45 |
| -public class IndexVersion { |
46 |
| - private static final Logger LOGGER = LoggerFactory.getLogger(IndexVersion.class); |
| 45 | +public class IndexCheck { |
| 46 | + private static final Logger LOGGER = LoggerFactory.getLogger(IndexCheck.class); |
47 | 47 |
|
48 | 48 | /**
|
49 | 49 | * Exception thrown when index version does not match Lucene version.
|
50 | 50 | */
|
51 | 51 | public static class IndexVersionException extends Exception {
|
52 | 52 |
|
53 |
| - private static final long serialVersionUID = -756788782277552544L; |
| 53 | + private static final long serialVersionUID = 5693446916108385595L; |
54 | 54 |
|
55 |
| - public IndexVersionException(String s) { |
| 55 | + private int luceneIndexVersion = -1; |
| 56 | + private int indexVersion = -1; |
| 57 | + |
| 58 | + public IndexVersionException(String s, int luceneIndexVersion, int indexVersion) { |
56 | 59 | super(s);
|
| 60 | + |
| 61 | + this.indexVersion = indexVersion; |
| 62 | + this.luceneIndexVersion = luceneIndexVersion; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String toString() { |
| 67 | + return getMessage() + ": " + String.format("Lucene version = %d", luceneIndexVersion) + ", " + |
| 68 | + String.format("index version = %d", indexVersion); |
57 | 69 | }
|
58 | 70 | }
|
59 | 71 |
|
60 |
| - private IndexVersion() { |
| 72 | + private IndexCheck() { |
| 73 | + // utility class |
61 | 74 | }
|
62 | 75 |
|
63 | 76 | /**
|
64 | 77 | * Check if version of index(es) matches major Lucene version.
|
65 | 78 | * @param subFilesList list of paths. If non-empty, only projects matching these paths will be checked.
|
66 |
| - * @throws Exception otherwise |
| 79 | + * @return true on success, false on failure |
67 | 80 | */
|
68 |
| - public static void check(List<String> subFilesList) throws Exception { |
| 81 | + public static boolean check(List<String> subFilesList) { |
69 | 82 | RuntimeEnvironment env = RuntimeEnvironment.getInstance();
|
70 | 83 | File indexRoot = new File(env.getDataRootPath(), IndexDatabase.INDEX_DIR);
|
71 | 84 | LOGGER.log(Level.FINE, "Checking for Lucene index version mismatch in {0}",
|
72 | 85 | indexRoot);
|
| 86 | + int ret = 0; |
73 | 87 |
|
74 | 88 | if (!subFilesList.isEmpty()) {
|
75 | 89 | // Assumes projects are enabled.
|
76 | 90 | for (String projectName : subFilesList) {
|
77 | 91 | LOGGER.log(Level.FINER,
|
78 | 92 | "Checking Lucene index version in project {0}",
|
79 | 93 | projectName);
|
80 |
| - checkDir(new File(indexRoot, projectName)); |
| 94 | + ret |= checkDirNoExceptions(new File(indexRoot, projectName)); |
81 | 95 | }
|
82 | 96 | } else {
|
83 | 97 | if (env.isProjectsEnabled()) {
|
84 | 98 | for (String projectName : env.getProjects().keySet()) {
|
85 | 99 | LOGGER.log(Level.FINER,
|
86 | 100 | "Checking Lucene index version in project {0}",
|
87 | 101 | projectName);
|
88 |
| - checkDir(new File(indexRoot, projectName)); |
| 102 | + ret |= checkDirNoExceptions(new File(indexRoot, projectName)); |
89 | 103 | }
|
90 | 104 | } else {
|
91 | 105 | LOGGER.log(Level.FINER, "Checking Lucene index version in {0}",
|
92 | 106 | indexRoot);
|
93 |
| - checkDir(indexRoot); |
| 107 | + ret |= checkDirNoExceptions(indexRoot); |
94 | 108 | }
|
95 | 109 | }
|
| 110 | + |
| 111 | + return ret == 0; |
| 112 | + } |
| 113 | + |
| 114 | + private static int checkDirNoExceptions(File dir) { |
| 115 | + try { |
| 116 | + checkDir(dir); |
| 117 | + } catch (Exception e) { |
| 118 | + LOGGER.log(Level.WARNING, "Index check for directory " + dir.toString() + " failed", e); |
| 119 | + return 1; |
| 120 | + } |
| 121 | + |
| 122 | + return 0; |
96 | 123 | }
|
97 | 124 |
|
98 | 125 | /**
|
99 |
| - * Check index version in given directory. It assumes that that all commits |
| 126 | + * Check index in given directory. It assumes that that all commits (if any) |
100 | 127 | * in the Lucene segment file were done with the same version.
|
101 | 128 | *
|
102 | 129 | * @param dir directory with index
|
103 |
| - * @thows IOException if the directory cannot be opened |
| 130 | + * @throws IOException if the directory cannot be opened |
| 131 | + * @throws IndexVersionException if the version of the index does not match Lucene index version |
104 | 132 | */
|
105 |
| - private static void checkDir(File dir) throws Exception { |
106 |
| - LockFactory lockfact = NativeFSLockFactory.INSTANCE; |
| 133 | + public static void checkDir(File dir) throws IndexVersionException, IOException { |
| 134 | + LockFactory lockFactory = NativeFSLockFactory.INSTANCE; |
107 | 135 | int segVersion;
|
108 | 136 |
|
109 |
| - try (Directory indexDirectory = FSDirectory.open(dir.toPath(), lockfact)) { |
110 |
| - SegmentInfos segInfos = null; |
| 137 | + try (Directory indexDirectory = FSDirectory.open(dir.toPath(), lockFactory)) { |
| 138 | + SegmentInfos segInfos; |
111 | 139 |
|
112 |
| - try { |
113 |
| - segInfos = SegmentInfos.readLatestCommit(indexDirectory); |
114 |
| - segVersion = segInfos.getIndexCreatedVersionMajor(); |
115 |
| - } catch (IndexNotFoundException e) { |
116 |
| - return; |
117 |
| - } |
| 140 | + segInfos = SegmentInfos.readLatestCommit(indexDirectory); |
| 141 | + segVersion = segInfos.getIndexCreatedVersionMajor(); |
118 | 142 | }
|
119 | 143 |
|
120 | 144 | if (segVersion != Version.LATEST.major) {
|
121 | 145 | throw new IndexVersionException(
|
122 |
| - String.format("Directory %s has index of version %d and Lucene has %d", |
123 |
| - dir.toString(), segVersion, Version.LATEST.major)); |
| 146 | + String.format("Directory %s has index version discrepancy", dir.toString()), |
| 147 | + Version.LATEST.major, segVersion); |
124 | 148 | }
|
125 | 149 | }
|
126 | 150 | }
|
0 commit comments