Search in sources :

Example 31 with CorruptIndexException

use of org.apache.lucene.index.CorruptIndexException in project lucene-solr by apache.

the class Lucene50LiveDocsFormat method readLiveDocs.

@Override
public Bits readLiveDocs(Directory dir, SegmentCommitInfo info, IOContext context) throws IOException {
    long gen = info.getDelGen();
    String name = IndexFileNames.fileNameFromGeneration(info.info.name, EXTENSION, gen);
    final int length = info.info.maxDoc();
    try (ChecksumIndexInput input = dir.openChecksumInput(name, context)) {
        Throwable priorE = null;
        try {
            CodecUtil.checkIndexHeader(input, CODEC_NAME, VERSION_START, VERSION_CURRENT, info.info.getId(), Long.toString(gen, Character.MAX_RADIX));
            long[] data = new long[FixedBitSet.bits2words(length)];
            for (int i = 0; i < data.length; i++) {
                data[i] = input.readLong();
            }
            FixedBitSet fbs = new FixedBitSet(data, length);
            if (fbs.length() - fbs.cardinality() != info.getDelCount()) {
                throw new CorruptIndexException("bits.deleted=" + (fbs.length() - fbs.cardinality()) + " info.delcount=" + info.getDelCount(), input);
            }
            return fbs;
        } catch (Throwable exception) {
            priorE = exception;
        } finally {
            CodecUtil.checkFooter(input, priorE);
        }
    }
    throw new AssertionError();
}
Also used : ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) FixedBitSet(org.apache.lucene.util.FixedBitSet) CorruptIndexException(org.apache.lucene.index.CorruptIndexException)

Example 32 with CorruptIndexException

use of org.apache.lucene.index.CorruptIndexException in project lucene-solr by apache.

the class Lucene50SegmentInfoFormat method read.

@Override
public SegmentInfo read(Directory dir, String segment, byte[] segmentID, IOContext context) throws IOException {
    final String fileName = IndexFileNames.segmentFileName(segment, "", Lucene50SegmentInfoFormat.SI_EXTENSION);
    try (ChecksumIndexInput input = dir.openChecksumInput(fileName, context)) {
        Throwable priorE = null;
        SegmentInfo si = null;
        try {
            CodecUtil.checkIndexHeader(input, Lucene50SegmentInfoFormat.CODEC_NAME, Lucene50SegmentInfoFormat.VERSION_START, Lucene50SegmentInfoFormat.VERSION_CURRENT, segmentID, "");
            final Version version = Version.fromBits(input.readInt(), input.readInt(), input.readInt());
            final int docCount = input.readInt();
            if (docCount < 0) {
                throw new CorruptIndexException("invalid docCount: " + docCount, input);
            }
            final boolean isCompoundFile = input.readByte() == SegmentInfo.YES;
            final Map<String, String> diagnostics = input.readMapOfStrings();
            final Set<String> files = input.readSetOfStrings();
            final Map<String, String> attributes = input.readMapOfStrings();
            si = new SegmentInfo(dir, version, null, segment, docCount, isCompoundFile, null, diagnostics, segmentID, attributes, null);
            si.setFiles(files);
        } catch (Throwable exception) {
            priorE = exception;
        } finally {
            CodecUtil.checkFooter(input, priorE);
        }
        return si;
    }
}
Also used : ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) Version(org.apache.lucene.util.Version) SegmentInfo(org.apache.lucene.index.SegmentInfo) CorruptIndexException(org.apache.lucene.index.CorruptIndexException)

Example 33 with CorruptIndexException

use of org.apache.lucene.index.CorruptIndexException in project lucene-solr by apache.

the class CodecUtil method checksumEntireFile.

/** 
   * Clones the provided input, reads all bytes from the file, and calls {@link #checkFooter} 
   * <p>
   * Note that this method may be slow, as it must process the entire file.
   * If you just need to extract the checksum value, call {@link #retrieveChecksum}.
   */
public static long checksumEntireFile(IndexInput input) throws IOException {
    IndexInput clone = input.clone();
    clone.seek(0);
    ChecksumIndexInput in = new BufferedChecksumIndexInput(clone);
    assert in.getFilePointer() == 0;
    if (in.length() < footerLength()) {
        throw new CorruptIndexException("misplaced codec footer (file truncated?): length=" + in.length() + " but footerLength==" + footerLength(), input);
    }
    in.seek(in.length() - footerLength());
    return checkFooter(in);
}
Also used : ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) IndexInput(org.apache.lucene.store.IndexInput) ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) CorruptIndexException(org.apache.lucene.index.CorruptIndexException)

