|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | +package org.elasticsearch.snapshots; |
| 10 | + |
| 11 | +import org.elasticsearch.cluster.SnapshotsInProgress; |
| 12 | +import org.elasticsearch.cluster.service.ClusterService; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.core.CheckedRunnable; |
| 15 | +import org.elasticsearch.index.shard.ShardId; |
| 16 | +import org.elasticsearch.indices.IndicesService; |
| 17 | +import org.elasticsearch.plugins.Plugin; |
| 18 | +import org.elasticsearch.snapshots.mockstore.MockRepository; |
| 19 | +import org.elasticsearch.test.ESIntegTestCase; |
| 20 | +import org.elasticsearch.test.transport.MockTransportService; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | +import java.util.stream.IntStream; |
| 29 | +import java.util.stream.StreamSupport; |
| 30 | + |
| 31 | +import static org.hamcrest.Matchers.equalTo; |
| 32 | +import static org.hamcrest.Matchers.is; |
| 33 | + |
| 34 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 35 | +public class SnapshotAndRelocationIT extends AbstractSnapshotIntegTestCase { |
| 36 | + |
| 37 | + @Override |
| 38 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 39 | + return Arrays.asList(MockTransportService.TestPlugin.class, MockRepository.Plugin.class); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { |
| 44 | + return Settings.builder() |
| 45 | + .put(super.nodeSettings(nodeOrdinal, otherSettings)) |
| 46 | + .put(SnapshotsService.SHARD_SNAPSHOT_PER_NODE_LIMIT_SETTING.getKey(), 1) |
| 47 | + .build(); |
| 48 | + } |
| 49 | + |
| 50 | + public void testLimitingInitAndRelocationForAssignedQueueShards() throws Exception { |
| 51 | + final String masterNode = internalCluster().startMasterOnlyNode(); |
| 52 | + final String dataNodeA = internalCluster().startDataOnlyNode(); |
| 53 | + final String dataNodeB = internalCluster().startDataOnlyNode(); |
| 54 | + final String repoName = "test-repo"; |
| 55 | + createRepository(repoName, "mock"); |
| 56 | + |
| 57 | + final AtomicBoolean delayOnce = new AtomicBoolean(false); |
| 58 | + final AtomicReference<CheckedRunnable<Exception>> delayedAction = new AtomicReference<>(); |
| 59 | + final var delayedActionSetLatch = new CountDownLatch(1); |
| 60 | + MockTransportService.getInstance(masterNode) |
| 61 | + .addRequestHandlingBehavior(SnapshotsService.UPDATE_SNAPSHOT_STATUS_ACTION_NAME, (handler, request, channel, task) -> { |
| 62 | + if (delayOnce.compareAndSet(false, true)) { |
| 63 | + delayedAction.set(() -> handler.messageReceived(request, channel, task)); |
| 64 | + delayedActionSetLatch.countDown(); |
| 65 | + } else { |
| 66 | + handler.messageReceived(request, channel, task); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + final var numIndices = between(2, 4); |
| 71 | + final var indexNames = IntStream.range(0, numIndices).mapToObj(i -> "index-" + i).toList(); |
| 72 | + |
| 73 | + for (var indexName : indexNames) { |
| 74 | + createIndex(indexName, indexSettings(1, 0).put("index.routing.allocation.include._name", dataNodeA).build()); |
| 75 | + indexRandomDocs(indexName, between(10, 42)); |
| 76 | + } |
| 77 | + ensureGreen(); |
| 78 | + |
| 79 | + final var future = startFullSnapshot(repoName, "snapshot"); |
| 80 | + safeAwait(delayedActionSetLatch); |
| 81 | + |
| 82 | + final var clusterService = internalCluster().getCurrentMasterNodeInstance(ClusterService.class); |
| 83 | + final SnapshotsInProgress.Entry snapshot = SnapshotsInProgress.get(clusterService.state()).asStream().iterator().next(); |
| 84 | + logger.info("--> snapshot=[{}]", snapshot); |
| 85 | + final var shards = snapshot.shards(); |
| 86 | + assertThat(shards.size(), equalTo(numIndices)); |
| 87 | + |
| 88 | + final var dataNodeAId = getNodeId(dataNodeA); |
| 89 | + final var initShards = shards.entrySet() |
| 90 | + .stream() |
| 91 | + .filter(entry -> entry.getValue().state() == SnapshotsInProgress.ShardState.INIT) |
| 92 | + .peek(entry -> assertThat(entry.getValue().nodeId(), equalTo(dataNodeAId))) |
| 93 | + .map(Map.Entry::getKey) |
| 94 | + .toList(); |
| 95 | + logger.info("--> init shards [{}]", initShards); |
| 96 | + assertThat(initShards.size(), equalTo(1)); |
| 97 | + |
| 98 | + final var assignedQueuedShards = shards.entrySet() |
| 99 | + .stream() |
| 100 | + .filter(entry -> entry.getValue().isAssignedQueued()) |
| 101 | + .peek(entry -> assertThat(entry.getValue().nodeId(), equalTo(dataNodeAId))) |
| 102 | + .map(Map.Entry::getKey) |
| 103 | + .toList(); |
| 104 | + logger.info("--> assigned queued shards [{}]", assignedQueuedShards); |
| 105 | + assertThat(assignedQueuedShards.size(), equalTo(numIndices - 1)); |
| 106 | + |
| 107 | + // Relocate indices that are assigned queued |
| 108 | + final String[] indices = assignedQueuedShards.stream().map(ShardId::getIndexName).toArray(String[]::new); |
| 109 | + logger.info("--> relocate indices [{}]", Arrays.toString(indices)); |
| 110 | + updateIndexSettings(Settings.builder().put("index.routing.allocation.include._name", dataNodeB), indices); |
| 111 | + ensureGreen(indices); |
| 112 | + |
| 113 | + final var dataNodeBIndicesService = internalCluster().getInstance(IndicesService.class, dataNodeB); |
| 114 | + for (var shardId : assignedQueuedShards) { |
| 115 | + assertTrue( |
| 116 | + "indices: " |
| 117 | + + StreamSupport.stream(dataNodeBIndicesService.spliterator(), false) |
| 118 | + .map(indexService -> indexService.index().getName()) |
| 119 | + .toList(), |
| 120 | + dataNodeBIndicesService.hasIndex(shardId.getIndex()) |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + assertThat(future.isDone(), is(false)); |
| 125 | + logger.info("--> run delayed action"); |
| 126 | + delayedAction.get().run(); |
| 127 | + assertSuccessful(future); |
| 128 | + } |
| 129 | +} |
0 commit comments