From cfdc632f3bb4f57d2ab8d027fc910573b18b4154 Mon Sep 17 00:00:00 2001 From: David Turner Date: Mon, 17 Jun 2019 12:47:25 +0100 Subject: [PATCH 1/2] Relocation targets are assigned shards too Adds relocation targets to the output of `IndexShardRoutingTable#assignedShards`. --- .../cluster/routing/IndexShardRoutingTable.java | 9 ++++----- .../cluster/routing/allocation/IndexMetaDataUpdater.java | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java b/server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java index ca3661f3e6f9c..b3c04b09a774a 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java @@ -105,6 +105,10 @@ public class IndexShardRoutingTable implements Iterable { // create the target initializing shard routing on the node the shard is relocating to allInitializingShards.add(shard.getTargetRelocatingShard()); allAllocationIds.add(shard.getTargetRelocatingShard().allocationId().getId()); + + assert shard.assignedToNode() : "relocating from unassigned " + shard; + assert shard.getTargetRelocatingShard().assignedToNode() : "relocating to unassigned " + shard.getTargetRelocatingShard(); + assignedShards.add(shard.getTargetRelocatingShard()); } if (shard.assignedToNode()) { assignedShards.add(shard); @@ -518,11 +522,6 @@ public ShardRouting getByAllocationId(String allocationId) { if (shardRouting.allocationId().getId().equals(allocationId)) { return shardRouting; } - if (shardRouting.relocating()) { - if (shardRouting.getTargetRelocatingShard().allocationId().getId().equals(allocationId)) { - return shardRouting.getTargetRelocatingShard(); - } - } } return null; } diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetaDataUpdater.java b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetaDataUpdater.java index 54625a15e8d80..a9de2b2ce63b5 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetaDataUpdater.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetaDataUpdater.java @@ -197,7 +197,7 @@ private IndexMetaData.Builder updateInSyncAllocations(RoutingTable newRoutingTab if (inSyncAllocationIds.size() > oldInSyncAllocationIds.size() && inSyncAllocationIds.size() > maxActiveShards) { // trim entries that have no corresponding shard routing in the cluster state (i.e. trim unavailable copies) List assignedShards = newShardRoutingTable.assignedShards(); - assert assignedShards.size() <= maxActiveShards : + assert assignedShards.stream().filter(s -> s.isRelocationTarget() == false).count() <= maxActiveShards : "cannot have more assigned shards " + assignedShards + " than maximum possible active shards " + maxActiveShards; Set assignedAllocations = assignedShards.stream().map(s -> s.allocationId().getId()).collect(Collectors.toSet()); inSyncAllocationIds = inSyncAllocationIds.stream() From c49e27580b2ac7cd1b6b75e2bc216ed2908b5bdc Mon Sep 17 00:00:00 2001 From: David Turner Date: Mon, 17 Jun 2019 13:42:52 +0100 Subject: [PATCH 2/2] Assert, but then filter out, relocating shards --- .../cluster/routing/IndexShardRoutingTable.java | 2 +- .../cluster/routing/allocation/IndexMetaDataUpdater.java | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java b/server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java index b3c04b09a774a..5a98e9456f43d 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java @@ -215,7 +215,7 @@ public List getActiveShards() { } /** - * Returns a {@link List} of assigned shards + * Returns a {@link List} of assigned shards, including relocation targets * * @return a {@link List} of shards */ diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetaDataUpdater.java b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetaDataUpdater.java index a9de2b2ce63b5..18d3b45fd9c7b 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetaDataUpdater.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetaDataUpdater.java @@ -194,10 +194,14 @@ private IndexMetaData.Builder updateInSyncAllocations(RoutingTable newRoutingTab // of replicas was decreased while shards were unassigned. int maxActiveShards = oldIndexMetaData.getNumberOfReplicas() + 1; // +1 for the primary IndexShardRoutingTable newShardRoutingTable = newRoutingTable.shardRoutingTable(shardId); + assert newShardRoutingTable.assignedShards().stream() + .filter(ShardRouting::isRelocationTarget).map(s -> s.allocationId().getId()).noneMatch(inSyncAllocationIds::contains) + : newShardRoutingTable.assignedShards() + " vs " + inSyncAllocationIds; if (inSyncAllocationIds.size() > oldInSyncAllocationIds.size() && inSyncAllocationIds.size() > maxActiveShards) { // trim entries that have no corresponding shard routing in the cluster state (i.e. trim unavailable copies) - List assignedShards = newShardRoutingTable.assignedShards(); - assert assignedShards.stream().filter(s -> s.isRelocationTarget() == false).count() <= maxActiveShards : + List assignedShards = newShardRoutingTable.assignedShards() + .stream().filter(s -> s.isRelocationTarget() == false).collect(Collectors.toList()); + assert assignedShards.size() <= maxActiveShards : "cannot have more assigned shards " + assignedShards + " than maximum possible active shards " + maxActiveShards; Set assignedAllocations = assignedShards.stream().map(s -> s.allocationId().getId()).collect(Collectors.toSet()); inSyncAllocationIds = inSyncAllocationIds.stream()