Example 34 with CorruptIndexException

use of org.apache.lucene.index.CorruptIndexException in project lucene-solr by apache.

the class CodecUtil method checkFooter.

/** 
   * Validates the codec footer previously written by {@link #writeFooter}, optionally
   * passing an unexpected exception that has already occurred.
   * <p>
   * When a {@code priorException} is provided, this method will add a suppressed exception 
   * indicating whether the checksum for the stream passes, fails, or cannot be computed, and 
   * rethrow it. Otherwise it behaves the same as {@link #checkFooter(ChecksumIndexInput)}.
   * <p>
   * Example usage:
   * <pre class="prettyprint">
   * try (ChecksumIndexInput input = ...) {
   *   Throwable priorE = null;
   *   try {
   *     // ... read a bunch of stuff ... 
   *   } catch (Throwable exception) {
   *     priorE = exception;
   *   } finally {
   *     CodecUtil.checkFooter(input, priorE);
   *   }
   * }
   * </pre>
   */
public static void checkFooter(ChecksumIndexInput in, Throwable priorException) throws IOException {
    if (priorException == null) {
        checkFooter(in);
    } else {
        try {
            long remaining = in.length() - in.getFilePointer();
            if (remaining < footerLength()) {
                // corruption caused us to read into the checksum footer already: we can't proceed
                priorException.addSuppressed(new CorruptIndexException("checksum status indeterminate: remaining=" + remaining + ", please run checkindex for more details", in));
            } else {
                // otherwise, skip any unread bytes.
                in.skipBytes(remaining - footerLength());
                // now check the footer
                try {
                    long checksum = checkFooter(in);
                    priorException.addSuppressed(new CorruptIndexException("checksum passed (" + Long.toHexString(checksum) + "). possibly transient resource issue, or a Lucene or JVM bug", in));
                } catch (CorruptIndexException t) {
                    priorException.addSuppressed(t);
                }
            }
        } catch (Throwable t) {
            // catch-all for things that shouldn't go wrong (e.g. OOM during readInt) but could...
            priorException.addSuppressed(new CorruptIndexException("checksum status indeterminate: unexpected exception", in, t));
        }
        throw IOUtils.rethrowAlways(priorException);
    }
}
Also used : CorruptIndexException(org.apache.lucene.index.CorruptIndexException)

Example 35 with CorruptIndexException

use of org.apache.lucene.index.CorruptIndexException in project lucene-solr by apache.

the class CodecUtil method validateFooter.

private static void validateFooter(IndexInput in) throws IOException {
    long remaining = in.length() - in.getFilePointer();
    long expected = footerLength();
    if (remaining < expected) {
        throw new CorruptIndexException("misplaced codec footer (file truncated?): remaining=" + remaining + ", expected=" + expected + ", fp=" + in.getFilePointer(), in);
    } else if (remaining > expected) {
        throw new CorruptIndexException("misplaced codec footer (file extended?): remaining=" + remaining + ", expected=" + expected + ", fp=" + in.getFilePointer(), in);
    }
    final int magic = in.readInt();
    if (magic != FOOTER_MAGIC) {
        throw new CorruptIndexException("codec footer mismatch (file truncated?): actual footer=" + magic + " vs expected footer=" + FOOTER_MAGIC, in);
    }
    final int algorithmID = in.readInt();
    if (algorithmID != 0) {
        throw new CorruptIndexException("codec footer mismatch: unknown algorithmID: " + algorithmID, in);
    }
}
Also used : CorruptIndexException(org.apache.lucene.index.CorruptIndexException)

Aggregations

CorruptIndexException (org.apache.lucene.index.CorruptIndexException)64 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)19 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)17 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)17 Directory (org.apache.lucene.store.Directory)16 IndexInput (org.apache.lucene.store.IndexInput)16 IndexOutput (org.apache.lucene.store.IndexOutput)15 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)9 FileNotFoundException (java.io.FileNotFoundException)8 RAMDirectory (org.apache.lucene.store.RAMDirectory)8 BytesRef (org.apache.lucene.util.BytesRef)8 EOFException (java.io.EOFException)7 HashMap (java.util.HashMap)7 IOContext (org.apache.lucene.store.IOContext)7 NoSuchFileException (java.nio.file.NoSuchFileException)6 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)6 List (java.util.List)5 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 AccessDeniedException (java.nio.file.AccessDeniedException)4