summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonmv <venstad@gmail.com>2023-07-07 13:28:50 +0200
committerjonmv <venstad@gmail.com>2023-07-07 13:28:50 +0200
commitb3f69492a602cb150df8b764875e6597055288ea (patch)
tree9ead503a79b28e6564658a08027c32563f4491ac
parent5b4fa659348100f093e72e86b2e0528bae649cad (diff)
Avoid overflow for 146 years of JVM time for common timers
-rw-r--r--container-search/src/main/java/com/yahoo/search/dispatch/InvokerFactory.java14
-rw-r--r--vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java23
-rw-r--r--vespajlib/src/main/java/com/yahoo/concurrent/Timer.java4
3 files changed, 16 insertions, 25 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/dispatch/InvokerFactory.java b/container-search/src/main/java/com/yahoo/search/dispatch/InvokerFactory.java
index d6fb6de6354..b488662591a 100644
--- a/container-search/src/main/java/com/yahoo/search/dispatch/InvokerFactory.java
+++ b/container-search/src/main/java/com/yahoo/search/dispatch/InvokerFactory.java
@@ -61,16 +61,10 @@ public abstract class InvokerFactory {
List<SearchInvoker> invokers = new ArrayList<>(nodes.size());
Set<Integer> failed = null;
for (Node node : nodes) {
- boolean nodeAdded = false;
- if (node.isWorking() != Boolean.FALSE) {
- Optional<SearchInvoker> invoker = createNodeSearchInvoker(searcher, query, maxHits, node);
- if (invoker.isPresent()) {
- invokers.add(invoker.get());
- nodeAdded = true;
- }
- }
-
- if ( ! nodeAdded) {
+ if ( node.isWorking() == Boolean.FALSE
+ || createNodeSearchInvoker(searcher, query, maxHits, node)
+ .map(invoker -> { invokers.add(invoker); return invoker; })
+ .isEmpty()) {
if (failed == null) {
failed = new HashSet<>();
}
diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java b/vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java
index 8111d52a10f..9bb320ad0a9 100644
--- a/vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java
+++ b/vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java
@@ -42,21 +42,18 @@ public enum SystemTimer implements Timer {
SystemTimer() {
long napTime = adjustTimeoutByDetectedHz(Duration.ofMillis(1)).toMillis();
- millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
- Thread thread = new Thread() {
-
- @Override
- public void run() {
- while (true) {
- millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
- try {
- Thread.sleep(napTime);
- } catch (InterruptedException e) {
- break;
- }
+ long creationNanos = System.nanoTime();
+ millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - creationNanos);
+ Thread thread = new Thread(() -> {
+ while (true) {
+ millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) - creationNanos;
+ try {
+ Thread.sleep(napTime);
+ } catch (InterruptedException e) {
+ break;
}
}
- };
+ });
thread.setDaemon(true);
thread.setName("vespa-system-timer");
thread.start();
diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/Timer.java b/vespajlib/src/main/java/com/yahoo/concurrent/Timer.java
index c41c762c989..46d72f6d5ad 100644
--- a/vespajlib/src/main/java/com/yahoo/concurrent/Timer.java
+++ b/vespajlib/src/main/java/com/yahoo/concurrent/Timer.java
@@ -20,7 +20,8 @@ public interface Timer {
* @return The current value of the timer, in milliseconds.
*/
long milliTime();
- Timer monotonic = () -> TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
+ long creationNanos = System.nanoTime(); // Avoid monotonic timer overflow for the first 146 years of JVM uptime.
+ Timer monotonic = () -> TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - creationNanos);
static Timer wrap(Clock original) {
return new Timer() {
private final Clock clock = original;
@@ -30,6 +31,5 @@ public interface Timer {
return clock.millis();
}
}; }
-
default Instant instant() { return Instant.ofEpochMilli(milliTime()); }
}