Skip to content

Commit b3588f3

Browse files
Defensively guard against uninitialized startedRelativeTimeInNanos
1 parent 2a26afa commit b3588f3

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

server/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public class IndexShard extends AbstractIndexShardComponent implements IndicesCl
298298
private final RefreshFieldHasValueListener refreshFieldHasValueListener;
299299
private volatile boolean useRetentionLeasesInPeerRecovery;
300300
private final LongSupplier relativeTimeInNanosSupplier;
301-
private volatile long startedRelativeTimeInNanos;
301+
private volatile long startedRelativeTimeInNanos = -1L; // use -1 to indicate this has not yet been set to its true value
302302
private volatile long indexingTimeBeforeShardStartedInNanos;
303303
private volatile double recentIndexingLoadAtShardStarted;
304304
private final SubscribableListener<Void> waitForEngineOrClosedShardListeners = new SubscribableListener<>();
@@ -557,7 +557,10 @@ public void updateShardState(
557557
: "a primary relocation is completed by the master, but primary mode is not active " + currentRouting;
558558

559559
changeState(IndexShardState.STARTED, "global state is [" + newRouting.state() + "]");
560-
startedRelativeTimeInNanos = getRelativeTimeInNanos();
560+
long relativeTimeInNanos = getRelativeTimeInNanos();
561+
// We use -1 to indicate that startedRelativeTimeInNanos has yet not been set to its true value. So in the vanishingly
562+
// unlikely case that getRelativeTimeInNanos() returns exactly -1, we advance by 1ns to avoid that special value.
563+
startedRelativeTimeInNanos = (relativeTimeInNanos != -1L) ? relativeTimeInNanos : 0L;
561564
indexingTimeBeforeShardStartedInNanos = internalIndexingStats.totalIndexingTimeInNanos();
562565
recentIndexingLoadAtShardStarted = internalIndexingStats.recentIndexingLoad(startedRelativeTimeInNanos);
563566
} else if (currentRouting.primary()
@@ -1370,11 +1373,14 @@ public IndexingStats indexingStats() {
13701373
}
13711374

13721375
long currentTimeInNanos = getRelativeTimeInNanos();
1376+
// We use -1 to indicate that startedRelativeTimeInNanos has yet not been set to its true value, i.e the shard has not started.
1377+
// In that case, we set timeSinceShardStartedInNanos to zero (which will result in all load metrics definitely being zero).
1378+
long timeSinceShardStartedInNanos = (startedRelativeTimeInNanos != -1L) ? (currentTimeInNanos - startedRelativeTimeInNanos) : 0L;
13731379
return internalIndexingStats.stats(
13741380
throttled,
13751381
throttleTimeInMillis,
13761382
indexingTimeBeforeShardStartedInNanos,
1377-
currentTimeInNanos - startedRelativeTimeInNanos,
1383+
timeSinceShardStartedInNanos,
13781384
currentTimeInNanos,
13791385
recentIndexingLoadAtShardStarted
13801386
);

0 commit comments

Comments
 (0)