Search in sources :

Example 1 with FakeIOException

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

the class BaseSegmentInfoFormatTestCase method testExceptionOnOpenInput.

/** 
   * Test segment infos read that hits exception immediately on open.
   * make sure we get our exception back, no file handle leaks, etc. 
   */
public void testExceptionOnOpenInput() throws Exception {
    Failure fail = new Failure() {

        @Override
        public void eval(MockDirectoryWrapper dir) throws IOException {
            for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
                if (doFail && "openInput".equals(e.getMethodName())) {
                    throw new FakeIOException();
                }
            }
        }
    };
    MockDirectoryWrapper dir = newMockDirectory();
    dir.failOn(fail);
    Codec codec = getCodec();
    byte[] id = StringHelper.randomId();
    SegmentInfo info = new SegmentInfo(dir, getVersions()[0], getVersions()[0], "_123", 1, false, codec, Collections.<String, String>emptyMap(), id, new HashMap<>(), null);
    info.setFiles(Collections.<String>emptySet());
    codec.segmentInfoFormat().write(dir, info, IOContext.DEFAULT);
    fail.setDoFail();
    expectThrows(FakeIOException.class, () -> {
        codec.segmentInfoFormat().read(dir, "_123", id, IOContext.DEFAULT);
    });
    fail.clearDoFail();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) Codec(org.apache.lucene.codecs.Codec) Failure(org.apache.lucene.store.MockDirectoryWrapper.Failure)

Example 2 with FakeIOException

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

the class BaseSegmentInfoFormatTestCase method testExceptionOnCloseInput.

/** 
   * Test segment infos read that hits exception on close
   * make sure we get our exception back, no file handle leaks, etc. 
   */
public void testExceptionOnCloseInput() throws Exception {
    Failure fail = new Failure() {

        @Override
        public void eval(MockDirectoryWrapper dir) throws IOException {
            for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
                if (doFail && "close".equals(e.getMethodName())) {
                    throw new FakeIOException();
                }
            }
        }
    };
    MockDirectoryWrapper dir = newMockDirectory();
    dir.failOn(fail);
    Codec codec = getCodec();
    byte[] id = StringHelper.randomId();
    SegmentInfo info = new SegmentInfo(dir, getVersions()[0], getVersions()[0], "_123", 1, false, codec, Collections.<String, String>emptyMap(), id, new HashMap<>(), null);
    info.setFiles(Collections.<String>emptySet());
    codec.segmentInfoFormat().write(dir, info, IOContext.DEFAULT);
    fail.setDoFail();
    expectThrows(FakeIOException.class, () -> {
        codec.segmentInfoFormat().read(dir, "_123", id, IOContext.DEFAULT);
    });
    fail.clearDoFail();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) Codec(org.apache.lucene.codecs.Codec) Failure(org.apache.lucene.store.MockDirectoryWrapper.Failure)

Example 3 with FakeIOException

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

the class TestIndexWriterReader method testNRTOpenExceptions.

@Test
public void testNRTOpenExceptions() throws Exception {
    // LUCENE-5262: test that several failed attempts to obtain an NRT reader
    // don't leak file handles.
    MockDirectoryWrapper dir = (MockDirectoryWrapper) getAssertNoDeletesDirectory(newMockDirectory());
    final AtomicBoolean shouldFail = new AtomicBoolean();
    dir.failOn(new MockDirectoryWrapper.Failure() {

        @Override
        public void eval(MockDirectoryWrapper dir) throws IOException {
            StackTraceElement[] trace = new Exception().getStackTrace();
            if (shouldFail.get()) {
                for (int i = 0; i < trace.length; i++) {
                    if ("getReadOnlyClone".equals(trace[i].getMethodName())) {
                        if (VERBOSE) {
                            System.out.println("TEST: now fail; exc:");
                            new Throwable().printStackTrace(System.out);
                        }
                        shouldFail.set(false);
                        throw new FakeIOException();
                    }
                }
            }
        }
    });
    IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
    // prevent merges from getting in the way
    conf.setMergePolicy(NoMergePolicy.INSTANCE);
    IndexWriter writer = new IndexWriter(dir, conf);
    // create a segment and open an NRT reader
    writer.addDocument(new Document());
    writer.getReader().close();
    // add a new document so a new NRT reader is required
    writer.addDocument(new Document());
    // other NRT reader, since it is already marked closed!
    for (int i = 0; i < 2; i++) {
        shouldFail.set(true);
        expectThrows(FakeIOException.class, () -> {
            writer.getReader().close();
        });
    }
    writer.close();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) IOException(java.io.IOException) ThreadInterruptedException(org.apache.lucene.util.ThreadInterruptedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Test(org.junit.Test)

Example 4 with FakeIOException

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

the class TestIndexWriterExceptions method testRandomExceptionDuringRollback.

public void testRandomExceptionDuringRollback() throws Exception {
    // fail in random places on i/o
    final int numIters = RANDOM_MULTIPLIER * 75;
    for (int iter = 0; iter < numIters; iter++) {
        MockDirectoryWrapper dir = newMockDirectory();
        dir.failOn(new MockDirectoryWrapper.Failure() {

            @Override
            public void eval(MockDirectoryWrapper dir) throws IOException {
                if (random().nextInt(10) != 0) {
                    return;
                }
                boolean maybeFail = false;
                StackTraceElement[] trace = Thread.currentThread().getStackTrace();
                for (int i = 0; i < trace.length; i++) {
                    if ("rollbackInternal".equals(trace[i].getMethodName())) {
                        maybeFail = true;
                        break;
                    }
                }
                if (maybeFail) {
                    if (VERBOSE) {
                        System.out.println("TEST: now fail; thread=" + Thread.currentThread().getName() + " exc:");
                        new Throwable().printStackTrace(System.out);
                    }
                    throw new FakeIOException();
                }
            }
        });
        IndexWriterConfig iwc = new IndexWriterConfig(null);
        IndexWriter iw = new IndexWriter(dir, iwc);
        Document doc = new Document();
        for (int i = 0; i < 10; i++) {
            iw.addDocument(doc);
        }
        iw.commit();
        iw.addDocument(doc);
        // pool readers
        DirectoryReader r = DirectoryReader.open(iw);
        // sometimes sneak in a pending commit: we don't want to leak a file handle to that segments_N
        if (random().nextBoolean()) {
            iw.prepareCommit();
        }
        try {
            iw.rollback();
        } catch (FakeIOException expected) {
        // ok, we randomly hit exc here
        }
        r.close();
        // even though we hit exception: we are closed, no locks or files held, index in good state
        assertTrue(iw.isClosed());
        dir.obtainLock(IndexWriter.WRITE_LOCK_NAME).close();
        r = DirectoryReader.open(dir);
        assertEquals(10, r.maxDoc());
        r.close();
        // no leaks
        dir.close();
    }
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document)

Example 5 with FakeIOException

use of org.apache.lucene.store.MockDirectoryWrapper.FakeIOException 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)13 FakeIOException (org.apache.lucene.store.MockDirectoryWrapper.FakeIOException)13 Codec (org.apache.lucene.codecs.Codec)8 Failure (org.apache.lucene.store.MockDirectoryWrapper.Failure)8 IOException (java.io.IOException)5 Document (org.apache.lucene.document.Document)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)3 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 StringField (org.apache.lucene.document.StringField)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 TermQuery (org.apache.lucene.search.TermQuery)1 Bits (org.apache.lucene.util.Bits)1 BytesRef (org.apache.lucene.util.BytesRef)1 ThreadInterruptedException (org.apache.lucene.util.ThreadInterruptedException)1 Test (org.junit.Test)1