Skip to content

Commit 2c78505

Browse files
committed
move all allocations to be Decision base
this again will allow to provide better output as to why a specific decision has been made (like a shard needing to move from a node)
1 parent a963bc1 commit 2c78505

10 files changed

+54
-41
lines changed

src/main/java/org/elasticsearch/cluster/routing/allocation/AllocationService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocators;
2929
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommands;
3030
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders;
31+
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
3132
import org.elasticsearch.common.component.AbstractComponent;
3233
import org.elasticsearch.common.inject.Inject;
3334
import org.elasticsearch.common.settings.ImmutableSettings;
@@ -223,7 +224,8 @@ private boolean moveShards(RoutingAllocation allocation) {
223224
continue;
224225
}
225226
RoutingNode routingNode = allocation.routingNodes().node(shardRouting.currentNodeId());
226-
if (!allocation.deciders().canRemain(shardRouting, routingNode, allocation)) {
227+
Decision decision = allocation.deciders().canRemain(shardRouting, routingNode, allocation);
228+
if (decision.type() == Decision.Type.NO) {
227229
logger.debug("[{}][{}] allocated on [{}], but can no longer be allocated on it, moving...", shardRouting.index(), shardRouting.id(), routingNode.node());
228230
boolean moved = shardsAllocators.move(shardRouting, routingNode, allocation);
229231
if (!moved) {

src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/EvenShardsCountAllocator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,13 @@ public boolean rebalance(RoutingAllocation allocation) {
141141
boolean relocated = false;
142142
List<MutableShardRouting> startedShards = highRoutingNode.shardsWithState(STARTED);
143143
for (MutableShardRouting startedShard : startedShards) {
144-
if (!allocation.deciders().canRebalance(startedShard, allocation)) {
144+
Decision rebalanceDecision = allocation.deciders().canRebalance(startedShard, allocation);
145+
if (rebalanceDecision.type() == Decision.Type.NO) {
145146
continue;
146147
}
147148

148-
Decision decision = allocation.deciders().canAllocate(startedShard, lowRoutingNode, allocation);
149-
if (decision.type() == Decision.Type.YES) {
149+
Decision allocateDecision = allocation.deciders().canAllocate(startedShard, lowRoutingNode, allocation);
150+
if (allocateDecision.type() == Decision.Type.YES) {
150151
changed = true;
151152
lowRoutingNode.add(new MutableShardRouting(startedShard.index(), startedShard.id(),
152153
lowRoutingNode.nodeId(), startedShard.currentNodeId(),

src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDecider.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,24 @@ protected AllocationDecider(Settings settings) {
3434
super(settings);
3535
}
3636

37-
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
38-
return true;
37+
/**
38+
* Are we allowed to rebalance this shard?
39+
*/
40+
public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
41+
return Decision.ALWAYS;
3942
}
4043

44+
/**
45+
* Can the provided shard routing be allocated on the node.
46+
*/
4147
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
4248
return Decision.ALWAYS;
4349
}
4450

4551
/**
4652
* Can the provided shard routing remain on the node?
4753
*/
48-
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
49-
return true;
54+
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
55+
return Decision.ALWAYS;
5056
}
5157
}

src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDeciders.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ public AllocationDeciders(Settings settings, Set<AllocationDecider> allocations)
5959
}
6060

6161
@Override
62-
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
63-
for (AllocationDecider allocation1 : allocations) {
64-
if (!allocation1.canRebalance(shardRouting, allocation)) {
65-
return false;
62+
public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
63+
Decision.Multi ret = new Decision.Multi();
64+
for (AllocationDecider allocationDecider : allocations) {
65+
Decision decision = allocationDecider.canRebalance(shardRouting, allocation);
66+
if (decision != Decision.ALWAYS) {
67+
ret.add(decision);
6668
}
6769
}
68-
return true;
70+
return ret;
6971
}
7072

7173
@Override
@@ -86,15 +88,17 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
8688
}
8789

8890
@Override
89-
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
91+
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
9092
if (allocation.shouldIgnoreShardForNode(shardRouting.shardId(), node.nodeId())) {
91-
return false;
93+
return Decision.NO;
9294
}
93-
for (AllocationDecider allocation1 : allocations) {
94-
if (!allocation1.canRemain(shardRouting, node, allocation)) {
95-
return false;
95+
Decision.Multi ret = new Decision.Multi();
96+
for (AllocationDecider allocationDecider : allocations) {
97+
Decision decision = allocationDecider.canRemain(shardRouting, node, allocation);
98+
if (decision != Decision.ALWAYS) {
99+
ret.add(decision);
96100
}
97101
}
98-
return true;
102+
return ret;
99103
}
100104
}

src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AwarenessAllocationDecider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
107107
}
108108

109109
@Override
110-
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
111-
return underCapacity(shardRouting, node, allocation, false);
110+
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
111+
return underCapacity(shardRouting, node, allocation, false) ? Decision.YES : Decision.NO;
112112
}
113113

114114
private boolean underCapacity(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation, boolean moveToNode) {

src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ClusterRebalanceAllocationDecider.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,39 @@ public ClusterRebalanceAllocationDecider(Settings settings) {
5656
}
5757

5858
@Override
59-
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
59+
public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
6060
if (type == ClusterRebalanceType.INDICES_PRIMARIES_ACTIVE) {
6161
for (MutableShardRouting shard : allocation.routingNodes().unassigned()) {
6262
if (shard.primary()) {
63-
return false;
63+
return Decision.NO;
6464
}
6565
}
6666
for (RoutingNode node : allocation.routingNodes()) {
6767
List<MutableShardRouting> shards = node.shards();
6868
for (int i = 0; i < shards.size(); i++) {
6969
MutableShardRouting shard = shards.get(i);
7070
if (shard.primary() && !shard.active() && shard.relocatingNodeId() == null) {
71-
return false;
71+
return Decision.NO;
7272
}
7373
}
7474
}
75-
return true;
75+
return Decision.YES;
7676
}
7777
if (type == ClusterRebalanceType.INDICES_ALL_ACTIVE) {
7878
if (!allocation.routingNodes().unassigned().isEmpty()) {
79-
return false;
79+
return Decision.NO;
8080
}
8181
for (RoutingNode node : allocation.routingNodes()) {
8282
List<MutableShardRouting> shards = node.shards();
8383
for (int i = 0; i < shards.size(); i++) {
8484
MutableShardRouting shard = shards.get(i);
8585
if (!shard.active() && shard.relocatingNodeId() == null) {
86-
return false;
86+
return Decision.NO;
8787
}
8888
}
8989
}
9090
}
9191
// type == Type.ALWAYS
92-
return true;
92+
return Decision.YES;
9393
}
9494
}

src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ConcurrentRebalanceAllocationDecider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public ConcurrentRebalanceAllocationDecider(Settings settings, NodeSettingsServi
6161
}
6262

6363
@Override
64-
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
64+
public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
6565
if (clusterConcurrentRebalance == -1) {
66-
return true;
66+
return Decision.YES;
6767
}
6868
int rebalance = 0;
6969
for (RoutingNode node : allocation.routingNodes()) {
@@ -75,8 +75,8 @@ public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocat
7575
}
7676
}
7777
if (rebalance >= clusterConcurrentRebalance) {
78-
return false;
78+
return Decision.NO;
7979
}
80-
return true;
80+
return Decision.YES;
8181
}
8282
}

