Search in sources :

Example 36 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project lucene-solr by apache.

the class TestIndexFileDeleter method testSegmentNameInflation.

public void testSegmentNameInflation() throws IOException {
    MockDirectoryWrapper dir = newMockDirectory();
    // empty commit
    new IndexWriter(dir, new IndexWriterConfig(null)).close();
    SegmentInfos sis = SegmentInfos.readLatestCommit(dir);
    assertEquals(0, sis.counter);
    // no inflation
    inflateGens(sis, Arrays.asList(dir.listAll()), InfoStream.getDefault());
    assertEquals(0, sis.counter);
    // add trash per-segment file
    dir.createOutput(IndexFileNames.segmentFileName("_0", "", "foo"), IOContext.DEFAULT).close();
    // ensure inflation
    inflateGens(sis, Arrays.asList(dir.listAll()), InfoStream.getDefault());
    assertEquals(1, sis.counter);
    // add trash per-segment file
    dir.createOutput(IndexFileNames.segmentFileName("_3", "", "foo"), IOContext.DEFAULT).close();
    inflateGens(sis, Arrays.asList(dir.listAll()), InfoStream.getDefault());
    assertEquals(4, sis.counter);
    // ensure we write _4 segment next
    IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(null));
    iw.addDocument(new Document());
    iw.commit();
    iw.close();
    sis = SegmentInfos.readLatestCommit(dir);
    assertEquals("_4", sis.info(0).info.name);
    assertEquals(5, sis.counter);
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Document(org.apache.lucene.document.Document)

Example 37 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project lucene-solr by apache.

the class TestIndexFileDeleter method testExcInDecRef.

// LUCENE-5919
public void testExcInDecRef() throws Throwable {
    MockDirectoryWrapper dir = newMockDirectory();
    // disable slow things: we don't rely upon sleeps here.
    dir.setThrottling(MockDirectoryWrapper.Throttling.NEVER);
    dir.setUseSlowOpenClosers(false);
    final AtomicBoolean doFailExc = new AtomicBoolean();
    dir.failOn(new MockDirectoryWrapper.Failure() {

        @Override
        public void eval(MockDirectoryWrapper dir) throws IOException {
            if (doFailExc.get() && random().nextInt(4) == 1) {
                Exception e = new Exception();
                StackTraceElement[] stack = e.getStackTrace();
                for (int i = 0; i < stack.length; i++) {
                    if (stack[i].getClassName().equals(IndexFileDeleter.class.getName()) && stack[i].getMethodName().equals("decRef")) {
                        throw new RuntimeException("fake fail");
                    }
                }
            }
        }
    });
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    //iwc.setMergeScheduler(new SerialMergeScheduler());
    MergeScheduler ms = iwc.getMergeScheduler();
    if (ms instanceof ConcurrentMergeScheduler) {
        final ConcurrentMergeScheduler suppressFakeFail = new ConcurrentMergeScheduler() {

            @Override
            protected void handleMergeException(Directory dir, Throwable exc) {
                // suppress only FakeIOException:
                if (exc instanceof RuntimeException && exc.getMessage().equals("fake fail")) {
                // ok to ignore
                } else if ((exc instanceof AlreadyClosedException || exc instanceof IllegalStateException) && exc.getCause() != null && "fake fail".equals(exc.getCause().getMessage())) {
                // also ok to ignore
                } else {
                    super.handleMergeException(dir, exc);
                }
            }
        };
        final ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler) ms;
        suppressFakeFail.setMaxMergesAndThreads(cms.getMaxMergeCount(), cms.getMaxThreadCount());
        iwc.setMergeScheduler(suppressFakeFail);
    }
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    // Since we hit exc during merging, a partial
    // forceMerge can easily return when there are still
    // too many segments in the index:
    w.setDoRandomForceMergeAssert(false);
    doFailExc.set(true);
    int ITERS = atLeast(1000);
    for (int iter = 0; iter < ITERS; iter++) {
        try {
            if (random().nextInt(10) == 5) {
                w.commit();
            } else if (random().nextInt(10) == 7) {
                w.getReader().close();
            } else {
                Document doc = new Document();
                doc.add(newTextField("field", "some text", Field.Store.NO));
                w.addDocument(doc);
            }
        } catch (Throwable t) {
            if (t.toString().contains("fake fail") || (t.getCause() != null && t.getCause().toString().contains("fake fail"))) {
            // ok
            } else {
                throw t;
            }
        }
    }
    doFailExc.set(false);
    w.close();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) Document(org.apache.lucene.document.Document) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Directory(org.apache.lucene.store.Directory)

Example 38 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project lucene-solr by apache.

the class TestIndexFileDeleter method testGenerationInflation.

public void testGenerationInflation() throws IOException {
    MockDirectoryWrapper dir = newMockDirectory();
    // initial commit
    IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(null));
    iw.addDocument(new Document());
    iw.commit();
    iw.close();
    // no deletes: start at 1
    SegmentInfos sis = SegmentInfos.readLatestCommit(dir);
    assertEquals(1, sis.info(0).getNextDelGen());
    // no inflation
    inflateGens(sis, Arrays.asList(dir.listAll()), InfoStream.getDefault());
    assertEquals(1, sis.info(0).getNextDelGen());
    // add trash per-segment deletes file
    dir.createOutput(IndexFileNames.fileNameFromGeneration("_0", "del", 2), IOContext.DEFAULT).close();
    // ensure inflation
    inflateGens(sis, Arrays.asList(dir.listAll()), InfoStream.getDefault());
    assertEquals(3, sis.info(0).getNextDelGen());
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Document(org.apache.lucene.document.Document)

