Search in sources :

Example 1 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project elasticsearch by elastic.

the class ShadowEngineTests method testFailEngineOnCorruption.

public void testFailEngineOnCorruption() throws IOException {
    ParsedDocument doc = testParsedDocument("1", "test", null, testDocumentWithTextField(), B_1, null);
    primaryEngine.index(indexForDoc(doc));
    primaryEngine.flush();
    MockDirectoryWrapper leaf = DirectoryUtils.getLeaf(replicaEngine.config().getStore().directory(), MockDirectoryWrapper.class);
    leaf.setRandomIOExceptionRate(1.0);
    leaf.setRandomIOExceptionRateOnOpen(1.0);
    try {
        replicaEngine.refresh("foo");
        fail("exception expected");
    } catch (Exception ex) {
    }
    try {
        Engine.Searcher searchResult = replicaEngine.acquireSearcher("test");
        MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
        MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1));
        searchResult.close();
        fail("exception expected");
    } catch (AlreadyClosedException ex) {
    // all is well
    }
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) TermQuery(org.apache.lucene.search.TermQuery) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Term(org.apache.lucene.index.Term) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ElasticsearchException(org.elasticsearch.ElasticsearchException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException)

Example 2 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project elasticsearch by elastic.

the class InternalEngineTests method testFailStart.

/**
     * Random test that throws random exception and ensures all references are
     * counted down / released and resources are closed.
     */
public void testFailStart() throws IOException {
    // this test fails if any reader, searcher or directory is not closed - MDW FTW
    final int iters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < iters; i++) {
        MockDirectoryWrapper wrapper = newMockDirectory();
        wrapper.setFailOnOpenInput(randomBoolean());
        wrapper.setAllowRandomFileNotFoundException(randomBoolean());
        wrapper.setRandomIOExceptionRate(randomDouble());
        wrapper.setRandomIOExceptionRateOnOpen(randomDouble());
        final Path translogPath = createTempDir("testFailStart");
        try (Store store = createStore(wrapper)) {
            int refCount = store.refCount();
            assertTrue("refCount: " + store.refCount(), store.refCount() > 0);
            InternalEngine holder;
            try {
                holder = createEngine(store, translogPath);
            } catch (EngineCreationFailureException ex) {
                assertEquals(store.refCount(), refCount);
                continue;
            }
            assertEquals(store.refCount(), refCount + 1);
            final int numStarts = scaledRandomIntBetween(1, 5);
            for (int j = 0; j < numStarts; j++) {
                try {
                    assertEquals(store.refCount(), refCount + 1);
                    holder.close();
                    holder = createEngine(store, translogPath);
                    assertEquals(store.refCount(), refCount + 1);
                } catch (EngineCreationFailureException ex) {
                    // all is fine
                    assertEquals(store.refCount(), refCount);
                    break;
                }
            }
            holder.close();
            assertEquals(store.refCount(), refCount);
        }
    }
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Path(java.nio.file.Path) ContentPath(org.elasticsearch.index.mapper.ContentPath) Store(org.elasticsearch.index.store.Store) LongPoint(org.apache.lucene.document.LongPoint)

Example 3 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project elasticsearch by elastic.

the class LuceneTests method testCleanIndex.

