Search in sources :

Example 6 with InfoStream

use of org.apache.lucene.util.InfoStream in project lucene-solr by apache.

the class TestIndexWriterExceptions method testExceptionDuringRollback.

public void testExceptionDuringRollback() throws Exception {
    // currently: fail in two different places
    final String messageToFailOn = random().nextBoolean() ? "rollback: done finish merges" : "rollback before checkpoint";
    // infostream that throws exception during rollback
    InfoStream evilInfoStream = new InfoStream() {

        @Override
        public void message(String component, String message) {
            if (messageToFailOn.equals(message)) {
                throw new RuntimeException("BOOM!");
            }
        }

        @Override
        public boolean isEnabled(String component) {
            return true;
        }

        @Override
        public void close() throws IOException {
        }
    };
    // we want to ensure we don't leak any locks or file handles
    Directory dir = newMockDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(null);
    iwc.setInfoStream(evilInfoStream);
    IndexWriter iw = new IndexWriter(dir, iwc);
    // TODO: cutover to RandomIndexWriter.mockIndexWriter?
    iw.enableTestPoints = true;
    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();
    }
    RuntimeException expected = expectThrows(RuntimeException.class, () -> {
        iw.rollback();
    });
    assertEquals("BOOM!", expected.getMessage());
    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 : InfoStream(org.apache.lucene.util.InfoStream) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 7 with InfoStream

use of org.apache.lucene.util.InfoStream in project lucene-solr by apache.

the class TestIndexWriterExceptions method testOutOfMemoryErrorCausesCloseToFail.

// LUCENE-1429
public void testOutOfMemoryErrorCausesCloseToFail() throws Exception {
    final AtomicBoolean thrown = new AtomicBoolean(false);
    final Directory dir = newDirectory();
    final IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setInfoStream(new InfoStream() {

        @Override
        public void message(String component, final String message) {
            if (message.startsWith("now flush at close") && thrown.compareAndSet(false, true)) {
                throw new OutOfMemoryError("fake OOME at " + message);
            }
        }

        @Override
        public boolean isEnabled(String component) {
            return true;
        }

        @Override
        public void close() {
        }
    }));
    expectThrows(OutOfMemoryError.class, () -> {
        writer.close();
    });
    // throws IllegalStateEx w/o bug fix
    writer.close();
    dir.close();
}
Also used : InfoStream(org.apache.lucene.util.InfoStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 8 with InfoStream

use of org.apache.lucene.util.InfoStream in project lucene-solr by apache.

the class TestIndexWriterExceptions method testOutOfMemoryErrorRollback.

/** If IW hits OOME during indexing, it should refuse to commit any further changes. */
public void testOutOfMemoryErrorRollback() throws Exception {
    final AtomicBoolean thrown = new AtomicBoolean(false);
    final Directory dir = newDirectory();
    final IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setInfoStream(new InfoStream() {

        @Override
        public void message(String component, final String message) {
            if (message.contains("startFullFlush") && thrown.compareAndSet(false, true)) {
                throw new OutOfMemoryError("fake OOME at " + message);
            }
        }

        @Override
        public boolean isEnabled(String component) {
            return true;
        }

        @Override
        public void close() {
        }
    }));
    writer.addDocument(new Document());
    expectThrows(OutOfMemoryError.class, () -> {
        writer.commit();
    });
    try {
        writer.close();
    } catch (IllegalArgumentException ok) {
    // ok
    }
    expectThrows(AlreadyClosedException.class, () -> {
        writer.addDocument(new Document());
    });
    // IW should have done rollback() during close, since it hit OOME, and so no index should exist:
    assertFalse(DirectoryReader.indexExists(dir));
    dir.close();
}
Also used : InfoStream(org.apache.lucene.util.InfoStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 9 with InfoStream

use of org.apache.lucene.util.InfoStream in project lucene-solr by apache.

the class TestIndexWriter method testCloseWhileMergeIsRunning.

public void testCloseWhileMergeIsRunning() throws IOException {
    Directory dir = newDirectory();
    final CountDownLatch mergeStarted = new CountDownLatch(1);
    final CountDownLatch closeStarted = new CountDownLatch(1);
    IndexWriterConfig iwc = newIndexWriterConfig(random(), new MockAnalyzer(random())).setCommitOnClose(false);
    LogDocMergePolicy mp = new LogDocMergePolicy();
    mp.setMergeFactor(2);
    iwc.setMergePolicy(mp);
    iwc.setInfoStream(new InfoStream() {

        @Override
        public boolean isEnabled(String component) {
            return true;
        }

        @Override
        public void message(String component, String message) {
            if (message.equals("rollback")) {
                closeStarted.countDown();
            }
        }

        @Override
        public void close() {
        }
    });
    iwc.setMergeScheduler(new ConcurrentMergeScheduler() {

        @Override
        public void doMerge(IndexWriter writer, MergePolicy.OneMerge merge) throws IOException {
            mergeStarted.countDown();
            try {
                closeStarted.await();
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(ie);
            }
            super.doMerge(writer, merge);
        }

        @Override
        public void close() {
        }
    });
    IndexWriter w = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new SortedDocValuesField("dv", new BytesRef("foo!")));
    w.addDocument(doc);
    w.commit();
    w.addDocument(doc);
    w.commit();
    w.close();
    dir.close();
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Document(org.apache.lucene.document.Document) ThreadInterruptedException(org.apache.lucene.util.ThreadInterruptedException) InfoStream(org.apache.lucene.util.InfoStream) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory)

Example 10 with InfoStream

use of org.apache.lucene.util.InfoStream in project lucene-solr by apache.

the class TestInfoStream method testTestPointsOff.

/** we shouldn't have test points unless we ask */
public void testTestPointsOff() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(null);
    iwc.setInfoStream(new InfoStream() {

        @Override
        public void close() throws IOException {
        }

        @Override
        public void message(String component, String message) {
            assertFalse("TP".equals(component));
        }

        @Override
        public boolean isEnabled(String component) {
            assertFalse("TP".equals(component));
            return true;
        }
    });
    IndexWriter iw = new IndexWriter(dir, iwc);
    iw.addDocument(new Document());
    iw.close();
    dir.close();
}
Also used : InfoStream(org.apache.lucene.util.InfoStream) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory)

Aggregations

InfoStream (org.apache.lucene.util.InfoStream)12 Directory (org.apache.lucene.store.Directory)9 Document (org.apache.lucene.document.Document)6 IOException (java.io.IOException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)4 RAMDirectory (org.apache.lucene.store.RAMDirectory)4 FSDirectory (org.apache.lucene.store.FSDirectory)3 IndexWriter (org.apache.lucene.index.IndexWriter)2 Closeable (java.io.Closeable)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1