LUCENE-6075: don't overflow int in SimpleRateLimiter

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1641584 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2014-11-25 11:40:47 +00:00
parent f60f243b17
commit ebb9a5d971
3 changed files with 32 additions and 3 deletions

View File

@ -281,6 +281,9 @@ Bug Fixes
* LUCENE-6062: Pass correct fieldinfos to docvalues producer when the
segment has updates. (Mike McCandless, Shai Erera, Robert Muir)
* LUCENE-6075: Don't overflow int in SimpleRateLimiter (Boaz Leskes
via Mike McCandless)
Documentation

View File

@ -138,7 +138,17 @@ public abstract class RateLimiter {
// NOTE: except maybe on real-time JVMs, minimum realistic sleep time
// is 1 msec; if you pass just 1 nsec the default impl rounds
// this up to 1 msec:
Thread.sleep((int) (pauseNS/1000000), (int) (pauseNS % 1000000));
int sleepNS;
int sleepMS;
if (pauseNS > 100000L * Integer.MAX_VALUE) {
// Not really practical (sleeping for 25 days) but we shouldn't overflow int:
sleepMS = Integer.MAX_VALUE;
sleepNS = 0;
} else {
sleepMS = (int) (pauseNS/1000000);
sleepNS = (int) (pauseNS % 1000000);
}
Thread.sleep(sleepMS, sleepNS);
} catch (InterruptedException ie) {
throw new ThreadInterruptedException(ie);
}

View File

@ -17,10 +17,8 @@ package org.apache.lucene.store;
* limitations under the License.
*/
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.lucene.store.RateLimiter.SimpleRateLimiter;
@ -44,6 +42,24 @@ public final class TestRateLimiter extends LuceneTestCase {
assertTrue("we should sleep at least 1 second but did only: " + convert + " millis", convert > 1000l);
}
// LUCENE-6075
public void testOverflowInt() throws Exception {
Thread t = new Thread() {
@Override
public void run() {
try {
new SimpleRateLimiter(1).pause((long) (1.5*Integer.MAX_VALUE*1024*1024/1000));
fail("should have been interrupted");
} catch (ThreadInterruptedException tie) {
// expected
}
}
};
t.start();
Thread.sleep(10);
t.interrupt();
}
public void testThreads() throws Exception {
double targetMBPerSec = 10.0 + 20 * random().nextDouble();