|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.cluster.routing.allocation.decider; |
| 10 | + |
| 11 | +import org.elasticsearch.Version; |
| 12 | +import org.elasticsearch.cluster.ClusterState; |
| 13 | +import org.elasticsearch.cluster.ESAllocationTestCase; |
| 14 | +import org.elasticsearch.cluster.EmptyClusterInfoService; |
| 15 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 16 | +import org.elasticsearch.cluster.metadata.Metadata; |
| 17 | +import org.elasticsearch.cluster.metadata.NodesShutdownMetadata; |
| 18 | +import org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata; |
| 19 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 20 | +import org.elasticsearch.cluster.node.DiscoveryNodeRole; |
| 21 | +import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 22 | +import org.elasticsearch.cluster.routing.RecoverySource; |
| 23 | +import org.elasticsearch.cluster.routing.RoutingNode; |
| 24 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 25 | +import org.elasticsearch.cluster.routing.UnassignedInfo; |
| 26 | +import org.elasticsearch.cluster.routing.allocation.AllocationService; |
| 27 | +import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; |
| 28 | +import org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator; |
| 29 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 30 | +import org.elasticsearch.common.settings.Settings; |
| 31 | +import org.elasticsearch.index.shard.ShardId; |
| 32 | +import org.elasticsearch.snapshots.EmptySnapshotsInfoService; |
| 33 | +import org.elasticsearch.test.gateway.TestGatewayAllocator; |
| 34 | + |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import static org.hamcrest.Matchers.equalTo; |
| 41 | + |
| 42 | +public class NodeShutdownAllocationDeciderTests extends ESAllocationTestCase { |
| 43 | + private static final DiscoveryNode DATA_NODE = newNode("node-data", Collections.singleton(DiscoveryNodeRole.DATA_ROLE)); |
| 44 | + private final ShardRouting shard = ShardRouting.newUnassigned( |
| 45 | + new ShardId("myindex", "myindex", 0), |
| 46 | + true, |
| 47 | + RecoverySource.EmptyStoreRecoverySource.INSTANCE, |
| 48 | + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "index created") |
| 49 | + ); |
| 50 | + private final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); |
| 51 | + private NodeShutdownAllocationDecider decider = new NodeShutdownAllocationDecider(); |
| 52 | + private final AllocationDeciders allocationDeciders = new AllocationDeciders( |
| 53 | + Arrays.asList( |
| 54 | + decider, |
| 55 | + new SameShardAllocationDecider(Settings.EMPTY, clusterSettings), |
| 56 | + new ReplicaAfterPrimaryActiveAllocationDecider() |
| 57 | + ) |
| 58 | + ); |
| 59 | + private final AllocationService service = new AllocationService( |
| 60 | + allocationDeciders, |
| 61 | + new TestGatewayAllocator(), |
| 62 | + new BalancedShardsAllocator(Settings.EMPTY), |
| 63 | + EmptyClusterInfoService.INSTANCE, |
| 64 | + EmptySnapshotsInfoService.INSTANCE |
| 65 | + ); |
| 66 | + |
| 67 | + private final String idxName = "test-idx"; |
| 68 | + private final String idxUuid = "test-idx-uuid"; |
| 69 | + private final IndexMetadata indexMetadata = IndexMetadata.builder(idxName) |
| 70 | + .settings( |
| 71 | + Settings.builder() |
| 72 | + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) |
| 73 | + .put(IndexMetadata.SETTING_INDEX_UUID, idxUuid) |
| 74 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 75 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 76 | + .build() |
| 77 | + ) |
| 78 | + .build(); |
| 79 | + |
| 80 | + public void testCanAllocateShardsToRestartingNode() { |
| 81 | + ClusterState state = prepareState( |
| 82 | + service.reroute(ClusterState.EMPTY_STATE, "initial state"), |
| 83 | + SingleNodeShutdownMetadata.Type.RESTART |
| 84 | + ); |
| 85 | + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, null, 0); |
| 86 | + RoutingNode routingNode = new RoutingNode(DATA_NODE.getId(), DATA_NODE, shard); |
| 87 | + allocation.debugDecision(true); |
| 88 | + |
| 89 | + Decision decision = decider.canAllocate(shard, routingNode, allocation); |
| 90 | + assertThat(decision.type(), equalTo(Decision.Type.YES)); |
| 91 | + assertThat( |
| 92 | + decision.getExplanation(), |
| 93 | + equalTo("node [" + DATA_NODE.getId() + "] is preparing to restart, but will remain in the cluster") |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + public void testCannotAllocateShardsToRemovingNode() { |
| 99 | + ClusterState state = prepareState( |
| 100 | + service.reroute(ClusterState.EMPTY_STATE, "initial state"), |
| 101 | + SingleNodeShutdownMetadata.Type.REMOVE |
| 102 | + ); |
| 103 | + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, null, 0); |
| 104 | + RoutingNode routingNode = new RoutingNode(DATA_NODE.getId(), DATA_NODE, shard); |
| 105 | + allocation.debugDecision(true); |
| 106 | + |
| 107 | + Decision decision = decider.canAllocate(shard, routingNode, allocation); |
| 108 | + assertThat(decision.type(), equalTo(Decision.Type.NO)); |
| 109 | + assertThat( |
| 110 | + decision.getExplanation(), |
| 111 | + equalTo("node [" + DATA_NODE.getId() + "] is preparing to be removed from the cluster") |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + public void testShardsCanRemainOnRestartingNode() { |
| 116 | + ClusterState state = prepareState( |
| 117 | + service.reroute(ClusterState.EMPTY_STATE, "initial state"), |
| 118 | + SingleNodeShutdownMetadata.Type.RESTART |
| 119 | + ); |
| 120 | + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, null, 0); |
| 121 | + RoutingNode routingNode = new RoutingNode(DATA_NODE.getId(), DATA_NODE, shard); |
| 122 | + allocation.debugDecision(true); |
| 123 | + |
| 124 | + Decision decision = decider.canRemain(shard, routingNode, allocation); |
| 125 | + assertThat(decision.type(), equalTo(Decision.Type.YES)); |
| 126 | + assertThat( |
| 127 | + decision.getExplanation(), |
| 128 | + equalTo("node [" + DATA_NODE.getId() + "] is preparing to restart, but will remain in the cluster") |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + public void testShardsCannotRemainOnRemovingNode() { |
| 133 | + ClusterState state = prepareState( |
| 134 | + service.reroute(ClusterState.EMPTY_STATE, "initial state"), |
| 135 | + SingleNodeShutdownMetadata.Type.REMOVE |
| 136 | + ); |
| 137 | + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, null, 0); |
| 138 | + RoutingNode routingNode = new RoutingNode(DATA_NODE.getId(), DATA_NODE, shard); |
| 139 | + allocation.debugDecision(true); |
| 140 | + |
| 141 | + Decision decision = decider.canRemain(shard, routingNode, allocation); |
| 142 | + assertThat(decision.type(), equalTo(Decision.Type.NO)); |
| 143 | + assertThat( |
| 144 | + decision.getExplanation(), |
| 145 | + equalTo("node [" + DATA_NODE.getId() + "] is preparing to be removed from the cluster") |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + public void testCannotAutoExpandToRestartingNode() { |
| 150 | + ClusterState state = prepareState( |
| 151 | + service.reroute(ClusterState.EMPTY_STATE, "initial state"), |
| 152 | + SingleNodeShutdownMetadata.Type.RESTART |
| 153 | + ); |
| 154 | + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, null, 0); |
| 155 | + allocation.debugDecision(true); |
| 156 | + |
| 157 | + Decision decision = decider.shouldAutoExpandToNode(indexMetadata, DATA_NODE, allocation); |
| 158 | + assertThat(decision.type(), equalTo(Decision.Type.NO)); |
| 159 | + assertThat( |
| 160 | + decision.getExplanation(), |
| 161 | + equalTo("node [" + DATA_NODE.getId() + "] is preparing to restart, auto-expansion waiting until it is complete") |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + public void testCannotAutoExpandToRemovingNode() { |
| 166 | + ClusterState state = prepareState( |
| 167 | + service.reroute(ClusterState.EMPTY_STATE, "initial state"), |
| 168 | + SingleNodeShutdownMetadata.Type.REMOVE |
| 169 | + ); |
| 170 | + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, null, 0); |
| 171 | + allocation.debugDecision(true); |
| 172 | + |
| 173 | + Decision decision = decider.shouldAutoExpandToNode(indexMetadata, DATA_NODE, allocation); |
| 174 | + assertThat(decision.type(), equalTo(Decision.Type.NO)); |
| 175 | + assertThat(decision.getExplanation(), equalTo("node [" + DATA_NODE.getId() + "] is preparing for removal from the cluster")); |
| 176 | + } |
| 177 | + |
| 178 | + private ClusterState prepareState(ClusterState initialState, SingleNodeShutdownMetadata.Type shutdownType) { |
| 179 | + Map<String, SingleNodeShutdownMetadata> nodesShutdownInfo = new HashMap<>(); |
| 180 | + |
| 181 | + final SingleNodeShutdownMetadata nodeShutdownMetadata = SingleNodeShutdownMetadata.builder() |
| 182 | + .setNodeId(DATA_NODE.getId()) |
| 183 | + .setType(shutdownType) |
| 184 | + .setReason(this.getTestName()) |
| 185 | + .setStartedAtMillis(1L) |
| 186 | + .build(); |
| 187 | + NodesShutdownMetadata nodesShutdownMetadata = new NodesShutdownMetadata(new HashMap<>()).putSingleNodeMetadata( |
| 188 | + nodeShutdownMetadata |
| 189 | + ); |
| 190 | + return ClusterState.builder(initialState) |
| 191 | + .nodes(DiscoveryNodes.builder().add(DATA_NODE).build()) |
| 192 | + .metadata( |
| 193 | + Metadata.builder().put(IndexMetadata.builder(indexMetadata)).putCustom(NodesShutdownMetadata.TYPE, nodesShutdownMetadata) |
| 194 | + ) |
| 195 | + .build(); |
| 196 | + } |
| 197 | +} |
0 commit comments