diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/RoutingService.java b/core/src/main/java/org/elasticsearch/cluster/routing/RoutingService.java index ed7c5c46aa3..db0a46de633 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/RoutingService.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/RoutingService.java @@ -118,11 +118,17 @@ public class RoutingService extends AbstractLifecycleComponent i if (nextDelaySetting > 0 && nextDelaySetting < registeredNextDelaySetting) { FutureUtils.cancel(registeredNextDelayFuture); registeredNextDelaySetting = nextDelaySetting; - // We use System.currentTimeMillis here because we want the - // next delay from the "now" perspective, rather than the - // delay from the last time the GatewayAllocator tried to - // assign/delay the shard - TimeValue nextDelay = TimeValue.timeValueMillis(UnassignedInfo.findNextDelayedAllocationIn(System.currentTimeMillis(), settings, event.state())); + // We calculate nextDelay based on System.currentTimeMillis() here because we want the next delay from the "now" perspective + // rather than the delay from the last time the GatewayAllocator tried to assign/delay the shard. + // The actual calculation is based on the latter though, to account for shards that should have been allocated + // between unassignedShardsAllocatedTimestamp and System.currentTimeMillis() + long nextDelayBasedOnUnassignedShardsAllocatedTimestamp = UnassignedInfo.findNextDelayedAllocationIn(unassignedShardsAllocatedTimestamp, settings, event.state()); + // adjust from unassignedShardsAllocatedTimestamp to now + long nextDelayMillis = nextDelayBasedOnUnassignedShardsAllocatedTimestamp - (System.currentTimeMillis() - unassignedShardsAllocatedTimestamp); + if (nextDelayMillis < 0) { + nextDelayMillis = 0; + } + TimeValue nextDelay = TimeValue.timeValueMillis(nextDelayMillis); int unassignedDelayedShards = UnassignedInfo.getNumberOfDelayedUnassigned(unassignedShardsAllocatedTimestamp, settings, event.state()); if (unassignedDelayedShards > 0) { logger.info("delaying allocation for [{}] unassigned shards, next check in [{}]",