Search in sources :

Example 91 with CorruptIndexException

use of org.apache.lucene.index.CorruptIndexException in project crate by crate.

the class StoreTests method testCanOpenIndex.

public void testCanOpenIndex() throws IOException {
    final ShardId shardId = new ShardId("index", "_na_", 1);
    IndexWriterConfig iwc = newIndexWriterConfig();
    Path tempDir = createTempDir();
    final BaseDirectoryWrapper dir = newFSDirectory(tempDir);
    assertFalse(StoreUtils.canOpenIndex(logger, tempDir, shardId, (id, l, d) -> new DummyShardLock(id)));
    IndexWriter writer = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new StringField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    writer.commit();
    writer.close();
    assertTrue(StoreUtils.canOpenIndex(logger, tempDir, shardId, (id, l, d) -> new DummyShardLock(id)));
    Store store = new Store(shardId, INDEX_SETTINGS, dir, new DummyShardLock(shardId));
    store.markStoreCorrupted(new CorruptIndexException("foo", "bar"));
    assertFalse(StoreUtils.canOpenIndex(logger, tempDir, shardId, (id, l, d) -> new DummyShardLock(id)));
    store.close();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Path(java.nio.file.Path) IndexNotFoundException(org.apache.lucene.index.IndexNotFoundException) ShardId(org.elasticsearch.index.shard.ShardId) NoMergePolicy(org.apache.lucene.index.NoMergePolicy) NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) IndexSettingsModule(org.elasticsearch.test.IndexSettingsModule) Date(java.util.Date) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Term(org.apache.lucene.index.Term) Matchers.not(org.hamcrest.Matchers.not) Random(java.util.Random) ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) Matchers.hasKey(org.hamcrest.Matchers.hasKey) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput) Document(org.apache.lucene.document.Document) Settings(org.elasticsearch.common.settings.Settings) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CodecUtil(org.apache.lucene.codecs.CodecUtil) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) IOContext(org.apache.lucene.store.IOContext) Path(java.nio.file.Path) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) BytesRef(org.apache.lucene.util.BytesRef) DirectoryReader(org.apache.lucene.index.DirectoryReader) UUIDs(org.elasticsearch.common.UUIDs) SegmentInfos(org.apache.lucene.index.SegmentInfos) FileNotFoundException(java.io.FileNotFoundException) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Engine(org.elasticsearch.index.engine.Engine) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IndexWriter(org.apache.lucene.index.IndexWriter) List(java.util.List) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TimeValue(io.crate.common.unit.TimeValue) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) ReplicationTracker(org.elasticsearch.index.seqno.ReplicationTracker) Matchers.anyOf(org.hamcrest.Matchers.anyOf) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.endsWith(org.hamcrest.Matchers.endsWith) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringField(org.apache.lucene.document.StringField) TestUtil(org.apache.lucene.util.TestUtil) RAMDirectory(org.apache.lucene.store.RAMDirectory) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) Lucene(org.elasticsearch.common.lucene.Lucene) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ArrayList(java.util.ArrayList) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) RetentionLease(org.elasticsearch.index.seqno.RetentionLease) NoDeletionPolicy(org.apache.lucene.index.NoDeletionPolicy) IndexSettings(org.elasticsearch.index.IndexSettings) ESTestCase(org.elasticsearch.test.ESTestCase) VersionUtils.randomVersion(org.elasticsearch.test.VersionUtils.randomVersion) IndexOutput(org.apache.lucene.store.IndexOutput) TransportNodesListShardStoreMetadata(org.elasticsearch.indices.store.TransportNodesListShardStoreMetadata) Matchers.empty(org.hamcrest.Matchers.empty) IndexInput(org.apache.lucene.store.IndexInput) Iterator(java.util.Iterator) SnapshotDeletionPolicy(org.apache.lucene.index.SnapshotDeletionPolicy) IOUtils(io.crate.common.io.IOUtils) IndexFileNames(org.apache.lucene.index.IndexFileNames) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) ShardLock(org.elasticsearch.env.ShardLock) Version(org.apache.lucene.util.Version) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) KeepOnlyLastCommitDeletionPolicy(org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy) Field(org.apache.lucene.document.Field) FilterDirectory(org.apache.lucene.store.FilterDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) Translog(org.elasticsearch.index.translog.Translog) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) TextField(org.apache.lucene.document.TextField) DummyShardLock(org.elasticsearch.test.DummyShardLock) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) DummyShardLock(org.elasticsearch.test.DummyShardLock) Document(org.apache.lucene.document.Document) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 92 with CorruptIndexException

