Skip to content
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

Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex #12872

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -947,7 +947,7 @@ private Status.SegmentInfoStatus testSegment(

final Version version = info.info.getVersion();
if (info.info.maxDoc() <= 0) {
throw new CheckIndexException(" illegal number of documents: maxDoc=" + info.info.maxDoc());
throw new CheckIndexException("illegal number of documents: maxDoc=" + info.info.maxDoc());
}

int toLoseDocCount = info.info.maxDoc();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.index;

import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.DataOutput;

/**
* This exception is thrown when Lucene is unable to read a SegmentInfo file _N.si (When it is
* missing or corrupt)
*/
public class CorruptSegmentInfoException extends CorruptIndexException {

// The segment name of the missing .si file is always stored for accurate error reporting
String segmentName;

/** Create exception with the segmentName and message only */
public CorruptSegmentInfoException(String segmentName, String message, DataInput input) {
super(message, input);
this.segmentName = segmentName;
}

/** Create exception with the segmentName and message only */
public CorruptSegmentInfoException(String segmentName, String message, DataOutput output) {
super(message, output);
this.segmentName = segmentName;
}

/** Create exception with the segmentName, message and root cause. */
public CorruptSegmentInfoException(
String segmentName, String message, DataInput input, Throwable cause) {
super(message, input, cause);
this.segmentName = segmentName;
}

/** Create exception with the segmentName, message and root cause. */
public CorruptSegmentInfoException(
String segmentName, String message, DataOutput output, Throwable cause) {
super(message, output, cause);
this.segmentName = segmentName;
}

/** Create exception with the segmentName and message only */
public CorruptSegmentInfoException(
String segmentName, String message, String resourceDescription) {
super(message, resourceDescription);
this.segmentName = segmentName;
}

/** Create exception with the segmentName, message and root cause. */
public CorruptSegmentInfoException(
String segmentName, String message, String resourceDescription, Throwable cause) {
super(message, resourceDescription, cause);
this.segmentName = segmentName;
}
}
21 changes: 15 additions & 6 deletions lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.CollectionUtil;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.Version;
import org.apache.lucene.util.*;

/**
* A collection of segmentInfo objects with methods for operating on those segments in relation to
Expand Down Expand Up @@ -389,13 +386,25 @@ private static void parseSegmentInfos(
}

long totalDocs = 0;
SegmentInfo info;
for (int seg = 0; seg < numSegments; seg++) {
String segName = input.readString();
byte[] segmentID = new byte[StringHelper.ID_LENGTH];
input.readBytes(segmentID, 0, segmentID.length);
Codec codec = readCodec(input);
SegmentInfo info =
codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
try {
info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
} catch (ThreadInterruptedException e) {
throw e;
} catch (Exception e) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching a general-purpose exception here was tricky !

  • TestIndexWrtier#testThreadInterruptDeadlock expected a ThreadInterruptedException to be thrown as is
  • TestTransactions#testTransactions expected an 'on-purpose' IOException to be thrown as is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gokaai!

if (infoStream != null) {
message(e.getMessage());
}
throw new CorruptSegmentInfoException(
segName,
"segment info file: " + segName + ".si cannot be read - it may be missing or corrupt",
input);
}
info.setCodec(codec);
totalDocs += info.maxDoc();
long delGen = CodecUtil.readBELong(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ public void doWork() throws Throwable {
} catch (Exception e) {
// can be rethrown as RuntimeException if it happens during a close listener
if (!e.getMessage().contains("on purpose")) {
throw e;
// Caught "on-purpose" IOException can be rethrown as CorruptSegmentInfoException
if (!(e instanceof CorruptSegmentInfoException)) {
throw e;
}
}
// release resources
IOUtils.closeWhileHandlingException(r1, r2);
Expand Down
Loading