Skip to content

Apache Lucene open-source search software

LUCENE-2170: Fix thread starvation problems

Uwe Schindlerc9ddd1b9fd96
diff --git a/CHANGES.txt b/CHANGES.txt
index 98eee52d9c2..9f154524977 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -136,6 +136,8 @@ Test Cases
* LUCENE-2155: Fix time and zone dependent localization test failures
in queryparser tests. (Uwe Schindler, Chris Male, Robert Muir)
+* LUCENE-2170: Fix thread starvation problems. (Uwe Schindler)
+
======================= Release 3.0.0 2009-11-25 =======================
Changes in backwards compatibility policy
diff --git a/common-build.xml b/common-build.xml
index 3d7f33f6357..4cf695a3366 100644
--- a/common-build.xml
+++ b/common-build.xml
@@ -42,7 +42,7 @@
<property name="Name" value="Lucene"/>
<property name="dev.version" value="3.1-dev"/>
<property name="version" value="${dev.version}"/>
- <property name="compatibility.tag" value="lucene_3_0_back_compat_tests_20091217"/>
+ <property name="compatibility.tag" value="lucene_3_0_back_compat_tests_20091218"/>
<property name="spec.version" value="${version}"/>
<property name="year" value="2000-${current.year}"/>
<property name="final.name" value="lucene-${name}-${version}"/>
diff --git a/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java b/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
index 7714cde46f9..4f7a8b94d64 100644
--- a/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
+++ b/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
@@ -121,7 +121,7 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase
public void run() {
Document doc = new Document();
doc.add(new Field("content", "aaa", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
- while(System.currentTimeMillis() < stopTime) {
+ do {
for(int i=0;i<27;i++) {
try {
writer.addDocument(doc);
@@ -142,7 +142,7 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase
} catch (InterruptedException ie) {
throw new ThreadInterruptedException(ie);
}
- }
+ } while(System.currentTimeMillis() < stopTime);
}
};
@@ -150,12 +150,10 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase
// While the above indexing thread is running, take many
// backups:
- while(System.currentTimeMillis() < stopTime) {
+ do {
backupIndex(dir, dp);
Thread.sleep(20);
- if (!t.isAlive())
- break;
- }
+ } while(t.isAlive());
t.join();
diff --git a/src/test/org/apache/lucene/index/TestAtomicUpdate.java b/src/test/org/apache/lucene/index/TestAtomicUpdate.java
index f8b7d0e4b3b..3686bf5c672 100644
--- a/src/test/org/apache/lucene/index/TestAtomicUpdate.java
+++ b/src/test/org/apache/lucene/index/TestAtomicUpdate.java
@@ -45,7 +45,7 @@ public class TestAtomicUpdate extends LuceneTestCase {
}
private static abstract class TimedThread extends Thread {
- boolean failed;
+ volatile boolean failed;
int count;
private static float RUN_TIME_SEC = 0.5f;
private TimedThread[] allThreads;
@@ -63,10 +63,11 @@ public class TestAtomicUpdate extends LuceneTestCase {
count = 0;
try {
- while(System.currentTimeMillis() < stopTime && !anyErrors()) {
+ do {
+ if (anyErrors()) break;
doWork();
count++;
- }
+ } while(System.currentTimeMillis() < stopTime);
} catch (Throwable e) {
System.out.println(Thread.currentThread().getName() + ": exc");
e.printStackTrace(System.out);
diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.java
index abb3ce76ac8..63f1fd3d77d 100644
--- a/src/test/org/apache/lucene/index/TestIndexWriter.java
+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java
@@ -2206,7 +2206,7 @@ public class TestIndexWriter extends LuceneTestCase {
int fullCount = 0;
final long stopTime = System.currentTimeMillis() + 200;
- while(System.currentTimeMillis() < stopTime) {
+ do {
try {
writer.updateDocument(new Term("id", ""+(idUpto++)), doc);
addCount++;
@@ -2240,7 +2240,7 @@ public class TestIndexWriter extends LuceneTestCase {
}
break;
}
- }
+ } while(System.currentTimeMillis() < stopTime);
}
}
@@ -4640,7 +4640,8 @@ public class TestIndexWriter extends LuceneTestCase {
Field f = new Field("f", "", Field.Store.NO, Field.Index.NOT_ANALYZED);
doc.add(f);
int count = 0;
- while(System.currentTimeMillis() < endTime && !failed.get()) {
+ do {
+ if (failed.get()) break;
for(int j=0;j<10;j++) {
final String s = finalI + "_" + String.valueOf(count++);
f.setValue(s);
@@ -4652,7 +4653,7 @@ public class TestIndexWriter extends LuceneTestCase {
r = r2;
assertEquals("term=f:" + s, 1, r.docFreq(new Term("f", s)));
}
- }
+ } while(System.currentTimeMillis() < endTime);
r.close();
} catch (Throwable t) {
failed.set(true);
diff --git a/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java b/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
index dfc8b05eee1..c357e436642 100644
--- a/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
+++ b/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
@@ -65,7 +65,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
final long stopTime = System.currentTimeMillis() + 500;
- while(System.currentTimeMillis() < stopTime) {
+ do {
doFail.set(this);
final String id = ""+r.nextInt(50);
idField.setValue(id);
@@ -105,7 +105,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
failure = t;
break;
}
- }
+ } while(System.currentTimeMillis() < stopTime);
}
}
diff --git a/src/test/org/apache/lucene/index/TestIndexWriterReader.java b/src/test/org/apache/lucene/index/TestIndexWriterReader.java
index a86c7d01f1f..343382129ff 100644
--- a/src/test/org/apache/lucene/index/TestIndexWriterReader.java
+++ b/src/test/org/apache/lucene/index/TestIndexWriterReader.java
@@ -730,14 +730,14 @@ public class TestIndexWriterReader extends LuceneTestCase {
threads[i] = new Thread() {
@Override
public void run() {
- while(System.currentTimeMillis() < endTime) {
+ do {
try {
writer.addIndexesNoOptimize(dirs);
} catch (Throwable t) {
excs.add(t);
throw new RuntimeException(t);
}
- }
+ } while(System.currentTimeMillis() < endTime);
}
};
threads[i].setDaemon(true);
@@ -760,6 +760,15 @@ public class TestIndexWriterReader extends LuceneTestCase {
for(int i=0;i<NUM_THREAD;i++) {
threads[i].join();
}
+ // final check
+ IndexReader r2 = r.reopen();
+ if (r2 != r) {
+ r.close();
+ r = r2;
+ }
+ Query q = new TermQuery(new Term("indexname", "test"));
+ final int count = new IndexSearcher(r).search(q, 10).totalHits;
+ assertTrue(count >= lastCount);
assertEquals(0, excs.size());
writer.close();
@@ -796,7 +805,7 @@ public class TestIndexWriterReader extends LuceneTestCase {
public void run() {
int count = 0;
final Random r = new Random();
- while(System.currentTimeMillis() < endTime) {
+ do {
try {
for(int i=0;i<10;i++) {
writer.addDocument(createDocument(10*count+i, "test", 4));
@@ -811,7 +820,7 @@ public class TestIndexWriterReader extends LuceneTestCase {
excs.add(t);
throw new RuntimeException(t);
}
- }
+ } while(System.currentTimeMillis() < endTime);
}
};
threads[i].setDaemon(true);
@@ -832,7 +841,16 @@ public class TestIndexWriterReader extends LuceneTestCase {
for(int i=0;i<NUM_THREAD;i++) {
threads[i].join();
}
- assertTrue(sum > 0);
+ // at least search once
+ IndexReader r2 = r.reopen();
+ if (r2 != r) {
+ r.close();
+ r = r2;
+ }
+ Query q = new TermQuery(new Term("indexname", "test"));
+ sum += new IndexSearcher(r).search(q, 10).totalHits;
+
+ assertTrue("no documents found at all", sum > 0);
assertEquals(0, excs.size());
writer.close();
diff --git a/src/test/org/apache/lucene/index/TestStressIndexing.java b/src/test/org/apache/lucene/index/TestStressIndexing.java
index ac149738c4c..a62af9be682 100644
--- a/src/test/org/apache/lucene/index/TestStressIndexing.java
+++ b/src/test/org/apache/lucene/index/TestStressIndexing.java
@@ -30,7 +30,7 @@ public class TestStressIndexing extends LuceneTestCase {
private Random RANDOM;
private static abstract class TimedThread extends Thread {
- boolean failed;
+ volatile boolean failed;
int count;
private static int RUN_TIME_SEC = 1;
private TimedThread[] allThreads;
@@ -48,10 +48,11 @@ public class TestStressIndexing extends LuceneTestCase {
count = 0;
try {
- while(System.currentTimeMillis() < stopTime && !anyErrors()) {
+ do {
+ if (anyErrors()) break;
doWork();
count++;
- }
+ } while(System.currentTimeMillis() < stopTime);
} catch (Throwable e) {
System.out.println(Thread.currentThread() + ": exc");
e.printStackTrace(System.out);
diff --git a/src/test/org/apache/lucene/index/TestTransactions.java b/src/test/org/apache/lucene/index/TestTransactions.java
index 7bbffe24ff7..999dee0bc91 100644
--- a/src/test/org/apache/lucene/index/TestTransactions.java
+++ b/src/test/org/apache/lucene/index/TestTransactions.java
@@ -38,7 +38,7 @@ public class TestTransactions extends LuceneTestCase
}
private static abstract class TimedThread extends Thread {
- boolean failed;
+ volatile boolean failed;
private static float RUN_TIME_SEC = 0.5f;
private TimedThread[] allThreads;
@@ -53,8 +53,10 @@ public class TestTransactions extends LuceneTestCase
final long stopTime = System.currentTimeMillis() + (long) (1000*RUN_TIME_SEC);
try {
- while(System.currentTimeMillis() < stopTime && !anyErrors())
+ do {
+ if (anyErrors()) break;
doWork();
+ } while (System.currentTimeMillis() < stopTime);
} catch (Throwable e) {
System.out.println(Thread.currentThread() + ": exc");
e.printStackTrace(System.out);