|
| 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 | +package org.elasticsearch.repositories.blobstore; |
| 20 | + |
| 21 | +import org.elasticsearch.action.ActionRunnable; |
| 22 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 23 | +import org.elasticsearch.cluster.RepositoryCleanupInProgress; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.common.unit.ByteSizeUnit; |
| 26 | +import org.elasticsearch.common.unit.TimeValue; |
| 27 | +import org.elasticsearch.repositories.RepositoriesService; |
| 28 | +import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; |
| 29 | +import org.elasticsearch.test.ESIntegTestCase; |
| 30 | + |
| 31 | +import java.io.ByteArrayInputStream; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | + |
| 34 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 35 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; |
| 36 | + |
| 37 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 38 | +public class BlobStoreRepositoryCleanupIT extends AbstractSnapshotIntegTestCase { |
| 39 | + |
| 40 | + public void testMasterFailoverDuringCleanup() throws Exception { |
| 41 | + startBlockedCleanup("test-repo"); |
| 42 | + |
| 43 | + logger.info("--> stopping master node"); |
| 44 | + internalCluster().stopCurrentMasterNode(); |
| 45 | + |
| 46 | + logger.info("--> wait for cleanup to finish and disappear from cluster state"); |
| 47 | + assertBusy(() -> { |
| 48 | + RepositoryCleanupInProgress cleanupInProgress = |
| 49 | + client().admin().cluster().prepareState().get().getState().custom(RepositoryCleanupInProgress.TYPE); |
| 50 | + assertFalse(cleanupInProgress.hasCleanupInProgress()); |
| 51 | + }, 30, TimeUnit.SECONDS); |
| 52 | + } |
| 53 | + |
| 54 | + public void testRepeatCleanupsDontRemove() throws Exception { |
| 55 | + final String masterNode = startBlockedCleanup("test-repo"); |
| 56 | + |
| 57 | + logger.info("--> sending another cleanup"); |
| 58 | + assertThrows(client().admin().cluster().prepareCleanupRepository("test-repo").execute(), IllegalStateException.class); |
| 59 | + |
| 60 | + logger.info("--> ensure cleanup is still in progress"); |
| 61 | + final RepositoryCleanupInProgress cleanup = |
| 62 | + client().admin().cluster().prepareState().get().getState().custom(RepositoryCleanupInProgress.TYPE); |
| 63 | + assertTrue(cleanup.hasCleanupInProgress()); |
| 64 | + |
| 65 | + logger.info("--> unblocking master node"); |
| 66 | + unblockNode("test-repo", masterNode); |
| 67 | + |
| 68 | + logger.info("--> wait for cleanup to finish and disappear from cluster state"); |
| 69 | + assertBusy(() -> { |
| 70 | + RepositoryCleanupInProgress cleanupInProgress = |
| 71 | + client().admin().cluster().prepareState().get().getState().custom(RepositoryCleanupInProgress.TYPE); |
| 72 | + assertFalse(cleanupInProgress.hasCleanupInProgress()); |
| 73 | + }, 30, TimeUnit.SECONDS); |
| 74 | + } |
| 75 | + |
| 76 | + private String startBlockedCleanup(String repoName) throws Exception { |
| 77 | + logger.info("--> starting two master nodes and one data node"); |
| 78 | + internalCluster().startMasterOnlyNodes(2); |
| 79 | + internalCluster().startDataOnlyNodes(1); |
| 80 | + |
| 81 | + logger.info("--> creating repository"); |
| 82 | + assertAcked(client().admin().cluster().preparePutRepository(repoName) |
| 83 | + .setType("mock").setSettings(Settings.builder() |
| 84 | + .put("location", randomRepoPath()) |
| 85 | + .put("compress", randomBoolean()) |
| 86 | + .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES))); |
| 87 | + |
| 88 | + logger.info("--> snapshot"); |
| 89 | + client().admin().cluster().prepareCreateSnapshot(repoName, "test-snap") |
| 90 | + .setWaitForCompletion(true).get(); |
| 91 | + |
| 92 | + final RepositoriesService service = internalCluster().getInstance(RepositoriesService.class, internalCluster().getMasterName()); |
| 93 | + final BlobStoreRepository repository = (BlobStoreRepository) service.repository(repoName); |
| 94 | + |
| 95 | + logger.info("--> creating a garbage data blob"); |
| 96 | + final PlainActionFuture<Void> garbageFuture = PlainActionFuture.newFuture(); |
| 97 | + repository.threadPool().generic().execute(ActionRunnable.run(garbageFuture, () -> repository.blobStore() |
| 98 | + .blobContainer(repository.basePath()).writeBlob("snap-foo.dat", new ByteArrayInputStream(new byte[1]), 1, true))); |
| 99 | + garbageFuture.get(); |
| 100 | + |
| 101 | + final String masterNode = blockMasterFromFinalizingSnapshotOnIndexFile(repoName); |
| 102 | + |
| 103 | + logger.info("--> starting repository cleanup"); |
| 104 | + client().admin().cluster().prepareCleanupRepository(repoName).execute(); |
| 105 | + |
| 106 | + logger.info("--> waiting for block to kick in on " + masterNode); |
| 107 | + waitForBlock(masterNode, repoName, TimeValue.timeValueSeconds(60)); |
| 108 | + return masterNode; |
| 109 | + } |
| 110 | +} |
0 commit comments