Skip to content

Commit 612cde2

Browse files
committed
restore Store.readSegmentsInfo(IndexCommit, Directory)
1 parent 65edd3c commit 612cde2

File tree

1 file changed

+22
-16
lines changed
  • server/src/main/java/org/elasticsearch/index/store

1 file changed

+22
-16
lines changed

server/src/main/java/org/elasticsearch/index/store/Store.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,33 @@ public Directory directory() {
206206
public SegmentInfos readLastCommittedSegmentsInfo() throws IOException {
207207
failIfCorrupted();
208208
try {
209-
return Lucene.readSegmentInfos(directory);
209+
return readSegmentsInfo(null, directory());
210210
} catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
211211
markStoreCorrupted(ex);
212212
throw ex;
213213
}
214214
}
215215

216+
/**
217+
* Returns the segments info for the given commit or for the latest commit if the given commit is <code>null</code>
218+
*
219+
* @throws IOException if the index is corrupted or the segments file is not present
220+
*/
221+
private static SegmentInfos readSegmentsInfo(IndexCommit commit, Directory directory) throws IOException {
222+
assert commit == null || commit.getDirectory() == directory;
223+
try {
224+
return commit == null ? Lucene.readSegmentInfos(directory) : Lucene.readSegmentInfos(commit);
225+
} catch (EOFException eof) {
226+
// TODO this should be caught by lucene - EOF is almost certainly an index corruption
227+
throw new CorruptIndexException("Read past EOF while reading segment infos", "commit(" + commit + ")", eof);
228+
} catch (IOException exception) {
229+
throw exception; // IOExceptions like too many open files are not necessarily a corruption - just bubble it up
230+
} catch (Exception ex) {
231+
throw new CorruptIndexException("Hit unexpected exception while reading segment infos", "commit(" + commit + ")", ex);
232+
}
233+
234+
}
235+
216236
final void ensureOpen() {
217237
if (this.refCounter.refCount() <= 0) {
218238
throw new AlreadyClosedException("store is already closed");
@@ -804,21 +824,7 @@ static MetadataSnapshot loadFromIndexCommit(@Nullable IndexCommit commit, Direct
804824
final Map<String, StoreFileMetadata> metadataByFile = new HashMap<>();
805825
final Map<String, String> commitUserData;
806826
try {
807-
assert commit == null || commit.getDirectory() == directory;
808-
final SegmentInfos segmentCommitInfos;
809-
try {
810-
if (commit == null) {
811-
segmentCommitInfos = Lucene.readSegmentInfos(directory);
812-
} else {
813-
segmentCommitInfos = Lucene.readSegmentInfos(commit);
814-
}
815-
} catch (EOFException eof) {
816-
// TODO this should be caught by lucene - EOF is almost certainly an index corruption
817-
throw new CorruptIndexException("Read past EOF while reading segment infos", "commit(" + commit + ")", eof);
818-
} catch (Exception ex) {
819-
throw new CorruptIndexException("Hit unexpected exception while reading segment infos", "commit(" + commit + ")", ex);
820-
}
821-
827+
final SegmentInfos segmentCommitInfos = readSegmentsInfo(commit, directory);
822828
numDocs = Lucene.getNumDocs(segmentCommitInfos);
823829
commitUserData = Map.copyOf(segmentCommitInfos.getUserData());
824830
// we don't know which version was used to write so we take the max version.

0 commit comments

Comments
 (0)