From ac4674aa2004fe04baffb5260db12d2df5751fcc Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Thu, 4 Nov 2010 16:52:27 +0000 Subject: [PATCH] LUCENE-2739: refactor TestIndexWriter, pull out _OnDiskFull and _WithThreads git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1031076 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/index/TestIndexWriter.java | 848 +----------------- .../index/TestIndexWriterOnDiskFull.java | 525 +++++++++++ .../index/TestIndexWriterWithThreads.java | 422 +++++++++ 3 files changed, 948 insertions(+), 847 deletions(-) create mode 100644 lucene/src/test/org/apache/lucene/index/TestIndexWriterOnDiskFull.java create mode 100644 lucene/src/test/org/apache/lucene/index/TestIndexWriterWithThreads.java diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java index fdab74d588f..3684e3f6cad 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -159,377 +159,7 @@ public class TestIndexWriter extends LuceneTestCase { writer.addDocument(doc); } - /* - Test: make sure when we run out of disk space or hit - random IOExceptions in any of the addIndexes(*) calls - that 1) index is not corrupt (searcher can open/search - it) and 2) transactional semantics are followed: - either all or none of the incoming documents were in - fact added. - */ - public void testAddIndexOnDiskFull() throws IOException - { - int START_COUNT = 57; - int NUM_DIR = 50; - int END_COUNT = START_COUNT + NUM_DIR*25; - - // Build up a bunch of dirs that have indexes which we - // will then merge together by calling addIndexes(*): - Directory[] dirs = new Directory[NUM_DIR]; - long inputDiskUsage = 0; - for(int i=0;i= 2.0) { - rate /= 2; - } - if (diskRatio >= 4.0) { - rate /= 2; - } - if (diskRatio >= 6.0) { - rate = 0.0; - } - if (VERBOSE) - testName = "disk full test " + methodName + " with disk full at " + diskFree + " bytes"; - } else { - thisDiskFree = 0; - rate = 0.0; - if (VERBOSE) - testName = "disk full test " + methodName + " with unlimited disk space"; - } - - if (VERBOSE) - System.out.println("\ncycle: " + testName); - - dir.setTrackDiskUsage(true); - dir.setMaxSizeInBytes(thisDiskFree); - dir.setRandomIOExceptionRate(rate, diskFree); - - try { - - if (0 == method) { - writer.addIndexes(dirs); - writer.optimize(); - } else if (1 == method) { - IndexReader readers[] = new IndexReader[dirs.length]; - for(int i=0;i= 5) - break; - } else { - if (noErrors) { - System.out.println(Thread.currentThread().getName() + ": ERROR: unexpected IOException:"); - ioe.printStackTrace(System.out); - error = ioe; - } - break; - } - } catch (Throwable t) { - //t.printStackTrace(System.out); - if (noErrors) { - System.out.println(Thread.currentThread().getName() + ": ERROR: unexpected Throwable:"); - t.printStackTrace(System.out); - error = t; - } - break; - } - } while(System.currentTimeMillis() < stopTime); - } - } - - // LUCENE-1130: make sure we can close() even while - // threads are trying to add documents. Strictly - // speaking, this isn't valid us of Lucene's APIs, but we - // still want to be robust to this case: - public void testCloseWithThreads() throws Exception { - int NUM_THREADS = 3; - - for(int iter=0;iter<7;iter++) { - Directory dir = newDirectory(); - IndexWriterConfig conf = newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()) - .setMaxBufferedDocs(10).setMergeScheduler(new ConcurrentMergeScheduler()); - // We expect AlreadyClosedException - ((ConcurrentMergeScheduler) conf.getMergeScheduler()).setSuppressExceptions(); - IndexWriter writer = new IndexWriter(dir, conf); - ((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(4); - - IndexerThread[] threads = new IndexerThread[NUM_THREADS]; - - for(int i=0;i 0) { - done = true; - break; - } - } - - writer.close(false); - - // Make sure threads that are adding docs are not hung: - for(int i=0;i 0); - reader.close(); - - dir.close(); - } - } - - // LUCENE-1130: make sure immeidate disk full on creating - // an IndexWriter (hit during DW.ThreadState.init()) is - // OK: - public void testImmediateDiskFull() throws IOException { - MockDirectoryWrapper dir = newDirectory(); - IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()) - .setMaxBufferedDocs(2).setMergeScheduler(new ConcurrentMergeScheduler())); - dir.setMaxSizeInBytes(Math.max(1, dir.getRecomputedActualSizeInBytes())); - final Document doc = new Document(); - doc.add(newField("field", "aaa bbb ccc ddd eee fff ggg hhh iii jjj", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); - try { - writer.addDocument(doc); - fail("did not hit disk full"); - } catch (IOException ioe) { - } - // Without fix for LUCENE-1130: this call will hang: - try { - writer.addDocument(doc); - fail("did not hit disk full"); - } catch (IOException ioe) { - } - try { - writer.close(false); - fail("did not hit disk full"); - } catch (IOException ioe) { - } - - // Make sure once disk space is avail again, we can - // cleanly close: - dir.setMaxSizeInBytes(0); - writer.close(false); - dir.close(); - } - - // LUCENE-1130: make sure immediate disk full on creating - // an IndexWriter (hit during DW.ThreadState.init()), with - // multiple threads, is OK: - public void testImmediateDiskFullWithThreads() throws Exception { - - int NUM_THREADS = 3; - - for(int iter=0;iter<10;iter++) { - MockDirectoryWrapper dir = newDirectory(); - IndexWriterConfig conf = newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()) - .setMaxBufferedDocs(2).setMergeScheduler(new ConcurrentMergeScheduler()); - // We expect disk full exceptions in the merge threads - ((ConcurrentMergeScheduler) conf.getMergeScheduler()).setSuppressExceptions(); - IndexWriter writer = new IndexWriter(dir, conf); - ((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(4); - dir.setMaxSizeInBytes(4*1024+20*iter); - - IndexerThread[] threads = new IndexerThread[NUM_THREADS]; - - for(int i=0;i= 2.0) { + rate /= 2; + } + if (diskRatio >= 4.0) { + rate /= 2; + } + if (diskRatio >= 6.0) { + rate = 0.0; + } + if (VERBOSE) + testName = "disk full test " + methodName + " with disk full at " + diskFree + " bytes"; + } else { + thisDiskFree = 0; + rate = 0.0; + if (VERBOSE) + testName = "disk full test " + methodName + " with unlimited disk space"; + } + + if (VERBOSE) + System.out.println("\ncycle: " + testName); + + dir.setTrackDiskUsage(true); + dir.setMaxSizeInBytes(thisDiskFree); + dir.setRandomIOExceptionRate(rate, diskFree); + + try { + + if (0 == method) { + writer.addIndexes(dirs); + writer.optimize(); + } else if (1 == method) { + IndexReader readers[] = new IndexReader[dirs.length]; + for(int i=0;i= 5) + break; + } else { + if (noErrors) { + System.out.println(Thread.currentThread().getName() + ": ERROR: unexpected IOException:"); + ioe.printStackTrace(System.out); + error = ioe; + } + break; + } + } catch (Throwable t) { + //t.printStackTrace(System.out); + if (noErrors) { + System.out.println(Thread.currentThread().getName() + ": ERROR: unexpected Throwable:"); + t.printStackTrace(System.out); + error = t; + } + break; + } + } while(System.currentTimeMillis() < stopTime); + } + } + + // LUCENE-1130: make sure immediate disk full on creating + // an IndexWriter (hit during DW.ThreadState.init()), with + // multiple threads, is OK: + public void testImmediateDiskFullWithThreads() throws Exception { + + int NUM_THREADS = 3; + + for(int iter=0;iter<10;iter++) { + MockDirectoryWrapper dir = newDirectory(); + IndexWriterConfig conf = newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer()) + .setMaxBufferedDocs(2).setMergeScheduler(new ConcurrentMergeScheduler()); + // We expect disk full exceptions in the merge threads + ((ConcurrentMergeScheduler) conf.getMergeScheduler()).setSuppressExceptions(); + IndexWriter writer = new IndexWriter(dir, conf); + ((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(4); + dir.setMaxSizeInBytes(4*1024+20*iter); + + IndexerThread[] threads = new IndexerThread[NUM_THREADS]; + + for(int i=0;i 0) { + done = true; + break; + } + } + + writer.close(false); + + // Make sure threads that are adding docs are not hung: + for(int i=0;i 0); + reader.close(); + + dir.close(); + } + } + + // Runs test, with multiple threads, using the specific + // failure to trigger an IOException + public void _testMultipleThreadsFailure(MockDirectoryWrapper.Failure failure) throws Exception { + + int NUM_THREADS = 3; + + for(int iter=0;iter<2;iter++) { + MockDirectoryWrapper dir = newDirectory(); + IndexWriterConfig conf = newIndexWriterConfig( TEST_VERSION_CURRENT, + new MockAnalyzer()).setMaxBufferedDocs(2).setMergeScheduler(new ConcurrentMergeScheduler()); + // We expect disk full exceptions in the merge threads + ((ConcurrentMergeScheduler) conf.getMergeScheduler()).setSuppressExceptions(); + IndexWriter writer = new IndexWriter(dir, conf); + ((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(4); + + IndexerThread[] threads = new IndexerThread[NUM_THREADS]; + + for(int i=0;i