Skip to content

Commit 6d0d449

Browse files
authored
Relocation targets are assigned shards too (#43276)
Adds relocation targets to the output of `IndexShardRoutingTable#assignedShards`.
1 parent 3ba3861 commit 6d0d449

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public class IndexShardRoutingTable implements Iterable<ShardRouting> {
105105
// create the target initializing shard routing on the node the shard is relocating to
106106
allInitializingShards.add(shard.getTargetRelocatingShard());
107107
allAllocationIds.add(shard.getTargetRelocatingShard().allocationId().getId());
108+
109+
assert shard.assignedToNode() : "relocating from unassigned " + shard;
110+
assert shard.getTargetRelocatingShard().assignedToNode() : "relocating to unassigned " + shard.getTargetRelocatingShard();
111+
assignedShards.add(shard.getTargetRelocatingShard());
108112
}
109113
if (shard.assignedToNode()) {
110114
assignedShards.add(shard);
@@ -211,7 +215,7 @@ public List<ShardRouting> getActiveShards() {
211215
}
212216

213217
/**
214-
* Returns a {@link List} of assigned shards
218+
* Returns a {@link List} of assigned shards, including relocation targets
215219
*
216220
* @return a {@link List} of shards
217221
*/
@@ -518,11 +522,6 @@ public ShardRouting getByAllocationId(String allocationId) {
518522
if (shardRouting.allocationId().getId().equals(allocationId)) {
519523
return shardRouting;
520524
}
521-
if (shardRouting.relocating()) {
522-
if (shardRouting.getTargetRelocatingShard().allocationId().getId().equals(allocationId)) {
523-
return shardRouting.getTargetRelocatingShard();
524-
}
525-
}
526525
}
527526
return null;
528527
}

server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetaDataUpdater.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,13 @@ private IndexMetaData.Builder updateInSyncAllocations(RoutingTable newRoutingTab
194194
// of replicas was decreased while shards were unassigned.
195195
int maxActiveShards = oldIndexMetaData.getNumberOfReplicas() + 1; // +1 for the primary
196196
IndexShardRoutingTable newShardRoutingTable = newRoutingTable.shardRoutingTable(shardId);
197+
assert newShardRoutingTable.assignedShards().stream()
198+
.filter(ShardRouting::isRelocationTarget).map(s -> s.allocationId().getId()).noneMatch(inSyncAllocationIds::contains)
199+
: newShardRoutingTable.assignedShards() + " vs " + inSyncAllocationIds;
197200
if (inSyncAllocationIds.size() > oldInSyncAllocationIds.size() && inSyncAllocationIds.size() > maxActiveShards) {
198201
// trim entries that have no corresponding shard routing in the cluster state (i.e. trim unavailable copies)
199-
List<ShardRouting> assignedShards = newShardRoutingTable.assignedShards();
202+
List<ShardRouting> assignedShards = newShardRoutingTable.assignedShards()
203+
.stream().filter(s -> s.isRelocationTarget() == false).collect(Collectors.toList());
200204
assert assignedShards.size() <= maxActiveShards :
201205
"cannot have more assigned shards " + assignedShards + " than maximum possible active shards " + maxActiveShards;
202206
Set<String> assignedAllocations = assignedShards.stream().map(s -> s.allocationId().getId()).collect(Collectors.toSet());

0 commit comments

Comments
 (0)