Skip to content

Commit ed462d0

Browse files
committed
Fix calculation of age of pending tasks
This commit backports commit aa28133 from master to 2.2. Relates #15995
1 parent 6477395 commit ed462d0

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

core/src/main/java/org/elasticsearch/common/util/concurrent/PrioritizedRunnable.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,51 @@
1919
package org.elasticsearch.common.util.concurrent;
2020

2121
import org.elasticsearch.common.Priority;
22-
import org.elasticsearch.common.unit.TimeValue;
22+
23+
import java.util.concurrent.TimeUnit;
2324

2425
/**
2526
*
2627
*/
2728
public abstract class PrioritizedRunnable implements Runnable, Comparable<PrioritizedRunnable> {
2829

30+
// package visible for testing
31+
interface LongSupplier {
32+
long getAsLong();
33+
}
34+
2935
private final Priority priority;
3036
private final long creationDate;
37+
private final LongSupplier relativeTimeProvider;
38+
39+
private static final LongSupplier SYSTEM_NANO_TIME = new LongSupplier() {
40+
@Override
41+
public long getAsLong() {
42+
return System.nanoTime();
43+
}
44+
};
3145

3246
public static PrioritizedRunnable wrap(Runnable runnable, Priority priority) {
3347
return new Wrapped(runnable, priority);
3448
}
3549

3650
protected PrioritizedRunnable(Priority priority) {
51+
this(priority, SYSTEM_NANO_TIME);
52+
}
53+
54+
// package visible for testing
55+
PrioritizedRunnable(Priority priority, LongSupplier relativeTimeProvider) {
3756
this.priority = priority;
38-
creationDate = System.nanoTime();
57+
this.creationDate = relativeTimeProvider.getAsLong();
58+
this.relativeTimeProvider = relativeTimeProvider;
3959
}
4060

4161
public long getCreationDateInNanos() {
4262
return creationDate;
4363
}
4464

4565
public long getAgeInMillis() {
46-
return Math.max(0, (System.nanoTime() - creationDate) / 1000);
66+
return TimeUnit.MILLISECONDS.convert(relativeTimeProvider.getAsLong() - creationDate, TimeUnit.NANOSECONDS);
4767
}
4868

4969
@Override
@@ -69,4 +89,4 @@ public void run() {
6989
runnable.run();
7090
}
7191
}
72-
}
92+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.util.concurrent;
21+
22+
import org.elasticsearch.common.Priority;
23+
import org.elasticsearch.test.ESTestCase;
24+
25+
import java.util.concurrent.TimeUnit;
26+
import java.util.concurrent.atomic.AtomicLong;
27+
28+
import static org.hamcrest.Matchers.greaterThan;
29+
30+
public class PrioritizedRunnableTests extends ESTestCase {
31+
public void testGetAgeInMillis() throws Exception {
32+
final AtomicLong time = new AtomicLong();
33+
34+
PrioritizedRunnable runnable = new PrioritizedRunnable(Priority.NORMAL, new PrioritizedRunnable.LongSupplier() {
35+
@Override
36+
public long getAsLong() {
37+
return time.get();
38+
}
39+
}) {
40+
@Override
41+
public void run() {
42+
43+
}
44+
};
45+
assertEquals(0, runnable.getAgeInMillis());
46+
int milliseconds = randomIntBetween(1, 256);
47+
time.addAndGet(TimeUnit.NANOSECONDS.convert(milliseconds, TimeUnit.MILLISECONDS));
48+
assertEquals(milliseconds, runnable.getAgeInMillis());
49+
}
50+
}

0 commit comments

Comments
 (0)