~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/backwards/src/test/org/apache/lucene/index/TestTransactions.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.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;
30
 
 
31
 
public class TestTransactions extends LuceneTestCase {
32
 
  
33
 
  private static volatile boolean doFail;
34
 
 
35
 
  private class RandomFailure extends MockDirectoryWrapper.Failure {
36
 
    @Override
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");
40
 
    }
41
 
  }
42
 
 
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;
47
 
 
48
 
    abstract public void doWork() throws Throwable;
49
 
 
50
 
    TimedThread(TimedThread[] threads) {
51
 
      this.allThreads = threads;
52
 
    }
53
 
 
54
 
    @Override
55
 
    public void run() {
56
 
      final long stopTime = System.currentTimeMillis() + (long) (RUN_TIME_MSEC);
57
 
 
58
 
      try {
59
 
        do {
60
 
          if (anyErrors()) break;
61
 
          doWork();
62
 
        } while (System.currentTimeMillis() < stopTime);
63
 
      } catch (Throwable e) {
64
 
        System.out.println(Thread.currentThread() + ": exc");
65
 
        e.printStackTrace(System.out);
66
 
        failed = true;
67
 
      }
68
 
    }
69
 
 
70
 
    private boolean anyErrors() {
71
 
      for(int i=0;i<allThreads.length;i++)
72
 
        if (allThreads[i] != null && allThreads[i].failed)
73
 
          return true;
74
 
      return false;
75
 
    }
76
 
  }
77
 
 
78
 
  private class IndexerThread extends TimedThread {
79
 
    Directory dir1;
80
 
    Directory dir2;
81
 
    Object lock;
82
 
    int nextID;
83
 
 
84
 
    public IndexerThread(Object lock, Directory dir1, Directory dir2, TimedThread[] threads) {
85
 
      super(threads);
86
 
      this.lock = lock;
87
 
      this.dir1 = dir1;
88
 
      this.dir2 = dir2;
89
 
    }
90
 
 
91
 
    @Override
92
 
    public void doWork() throws Throwable {
93
 
 
94
 
      IndexWriter writer1 = new IndexWriter(
95
 
          dir1,
96
 
          newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
97
 
              setMaxBufferedDocs(3).
98
 
              setMergeScheduler(new ConcurrentMergeScheduler()).
99
 
              setMergePolicy(newLogMergePolicy(2))
100
 
      );
101
 
      ((ConcurrentMergeScheduler) writer1.getConfig().getMergeScheduler()).setSuppressExceptions();
102
 
 
103
 
      // Intentionally use different params so flush/merge
104
 
      // happen @ different times
105
 
      IndexWriter writer2 = new IndexWriter(
106
 
          dir2,
107
 
          newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).
108
 
              setMaxBufferedDocs(2).
109
 
              setMergeScheduler(new ConcurrentMergeScheduler()).
110
 
              setMergePolicy(newLogMergePolicy(3))
111
 
      );
112
 
      ((ConcurrentMergeScheduler) writer2.getConfig().getMergeScheduler()).setSuppressExceptions();
113
 
 
114
 
      update(writer1);
115
 
      update(writer2);
116
 
 
117
 
      TestTransactions.doFail = true;
118
 
      try {
119
 
        synchronized(lock) {
120
 
          try {
121
 
            writer1.prepareCommit();
122
 
          } catch (Throwable t) {
123
 
            writer1.rollback();
124
 
            writer2.rollback();
125
 
            return;
126
 
          }
127
 
          try {
128
 
            writer2.prepareCommit();
129
 
          } catch (Throwable t) {       
130
 
            writer1.rollback();
131
 
            writer2.rollback();
132
 
            return;
133
 
          }
134
 
 
135
 
          writer1.commit();
136
 
          writer2.commit();
137
 
        }
138
 
      } finally {
139
 
        TestTransactions.doFail = false;
140
 
      }  
141
 
 
142
 
      writer1.close();
143
 
      writer2.close();
144
 
    }
145
 
 
146
 
    public void update(IndexWriter writer) throws IOException {
147
 
      // Add 10 docs:
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);
154
 
      }
155
 
 
156
 
      // Delete 5 docs:
157
 
      int deleteID = nextID-1;
158
 
      for(int j=0; j<5; j++) {
159
 
        writer.deleteDocuments(new Term("id", ""+deleteID));
160
 
        deleteID -= 2;
161
 
      }
162
 
    }
163
 
  }
164
 
 
165
 
  private static class SearcherThread extends TimedThread {
166
 
    Directory dir1;
167
 
    Directory dir2;
168
 
    Object lock;
169
 
 
170
 
    public SearcherThread(Object lock, Directory dir1, Directory dir2, TimedThread[] threads) {
171
 
      super(threads);
172
 
      this.lock = lock;
173
 
      this.dir1 = dir1;
174
 
      this.dir2 = dir2;
175
 
    }
176
 
 
177
 
    @Override
178
 
    public void doWork() throws Throwable {
179
 
      IndexReader r1, r2;
180
 
      synchronized(lock) {
181
 
        r1 = IndexReader.open(dir1, true);
182
 
        r2 = IndexReader.open(dir2, true);
183
 
      }
184
 
      if (r1.numDocs() != r2.numDocs())
185
 
        throw new RuntimeException("doc counts differ: r1=" + r1.numDocs() + " r2=" + r2.numDocs());
186
 
      r1.close();
187
 
      r2.close();
188
 
    }
189
 
  }
190
 
 
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);
198
 
    }
199
 
    writer.close();
200
 
  }
201
 
 
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());
210
 
 
211
 
    initIndex(dir1);
212
 
    initIndex(dir2);
213
 
 
214
 
    TimedThread[] threads = new TimedThread[3];
215
 
    int numThread = 0;
216
 
 
217
 
    IndexerThread indexerThread = new IndexerThread(this, dir1, dir2, threads);
218
 
    threads[numThread++] = indexerThread;
219
 
    indexerThread.start();
220
 
 
221
 
    SearcherThread searcherThread1 = new SearcherThread(this, dir1, dir2, threads);
222
 
    threads[numThread++] = searcherThread1;
223
 
    searcherThread1.start();
224
 
 
225
 
    SearcherThread searcherThread2 = new SearcherThread(this, dir1, dir2, threads);
226
 
    threads[numThread++] = searcherThread2;
227
 
    searcherThread2.start();
228
 
 
229
 
    for(int i=0;i<numThread;i++)
230
 
      threads[i].join();
231
 
 
232
 
    for(int i=0;i<numThread;i++)
233
 
      assertTrue(!threads[i].failed);
234
 
    dir1.close();
235
 
    dir2.close();
236
 
  }
237
 
}