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.io.IOException;
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.store.Directory;
26
import org.apache.lucene.store.MockDirectoryWrapper;
27
import org.apache.lucene.store.RAMDirectory;
28
import org.apache.lucene.util.English;
29
import org.apache.lucene.util.LuceneTestCase;
31
public class TestTransactions extends LuceneTestCase {
33
private static volatile boolean doFail;
35
private class RandomFailure extends MockDirectoryWrapper.Failure {
37
public void eval(MockDirectoryWrapper dir) throws IOException {
38
if (TestTransactions.doFail && random.nextInt() % 10 <= 3)
39
throw new IOException("now failing randomly but on purpose");
43
private static abstract class TimedThread extends Thread {
44
volatile boolean failed;
45
private static float RUN_TIME_MSEC = atLeast(500);
46
private TimedThread[] allThreads;
48
abstract public void doWork() throws Throwable;
50
TimedThread(TimedThread[] threads) {
51
this.allThreads = threads;
56
final long stopTime = System.currentTimeMillis() + (long) (RUN_TIME_MSEC);
60
if (anyErrors()) break;
62
} while (System.currentTimeMillis() < stopTime);
63
} catch (Throwable e) {
64
System.out.println(Thread.currentThread() + ": exc");
65
e.printStackTrace(System.out);
70
private boolean anyErrors() {
71
for(int i=0;i<allThreads.length;i++)
72
if (allThreads[i] != null && allThreads[i].failed)
78
private class IndexerThread extends TimedThread {
84
public IndexerThread(Object lock, Directory dir1, Directory dir2, TimedThread[] threads) {
92
public void doWork() throws Throwable {
94
IndexWriter writer1 = new IndexWriter(
96
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
97
setMaxBufferedDocs(3).
98
setMergeScheduler(new ConcurrentMergeScheduler()).
99
setMergePolicy(newLogMergePolicy(2))
101
((ConcurrentMergeScheduler) writer1.getConfig().getMergeScheduler()).setSuppressExceptions();
103
// Intentionally use different params so flush/merge
104
// happen @ different times
105
IndexWriter writer2 = new IndexWriter(
107
newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).
108
setMaxBufferedDocs(2).
109
setMergeScheduler(new ConcurrentMergeScheduler()).
110
setMergePolicy(newLogMergePolicy(3))
112
((ConcurrentMergeScheduler) writer2.getConfig().getMergeScheduler()).setSuppressExceptions();
117
TestTransactions.doFail = true;
121
writer1.prepareCommit();
122
} catch (Throwable t) {
128
writer2.prepareCommit();
129
} catch (Throwable t) {
139
TestTransactions.doFail = false;
146
public void update(IndexWriter writer) throws IOException {
148
for(int j=0; j<10; j++) {
149
Document d = new Document();
150
int n = random.nextInt();
151
d.add(newField("id", Integer.toString(nextID++), Field.Store.YES, Field.Index.NOT_ANALYZED));
152
d.add(newField("contents", English.intToEnglish(n), Field.Store.NO, Field.Index.ANALYZED));
153
writer.addDocument(d);
157
int deleteID = nextID-1;
158
for(int j=0; j<5; j++) {
159
writer.deleteDocuments(new Term("id", ""+deleteID));
165
private static class SearcherThread extends TimedThread {
170
public SearcherThread(Object lock, Directory dir1, Directory dir2, TimedThread[] threads) {
178
public void doWork() throws Throwable {
181
r1 = IndexReader.open(dir1, true);
182
r2 = IndexReader.open(dir2, true);
184
if (r1.numDocs() != r2.numDocs())
185
throw new RuntimeException("doc counts differ: r1=" + r1.numDocs() + " r2=" + r2.numDocs());
191
public void initIndex(Directory dir) throws Throwable {
192
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
193
for(int j=0; j<7; j++) {
194
Document d = new Document();
195
int n = random.nextInt();
196
d.add(newField("contents", English.intToEnglish(n), Field.Store.NO, Field.Index.ANALYZED));
197
writer.addDocument(d);
202
public void testTransactions() throws Throwable {
203
// we cant use non-ramdir on windows, because this test needs to double-write.
204
MockDirectoryWrapper dir1 = new MockDirectoryWrapper(random, new RAMDirectory());
205
MockDirectoryWrapper dir2 = new MockDirectoryWrapper(random, new RAMDirectory());
206
dir1.setPreventDoubleWrite(false);
207
dir2.setPreventDoubleWrite(false);
208
dir1.failOn(new RandomFailure());
209
dir2.failOn(new RandomFailure());
214
TimedThread[] threads = new TimedThread[3];
217
IndexerThread indexerThread = new IndexerThread(this, dir1, dir2, threads);
218
threads[numThread++] = indexerThread;
219
indexerThread.start();
221
SearcherThread searcherThread1 = new SearcherThread(this, dir1, dir2, threads);
222
threads[numThread++] = searcherThread1;
223
searcherThread1.start();
225
SearcherThread searcherThread2 = new SearcherThread(this, dir1, dir2, threads);
226
threads[numThread++] = searcherThread2;
227
searcherThread2.start();
229
for(int i=0;i<numThread;i++)
232
for(int i=0;i<numThread;i++)
233
assertTrue(!threads[i].failed);