lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r589549 - in /lucene/java/trunk/src: java/org/apache/lucene/index/ConcurrentMergeScheduler.java test/org/apache/lucene/index/TestIndexWriter.java
Date Mon, 29 Oct 2007 09:00:57 GMT
Author: mikemccand
Date: Mon Oct 29 02:00:56 2007
New Revision: 589549

URL: http://svn.apache.org/viewvc?rev=589549&view=rev
Log:
LUCENE-1036: make sure the priority argument to Thread.setPriority(...) in ConcurrentMergeScheduler
is in-bounds

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java
    lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java?rev=589549&r1=589548&r2=589549&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java Mon Oct
29 02:00:56 2007
@@ -78,17 +78,14 @@
 
   /** 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 @@
   }
 
   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 @@
     }
   }
   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 @@
             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
@@ -210,6 +210,18 @@
 
     public synchronized MergePolicy.OneMerge getRunningMerge() {
       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() {

Modified: lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java?rev=589549&r1=589548&r2=589549&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java Mon Oct 29 02:00:56
2007
@@ -1584,6 +1584,27 @@
     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 {



Mime
View raw message