|
| 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; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.shutdown; |
| 9 | + |
| 10 | +import org.elasticsearch.Build; |
| 11 | +import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; |
| 12 | +import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; |
| 13 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 14 | +import org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata; |
| 15 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 16 | +import org.elasticsearch.common.settings.Settings; |
| 17 | +import org.elasticsearch.plugins.Plugin; |
| 18 | +import org.elasticsearch.test.ESIntegTestCase; |
| 19 | +import org.elasticsearch.test.InternalTestCluster; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collection; |
| 23 | + |
| 24 | +import static org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata.Status.COMPLETE; |
| 25 | +import static org.hamcrest.Matchers.equalTo; |
| 26 | + |
| 27 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0, transportClientRatio = 0) |
| 28 | +public class NodeShutdownShardsIT extends ESIntegTestCase { |
| 29 | + |
| 30 | + @Override |
| 31 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 32 | + return Arrays.asList(ShutdownPlugin.class); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Verifies that a node that's removed from the cluster with zero shards stays in the `COMPLETE` status after it leaves, rather than |
| 37 | + * reverting to `NOT_STARTED` (this was a bug in the initial implementation). |
| 38 | + */ |
| 39 | + public void testShardStatusStaysCompleteAfterNodeLeaves() throws Exception { |
| 40 | + assumeTrue("must be on a snapshot build of ES to run in order for the feature flag to be set", Build.CURRENT.isSnapshot()); |
| 41 | + final String nodeToRestartName = internalCluster().startNode(); |
| 42 | + final String nodeToRestartId = getNodeId(nodeToRestartName); |
| 43 | + internalCluster().startNode(); |
| 44 | + |
| 45 | + // Mark the node for shutdown |
| 46 | + PutShutdownNodeAction.Request putShutdownRequest = new PutShutdownNodeAction.Request( |
| 47 | + nodeToRestartId, |
| 48 | + SingleNodeShutdownMetadata.Type.REMOVE, |
| 49 | + this.getTestName(), |
| 50 | + null |
| 51 | + ); |
| 52 | + AcknowledgedResponse putShutdownResponse = client().execute(PutShutdownNodeAction.INSTANCE, putShutdownRequest).get(); |
| 53 | + assertTrue(putShutdownResponse.isAcknowledged()); |
| 54 | + |
| 55 | + internalCluster().stopNode(nodeToRestartName); |
| 56 | + |
| 57 | + NodesInfoResponse nodes = client().admin().cluster().prepareNodesInfo().clear().get(); |
| 58 | + assertThat(nodes.getNodes().size(), equalTo(1)); |
| 59 | + |
| 60 | + GetShutdownStatusAction.Response getResp = client().execute( |
| 61 | + GetShutdownStatusAction.INSTANCE, |
| 62 | + new GetShutdownStatusAction.Request(nodeToRestartId) |
| 63 | + ).get(); |
| 64 | + |
| 65 | + assertThat(getResp.getShutdownStatuses().get(0).migrationStatus().getStatus(), equalTo(COMPLETE)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Similar to the previous test, but ensures that the status stays at `COMPLETE` when the node is offline when the shutdown is |
| 70 | + * registered. This may happen if {@link NodeSeenService} isn't working as expected. |
| 71 | + */ |
| 72 | + public void testShardStatusStaysCompleteAfterNodeLeavesIfRegisteredWhileNodeOffline() throws Exception { |
| 73 | + assumeTrue("must be on a snapshot build of ES to run in order for the feature flag to be set", Build.CURRENT.isSnapshot()); |
| 74 | + final String nodeToRestartName = internalCluster().startNode(); |
| 75 | + final String nodeToRestartId = getNodeId(nodeToRestartName); |
| 76 | + internalCluster().startNode(); |
| 77 | + |
| 78 | + // Stop the node we're going to shut down and mark it as shutting down while it's offline. This checks that the cluster state |
| 79 | + // listener is working correctly. |
| 80 | + internalCluster().restartNode(nodeToRestartName, new InternalTestCluster.RestartCallback() { |
| 81 | + @Override |
| 82 | + public Settings onNodeStopped(String nodeName) throws Exception { |
| 83 | + PutShutdownNodeAction.Request putShutdownRequest = new PutShutdownNodeAction.Request( |
| 84 | + nodeToRestartId, |
| 85 | + SingleNodeShutdownMetadata.Type.REMOVE, |
| 86 | + "testShardStatusStaysCompleteAfterNodeLeavesIfRegisteredWhileNodeOffline", |
| 87 | + null |
| 88 | + ); |
| 89 | + AcknowledgedResponse putShutdownResponse = client().execute(PutShutdownNodeAction.INSTANCE, putShutdownRequest).get(); |
| 90 | + assertTrue(putShutdownResponse.isAcknowledged()); |
| 91 | + |
| 92 | + return super.onNodeStopped(nodeName); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + internalCluster().stopNode(nodeToRestartName); |
| 97 | + |
| 98 | + NodesInfoResponse nodes = client().admin().cluster().prepareNodesInfo().clear().get(); |
| 99 | + assertThat(nodes.getNodes().size(), equalTo(1)); |
| 100 | + |
| 101 | + GetShutdownStatusAction.Response getResp = client().execute( |
| 102 | + GetShutdownStatusAction.INSTANCE, |
| 103 | + new GetShutdownStatusAction.Request(nodeToRestartId) |
| 104 | + ).get(); |
| 105 | + |
| 106 | + assertThat(getResp.getShutdownStatuses().get(0).migrationStatus().getStatus(), equalTo(COMPLETE)); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Checks that non-data nodes that are registered for shutdown have a shard migration status of `COMPLETE` rather than `NOT_STARTED`. |
| 111 | + * (this was a bug in the initial implementation). |
| 112 | + */ |
| 113 | + public void testShardStatusIsCompleteOnNonDataNodes() throws Exception { |
| 114 | + assumeTrue("must be on a snapshot build of ES to run in order for the feature flag to be set", Build.CURRENT.isSnapshot()); |
| 115 | + final String nodeToShutDownName = internalCluster().startMasterOnlyNode(); |
| 116 | + internalCluster().startMasterOnlyNode(); // Just to have at least one other node |
| 117 | + final String nodeToRestartId = getNodeId(nodeToShutDownName); |
| 118 | + |
| 119 | + // Mark the node for shutdown |
| 120 | + PutShutdownNodeAction.Request putShutdownRequest = new PutShutdownNodeAction.Request( |
| 121 | + nodeToRestartId, |
| 122 | + SingleNodeShutdownMetadata.Type.REMOVE, |
| 123 | + this.getTestName(), |
| 124 | + null |
| 125 | + ); |
| 126 | + AcknowledgedResponse putShutdownResponse = client().execute(PutShutdownNodeAction.INSTANCE, putShutdownRequest).get(); |
| 127 | + assertTrue(putShutdownResponse.isAcknowledged()); |
| 128 | + |
| 129 | + GetShutdownStatusAction.Response getResp = client().execute( |
| 130 | + GetShutdownStatusAction.INSTANCE, |
| 131 | + new GetShutdownStatusAction.Request(nodeToRestartId) |
| 132 | + ).get(); |
| 133 | + |
| 134 | + assertThat(getResp.getShutdownStatuses().get(0).migrationStatus().getStatus(), equalTo(COMPLETE)); |
| 135 | + } |
| 136 | + |
| 137 | + private String getNodeId(String nodeName) throws Exception { |
| 138 | + NodesInfoResponse nodes = client().admin().cluster().prepareNodesInfo().clear().get(); |
| 139 | + return nodes.getNodes() |
| 140 | + .stream() |
| 141 | + .map(NodeInfo::getNode) |
| 142 | + .filter(node -> node.getName().equals(nodeName)) |
| 143 | + .map(DiscoveryNode::getId) |
| 144 | + .findFirst() |
| 145 | + .orElseThrow(() -> new AssertionError("requested node name [" + nodeName + "] not found")); |
| 146 | + } |
| 147 | +} |
0 commit comments