public void testCleanIndex() throws IOException {
    MockDirectoryWrapper dir = newMockDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig();
    iwc.setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE);
    iwc.setMergePolicy(NoMergePolicy.INSTANCE);
    iwc.setMaxBufferedDocs(2);
    IndexWriter writer = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new TextField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    writer.commit();
    doc = new Document();
    doc.add(new TextField("id", "2", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new TextField("id", "3", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    writer.commit();
    doc = new Document();
    doc.add(new TextField("id", "4", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    writer.deleteDocuments(new Term("id", "2"));
    writer.commit();
    try (DirectoryReader open = DirectoryReader.open(writer)) {
        assertEquals(3, open.numDocs());
        assertEquals(1, open.numDeletedDocs());
        assertEquals(4, open.maxDoc());
    }
    writer.close();
    if (random().nextBoolean()) {
        for (String file : dir.listAll()) {
            if (file.startsWith("_1")) {
                // delete a random file
                dir.deleteFile(file);
                break;
            }
        }
    }
    Lucene.cleanLuceneIndex(dir);
    if (dir.listAll().length > 0) {
        for (String file : dir.listAll()) {
            if (file.startsWith("extra") == false) {
                assertEquals(file, "write.lock");
            }
        }
    }
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) TextField(org.apache.lucene.document.TextField) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 4 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project elasticsearch by elastic.

the class LuceneTests method testNumDocs.

public void testNumDocs() throws IOException {
    MockDirectoryWrapper dir = newMockDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig();
    IndexWriter writer = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new TextField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    writer.commit();
    SegmentInfos segmentCommitInfos = Lucene.readSegmentInfos(dir);
    assertEquals(1, Lucene.getNumDocs(segmentCommitInfos));
    doc = new Document();
    doc.add(new TextField("id", "2", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new TextField("id", "3", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    segmentCommitInfos = Lucene.readSegmentInfos(dir);
    assertEquals(1, Lucene.getNumDocs(segmentCommitInfos));
    writer.commit();
    segmentCommitInfos = Lucene.readSegmentInfos(dir);
    assertEquals(3, Lucene.getNumDocs(segmentCommitInfos));
    writer.deleteDocuments(new Term("id", "2"));
    writer.commit();
    segmentCommitInfos = Lucene.readSegmentInfos(dir);
    assertEquals(2, Lucene.getNumDocs(segmentCommitInfos));
    int numDocsToIndex = randomIntBetween(10, 50);
    List<Term> deleteTerms = new ArrayList<>();
    for (int i = 0; i < numDocsToIndex; i++) {
        doc = new Document();
        doc.add(new TextField("id", "extra_" + i, random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
        deleteTerms.add(new Term("id", "extra_" + i));
        writer.addDocument(doc);
    }
    int numDocsToDelete = randomIntBetween(0, numDocsToIndex);
    Collections.shuffle(deleteTerms, random());
    for (int i = 0; i < numDocsToDelete; i++) {
        Term remove = deleteTerms.remove(0);
        writer.deleteDocuments(remove);
    }
    writer.commit();
    segmentCommitInfos = Lucene.readSegmentInfos(dir);
    assertEquals(2 + deleteTerms.size(), Lucene.getNumDocs(segmentCommitInfos));
    writer.close();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) SegmentInfos(org.apache.lucene.index.SegmentInfos) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) ArrayList(java.util.ArrayList) TextField(org.apache.lucene.document.TextField) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 5 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project elasticsearch by elastic.

the class LuceneTests method testWaitForIndex.

public void testWaitForIndex() throws Exception {
    final MockDirectoryWrapper dir = newMockDirectory();
    final AtomicBoolean succeeded = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    // Create a shadow Engine, which will freak out because there is no
    // index yet
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                latch.await();
                if (Lucene.waitForIndex(dir, 5000)) {
                    succeeded.set(true);
                } else {
                    fail("index should have eventually existed!");
                }
            } catch (InterruptedException e) {
            // ignore interruptions
            } catch (Exception e) {
                fail("should have been able to create the engine! " + e.getMessage());
            }
        }
    });
    t.start();
    // count down latch
    // now shadow engine should try to be created
    latch.countDown();
    IndexWriterConfig iwc = newIndexWriterConfig();
    iwc.setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE);
    iwc.setMergePolicy(NoMergePolicy.INSTANCE);
    iwc.setMaxBufferedDocs(2);
    IndexWriter writer = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new TextField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
    writer.addDocument(doc);
    writer.commit();
    t.join();
    writer.close();
    dir.close();
    assertTrue("index should have eventually existed", succeeded.get());
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) TextField(org.apache.lucene.document.TextField) CountDownLatch(java.util.concurrent.CountDownLatch) Document(org.apache.lucene.document.Document) IOException(java.io.IOException) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

MockDirectoryWrapper (org.apache.lucene.store.MockDirectoryWrapper)124 Document (org.apache.lucene.document.Document)61 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)55 IOException (java.io.IOException)34 Directory (org.apache.lucene.store.Directory)32 TextField (org.apache.lucene.document.TextField)17 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)16 RAMDirectory (org.apache.lucene.store.RAMDirectory)16 BaseDirectoryWrapper (org.apache.lucene.store.BaseDirectoryWrapper)15 FakeIOException (org.apache.lucene.store.MockDirectoryWrapper.FakeIOException)15 FieldType (org.apache.lucene.document.FieldType)14 Path (java.nio.file.Path)12 Field (org.apache.lucene.document.Field)12 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)11 Failure (org.apache.lucene.store.MockDirectoryWrapper.Failure)11 BytesRef (org.apache.lucene.util.BytesRef)11 Random (java.util.Random)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Codec (org.apache.lucene.codecs.Codec)10 LongPoint (org.apache.lucene.document.LongPoint)10