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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations