1
package org.apache.lucene.index;
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
11
* http://www.apache.org/licenses/LICENSE-2.0
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.
20
import java.util.Random;
22
import org.apache.lucene.analysis.Analyzer;
23
import org.apache.lucene.analysis.MockAnalyzer;
24
import org.apache.lucene.analysis.MockTokenizer;
25
import org.apache.lucene.document.Document;
26
import org.apache.lucene.document.Field;
27
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
28
import org.apache.lucene.store.Directory;
29
import org.apache.lucene.util.English;
30
import org.apache.lucene.util.LuceneTestCase;
32
public class TestThreadedForceMerge extends LuceneTestCase {
34
private static final Analyzer ANALYZER = new MockAnalyzer(random, MockTokenizer.SIMPLE, true);
36
private final static int NUM_THREADS = 3;
37
//private final static int NUM_THREADS = 5;
39
private final static int NUM_ITER = 1;
41
private final static int NUM_ITER2 = 1;
43
private volatile boolean failed;
45
private void setFailed() {
49
public void runTest(Random random, Directory directory) throws Exception {
51
IndexWriter writer = new IndexWriter(
53
newIndexWriterConfig(TEST_VERSION_CURRENT, ANALYZER).
54
setOpenMode(OpenMode.CREATE).
55
setMaxBufferedDocs(2).
56
setMergePolicy(newLogMergePolicy())
59
for(int iter=0;iter<NUM_ITER;iter++) {
60
final int iterFinal = iter;
62
((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(1000);
64
for(int i=0;i<200;i++) {
65
Document d = new Document();
66
d.add(newField("id", Integer.toString(i), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
67
d.add(newField("contents", English.intToEnglish(i), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
68
writer.addDocument(d);
71
((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(4);
73
Thread[] threads = new Thread[NUM_THREADS];
75
for(int i=0;i<NUM_THREADS;i++) {
77
final IndexWriter writerFinal = writer;
78
threads[i] = new Thread() {
82
for(int j=0;j<NUM_ITER2;j++) {
83
writerFinal.forceMerge(1, false);
84
for(int k=0;k<17*(1+iFinal);k++) {
85
Document d = new Document();
86
d.add(newField("id", iterFinal + "_" + iFinal + "_" + j + "_" + k, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
87
d.add(newField("contents", English.intToEnglish(iFinal+k), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
88
writerFinal.addDocument(d);
90
for(int k=0;k<9*(1+iFinal);k++)
91
writerFinal.deleteDocuments(new Term("id", iterFinal + "_" + iFinal + "_" + j + "_" + k));
92
writerFinal.forceMerge(1);
94
} catch (Throwable t) {
96
System.out.println(Thread.currentThread().getName() + ": hit exception");
97
t.printStackTrace(System.out);
103
for(int i=0;i<NUM_THREADS;i++)
106
for(int i=0;i<NUM_THREADS;i++)
111
final int expectedDocCount = (int) ((1+iter)*(200+8*NUM_ITER2*(NUM_THREADS/2.0)*(1+NUM_THREADS)));
113
assertEquals("index=" + writer.segString() + " numDocs=" + writer.numDocs() + " maxDoc=" + writer.maxDoc() + " config=" + writer.getConfig(), expectedDocCount, writer.numDocs());
114
assertEquals("index=" + writer.segString() + " numDocs=" + writer.numDocs() + " maxDoc=" + writer.maxDoc() + " config=" + writer.getConfig(), expectedDocCount, writer.maxDoc());
117
writer = new IndexWriter(directory, newIndexWriterConfig(
118
TEST_VERSION_CURRENT, ANALYZER).setOpenMode(
119
OpenMode.APPEND).setMaxBufferedDocs(2));
121
IndexReader reader = IndexReader.open(directory, true);
122
assertEquals("reader=" + reader, 1, reader.getSequentialSubReaders().length);
123
assertEquals(expectedDocCount, reader.numDocs());
130
Run above stress test against RAMDirectory and then
133
public void testThreadedForceMerge() throws Exception {
134
Directory directory = newDirectory();
135
runTest(random, directory);