src/main/java/org/elasticsearch/cluster/routing/allocation/decider/FilterAllocationDecider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
7272
}
7373

7474
@Override
75-
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
76-
return !shouldFilter(shardRouting, node, allocation);
75+
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
76+
return shouldFilter(shardRouting, node, allocation) ? Decision.NO : Decision.YES;
7777
}
7878

7979
private boolean shouldFilter(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {

src/main/java/org/elasticsearch/cluster/routing/allocation/decider/RebalanceOnlyWhenActiveAllocationDecider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public RebalanceOnlyWhenActiveAllocationDecider(Settings settings) {
3838
}
3939

4040
@Override
41-
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
41+
public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
4242
List<MutableShardRouting> shards = allocation.routingNodes().shardsRoutingFor(shardRouting);
4343
// its ok to check for active here, since in relocation, a shard is split into two in routing
4444
// nodes, once relocating, and one initializing
4545
for (int i = 0; i < shards.size(); i++) {
4646
if (!shards.get(i).active()) {
47-
return false;
47+
return Decision.NO;
4848
}
4949
}
50-
return true;
50+
return Decision.YES;
5151
}
5252
}

src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ShardsLimitAllocationDecider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
7575
}
7676

7777
@Override
78-
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
78+
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
7979
IndexMetaData indexMd = allocation.routingNodes().metaData().index(shardRouting.index());
8080
int totalShardsPerNode = indexMd.settings().getAsInt(INDEX_TOTAL_SHARDS_PER_NODE, -1);
8181
if (totalShardsPerNode <= 0) {
82-
return true;
82+
return Decision.YES;
8383
}
8484

8585
int nodeCount = 0;
@@ -96,8 +96,8 @@ public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAll
9696
nodeCount++;
9797
}
9898
if (nodeCount > totalShardsPerNode) {
99-
return false;
99+
return Decision.NO;
100100
}
101-
return true;
101+
return Decision.YES;
102102
}
103103
}

0 commit comments

Comments
 (0)