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

View File

@ -1584,6 +1584,27 @@ public class TestIndexWriter extends LuceneTestCase
dir.close(); 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 // Just intercepts all merges & verifies that we are never
// merging a segment with >= 20 (maxMergeDocs) docs // merging a segment with >= 20 (maxMergeDocs) docs
private class MyMergeScheduler implements MergeScheduler { private class MyMergeScheduler implements MergeScheduler {