LUCENE-1036: make sure the priority argument to Thread.setPriority(...) in ConcurrentMergeScheduler is in-bounds

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@589549 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-10-29 09:00:56 +00:00
parent e7debdf9e3
commit 526d97676d
2 changed files with 47 additions and 14 deletions

View File

@ -78,17 +78,14 @@ public class ConcurrentMergeScheduler implements MergeScheduler {
/** Return the priority that merge threads run at. */
public synchronized void setMergeThreadPriority(int pri) {
if (pri > Thread.MAX_PRIORITY || pri < Thread.MIN_PRIORITY)
throw new IllegalArgumentException("priority must be in range " + Thread.MIN_PRIORITY + " .. " + Thread.MAX_PRIORITY + " inclusive");
mergeThreadPriority = pri;
final int numThreads = mergeThreadCount();
for(int i=0;i<numThreads;i++) {
MergeThread merge = (MergeThread) mergeThreads.get(i);
try {
merge.setPriority(pri);
} catch (NullPointerException npe) {
// Strangely, Sun's JDK 1.5 on Linux sometimes
// throws NPE out of here...
}
merge.setThreadPriority(pri);
}
}
@ -98,10 +95,13 @@ public class ConcurrentMergeScheduler implements MergeScheduler {
}
private synchronized void initMergeThreadPriority() {
if (mergeThreadPriority == -1)
if (mergeThreadPriority == -1) {
// Default to slightly higher priority than our
// calling thread
mergeThreadPriority = 1+Thread.currentThread().getPriority();
if (mergeThreadPriority > Thread.MAX_PRIORITY)
mergeThreadPriority = Thread.MAX_PRIORITY;
}
}
public void close() {
@ -122,7 +122,12 @@ public class ConcurrentMergeScheduler implements MergeScheduler {
}
}
private synchronized int mergeThreadCount() {
return mergeThreads.size();
int count = 0;
final int numThreads = mergeThreads.size();
for(int i=0;i<numThreads;i++)
if (((MergeThread) mergeThreads.get(i)).isAlive())
count++;
return count;
}
public void merge(IndexWriter writer)
@ -174,12 +179,7 @@ public class ConcurrentMergeScheduler implements MergeScheduler {
MergeThread merger = new MergeThread(writer, merge);
mergeThreads.add(merger);
message(" launch new thread [" + merger.getName() + "]");
try {
merger.setPriority(mergeThreadPriority);
} catch (NullPointerException npe) {
// Strangely, Sun's JDK 1.5 on Linux sometimes
// throws NPE out of here...
}
merger.setThreadPriority(mergeThreadPriority);
merger.start();
continue;
} else
@ -212,6 +212,18 @@ public class ConcurrentMergeScheduler implements MergeScheduler {
return runningMerge;
}
public void setThreadPriority(int pri) {
try {
setPriority(pri);
} catch (NullPointerException npe) {
// Strangely, Sun's JDK 1.5 on Linux sometimes
// throws NPE out of here...
} catch (SecurityException se) {
// Ignore this because we will still run fine with
// normal thread priority
}
}
public void run() {
// First time through the while loop we do the merge

View File

@ -1584,6 +1584,27 @@ public class TestIndexWriter extends LuceneTestCase
dir.close();
}
// LUCENE-1036
public void testMaxThreadPriority() throws IOException {
int pri = Thread.currentThread().getPriority();
try {
MockRAMDirectory dir = new MockRAMDirectory();
IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(), true);
Document document = new Document();
document.add(new Field("tvtest", "a b c", Field.Store.NO, Field.Index.TOKENIZED,
Field.TermVector.YES));
iw.setMaxBufferedDocs(2);
iw.setMergeFactor(2);
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
for(int i=0;i<4;i++)
iw.addDocument(document);
iw.close();
} finally {
Thread.currentThread().setPriority(pri);
}
}
// Just intercepts all merges & verifies that we are never
// merging a segment with >= 20 (maxMergeDocs) docs
private class MyMergeScheduler implements MergeScheduler {