Skip to content

Commit bbd6002

Browse files
Refactor MoveDecision method names for clarity
1 parent f474779 commit bbd6002

File tree

5 files changed

+53
-42
lines changed

5 files changed

+53
-42
lines changed

server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ public void testAllocationFilteringPreventsShardMove() throws Exception {
538538
assertTrue(moveDecision.isDecisionTaken());
539539
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
540540
assertEquals(Explanations.Move.NO, moveDecision.getExplanation());
541-
assertFalse(moveDecision.canRemain());
542-
assertFalse(moveDecision.forceMove());
541+
assertFalse(moveDecision.canRemainYes());
542+
assertFalse(moveDecision.cannotRemainAndCanMove());
543543
assertFalse(moveDecision.canRebalanceCluster());
544544
assertNull(moveDecision.getClusterRebalanceDecision());
545545
assertNull(moveDecision.getTargetNode());
@@ -651,8 +651,8 @@ public void testRebalancingNotAllowed() throws Exception {
651651
assertTrue(moveDecision.isDecisionTaken());
652652
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
653653
assertEquals(Explanations.Rebalance.CANNOT_REBALANCE_CAN_ALLOCATE, moveDecision.getExplanation());
654-
assertTrue(moveDecision.canRemain());
655-
assertFalse(moveDecision.forceMove());
654+
assertTrue(moveDecision.canRemainYes());
655+
assertFalse(moveDecision.cannotRemainAndCanMove());
656656
assertFalse(moveDecision.canRebalanceCluster());
657657
assertNotNull(moveDecision.getCanRemainDecision());
658658
assertNull(moveDecision.getTargetNode());
@@ -757,8 +757,8 @@ public void testWorseBalance() throws Exception {
757757
assertTrue(moveDecision.isDecisionTaken());
758758
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
759759
assertEquals(Explanations.Rebalance.ALREADY_BALANCED, moveDecision.getExplanation());
760-
assertTrue(moveDecision.canRemain());
761-
assertFalse(moveDecision.forceMove());
760+
assertTrue(moveDecision.canRemainYes());
761+
assertFalse(moveDecision.cannotRemainAndCanMove());
762762
assertTrue(moveDecision.canRebalanceCluster());
763763
assertNotNull(moveDecision.getCanRemainDecision());
764764
assertNull(moveDecision.getTargetNode());
@@ -855,8 +855,8 @@ public void testBetterBalanceButCannotAllocate() throws Exception {
855855
assertTrue(moveDecision.isDecisionTaken());
856856
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
857857
assertEquals(Explanations.Rebalance.ALREADY_BALANCED, moveDecision.getExplanation());
858-
assertTrue(moveDecision.canRemain());
859-
assertFalse(moveDecision.forceMove());
858+
assertTrue(moveDecision.canRemainYes());
859+
assertFalse(moveDecision.cannotRemainAndCanMove());
860860
assertTrue(moveDecision.canRebalanceCluster());
861861
assertNotNull(moveDecision.getCanRemainDecision());
862862
assertNull(moveDecision.getTargetNode());
@@ -963,8 +963,8 @@ public void testAssignedReplicaOnSpecificNode() throws Exception {
963963
assertTrue(moveDecision.isDecisionTaken());
964964
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
965965
assertEquals(Explanations.Rebalance.CANNOT_REBALANCE_CANNOT_ALLOCATE, moveDecision.getExplanation());
966-
assertTrue(moveDecision.canRemain());
967-
assertFalse(moveDecision.forceMove());
966+
assertTrue(moveDecision.canRemainYes());
967+
assertFalse(moveDecision.cannotRemainAndCanMove());
968968
assertFalse(moveDecision.canRebalanceCluster());
969969
assertNotNull(moveDecision.getCanRemainDecision());
970970
assertNull(moveDecision.getTargetNode());

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ public void writeTo(StreamOutput out) throws IOException {
9292
* Creates a move decision for the shard being able to remain on its current node, so the shard won't
9393
* be forced to move to another node.
9494
*/
95-
public static MoveDecision remain(Decision canRemainDecision) {
95+
public static MoveDecision createMoveDecisionWithRemainYesDecision(Decision canRemainDecision) {
96+
assert canRemainDecision.type() != Type.NO;
9697
if (canRemainDecision == Decision.YES) {
9798
return CACHED_STAY_DECISION;
9899
}
99-
assert canRemainDecision.type() != Type.NO;
100100
return new MoveDecision(null, null, AllocationDecision.NO_ATTEMPT, canRemainDecision, null, 0);
101101
}
102102

@@ -150,16 +150,26 @@ public boolean isDecisionTaken() {
150150
* returns {@code false} otherwise. If {@link #isDecisionTaken()} returns {@code false},
151151
* then invoking this method will throw an {@code IllegalStateException}.
152152
*/
153-
public boolean forceMove() {
153+
public boolean cannotRemainAndCanMove() {
154+
checkDecisionState();
155+
return canRemainYes() == false && canMoveDecision == AllocationDecision.YES;
156+
}
157+
158+
/**
159+
* Returns {@code true} if the shard cannot remain on its current node and _cannot_ be moved.
160+
* returns {@code false} otherwise. If {@link #isDecisionTaken()} returns {@code false},
161+
* then invoking this method will throw an {@code IllegalStateException}.
162+
*/
163+
public boolean cannotRemainAndCannotMove() {
154164
checkDecisionState();
155-
return canRemain() == false && canMoveDecision == AllocationDecision.YES;
165+
return canRemainDecision.type() != Type.YES && canMoveDecision != AllocationDecision.YES;
156166
}
157167

158168
/**
159169
* Returns {@code true} if the shard can remain on its current node, returns {@code false} otherwise.
160170
* If {@link #isDecisionTaken()} returns {@code false}, then invoking this method will throw an {@code IllegalStateException}.
161171
*/
162-
public boolean canRemain() {
172+
public boolean canRemainYes() {
163173
checkDecisionState();
164174
return canRemainDecision.type() == Type.YES;
165175
}
@@ -187,7 +197,7 @@ public boolean canRebalanceCluster() {
187197

188198
/**
189199
* Returns the decision for being allowed to rebalance the shard. Invoking this method will return
190-
* {@code null} if {@link #canRemain()} ()} returns {@code false}, which means the node is not allowed to
200+
* {@code null} if {@link #canRemainYes()} ()} returns {@code false}, which means the node is not allowed to
191201
* remain on its current node, so the cluster is forced to attempt to move the shard to a different node,
192202
* as opposed to attempting to rebalance the shard if a better cluster balance is possible by moving it.
193203
* If {@link #isDecisionTaken()} returns {@code false}, then invoking this method will throw an
@@ -246,7 +256,7 @@ public String getExplanation() {
246256
};
247257
} else {
248258
// it was a decision to force move the shard
249-
assert canRemain() == false;
259+
assert canRemainYes() == false;
250260
return switch (canMoveDecision) {
251261
case YES -> Explanations.Move.YES;
252262
case THROTTLED -> Explanations.Move.THROTTLED;
@@ -268,8 +278,8 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
268278
discoveryNodeToXContent(targetNode, true, builder);
269279
builder.endObject();
270280
}
271-
builder.field("can_remain_on_current_node", canRemain() ? "yes" : "no");
272-
if (canRemain() == false && canRemainDecision.getDecisions().isEmpty() == false) {
281+
builder.field("can_remain_on_current_node", canRemainYes() ? "yes" : "no");
282+
if (canRemainYes() == false && canRemainDecision.getDecisions().isEmpty() == false) {
273283
builder.startArray("can_remain_decisions");
274284
canRemainDecision.toXContent(builder, params);
275285
builder.endArray();
@@ -287,7 +297,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
287297
builder.field("can_rebalance_to_other_node", canMoveDecision);
288298
builder.field("rebalance_explanation", getExplanation());
289299
} else {
290-
builder.field("can_move_to_other_node", forceMove() ? "yes" : "no");
300+
builder.field("can_move_to_other_node", cannotRemainAndCanMove() ? "yes" : "no");
291301
builder.field("move_explanation", getExplanation());
292302
}
293303
return builder;

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public ShardAllocationDecision decideShardAllocation(final ShardRouting shard, f
258258
allocateUnassignedDecision = balancer.decideAllocateUnassigned(index, shard);
259259
} else {
260260
moveDecision = balancer.decideMove(index, shard);
261-
if (moveDecision.isDecisionTaken() && moveDecision.canRemain()) {
261+
if (moveDecision.isDecisionTaken() && moveDecision.canRemainYes()) {
262262
moveDecision = balancer.decideRebalance(index, shard, moveDecision.getCanRemainDecision());
263263
}
264264
}
@@ -819,7 +819,7 @@ public boolean moveShards() {
819819
shardRouting,
820820
bestNonPreferredShardMovementsTracker::shardIsBetterThanCurrent
821821
);
822-
if (moveDecision.isDecisionTaken() && moveDecision.forceMove()) {
822+
if (moveDecision.isDecisionTaken() && moveDecision.cannotRemainAndCanMove()) {
823823
// Defer moving of not-preferred until we've moved the NOs
824824
if (moveDecision.getCanRemainDecision().type() == Type.NOT_PREFERRED) {
825825
bestNonPreferredShardMovementsTracker.putBestMoveDecision(shardRouting, moveDecision);
@@ -830,7 +830,7 @@ public boolean moveShards() {
830830
}
831831
shardMoved = true;
832832
}
833-
} else if (moveDecision.isDecisionTaken() && moveDecision.canRemain() == false) {
833+
} else if (moveDecision.isDecisionTaken() && moveDecision.canRemainYes() == false) {
834834
logger.trace("[{}][{}] can't move", shardRouting.index(), shardRouting.id());
835835
}
836836
}
@@ -843,7 +843,7 @@ public boolean moveShards() {
843843
// invalid, so we must call `decideMove` again. If not, we know we haven't made any moves, and we
844844
// can use the cached decision.
845845
final var moveDecision = shardMoved ? decideMove(index, shardRouting) : storedShardMovement.moveDecision();
846-
if (moveDecision.isDecisionTaken() && moveDecision.forceMove()) {
846+
if (moveDecision.isDecisionTaken() && moveDecision.cannotRemainAndCanMove()) {
847847
executeMove(shardRouting, index, moveDecision, "move-non-preferred");
848848
// Return after a single move so that the change can be simulated before further moves are made.
849849
return true;
@@ -896,8 +896,9 @@ public MoveDecision decideMove(final ProjectIndex index, final ShardRouting shar
896896
* 2. If the shard is allowed to remain on its current node, no attempt will be made to move the shard and
897897
* {@link MoveDecision#getCanRemainDecision} will have a decision type of YES. All other fields in the object will be null.
898898
* 3. If the shard is not allowed to remain on its current node, then {@link MoveDecision#getAllocationDecision()} will be
899-
* populated with the decision of moving to another node. If {@link MoveDecision#forceMove()} returns {@code true}, then
900-
* {@link MoveDecision#getTargetNode} will return a non-null value, otherwise the assignedNodeId will be null.
899+
* populated with the decision of moving to another node. If {@link MoveDecision#cannotRemainAndCanMove()} returns
900+
* {@code true}, then {@link MoveDecision#getTargetNode} will return a non-null value, otherwise the assignedNodeId will be
901+
* null.
901902
* 4. If the method is invoked in explain mode (e.g. from the cluster allocation explain APIs), then
902903
* {@link MoveDecision#getNodeDecisions} will have a non-null value.
903904
*
@@ -924,7 +925,7 @@ private MoveDecision decideMove(ProjectIndex index, ShardRouting shardRouting, P
924925
RoutingNode routingNode = sourceNode.getRoutingNode();
925926
Decision canRemain = allocation.deciders().canRemain(shardRouting, routingNode, allocation);
926927
if (canRemain.type() != Decision.Type.NO && canRemain.type() != Decision.Type.NOT_PREFERRED) {
927-
return MoveDecision.remain(canRemain);
928+
return MoveDecision.createMoveDecisionWithRemainYesDecision(canRemain);
928929
}
929930

930931
// Check predicate to decide whether to assess movement options
@@ -940,7 +941,7 @@ private MoveDecision decideMove(ProjectIndex index, ShardRouting shardRouting, P
940941
* allocate on the minimal eligible node.
941942
*/
942943
final MoveDecision moveDecision = decideMove(sorter, shardRouting, sourceNode, canRemain, this::decideCanAllocate);
943-
if (moveDecision.canRemain() == false && moveDecision.forceMove() == false) {
944+
if (moveDecision.cannotRemainAndCannotMove()) {
944945
final boolean shardsOnReplacedNode = allocation.metadata().nodeShutdowns().contains(shardRouting.currentNodeId(), REPLACE);
945946
if (shardsOnReplacedNode) {
946947
return decideMove(sorter, shardRouting, sourceNode, canRemain, this::decideCanForceAllocateForVacate);

server/src/test/java/org/elasticsearch/cluster/routing/allocation/MoveDecisionTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public class MoveDecisionTests extends ESTestCase {
2929

3030
public void testCachedDecisions() {
3131
// cached stay decision
32-
MoveDecision stay1 = MoveDecision.remain(Decision.YES);
33-
MoveDecision stay2 = MoveDecision.remain(Decision.YES);
32+
MoveDecision stay1 = MoveDecision.createMoveDecisionWithRemainYesDecision(Decision.YES);
33+
MoveDecision stay2 = MoveDecision.createMoveDecisionWithRemainYesDecision(Decision.YES);
3434
assertSame(stay1, stay2); // not in explain mode, so should use cached decision
3535

36-
stay1 = MoveDecision.remain(new Decision.Single(Type.YES, null, null, (Object[]) null));
37-
stay2 = MoveDecision.remain(new Decision.Single(Type.YES, null, null, (Object[]) null));
36+
stay1 = MoveDecision.createMoveDecisionWithRemainYesDecision(new Decision.Single(Type.YES, null, null, (Object[]) null));
37+
stay2 = MoveDecision.createMoveDecisionWithRemainYesDecision(new Decision.Single(Type.YES, null, null, (Object[]) null));
3838
assertNotSame(stay1, stay2);
3939

4040
// cached cannot move decision
@@ -57,16 +57,16 @@ public void testCachedDecisions() {
5757
}
5858

5959
public void testStayDecision() {
60-
MoveDecision stay = MoveDecision.remain(Decision.YES);
61-
assertTrue(stay.canRemain());
62-
assertFalse(stay.forceMove());
60+
MoveDecision stay = MoveDecision.createMoveDecisionWithRemainYesDecision(Decision.YES);
61+
assertTrue(stay.canRemainYes());
62+
assertFalse(stay.cannotRemainAndCanMove());
6363
assertTrue(stay.isDecisionTaken());
6464
assertNull(stay.getNodeDecisions());
6565
assertEquals(AllocationDecision.NO_ATTEMPT, stay.getAllocationDecision());
6666

67-
stay = MoveDecision.remain(Decision.YES);
68-
assertTrue(stay.canRemain());
69-
assertFalse(stay.forceMove());
67+
stay = MoveDecision.createMoveDecisionWithRemainYesDecision(Decision.YES);
68+
assertTrue(stay.canRemainYes());
69+
assertFalse(stay.cannotRemainAndCanMove());
7070
assertTrue(stay.isDecisionTaken());
7171
assertNull(stay.getNodeDecisions());
7272
assertEquals(AllocationDecision.NO_ATTEMPT, stay.getAllocationDecision());
@@ -114,9 +114,9 @@ public void testSerialization() throws IOException {
114114
BytesStreamOutput output = new BytesStreamOutput();
115115
moveDecision.writeTo(output);
116116
MoveDecision readDecision = new MoveDecision(output.bytes().streamInput());
117-
assertEquals(moveDecision.canRemain(), readDecision.canRemain());
117+
assertEquals(moveDecision.canRemainYes(), readDecision.canRemainYes());
118118
assertEquals(moveDecision.getExplanation(), readDecision.getExplanation());
119-
assertEquals(moveDecision.forceMove(), readDecision.forceMove());
119+
assertEquals(moveDecision.cannotRemainAndCanMove(), readDecision.cannotRemainAndCanMove());
120120
assertEquals(moveDecision.getNodeDecisions().size(), readDecision.getNodeDecisions().size());
121121
assertEquals(moveDecision.getTargetNode(), readDecision.getTargetNode());
122122
assertEquals(moveDecision.getAllocationDecision(), readDecision.getAllocationDecision());

x-pack/plugin/shutdown/src/main/java/org/elasticsearch/xpack/shutdown/TransportGetShutdownStatusAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ static ShutdownShardMigrationStatus shardMigrationStatus(
285285
.map(shardRouting -> new Tuple<>(shardRouting, allocationService.explainShardAllocation(shardRouting, allocation)))
286286
// Given that we're checking the status of a node that's shutting down, no shards should be allowed to remain
287287
.filter(pair -> {
288-
assert pair.v2().getMoveDecision().canRemain() == false
288+
assert pair.v2().getMoveDecision().canRemainYes() == false
289289
: "shard [" + pair + "] can remain on node [" + nodeId + "], but that node is shutting down";
290-
return pair.v2().getMoveDecision().canRemain() == false;
290+
return pair.v2().getMoveDecision().canRemainYes() == false;
291291
})
292292
// These shards will move as soon as possible
293293
.filter(pair -> pair.v2().getMoveDecision().getAllocationDecision().equals(AllocationDecision.YES) == false)

0 commit comments

Comments
 (0)