use of org.apache.lucene.index.CorruptIndexException in project crate by crate.

the class StoreTests method testMarkCorruptedOnTruncatedSegmentsFile.

public void testMarkCorruptedOnTruncatedSegmentsFile() throws IOException {
    IndexWriterConfig iwc = newIndexWriterConfig();
    final ShardId shardId = new ShardId("index", "_na_", 1);
    Store store = new Store(shardId, INDEX_SETTINGS, StoreTests.newDirectory(random()), new DummyShardLock(shardId));
    IndexWriter writer = new IndexWriter(store.directory(), iwc);
    int numDocs = 1 + random().nextInt(10);
    List<Document> docs = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        Document doc = new Document();
        doc.add(new StringField("id", "" + i, random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
        doc.add(new TextField("body", TestUtil.randomRealisticUnicodeString(random()), random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
        doc.add(new SortedDocValuesField("dv", new BytesRef(TestUtil.randomRealisticUnicodeString(random()))));
        docs.add(doc);
    }
    for (Document d : docs) {
        writer.addDocument(d);
    }
    writer.commit();
    writer.close();
    SegmentInfos segmentCommitInfos = store.readLastCommittedSegmentsInfo();
    store.directory().deleteFile(segmentCommitInfos.getSegmentsFileName());
    try (IndexOutput out = store.directory().createOutput(segmentCommitInfos.getSegmentsFileName(), IOContext.DEFAULT)) {
    // empty file
    }
    try {
        if (randomBoolean()) {
            store.getMetadata(null);
        } else {
            store.readLastCommittedSegmentsInfo();
        }
        fail("corrupted segments_N file");
    } catch (CorruptIndexException ex) {
    // expected
    }
    assertTrue(store.isMarkedCorrupted());
    // we have to remove the index since it's corrupted and might fail the MocKDirWrapper checkindex call
    Lucene.cleanLuceneIndex(store.directory());
    store.close();
}
Also used : SegmentInfos(org.apache.lucene.index.SegmentInfos) ArrayList(java.util.ArrayList) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IndexOutput(org.apache.lucene.store.IndexOutput) Document(org.apache.lucene.document.Document) ShardId(org.elasticsearch.index.shard.ShardId) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) TextField(org.apache.lucene.document.TextField) DummyShardLock(org.elasticsearch.test.DummyShardLock) BytesRef(org.apache.lucene.util.BytesRef) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 93 with CorruptIndexException

use of org.apache.lucene.index.CorruptIndexException in project alfresco-repository by Alfresco.

the class IndexInfo method destroyInstance.

public void destroyInstance() {
    getWriteLock();
    try {
        if (mainIndexReader != null) {
            try {
                ((ReferenceCounting) mainIndexReader).setInvalidForReuse();
            } catch (IOException e) {
            // OK filed to close
            }
            mainIndexReader = null;
            for (IndexReader reader : referenceCountingReadOnlyIndexReaders.values()) {
                ReferenceCounting referenceCounting = (ReferenceCounting) reader;
                try {
                    referenceCounting.setInvalidForReuse();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        for (IndexReader reader : indexReaders.values()) {
            try {
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        indexReaders.clear();
        for (IndexWriter writer : indexWriters.values()) {
            try {
                writer.close();
            } catch (CorruptIndexException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        indexWriters.clear();
        if (indexInfoRAF != null) {
            try {
                indexInfoRAF.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (indexInfoBackupRAF != null) {
            try {
                indexInfoBackupRAF.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    // TODO: should set some running flag .... to abort ungoing stuff
    // at the moment it will die ungracefully ....
    } finally {
        releaseWriteLock();
    }
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException)

Aggregations

CorruptIndexException (org.apache.lucene.index.CorruptIndexException)93 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)35 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)35 Directory (org.apache.lucene.store.Directory)26 IOException (java.io.IOException)25 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)24 IndexInput (org.apache.lucene.store.IndexInput)24 IndexOutput (org.apache.lucene.store.IndexOutput)23 ArrayList (java.util.ArrayList)16 RAMDirectory (org.apache.lucene.store.RAMDirectory)15 BytesRef (org.apache.lucene.util.BytesRef)14 FileNotFoundException (java.io.FileNotFoundException)12 ShardId (org.elasticsearch.index.shard.ShardId)12 NoSuchFileException (java.nio.file.NoSuchFileException)11 IOContext (org.apache.lucene.store.IOContext)11 EOFException (java.io.EOFException)10 HashMap (java.util.HashMap)10 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)10 FilterDirectory (org.apache.lucene.store.FilterDirectory)10 Document (org.apache.lucene.document.Document)8