X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/index/TestIndexWriterWithThreads.java?ds=sidebyside diff --git a/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/index/TestIndexWriterWithThreads.java b/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/index/TestIndexWriterWithThreads.java new file mode 100644 index 0000000..885d0b9 --- /dev/null +++ b/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/index/TestIndexWriterWithThreads.java @@ -0,0 +1,472 @@ +package org.apache.lucene.index; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +import org.apache.lucene.analysis.MockAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.store.AlreadyClosedException; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.MockDirectoryWrapper; +import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util.ThreadInterruptedException; + +/** + * MultiThreaded IndexWriter tests + */ +public class TestIndexWriterWithThreads extends LuceneTestCase { + + // Used by test cases below + private class IndexerThread extends Thread { + + boolean diskFull; + Throwable error; + AlreadyClosedException ace; + IndexWriter writer; + boolean noErrors; + volatile int addCount; + + public IndexerThread(IndexWriter writer, boolean noErrors) { + this.writer = writer; + this.noErrors = noErrors; + } + + @Override + public void run() { + + 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)); + + int idUpto = 0; + int fullCount = 0; + final long stopTime = System.currentTimeMillis() + 200; + + do { + try { + writer.updateDocument(new Term("id", ""+(idUpto++)), doc); + addCount++; + } catch (IOException ioe) { + if (VERBOSE) { + System.out.println("TEST: expected exc:"); + ioe.printStackTrace(System.out); + } + //System.out.println(Thread.currentThread().getName() + ": hit exc"); + //ioe.printStackTrace(System.out); + if (ioe.getMessage().startsWith("fake disk full at") || + ioe.getMessage().equals("now failing on purpose")) { + diskFull = true; + try { + Thread.sleep(1); + } catch (InterruptedException ie) { + throw new ThreadInterruptedException(ie); + } + if (fullCount++ >= 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++) { + if (VERBOSE) { + System.out.println("\nTEST: iter=" + iter); + } + MockDirectoryWrapper dir = newDirectory(); + IndexWriter writer = new IndexWriter( + dir, + newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)). + setMaxBufferedDocs(2). + setMergeScheduler(new ConcurrentMergeScheduler()). + setMergePolicy(newLogMergePolicy(4)) + ); + ((ConcurrentMergeScheduler) writer.getConfig().getMergeScheduler()).setSuppressExceptions(); + dir.setMaxSizeInBytes(4*1024+20*iter); + writer.setInfoStream(VERBOSE ? System.out : null); + + IndexerThread[] threads = new IndexerThread[NUM_THREADS]; + + for(int i=0;i 0) { + done = true; + break; + } else if (!threads[i].isAlive()) { + fail("thread failed before indexing a single document"); + } + } + + 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++) { + if (VERBOSE) { + System.out.println("TEST: iter=" + iter); + } + MockDirectoryWrapper dir = newDirectory(); + IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, + new MockAnalyzer(random)).setMaxBufferedDocs(2) + .setMergeScheduler(new ConcurrentMergeScheduler()) + .setMergePolicy(newLogMergePolicy(4)); + // We expect disk full exceptions in the merge threads + ((ConcurrentMergeScheduler) conf.getMergeScheduler()).setSuppressExceptions(); + IndexWriter writer = new IndexWriter(dir, conf); + writer.setInfoStream(VERBOSE ? System.out : null); + + IndexerThread[] threads = new IndexerThread[NUM_THREADS]; + + for(int i=0;i