Skip to content

Commit 092d381

Browse files
author
Yannick Welsch
committed
Merge pull request #16530 from ywelsch/fix/reuse-alloc-id
Reuse existing allocation id for primary shard allocation
2 parents ebcbe5d + 94f19d7 commit 092d381

15 files changed

+157
-119
lines changed

core/src/main/java/org/elasticsearch/cluster/routing/AllocationId.java

+7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public static AllocationId newInitializing() {
9696
return new AllocationId(Strings.randomBase64UUID(), null);
9797
}
9898

99+
/**
100+
* Creates a new allocation id for initializing allocation based on an existing id.
101+
*/
102+
public static AllocationId newInitializing(String existingAllocationId) {
103+
return new AllocationId(existingAllocationId, null);
104+
}
105+
99106
/**
100107
* Creates a new allocation id for the target initializing shard that is the result
101108
* of a relocation.

core/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.cluster.block.ClusterBlocks;
2727
import org.elasticsearch.cluster.metadata.MetaData;
2828
import org.elasticsearch.cluster.node.DiscoveryNode;
29+
import org.elasticsearch.common.Nullable;
2930
import org.elasticsearch.common.Randomness;
3031
import org.elasticsearch.common.collect.ImmutableOpenMap;
3132
import org.elasticsearch.index.Index;
@@ -420,11 +421,13 @@ public String prettyPrint() {
420421

421422
/**
422423
* Moves a shard from unassigned to initialize state
424+
*
425+
* @param existingAllocationId allocation id to use. If null, a fresh allocation id is generated.
423426
*/
424-
public void initialize(ShardRouting shard, String nodeId, long expectedSize) {
427+
public void initialize(ShardRouting shard, String nodeId, @Nullable String existingAllocationId, long expectedSize) {
425428
ensureMutable();
426429
assert shard.unassigned() : shard;
427-
shard.initialize(nodeId, expectedSize);
430+
shard.initialize(nodeId, existingAllocationId, expectedSize);
428431
node(nodeId).add(shard);
429432
inactiveShardCount++;
430433
if (shard.primary()) {
@@ -692,10 +695,12 @@ public ShardRouting next() {
692695

693696
/**
694697
* Initializes the current unassigned shard and moves it from the unassigned list.
698+
*
699+
* @param existingAllocationId allocation id to use. If null, a fresh allocation id is generated.
695700
*/
696-
public void initialize(String nodeId, long expectedShardSize) {
701+
public void initialize(String nodeId, @Nullable String existingAllocationId, long expectedShardSize) {
697702
innerRemove();
698-
nodes.initialize(new ShardRouting(current), nodeId, expectedShardSize);
703+
nodes.initialize(new ShardRouting(current), nodeId, existingAllocationId, expectedShardSize);
699704
}
700705

701706
/**
@@ -711,7 +716,7 @@ public void removeAndIgnore() {
711716

712717
/**
713718
* Unsupported operation, just there for the interface. Use {@link #removeAndIgnore()} or
714-
* {@link #initialize(String, long)}.
719+
* {@link #initialize(String, String, long)}.
715720
*/
716721
@Override
717722
public void remove() {

core/src/main/java/org/elasticsearch/cluster/routing/ShardRouting.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,20 @@ void moveToUnassigned(UnassignedInfo unassignedInfo) {
410410

411411
/**
412412
* Initializes an unassigned shard on a node.
413+
*
414+
* @param existingAllocationId allocation id to use. If null, a fresh allocation id is generated.
413415
*/
414-
void initialize(String nodeId, long expectedShardSize) {
416+
void initialize(String nodeId, @Nullable String existingAllocationId, long expectedShardSize) {
415417
ensureNotFrozen();
416418
assert state == ShardRoutingState.UNASSIGNED : this;
417419
assert relocatingNodeId == null : this;
418420
state = ShardRoutingState.INITIALIZING;
419421
currentNodeId = nodeId;
420-
allocationId = AllocationId.newInitializing();
422+
if (existingAllocationId == null) {
423+
allocationId = AllocationId.newInitializing();
424+
} else {
425+
allocationId = AllocationId.newInitializing(existingAllocationId);
426+
}
421427
this.expectedShardSize = expectedShardSize;
422428
}
423429

core/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ private boolean allocateUnassigned(RoutingNodes.UnassignedShards unassigned) {
702702
if (logger.isTraceEnabled()) {
703703
logger.trace("Assigned shard [{}] to [{}]", shard, minNode.getNodeId());
704704
}
705-
routingNodes.initialize(shard, minNode.getNodeId(), allocation.clusterInfo().getShardSize(shard, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
705+
routingNodes.initialize(shard, minNode.getNodeId(), null, allocation.clusterInfo().getShardSize(shard, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
706706
changed = true;
707707
continue; // don't add to ignoreUnassigned
708708
} else {
@@ -790,7 +790,7 @@ private boolean tryRelocateShard(ModelNode minNode, ModelNode maxNode, String id
790790
routingNodes.relocate(candidate, minNode.getNodeId(), allocation.clusterInfo().getShardSize(candidate, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
791791

792792
} else {
793-
routingNodes.initialize(candidate, minNode.getNodeId(), allocation.clusterInfo().getShardSize(candidate, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
793+
routingNodes.initialize(candidate, minNode.getNodeId(), null, allocation.clusterInfo().getShardSize(candidate, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
794794
}
795795
return true;
796796

core/src/main/java/org/elasticsearch/cluster/routing/allocation/command/AbstractAllocateAllocationCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ protected void initializeUnassignedShard(RoutingAllocation allocation, RoutingNo
242242
if (shardRoutingChanges != null) {
243243
shardRoutingChanges.accept(unassigned);
244244
}
245-
it.initialize(routingNode.nodeId(), allocation.clusterInfo().getShardSize(unassigned, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
245+
it.initialize(routingNode.nodeId(), null, allocation.clusterInfo().getShardSize(unassigned, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
246246
return;
247247
}
248248
assert false : "shard to initialize not found in list of unassigned shards";

0 commit comments

Comments
 (0)