Search in sources :

Example 86 with MockDirectoryWrapper

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

the class TestCrash method testCrashAfterReopen.

public void testCrashAfterReopen() throws IOException {
    IndexWriter writer = initIndex(random(), false);
    MockDirectoryWrapper dir = (MockDirectoryWrapper) writer.getDirectory();
    // We create leftover files because merging could be
    // running when we crash:
    dir.setAssertNoUnrefencedFilesOnClose(false);
    writer.close();
    writer = initIndex(random(), dir, false, true);
    assertEquals(314, writer.maxDoc());
    crash(writer);
    /*
    System.out.println("\n\nTEST: open reader");
    String[] l = dir.list();
    Arrays.sort(l);
    for(int i=0;i<l.length;i++)
      System.out.println("file " + i + " = " + l[i] + " " +
    dir.fileLength(l[i]) + " bytes");
    */
    IndexReader reader = DirectoryReader.open(dir);
    assertTrue(reader.numDocs() >= 157);
    reader.close();
    // Make a new dir, copying from the crashed dir, and
    // open IW on it, to confirm IW "recovers" after a
    // crash:
    Directory dir2 = newDirectory(dir);
    dir.close();
    new RandomIndexWriter(random(), dir2).close();
    dir2.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Directory(org.apache.lucene.store.Directory)

Example 87 with MockDirectoryWrapper

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

the class TestCrash method testCrashAfterCloseNoWait.

public void testCrashAfterCloseNoWait() throws IOException {
    Random random = random();
    MockDirectoryWrapper dir = newMockDirectory(random, NoLockFactory.INSTANCE);
    IndexWriter writer = initIndex(random, dir, false, false);
    try {
        writer.commit();
    } finally {
        writer.close();
    }
    dir.crash();
    /*
    String[] l = dir.list();
    Arrays.sort(l);
    for(int i=0;i<l.length;i++)
      System.out.println("file " + i + " = " + l[i] + " " + dir.fileLength(l[i]) + " bytes");
    */
    IndexReader reader = DirectoryReader.open(dir);
    assertEquals(157, reader.numDocs());
    reader.close();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Random(java.util.Random)

Example 88 with MockDirectoryWrapper

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

the class TestTragicIndexWriterDeadlock method testDeadlockExcNRTReaderCommit.

public void testDeadlockExcNRTReaderCommit() throws Exception {
    MockDirectoryWrapper dir = newMockDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig();
    if (iwc.getMergeScheduler() instanceof ConcurrentMergeScheduler) {
        iwc.setMergeScheduler(new SuppressingConcurrentMergeScheduler() {

            @Override
            protected boolean isOK(Throwable th) {
                return true;
            }
        });
    }
    final IndexWriter w = new IndexWriter(dir, iwc);
    final CountDownLatch startingGun = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    Thread commitThread = new Thread() {

        @Override
        public void run() {
            try {
                startingGun.await();
                while (done.get() == false) {
                    w.addDocument(new Document());
                    w.commit();
                }
            } catch (Throwable t) {
                done.set(true);
            //System.out.println("commit exc:");
            //t.printStackTrace(System.out);
            }
        }
    };
    commitThread.start();
    final DirectoryReader r0 = DirectoryReader.open(w);
    Thread nrtThread = new Thread() {

        @Override
        public void run() {
            DirectoryReader r = r0;
            try {
                try {
                    startingGun.await();
                    while (done.get() == false) {
                        DirectoryReader oldReader = r;
                        DirectoryReader r2 = DirectoryReader.openIfChanged(oldReader);
                        if (r2 != null) {
                            r = r2;
                            oldReader.decRef();
                        }
                    }
                } finally {
                    r.close();
                }
            } catch (Throwable t) {
                done.set(true);
            //System.out.println("nrt exc:");
            //t.printStackTrace(System.out);
            }
        }
    };
    nrtThread.start();
    dir.setRandomIOExceptionRate(.1);
    startingGun.countDown();
    commitThread.join();
    nrtThread.join();
    dir.setRandomIOExceptionRate(0.0);
    w.close();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) Document(org.apache.lucene.document.Document)

Example 89 with MockDirectoryWrapper

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

the class TestIndexWriterExceptions method testExceptionsDuringCommit.

public void testExceptionsDuringCommit() throws Throwable {
    FailOnlyInCommit[] failures = new FailOnlyInCommit[] { // fail during global field map is written
    new FailOnlyInCommit(false, FailOnlyInCommit.PREPARE_STAGE), // fail after global field map is written
    new FailOnlyInCommit(true, FailOnlyInCommit.PREPARE_STAGE), // fail while running finishCommit    
    new FailOnlyInCommit(false, FailOnlyInCommit.FINISH_STAGE) };
    for (FailOnlyInCommit failure : failures) {
        MockDirectoryWrapper dir = newMockDirectory();
        dir.setFailOnCreateOutput(false);
        int fileCount = dir.listAll().length;
        IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
        Document doc = new Document();
        doc.add(newTextField("field", "a field", Field.Store.YES));
        w.addDocument(doc);
        dir.failOn(failure);
        expectThrows(RuntimeException.class, () -> {
            w.close();
        });
        assertTrue("failOnCommit=" + failure.failOnCommit + " failOnDeleteFile=" + failure.failOnDeleteFile, failure.failOnCommit && failure.failOnDeleteFile);
        w.rollback();
        String[] files = dir.listAll();
        assertTrue(files.length == fileCount || (files.length == fileCount + 1 && Arrays.asList(files).contains(IndexWriter.WRITE_LOCK_NAME)));
        dir.close();
    }
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document)

Example 90 with MockDirectoryWrapper

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

the class TestIndexWriterExceptions method testMergeExceptionIsTragic.

public void testMergeExceptionIsTragic() throws Exception {
    MockDirectoryWrapper dir = newMockDirectory();
    final AtomicBoolean didFail = new AtomicBoolean();
    dir.failOn(new MockDirectoryWrapper.Failure() {

        @Override
        public void eval(MockDirectoryWrapper dir) throws IOException {
            if (random().nextInt(10) != 0) {
                return;
            }
            if (didFail.get()) {
                // Already failed
                return;
            }
            StackTraceElement[] trace = Thread.currentThread().getStackTrace();
            for (int i = 0; i < trace.length; i++) {
                if ("merge".equals(trace[i].getMethodName())) {
                    if (VERBOSE) {
                        System.out.println("TEST: now fail; thread=" + Thread.currentThread().getName() + " exc:");
                        new Throwable().printStackTrace(System.out);
                    }
                    didFail.set(true);
                    throw new FakeIOException();
                }
            }
        }
    });
    IndexWriterConfig iwc = newIndexWriterConfig();
    MergePolicy mp = iwc.getMergePolicy();
    if (mp instanceof TieredMergePolicy) {
        TieredMergePolicy tmp = (TieredMergePolicy) mp;
        if (tmp.getMaxMergedSegmentMB() < 0.2) {
            tmp.setMaxMergedSegmentMB(0.2);
        }
    }
    MergeScheduler ms = iwc.getMergeScheduler();
    if (ms instanceof ConcurrentMergeScheduler) {
        ((ConcurrentMergeScheduler) ms).setSuppressExceptions();
    }
    IndexWriter w = new IndexWriter(dir, iwc);
    while (true) {
        try {
            Document doc = new Document();
            doc.add(newStringField("field", "string", Field.Store.NO));
            w.addDocument(doc);
            if (random().nextInt(10) == 7) {
                // Flush new segment:
                DirectoryReader.open(w).close();
            }
        } catch (AlreadyClosedException ace) {
            // OK: e.g. CMS hit the exc in BG thread and closed the writer
            break;
        } catch (FakeIOException fioe) {
            // OK: e.g. SMS hit the exception
            break;
        }
    }
    assertNotNull(w.getTragicException());
    assertFalse(w.isOpen());
    assertTrue(didFail.get());
    if (ms instanceof ConcurrentMergeScheduler) {
        // Sneaky: CMS's merge thread will be concurrently rolling back IW due
        // to the tragedy, with this main thread, so we have to wait here
        // to ensure the rollback has finished, else MDW still sees open files:
        ((ConcurrentMergeScheduler) ms).sync();
    }
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) IOException(java.io.IOException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) Document(org.apache.lucene.document.Document) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException)

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