Skip to content

Commit aa643b1

Browse files
Refactor MoveDecision method names for clarity (#136139)
These method names reflect the purpose for which they were used in the past, rather than what they mean. Given how much the code has changed since class/methods were created, and the different ways in which they are now used, the names have become confusing. This commit changes some names to better reflect meaning.
1 parent 480d198 commit aa643b1

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ public void testAllocationFilteringPreventsShardMove() throws Exception {
539539
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
540540
assertEquals(Explanations.Move.NO, moveDecision.getExplanation());
541541
assertFalse(moveDecision.canRemain());
542-
assertFalse(moveDecision.forceMove());
542+
assertFalse(moveDecision.cannotRemainAndCanMove());
543543
assertFalse(moveDecision.canRebalanceCluster());
544544
assertNull(moveDecision.getClusterRebalanceDecision());
545545
assertNull(moveDecision.getTargetNode());
@@ -652,7 +652,7 @@ public void testRebalancingNotAllowed() throws Exception {
652652
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
653653
assertEquals(Explanations.Rebalance.CANNOT_REBALANCE_CAN_ALLOCATE, moveDecision.getExplanation());
654654
assertTrue(moveDecision.canRemain());
655-
assertFalse(moveDecision.forceMove());
655+
assertFalse(moveDecision.cannotRemainAndCanMove());
656656
assertFalse(moveDecision.canRebalanceCluster());
657657
assertNotNull(moveDecision.getCanRemainDecision());
658658
assertNull(moveDecision.getTargetNode());
@@ -758,7 +758,7 @@ public void testWorseBalance() throws Exception {
758758
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
759759
assertEquals(Explanations.Rebalance.ALREADY_BALANCED, moveDecision.getExplanation());
760760
assertTrue(moveDecision.canRemain());
761-
assertFalse(moveDecision.forceMove());
761+
assertFalse(moveDecision.cannotRemainAndCanMove());
762762
assertTrue(moveDecision.canRebalanceCluster());
763763
assertNotNull(moveDecision.getCanRemainDecision());
764764
assertNull(moveDecision.getTargetNode());
@@ -856,7 +856,7 @@ public void testBetterBalanceButCannotAllocate() throws Exception {
856856
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
857857
assertEquals(Explanations.Rebalance.ALREADY_BALANCED, moveDecision.getExplanation());
858858
assertTrue(moveDecision.canRemain());
859-
assertFalse(moveDecision.forceMove());
859+
assertFalse(moveDecision.cannotRemainAndCanMove());
860860
assertTrue(moveDecision.canRebalanceCluster());
861861
assertNotNull(moveDecision.getCanRemainDecision());
862862
assertNull(moveDecision.getTargetNode());
@@ -964,7 +964,7 @@ public void testAssignedReplicaOnSpecificNode() throws Exception {
964964
assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
965965
assertEquals(Explanations.Rebalance.CANNOT_REBALANCE_CANNOT_ALLOCATE, moveDecision.getExplanation());
966966
assertTrue(moveDecision.canRemain());
967-
assertFalse(moveDecision.forceMove());
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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ 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 createRemainYesDecision(Decision canRemainDecision) {
96+
assert canRemainDecision.type() != Type.NO;
97+
assert canRemainDecision.type() != Type.NOT_PREFERRED;
9698
if (canRemainDecision == Decision.YES) {
9799
return CACHED_STAY_DECISION;
98100
}
99-
assert canRemainDecision.type() != Type.NO;
100101
return new MoveDecision(null, null, AllocationDecision.NO_ATTEMPT, canRemainDecision, null, 0);
101102
}
102103

@@ -150,11 +151,21 @@ public boolean isDecisionTaken() {
150151
* returns {@code false} otherwise. If {@link #isDecisionTaken()} returns {@code false},
151152
* then invoking this method will throw an {@code IllegalStateException}.
152153
*/
153-
public boolean forceMove() {
154+
public boolean cannotRemainAndCanMove() {
154155
checkDecisionState();
155156
return canRemain() == false && canMoveDecision == AllocationDecision.YES;
156157
}
157158

159+
/**
160+
* Returns {@code true} if the shard cannot remain on its current node and _cannot_ be moved.
161+
* returns {@code false} otherwise. If {@link #isDecisionTaken()} returns {@code false},
162+
* then invoking this method will throw an {@code IllegalStateException}.
163+
*/
164+
public boolean cannotRemainAndCannotMove() {
165+
checkDecisionState();
166+
return canRemain() == false && canMoveDecision != AllocationDecision.YES;
167+
}
168+
158169
/**
159170
* Returns {@code true} if the shard can remain on its current node, returns {@code false} otherwise.
160171
* If {@link #isDecisionTaken()} returns {@code false}, then invoking this method will throw an {@code IllegalStateException}.
@@ -287,7 +298,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
287298
builder.field("can_rebalance_to_other_node", canMoveDecision);
288299
builder.field("rebalance_explanation", getExplanation());
289300
} else {
290-
builder.field("can_move_to_other_node", forceMove() ? "yes" : "no");
301+
builder.field("can_move_to_other_node", cannotRemainAndCanMove() ? "yes" : "no");
291302
builder.field("move_explanation", getExplanation());
292303
}
293304
return builder;

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ public boolean moveShards() {
821821
shardRouting,
822822
bestNonPreferredShardMovementsTracker::shardIsBetterThanCurrent
823823
);
824-
if (moveDecision.isDecisionTaken() && moveDecision.forceMove()) {
824+
if (moveDecision.isDecisionTaken() && moveDecision.cannotRemainAndCanMove()) {
825825
// Defer moving of not-preferred until we've moved the NOs
826826
if (moveDecision.getCanRemainDecision().type() == Type.NOT_PREFERRED) {
827827
bestNonPreferredShardMovementsTracker.putBestMoveDecision(shardRouting, moveDecision);
@@ -845,7 +845,7 @@ public boolean moveShards() {
845845
// invalid, so we must call `decideMove` again. If not, we know we haven't made any moves, and we
846846
// can use the cached decision.
847847
final var moveDecision = shardMoved ? decideMove(index, shardRouting) : storedShardMovement.moveDecision();
848-
if (moveDecision.isDecisionTaken() && moveDecision.forceMove()) {
848+
if (moveDecision.isDecisionTaken() && moveDecision.cannotRemainAndCanMove()) {
849849
executeMove(shardRouting, index, moveDecision, "move-non-preferred");
850850
// Return after a single move so that the change can be simulated before further moves are made.
851851
return true;
@@ -898,8 +898,9 @@ public MoveDecision decideMove(final ProjectIndex index, final ShardRouting shar
898898
* 2. If the shard is allowed to remain on its current node, no attempt will be made to move the shard and
899899
* {@link MoveDecision#getCanRemainDecision} will have a decision type of YES. All other fields in the object will be null.
900900
* 3. If the shard is not allowed to remain on its current node, then {@link MoveDecision#getAllocationDecision()} will be
901-
* populated with the decision of moving to another node. If {@link MoveDecision#forceMove()} returns {@code true}, then
902-
* {@link MoveDecision#getTargetNode} will return a non-null value, otherwise the assignedNodeId will be null.
901+
* populated with the decision of moving to another node. If {@link MoveDecision#cannotRemainAndCanMove()} returns
902+
* {@code true}, then {@link MoveDecision#getTargetNode} will return a non-null value, otherwise the assignedNodeId will be
903+
* null.
903904
* 4. If the method is invoked in explain mode (e.g. from the cluster allocation explain APIs), then
904905
* {@link MoveDecision#getNodeDecisions} will have a non-null value.
905906
*
@@ -926,7 +927,7 @@ private MoveDecision decideMove(ProjectIndex index, ShardRouting shardRouting, P
926927
RoutingNode routingNode = sourceNode.getRoutingNode();
927928
Decision canRemain = allocation.deciders().canRemain(shardRouting, routingNode, allocation);
928929
if (canRemain.type() != Decision.Type.NO && canRemain.type() != Decision.Type.NOT_PREFERRED) {
929-
return MoveDecision.remain(canRemain);
930+
return MoveDecision.createRemainYesDecision(canRemain);
930931
}
931932

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

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

Lines changed: 9 additions & 9 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.createRemainYesDecision(Decision.YES);
33+
MoveDecision stay2 = MoveDecision.createRemainYesDecision(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.createRemainYesDecision(new Decision.Single(Type.YES, null, null, (Object[]) null));
37+
stay2 = MoveDecision.createRemainYesDecision(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);
60+
MoveDecision stay = MoveDecision.createRemainYesDecision(Decision.YES);
6161
assertTrue(stay.canRemain());
62-
assertFalse(stay.forceMove());
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);
67+
stay = MoveDecision.createRemainYesDecision(Decision.YES);
6868
assertTrue(stay.canRemain());
69-
assertFalse(stay.forceMove());
69+
assertFalse(stay.cannotRemainAndCanMove());
7070
assertTrue(stay.isDecisionTaken());
7171
assertNull(stay.getNodeDecisions());
7272
assertEquals(AllocationDecision.NO_ATTEMPT, stay.getAllocationDecision());
@@ -116,7 +116,7 @@ public void testSerialization() throws IOException {
116116
MoveDecision readDecision = new MoveDecision(output.bytes().streamInput());
117117
assertEquals(moveDecision.canRemain(), readDecision.canRemain());
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());

0 commit comments

Comments
 (0)