|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.snapshots; |
| 21 | + |
| 22 | +import org.elasticsearch.action.ActionFuture; |
| 23 | +import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; |
| 24 | +import org.elasticsearch.action.admin.indices.recovery.RecoveryResponse; |
| 25 | +import org.elasticsearch.action.support.IndicesOptions; |
| 26 | +import org.elasticsearch.cluster.routing.RecoverySource; |
| 27 | +import org.elasticsearch.cluster.service.ClusterService; |
| 28 | +import org.elasticsearch.indices.recovery.RecoveryState; |
| 29 | +import org.elasticsearch.test.ESIntegTestCase; |
| 30 | +import org.elasticsearch.threadpool.ThreadPool; |
| 31 | +import org.elasticsearch.threadpool.ThreadPoolStats; |
| 32 | +import org.hamcrest.Matcher; |
| 33 | + |
| 34 | +import java.util.List; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | +import java.util.stream.StreamSupport; |
| 37 | + |
| 38 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 39 | +import static org.hamcrest.Matchers.equalTo; |
| 40 | +import static org.hamcrest.Matchers.greaterThan; |
| 41 | +import static org.hamcrest.Matchers.hasSize; |
| 42 | +import static org.hamcrest.Matchers.is; |
| 43 | + |
| 44 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 45 | +public class AbortedRestoreIT extends AbstractSnapshotIntegTestCase { |
| 46 | + |
| 47 | + public void testAbortedRestoreAlsoAbortFileRestores() throws Exception { |
| 48 | + internalCluster().startMasterOnlyNode(); |
| 49 | + final String dataNode = internalCluster().startDataOnlyNode(); |
| 50 | + |
| 51 | + final String indexName = "test-abort-restore"; |
| 52 | + createIndex(indexName, indexSettingsNoReplicas(1).build()); |
| 53 | + indexRandomDocs(indexName, scaledRandomIntBetween(10, 1_000)); |
| 54 | + ensureGreen(); |
| 55 | + forceMerge(); |
| 56 | + |
| 57 | + final String repositoryName = "repository"; |
| 58 | + createRepository(repositoryName, "mock"); |
| 59 | + |
| 60 | + final String snapshotName = "snapshot"; |
| 61 | + createFullSnapshot(repositoryName, snapshotName); |
| 62 | + assertAcked(client().admin().indices().prepareDelete(indexName)); |
| 63 | + |
| 64 | + logger.info("--> blocking all data nodes for repository [{}]", repositoryName); |
| 65 | + blockAllDataNodes(repositoryName); |
| 66 | + failReadsAllDataNodes(repositoryName); |
| 67 | + |
| 68 | + logger.info("--> starting restore"); |
| 69 | + final ActionFuture<RestoreSnapshotResponse> future = client().admin().cluster().prepareRestoreSnapshot(repositoryName, snapshotName) |
| 70 | + .setWaitForCompletion(true) |
| 71 | + .setIndices(indexName) |
| 72 | + .execute(); |
| 73 | + |
| 74 | + assertBusy(() -> { |
| 75 | + final RecoveryResponse recoveries = client().admin().indices().prepareRecoveries(indexName) |
| 76 | + .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN).setActiveOnly(true).get(); |
| 77 | + assertThat(recoveries.hasRecoveries(), is(true)); |
| 78 | + final List<RecoveryState> shardRecoveries = recoveries.shardRecoveryStates().get(indexName); |
| 79 | + assertThat(shardRecoveries, hasSize(1)); |
| 80 | + assertThat(future.isDone(), is(false)); |
| 81 | + |
| 82 | + for (RecoveryState shardRecovery : shardRecoveries) { |
| 83 | + assertThat(shardRecovery.getRecoverySource().getType(), equalTo(RecoverySource.Type.SNAPSHOT)); |
| 84 | + assertThat(shardRecovery.getStage(), equalTo(RecoveryState.Stage.INDEX)); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + final ThreadPool.Info snapshotThreadPoolInfo = threadPool(dataNode).info(ThreadPool.Names.SNAPSHOT); |
| 89 | + assertThat(snapshotThreadPoolInfo.getMax(), greaterThan(0)); |
| 90 | + |
| 91 | + logger.info("--> waiting for snapshot thread [max={}] pool to be full", snapshotThreadPoolInfo.getMax()); |
| 92 | + waitForMaxActiveSnapshotThreads(dataNode, equalTo(snapshotThreadPoolInfo.getMax())); |
| 93 | + |
| 94 | + logger.info("--> aborting restore by deleting the index"); |
| 95 | + assertAcked(client().admin().indices().prepareDelete(indexName)); |
| 96 | + |
| 97 | + logger.info("--> unblocking repository [{}]", repositoryName); |
| 98 | + unblockAllDataNodes(repositoryName); |
| 99 | + |
| 100 | + logger.info("--> restore should have failed"); |
| 101 | + final RestoreSnapshotResponse restoreSnapshotResponse = future.get(); |
| 102 | + assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(1)); |
| 103 | + assertThat(restoreSnapshotResponse.getRestoreInfo().successfulShards(), equalTo(0)); |
| 104 | + |
| 105 | + logger.info("--> waiting for snapshot thread pool to be empty"); |
| 106 | + waitForMaxActiveSnapshotThreads(dataNode, equalTo(0)); |
| 107 | + } |
| 108 | + |
| 109 | + private static void waitForMaxActiveSnapshotThreads(final String node, final Matcher<Integer> matcher) throws Exception { |
| 110 | + assertBusy(() -> assertThat(threadPoolStats(node, ThreadPool.Names.SNAPSHOT).getActive(), matcher), 30L, TimeUnit.SECONDS); |
| 111 | + } |
| 112 | + |
| 113 | + private static ThreadPool threadPool(final String node) { |
| 114 | + return internalCluster().getInstance(ClusterService.class, node).getClusterApplierService().threadPool(); |
| 115 | + } |
| 116 | + |
| 117 | + private static ThreadPoolStats.Stats threadPoolStats(final String node, final String threadPoolName) { |
| 118 | + return StreamSupport.stream(threadPool(node).stats().spliterator(), false) |
| 119 | + .filter(threadPool -> threadPool.getName().equals(threadPoolName)) |
| 120 | + .findFirst() |
| 121 | + .orElseThrow(() -> new AssertionError("Failed to find thread pool " + threadPoolName)); |
| 122 | + } |
| 123 | +} |
0 commit comments