Example 39 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project lucene-solr by apache.

the class TestIndexFileDeleter method testExcInDeleteFile.

// LUCENE-6835: make sure best-effort to not create an "apparently but not really" corrupt index is working:
public void testExcInDeleteFile() throws Throwable {
    int iters = atLeast(10);
    for (int iter = 0; iter < iters; iter++) {
        if (VERBOSE) {
            System.out.println("TEST: iter=" + iter);
        }
        MockDirectoryWrapper dir = newMockDirectory();
        final AtomicBoolean doFailExc = new AtomicBoolean();
        dir.failOn(new MockDirectoryWrapper.Failure() {

            @Override
            public void eval(MockDirectoryWrapper dir) throws IOException {
                if (doFailExc.get() && random().nextInt(4) == 1) {
                    Exception e = new Exception();
                    StackTraceElement[] stack = e.getStackTrace();
                    for (int i = 0; i < stack.length; i++) {
                        if (stack[i].getClassName().equals(MockDirectoryWrapper.class.getName()) && stack[i].getMethodName().equals("deleteFile")) {
                            throw new MockDirectoryWrapper.FakeIOException();
                        }
                    }
                }
            }
        });
        IndexWriterConfig iwc = newIndexWriterConfig();
        iwc.setMergeScheduler(new SerialMergeScheduler());
        RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
        w.addDocument(new Document());
        // makes segments_1
        if (VERBOSE) {
            System.out.println("TEST: now commit");
        }
        w.commit();
        w.addDocument(new Document());
        doFailExc.set(true);
        if (VERBOSE) {
            System.out.println("TEST: now close");
        }
        try {
            w.close();
            if (VERBOSE) {
                System.out.println("TEST: no exception (ok)");
            }
        } catch (RuntimeException re) {
            assertTrue(re.getCause() instanceof MockDirectoryWrapper.FakeIOException);
            // good
            if (VERBOSE) {
                System.out.println("TEST: got expected exception:");
                re.printStackTrace(System.out);
            }
        } catch (MockDirectoryWrapper.FakeIOException fioe) {
            // good
            if (VERBOSE) {
                System.out.println("TEST: got expected exception:");
                fioe.printStackTrace(System.out);
            }
        }
        doFailExc.set(false);
        assertFalse(w.w.isOpen());
        for (String name : dir.listAll()) {
            if (name.startsWith(IndexFileNames.SEGMENTS)) {
                if (VERBOSE) {
                    System.out.println("TEST: now read " + name);
                }
                SegmentInfos.readCommit(dir, name);
            }
        }
        dir.close();
    }
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Document(org.apache.lucene.document.Document) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 40 with MockDirectoryWrapper

use of org.apache.lucene.store.MockDirectoryWrapper in project lucene-solr by apache.

the class TestTransactions method testTransactions.

public void testTransactions() throws Throwable {
    // we cant use non-ramdir on windows, because this test needs to double-write.
    MockDirectoryWrapper dir1 = new MockDirectoryWrapper(random(), new RAMDirectory());
    MockDirectoryWrapper dir2 = new MockDirectoryWrapper(random(), new RAMDirectory());
    dir1.failOn(new RandomFailure());
    dir2.failOn(new RandomFailure());
    dir1.setFailOnOpenInput(false);
    dir2.setFailOnOpenInput(false);
    // We throw exceptions in deleteFile, which creates
    // leftover files:
    dir1.setAssertNoUnrefencedFilesOnClose(false);
    dir2.setAssertNoUnrefencedFilesOnClose(false);
    initIndex(dir1);
    initIndex(dir2);
    TimedThread[] threads = new TimedThread[3];
    int numThread = 0;
    IndexerThread indexerThread = new IndexerThread(this, dir1, dir2, threads);
    threads[numThread++] = indexerThread;
    indexerThread.start();
    SearcherThread searcherThread1 = new SearcherThread(this, dir1, dir2, threads);
    threads[numThread++] = searcherThread1;
    searcherThread1.start();
    SearcherThread searcherThread2 = new SearcherThread(this, dir1, dir2, threads);
    threads[numThread++] = searcherThread2;
    searcherThread2.start();
    for (int i = 0; i < numThread; i++) threads[i].join();
    for (int i = 0; i < numThread; i++) assertTrue(!threads[i].failed);
    dir1.close();
    dir2.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Aggregations

MockDirectoryWrapper (org.apache.lucene.store.MockDirectoryWrapper)121 Document (org.apache.lucene.document.Document)61 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)55 Directory (org.apache.lucene.store.Directory)32 IOException (java.io.IOException)30 TextField (org.apache.lucene.document.TextField)17 RAMDirectory (org.apache.lucene.store.RAMDirectory)17 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)15 BaseDirectoryWrapper (org.apache.lucene.store.BaseDirectoryWrapper)15 FakeIOException (org.apache.lucene.store.MockDirectoryWrapper.FakeIOException)15 FieldType (org.apache.lucene.document.FieldType)14 Field (org.apache.lucene.document.Field)12 Random (java.util.Random)11 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)11 Failure (org.apache.lucene.store.MockDirectoryWrapper.Failure)11 BytesRef (org.apache.lucene.util.BytesRef)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Codec (org.apache.lucene.codecs.Codec)10 StringField (org.apache.lucene.document.StringField)9 IndexSearcher (org.apache.lucene.search.IndexSearcher)9