pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / index / TestIndexWriterOnDiskFull.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.analysis.MockAnalyzer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
26 import org.apache.lucene.search.IndexSearcher;
27 import org.apache.lucene.search.ScoreDoc;
28 import org.apache.lucene.search.TermQuery;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.store.MockDirectoryWrapper;
31 import org.apache.lucene.store.RAMDirectory;
32 import org.apache.lucene.util.LuceneTestCase;
33 import org.apache.lucene.util._TestUtil;
34
35 import static org.apache.lucene.index.TestIndexWriter.assertNoUnreferencedFiles;
36
37 /**
38  * Tests for IndexWriter when the disk runs out of space
39  */
40 public class TestIndexWriterOnDiskFull extends LuceneTestCase {
41
42   /*
43    * Make sure IndexWriter cleans up on hitting a disk
44    * full exception in addDocument.
45    * TODO: how to do this on windows with FSDirectory?
46    */
47   public void testAddDocumentOnDiskFull() throws IOException {
48
49     for(int pass=0;pass<2;pass++) {
50       if (VERBOSE) {
51         System.out.println("TEST: pass=" + pass);
52       }
53       boolean doAbort = pass == 1;
54       long diskFree = _TestUtil.nextInt(random, 100, 300);
55       while(true) {
56         if (VERBOSE) {
57           System.out.println("TEST: cycle: diskFree=" + diskFree);
58         }
59         MockDirectoryWrapper dir = new MockDirectoryWrapper(random, new RAMDirectory());
60         dir.setMaxSizeInBytes(diskFree);
61         IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
62         writer.setInfoStream(VERBOSE ? System.out : null);
63         MergeScheduler ms = writer.getConfig().getMergeScheduler();
64         if (ms instanceof ConcurrentMergeScheduler) {
65           // This test intentionally produces exceptions
66           // in the threads that CMS launches; we don't
67           // want to pollute test output with these.
68           ((ConcurrentMergeScheduler) ms).setSuppressExceptions();
69         }
70
71         boolean hitError = false;
72         try {
73           for(int i=0;i<200;i++) {
74             addDoc(writer);
75           }
76           if (VERBOSE) {
77             System.out.println("TEST: done adding docs; now commit");
78           }
79           writer.commit();
80         } catch (IOException e) {
81           if (VERBOSE) {
82             System.out.println("TEST: exception on addDoc");
83             e.printStackTrace(System.out);
84           }
85           hitError = true;
86         }
87
88         if (hitError) {
89           if (doAbort) {
90             if (VERBOSE) {
91               System.out.println("TEST: now rollback");
92             }
93             writer.rollback();
94           } else {
95             try {
96               if (VERBOSE) {
97                 System.out.println("TEST: now close");
98               }
99               writer.close();
100             } catch (IOException e) {
101               if (VERBOSE) {
102                 System.out.println("TEST: exception on close; retry w/ no disk space limit");
103                 e.printStackTrace(System.out);
104               }
105               dir.setMaxSizeInBytes(0);
106               writer.close();
107             }
108           }
109
110           //_TestUtil.syncConcurrentMerges(ms);
111
112           if (_TestUtil.anyFilesExceptWriteLock(dir)) {
113             assertNoUnreferencedFiles(dir, "after disk full during addDocument");
114             
115             // Make sure reader can open the index:
116             IndexReader.open(dir, true).close();
117           }
118             
119           dir.close();
120           // Now try again w/ more space:
121
122           diskFree += TEST_NIGHTLY ? _TestUtil.nextInt(random, 400, 600) : _TestUtil.nextInt(random, 3000, 5000);
123         } else {
124           //_TestUtil.syncConcurrentMerges(writer);
125           dir.setMaxSizeInBytes(0);
126           writer.close();
127           dir.close();
128           break;
129         }
130       }
131     }
132   }
133
134   // TODO: make @Nightly variant that provokes more disk
135   // fulls
136
137   // TODO: have test fail if on any given top
138   // iter there was not a single IOE hit
139
140   /*
141   Test: make sure when we run out of disk space or hit
142   random IOExceptions in any of the addIndexes(*) calls
143   that 1) index is not corrupt (searcher can open/search
144   it) and 2) transactional semantics are followed:
145   either all or none of the incoming documents were in
146   fact added.
147    */
148   public void testAddIndexOnDiskFull() throws IOException
149   {
150     int START_COUNT = 57;
151     int NUM_DIR = 50;
152     int END_COUNT = START_COUNT + NUM_DIR*25;
153     
154     // Build up a bunch of dirs that have indexes which we
155     // will then merge together by calling addIndexes(*):
156     Directory[] dirs = new Directory[NUM_DIR];
157     long inputDiskUsage = 0;
158     for(int i=0;i<NUM_DIR;i++) {
159       dirs[i] = newDirectory();
160       IndexWriter writer  = new IndexWriter(dirs[i], newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
161       for(int j=0;j<25;j++) {
162         addDocWithIndex(writer, 25*i+j);
163       }
164       writer.close();
165       String[] files = dirs[i].listAll();
166       for(int j=0;j<files.length;j++) {
167         inputDiskUsage += dirs[i].fileLength(files[j]);
168       }
169     }
170     
171     // Now, build a starting index that has START_COUNT docs.  We
172     // will then try to addIndexesNoOptimize into a copy of this:
173     MockDirectoryWrapper startDir = newDirectory();
174     IndexWriter writer = new IndexWriter(startDir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
175     for(int j=0;j<START_COUNT;j++) {
176       addDocWithIndex(writer, j);
177     }
178     writer.close();
179     
180     // Make sure starting index seems to be working properly:
181     Term searchTerm = new Term("content", "aaa");        
182     IndexReader reader = IndexReader.open(startDir, true);
183     assertEquals("first docFreq", 57, reader.docFreq(searchTerm));
184     
185     IndexSearcher searcher = newSearcher(reader);
186     ScoreDoc[] hits = searcher.search(new TermQuery(searchTerm), null, 1000).scoreDocs;
187     assertEquals("first number of hits", 57, hits.length);
188     searcher.close();
189     reader.close();
190     
191     // Iterate with larger and larger amounts of free
192     // disk space.  With little free disk space,
193     // addIndexes will certainly run out of space &
194     // fail.  Verify that when this happens, index is
195     // not corrupt and index in fact has added no
196     // documents.  Then, we increase disk space by 2000
197     // bytes each iteration.  At some point there is
198     // enough free disk space and addIndexes should
199     // succeed and index should show all documents were
200     // added.
201     
202     // String[] files = startDir.listAll();
203     long diskUsage = startDir.sizeInBytes();
204     
205     long startDiskUsage = 0;
206     String[] files = startDir.listAll();
207     for(int i=0;i<files.length;i++) {
208       startDiskUsage += startDir.fileLength(files[i]);
209     }
210     
211     for(int iter=0;iter<3;iter++) {
212       
213       if (VERBOSE)
214         System.out.println("TEST: iter=" + iter);
215       
216       // Start with 100 bytes more than we are currently using:
217       long diskFree = diskUsage+_TestUtil.nextInt(random, 50, 200);
218       
219       int method = iter;
220       
221       boolean success = false;
222       boolean done = false;
223       
224       String methodName;
225       if (0 == method) {
226         methodName = "addIndexes(Directory[]) + optimize()";
227       } else if (1 == method) {
228         methodName = "addIndexes(IndexReader[])";
229       } else {
230         methodName = "addIndexes(Directory[])";
231       }
232       
233       while(!done) {
234         if (VERBOSE) {
235           System.out.println("TEST: cycle...");
236         }
237         
238         // Make a new dir that will enforce disk usage:
239         MockDirectoryWrapper dir = new MockDirectoryWrapper(random, new RAMDirectory(startDir));
240         writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND).setMergePolicy(newLogMergePolicy()));
241         IOException err = null;
242         writer.setInfoStream(VERBOSE ? System.out : null);
243
244         MergeScheduler ms = writer.getConfig().getMergeScheduler();
245         for(int x=0;x<2;x++) {
246           if (ms instanceof ConcurrentMergeScheduler)
247             // This test intentionally produces exceptions
248             // in the threads that CMS launches; we don't
249             // want to pollute test output with these.
250             if (0 == x)
251               ((ConcurrentMergeScheduler) ms).setSuppressExceptions();
252             else
253               ((ConcurrentMergeScheduler) ms).clearSuppressExceptions();
254           
255           // Two loops: first time, limit disk space &
256           // throw random IOExceptions; second time, no
257           // disk space limit:
258           
259           double rate = 0.05;
260           double diskRatio = ((double) diskFree)/diskUsage;
261           long thisDiskFree;
262           
263           String testName = null;
264           
265           if (0 == x) {
266             thisDiskFree = diskFree;
267             if (diskRatio >= 2.0) {
268               rate /= 2;
269             }
270             if (diskRatio >= 4.0) {
271               rate /= 2;
272             }
273             if (diskRatio >= 6.0) {
274               rate = 0.0;
275             }
276             if (VERBOSE)
277               testName = "disk full test " + methodName + " with disk full at " + diskFree + " bytes";
278           } else {
279             thisDiskFree = 0;
280             rate = 0.0;
281             if (VERBOSE)
282               testName = "disk full test " + methodName + " with unlimited disk space";
283           }
284           
285           if (VERBOSE)
286             System.out.println("\ncycle: " + testName);
287           
288           dir.setTrackDiskUsage(true);
289           dir.setMaxSizeInBytes(thisDiskFree);
290           dir.setRandomIOExceptionRate(rate);
291           
292           try {
293             
294             if (0 == method) {
295               writer.addIndexes(dirs);
296               writer.optimize();
297             } else if (1 == method) {
298               IndexReader readers[] = new IndexReader[dirs.length];
299               for(int i=0;i<dirs.length;i++) {
300                 readers[i] = IndexReader.open(dirs[i], true);
301               }
302               try {
303                 writer.addIndexes(readers);
304               } finally {
305                 for(int i=0;i<dirs.length;i++) {
306                   readers[i].close();
307                 }
308               }
309             } else {
310               writer.addIndexes(dirs);
311             }
312             
313             success = true;
314             if (VERBOSE) {
315               System.out.println("  success!");
316             }
317             
318             if (0 == x) {
319               done = true;
320             }
321             
322           } catch (IOException e) {
323             success = false;
324             err = e;
325             if (VERBOSE) {
326               System.out.println("  hit IOException: " + e);
327               e.printStackTrace(System.out);
328             }
329             
330             if (1 == x) {
331               e.printStackTrace(System.out);
332               fail(methodName + " hit IOException after disk space was freed up");
333             }
334           }
335           
336           // Make sure all threads from
337           // ConcurrentMergeScheduler are done
338           _TestUtil.syncConcurrentMerges(writer);
339           
340           if (VERBOSE) {
341             System.out.println("  now test readers");
342           }
343           
344           // Finally, verify index is not corrupt, and, if
345           // we succeeded, we see all docs added, and if we
346           // failed, we see either all docs or no docs added
347           // (transactional semantics):
348           try {
349             reader = IndexReader.open(dir, true);
350           } catch (IOException e) {
351             e.printStackTrace(System.out);
352             fail(testName + ": exception when creating IndexReader: " + e);
353           }
354           int result = reader.docFreq(searchTerm);
355           if (success) {
356             if (result != START_COUNT) {
357               fail(testName + ": method did not throw exception but docFreq('aaa') is " + result + " instead of expected " + START_COUNT);
358             }
359           } else {
360             // On hitting exception we still may have added
361             // all docs:
362             if (result != START_COUNT && result != END_COUNT) {
363               err.printStackTrace(System.out);
364               fail(testName + ": method did throw exception but docFreq('aaa') is " + result + " instead of expected " + START_COUNT + " or " + END_COUNT);
365             }
366           }
367           
368           searcher = newSearcher(reader);
369           try {
370             hits = searcher.search(new TermQuery(searchTerm), null, END_COUNT).scoreDocs;
371           } catch (IOException e) {
372             e.printStackTrace(System.out);
373             fail(testName + ": exception when searching: " + e);
374           }
375           int result2 = hits.length;
376           if (success) {
377             if (result2 != result) {
378               fail(testName + ": method did not throw exception but hits.length for search on term 'aaa' is " + result2 + " instead of expected " + result);
379             }
380           } else {
381             // On hitting exception we still may have added
382             // all docs:
383             if (result2 != result) {
384               err.printStackTrace(System.out);
385               fail(testName + ": method did throw exception but hits.length for search on term 'aaa' is " + result2 + " instead of expected " + result);
386             }
387           }
388           
389           searcher.close();
390           reader.close();
391           if (VERBOSE) {
392             System.out.println("  count is " + result);
393           }
394           
395           if (done || result == END_COUNT) {
396             break;
397           }
398         }
399         
400         if (VERBOSE) {
401           System.out.println("  start disk = " + startDiskUsage + "; input disk = " + inputDiskUsage + "; max used = " + dir.getMaxUsedSizeInBytes());
402         }
403         
404         if (done) {
405           // Javadocs state that temp free Directory space
406           // required is at most 2X total input size of
407           // indices so let's make sure:
408           assertTrue("max free Directory space required exceeded 1X the total input index sizes during " + methodName +
409                      ": max temp usage = " + (dir.getMaxUsedSizeInBytes()-startDiskUsage) + " bytes vs limit=" + (2*(startDiskUsage + inputDiskUsage)) +
410                      "; starting disk usage = " + startDiskUsage + " bytes; " +
411                      "input index disk usage = " + inputDiskUsage + " bytes",
412                      (dir.getMaxUsedSizeInBytes()-startDiskUsage) < 2*(startDiskUsage + inputDiskUsage));
413         }
414         
415         // Make sure we don't hit disk full during close below:
416         dir.setMaxSizeInBytes(0);
417         dir.setRandomIOExceptionRate(0.0);
418         
419         writer.close();
420         
421         // Wait for all BG threads to finish else
422         // dir.close() will throw IOException because
423         // there are still open files
424         _TestUtil.syncConcurrentMerges(ms);
425         
426         dir.close();
427         
428         // Try again with more free space:
429         diskFree += TEST_NIGHTLY ? _TestUtil.nextInt(random, 4000, 8000) : _TestUtil.nextInt(random, 40000, 80000);
430       }
431     }
432     
433     startDir.close();
434     for (Directory dir : dirs)
435       dir.close();
436   }
437   
438   private static class FailTwiceDuringMerge extends MockDirectoryWrapper.Failure {
439     public boolean didFail1;
440     public boolean didFail2;
441
442     @Override
443     public void eval(MockDirectoryWrapper dir)  throws IOException {
444       if (!doFail) {
445         return;
446       }
447       StackTraceElement[] trace = new Exception().getStackTrace();
448       for (int i = 0; i < trace.length; i++) {
449         if ("org.apache.lucene.index.SegmentMerger".equals(trace[i].getClassName()) && "mergeTerms".equals(trace[i].getMethodName()) && !didFail1) {
450           didFail1 = true;
451           throw new IOException("fake disk full during mergeTerms");
452         }
453         if ("org.apache.lucene.util.BitVector".equals(trace[i].getClassName()) && "write".equals(trace[i].getMethodName()) && !didFail2) {
454           didFail2 = true;
455           throw new IOException("fake disk full while writing BitVector");
456         }
457       }
458     }
459   }
460   
461   // LUCENE-2593
462   public void testCorruptionAfterDiskFullDuringMerge() throws IOException {
463     MockDirectoryWrapper dir = newDirectory();
464     //IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setReaderPooling(true));
465     IndexWriter w = new IndexWriter(
466         dir,
467         newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
468             setMergeScheduler(new SerialMergeScheduler()).
469             setReaderPooling(true).
470             setMergePolicy(newLogMergePolicy(2))
471     );
472
473     _TestUtil.keepFullyDeletedSegments(w);
474
475     ((LogMergePolicy) w.getMergePolicy()).setMergeFactor(2);
476
477     Document doc = new Document();
478     doc.add(newField("f", "doctor who", Field.Store.YES, Field.Index.ANALYZED));
479     w.addDocument(doc);
480     w.commit();
481
482     w.deleteDocuments(new Term("f", "who"));
483     w.addDocument(doc);
484     
485     // disk fills up!
486     FailTwiceDuringMerge ftdm = new FailTwiceDuringMerge();
487     ftdm.setDoFail();
488     dir.failOn(ftdm);
489
490     try {
491       w.commit();
492       fail("fake disk full IOExceptions not hit");
493     } catch (IOException ioe) {
494       // expected
495       assertTrue(ftdm.didFail1 || ftdm.didFail2);
496     }
497     _TestUtil.checkIndex(dir);
498     ftdm.clearDoFail();
499     w.addDocument(doc);
500     w.close();
501
502     dir.close();
503   }
504   
505   // LUCENE-1130: make sure immeidate disk full on creating
506   // an IndexWriter (hit during DW.ThreadState.init()) is
507   // OK:
508   public void testImmediateDiskFull() throws IOException {
509     MockDirectoryWrapper dir = newDirectory();
510     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random))
511         .setMaxBufferedDocs(2).setMergeScheduler(new ConcurrentMergeScheduler()));
512     dir.setMaxSizeInBytes(Math.max(1, dir.getRecomputedActualSizeInBytes()));
513     final Document doc = new Document();
514     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));
515     try {
516       writer.addDocument(doc);
517       fail("did not hit disk full");
518     } catch (IOException ioe) {
519     }
520     // Without fix for LUCENE-1130: this call will hang:
521     try {
522       writer.addDocument(doc);
523       fail("did not hit disk full");
524     } catch (IOException ioe) {
525     }
526     try {
527       writer.close(false);
528       fail("did not hit disk full");
529     } catch (IOException ioe) {
530     }
531
532     // Make sure once disk space is avail again, we can
533     // cleanly close:
534     dir.setMaxSizeInBytes(0);
535     writer.close(false);
536     dir.close();
537   }
538   
539   // TODO: these are also in TestIndexWriter... add a simple doc-writing method
540   // like this to LuceneTestCase?
541   private void addDoc(IndexWriter writer) throws IOException
542   {
543       Document doc = new Document();
544       doc.add(newField("content", "aaa", Field.Store.NO, Field.Index.ANALYZED));
545       writer.addDocument(doc);
546   }
547   
548   private void addDocWithIndex(IndexWriter writer, int index) throws IOException
549   {
550       Document doc = new Document();
551       doc.add(newField("content", "aaa " + index, Field.Store.YES, Field.Index.ANALYZED));
552       doc.add(newField("id", "" + index, Field.Store.YES, Field.Index.ANALYZED));
553       writer.addDocument(doc);
554   }
555 }