|
| 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.searchablesnapshots.allocation; |
| 9 | + |
| 10 | +import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; |
| 11 | +import org.elasticsearch.client.internal.Requests; |
| 12 | +import org.elasticsearch.cluster.ClusterState; |
| 13 | +import org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata; |
| 14 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.common.util.CollectionUtils; |
| 17 | +import org.elasticsearch.plugins.Plugin; |
| 18 | +import org.elasticsearch.snapshots.SnapshotId; |
| 19 | +import org.elasticsearch.test.ESIntegTestCase; |
| 20 | +import org.elasticsearch.test.InternalTestCluster; |
| 21 | +import org.elasticsearch.xpack.searchablesnapshots.BaseSearchableSnapshotsIntegTestCase; |
| 22 | +import org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots; |
| 23 | +import org.elasticsearch.xpack.searchablesnapshots.cache.full.CacheService; |
| 24 | +import org.elasticsearch.xpack.shutdown.DeleteShutdownNodeAction; |
| 25 | +import org.elasticsearch.xpack.shutdown.PutShutdownNodeAction; |
| 26 | +import org.elasticsearch.xpack.shutdown.ShutdownPlugin; |
| 27 | +import org.hamcrest.Matchers; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Locale; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
| 35 | +import java.util.concurrent.ExecutionException; |
| 36 | +import java.util.stream.Collectors; |
| 37 | +import java.util.stream.StreamSupport; |
| 38 | + |
| 39 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 40 | + |
| 41 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, minNumDataNodes = 2) |
| 42 | +public class SearchableSnapshotShutdownIntegTests extends BaseSearchableSnapshotsIntegTestCase { |
| 43 | + |
| 44 | + @Override |
| 45 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 46 | + return CollectionUtils.appendToCopy(super.nodePlugins(), ShutdownPlugin.class); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected int numberOfShards() { |
| 51 | + // use 1 shard per index and instead use multiple indices to have multiple shards. |
| 52 | + return 1; |
| 53 | + } |
| 54 | + |
| 55 | + public void testAllocationDisabledDuringShutdown() throws Exception { |
| 56 | + final List<String> restoredIndexNames = setupMountedIndices(); |
| 57 | + final String[] restoredIndexNamesArray = restoredIndexNames.toArray(String[]::new); |
| 58 | + final Set<String> indexNodes = restoredIndexNames.stream() |
| 59 | + .flatMap(index -> internalCluster().nodesInclude(index).stream()) |
| 60 | + .collect(Collectors.toSet()); |
| 61 | + final ClusterState state = client().admin().cluster().prepareState().clear().setRoutingTable(true).setNodes(true).get().getState(); |
| 62 | + final Map<String, String> nodeNameToId = state.getNodes() |
| 63 | + .stream() |
| 64 | + .collect(Collectors.toMap(DiscoveryNode::getName, DiscoveryNode::getId)); |
| 65 | + |
| 66 | + for (String indexNode : indexNodes) { |
| 67 | + final String indexNodeId = nodeNameToId.get(indexNode); |
| 68 | + putShutdown(indexNodeId); |
| 69 | + final int shards = (int) StreamSupport.stream(state.routingTable().allShards(restoredIndexNamesArray).spliterator(), false) |
| 70 | + .filter(s -> indexNodeId.equals(s.currentNodeId())) |
| 71 | + .count(); |
| 72 | + assert shards > 0; |
| 73 | + |
| 74 | + assertExecutorIsIdle(SearchableSnapshots.CACHE_FETCH_ASYNC_THREAD_POOL_NAME); |
| 75 | + assertExecutorIsIdle(SearchableSnapshots.CACHE_PREWARMING_THREAD_POOL_NAME); |
| 76 | + waitForBlobCacheFillsToComplete(); |
| 77 | + final CacheService cacheService = internalCluster().getInstance(CacheService.class, indexNode); |
| 78 | + cacheService.synchronizeCache(); |
| 79 | + |
| 80 | + logger.info("--> Restarting [{}/{}]", indexNodeId, indexNode); |
| 81 | + internalCluster().restartNode(indexNode, new InternalTestCluster.RestartCallback() { |
| 82 | + @Override |
| 83 | + public Settings onNodeStopped(String nodeName) throws Exception { |
| 84 | + assertBusy(() -> { |
| 85 | + ClusterHealthResponse response = client().admin() |
| 86 | + .cluster() |
| 87 | + .health(Requests.clusterHealthRequest(restoredIndexNamesArray)) |
| 88 | + .actionGet(); |
| 89 | + assertThat(response.getUnassignedShards(), Matchers.equalTo(shards)); |
| 90 | + }); |
| 91 | + return super.onNodeStopped(nodeName); |
| 92 | + } |
| 93 | + }); |
| 94 | + // leave shutdown in place for some nodes to check that shards get assigned anyway. |
| 95 | + if (randomBoolean()) { |
| 96 | + removeShutdown(indexNodeId); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + ensureGreen(restoredIndexNamesArray); |
| 101 | + } |
| 102 | + |
| 103 | + private List<String> setupMountedIndices() throws Exception { |
| 104 | + int count = between(1, 10); |
| 105 | + List<String> restoredIndices = new ArrayList<>(); |
| 106 | + final String repositoryName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); |
| 107 | + createRepository(repositoryName, "mock"); |
| 108 | + |
| 109 | + for (int i = 0; i < count; ++i) { |
| 110 | + final String indexName = "index_" + i; |
| 111 | + createAndPopulateIndex(indexName, Settings.builder()); |
| 112 | + |
| 113 | + final SnapshotId snapshotId = createSnapshot(repositoryName, "snapshot-" + i, List.of(indexName)).snapshotId(); |
| 114 | + assertAcked(client().admin().indices().prepareDelete(indexName)); |
| 115 | + restoredIndices.add(mountSnapshot(repositoryName, snapshotId.getName(), indexName, Settings.EMPTY)); |
| 116 | + } |
| 117 | + return restoredIndices; |
| 118 | + } |
| 119 | + |
| 120 | + private void putShutdown(String nodeToRestartId) throws InterruptedException, ExecutionException { |
| 121 | + PutShutdownNodeAction.Request putShutdownRequest = new PutShutdownNodeAction.Request( |
| 122 | + nodeToRestartId, |
| 123 | + SingleNodeShutdownMetadata.Type.RESTART, |
| 124 | + this.getTestName(), |
| 125 | + null, |
| 126 | + null |
| 127 | + ); |
| 128 | + assertTrue(client().execute(PutShutdownNodeAction.INSTANCE, putShutdownRequest).get().isAcknowledged()); |
| 129 | + } |
| 130 | + |
| 131 | + private void removeShutdown(String node) throws ExecutionException, InterruptedException { |
| 132 | + assertTrue(client().execute(DeleteShutdownNodeAction.INSTANCE, new DeleteShutdownNodeAction.Request(node)).get().isAcknowledged()); |
| 133 | + } |
| 134 | +} |
0 commit comments