|
| 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.snapshots; |
| 20 | + |
| 21 | +import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; |
| 22 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 23 | +import org.elasticsearch.client.Client; |
| 24 | +import org.elasticsearch.cluster.ClusterState; |
| 25 | +import org.elasticsearch.cluster.ClusterStateUpdateTask; |
| 26 | +import org.elasticsearch.cluster.metadata.MetaData; |
| 27 | +import org.elasticsearch.cluster.metadata.RepositoriesMetaData; |
| 28 | +import org.elasticsearch.cluster.service.ClusterService; |
| 29 | +import org.elasticsearch.common.settings.Settings; |
| 30 | +import org.elasticsearch.common.unit.ByteSizeUnit; |
| 31 | +import org.elasticsearch.repositories.RepositoriesService; |
| 32 | +import org.elasticsearch.repositories.Repository; |
| 33 | +import org.elasticsearch.repositories.RepositoryData; |
| 34 | +import org.elasticsearch.repositories.RepositoryException; |
| 35 | +import org.elasticsearch.repositories.blobstore.BlobStoreRepository; |
| 36 | + |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.nio.file.Path; |
| 39 | + |
| 40 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 41 | +import static org.hamcrest.Matchers.containsString; |
| 42 | +import static org.hamcrest.Matchers.equalTo; |
| 43 | +import static org.hamcrest.Matchers.greaterThan; |
| 44 | +import static org.hamcrest.Matchers.is; |
| 45 | + |
| 46 | +public class CorruptedBlobStoreRepositoryIT extends AbstractSnapshotIntegTestCase { |
| 47 | + |
| 48 | + public void testConcurrentlyChangeRepositoryContents() throws Exception { |
| 49 | + Client client = client(); |
| 50 | + |
| 51 | + Path repo = randomRepoPath(); |
| 52 | + final String repoName = "test-repo"; |
| 53 | + logger.info("--> creating repository at {}", repo.toAbsolutePath()); |
| 54 | + assertAcked(client.admin().cluster().preparePutRepository(repoName) |
| 55 | + .setType("fs").setSettings(Settings.builder() |
| 56 | + .put("location", repo) |
| 57 | + .put("compress", false) |
| 58 | + .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES))); |
| 59 | + |
| 60 | + createIndex("test-idx-1", "test-idx-2"); |
| 61 | + logger.info("--> indexing some data"); |
| 62 | + indexRandom(true, |
| 63 | + client().prepareIndex().setIndex("test-idx-1").setSource("foo", "bar"), |
| 64 | + client().prepareIndex().setIndex("test-idx-2").setSource("foo", "bar")); |
| 65 | + |
| 66 | + final String snapshot = "test-snap"; |
| 67 | + |
| 68 | + logger.info("--> creating snapshot"); |
| 69 | + CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repoName, snapshot) |
| 70 | + .setWaitForCompletion(true).setIndices("test-idx-*").get(); |
| 71 | + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0)); |
| 72 | + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), |
| 73 | + equalTo(createSnapshotResponse.getSnapshotInfo().totalShards())); |
| 74 | + |
| 75 | + logger.info("--> move index-N blob to next generation"); |
| 76 | + final RepositoryData repositoryData = |
| 77 | + getRepositoryData(internalCluster().getMasterNodeInstance(RepositoriesService.class).repository(repoName)); |
| 78 | + Files.move(repo.resolve("index-" + repositoryData.getGenId()), repo.resolve("index-" + (repositoryData.getGenId() + 1))); |
| 79 | + |
| 80 | + assertRepositoryBlocked(client, repoName, snapshot); |
| 81 | + |
| 82 | + if (randomBoolean()) { |
| 83 | + logger.info("--> move index-N blob back to initial generation"); |
| 84 | + Files.move(repo.resolve("index-" + (repositoryData.getGenId() + 1)), repo.resolve("index-" + repositoryData.getGenId())); |
| 85 | + |
| 86 | + logger.info("--> verify repository remains blocked"); |
| 87 | + assertRepositoryBlocked(client, repoName, snapshot); |
| 88 | + } |
| 89 | + |
| 90 | + logger.info("--> remove repository"); |
| 91 | + assertAcked(client.admin().cluster().prepareDeleteRepository(repoName)); |
| 92 | + |
| 93 | + logger.info("--> recreate repository"); |
| 94 | + assertAcked(client.admin().cluster().preparePutRepository(repoName) |
| 95 | + .setType("fs").setSettings(Settings.builder() |
| 96 | + .put("location", repo) |
| 97 | + .put("compress", false) |
| 98 | + .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES))); |
| 99 | + |
| 100 | + logger.info("--> delete snapshot"); |
| 101 | + client.admin().cluster().prepareDeleteSnapshot(repoName, snapshot).get(); |
| 102 | + |
| 103 | + logger.info("--> make sure snapshot doesn't exist"); |
| 104 | + expectThrows(SnapshotMissingException.class, () -> client.admin().cluster().prepareGetSnapshots(repoName) |
| 105 | + .addSnapshots(snapshot).get()); |
| 106 | + } |
| 107 | + |
| 108 | + public void testConcurrentlyChangeRepositoryContentsInBwCMode() throws Exception { |
| 109 | + Client client = client(); |
| 110 | + |
| 111 | + Path repo = randomRepoPath(); |
| 112 | + final String repoName = "test-repo"; |
| 113 | + logger.info("--> creating repository at {}", repo.toAbsolutePath()); |
| 114 | + assertAcked(client.admin().cluster().preparePutRepository(repoName) |
| 115 | + .setType("fs").setSettings(Settings.builder() |
| 116 | + .put("location", repo) |
| 117 | + .put("compress", false) |
| 118 | + .put(BlobStoreRepository.ALLOW_CONCURRENT_MODIFICATION.getKey(), true) |
| 119 | + .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES))); |
| 120 | + |
| 121 | + createIndex("test-idx-1", "test-idx-2"); |
| 122 | + logger.info("--> indexing some data"); |
| 123 | + indexRandom(true, |
| 124 | + client().prepareIndex().setIndex("test-idx-1").setSource("foo", "bar"), |
| 125 | + client().prepareIndex().setIndex("test-idx-2").setSource("foo", "bar")); |
| 126 | + |
| 127 | + final String snapshot = "test-snap"; |
| 128 | + |
| 129 | + logger.info("--> creating snapshot"); |
| 130 | + CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repoName, snapshot) |
| 131 | + .setWaitForCompletion(true).setIndices("test-idx-*").get(); |
| 132 | + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0)); |
| 133 | + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), |
| 134 | + equalTo(createSnapshotResponse.getSnapshotInfo().totalShards())); |
| 135 | + |
| 136 | + final Repository repository = internalCluster().getMasterNodeInstance(RepositoriesService.class).repository(repoName); |
| 137 | + |
| 138 | + logger.info("--> move index-N blob to next generation"); |
| 139 | + final RepositoryData repositoryData = getRepositoryData(repository); |
| 140 | + final long beforeMoveGen = repositoryData.getGenId(); |
| 141 | + Files.move(repo.resolve("index-" + beforeMoveGen), repo.resolve("index-" + (beforeMoveGen + 1))); |
| 142 | + |
| 143 | + logger.info("--> verify index-N blob is found at the new location"); |
| 144 | + assertThat(getRepositoryData(repository).getGenId(), is(beforeMoveGen + 1)); |
| 145 | + |
| 146 | + logger.info("--> delete snapshot"); |
| 147 | + client.admin().cluster().prepareDeleteSnapshot(repoName, snapshot).get(); |
| 148 | + |
| 149 | + logger.info("--> verify index-N blob is found at the expected location"); |
| 150 | + assertThat(getRepositoryData(repository).getGenId(), is(beforeMoveGen + 2)); |
| 151 | + |
| 152 | + logger.info("--> make sure snapshot doesn't exist"); |
| 153 | + expectThrows(SnapshotMissingException.class, () -> client.admin().cluster().prepareGetSnapshots(repoName) |
| 154 | + .addSnapshots(snapshot).get()); |
| 155 | + } |
| 156 | + |
| 157 | + public void testFindDanglingLatestGeneration() throws Exception { |
| 158 | + Path repo = randomRepoPath(); |
| 159 | + final String repoName = "test-repo"; |
| 160 | + logger.info("--> creating repository at {}", repo.toAbsolutePath()); |
| 161 | + assertAcked(client().admin().cluster().preparePutRepository(repoName) |
| 162 | + .setType("fs").setSettings(Settings.builder() |
| 163 | + .put("location", repo) |
| 164 | + .put("compress", false) |
| 165 | + .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES))); |
| 166 | + |
| 167 | + createIndex("test-idx-1", "test-idx-2"); |
| 168 | + logger.info("--> indexing some data"); |
| 169 | + indexRandom(true, |
| 170 | + client().prepareIndex().setIndex("test-idx-1").setSource("foo", "bar"), |
| 171 | + client().prepareIndex().setIndex("test-idx-2").setSource("foo", "bar")); |
| 172 | + |
| 173 | + final String snapshot = "test-snap"; |
| 174 | + |
| 175 | + logger.info("--> creating snapshot"); |
| 176 | + CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(repoName, snapshot) |
| 177 | + .setWaitForCompletion(true).setIndices("test-idx-*").get(); |
| 178 | + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0)); |
| 179 | + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), |
| 180 | + equalTo(createSnapshotResponse.getSnapshotInfo().totalShards())); |
| 181 | + |
| 182 | + final Repository repository = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName); |
| 183 | + |
| 184 | + logger.info("--> move index-N blob to next generation"); |
| 185 | + final RepositoryData repositoryData = getRepositoryData(repository); |
| 186 | + final long beforeMoveGen = repositoryData.getGenId(); |
| 187 | + Files.move(repo.resolve("index-" + beforeMoveGen), repo.resolve("index-" + (beforeMoveGen + 1))); |
| 188 | + |
| 189 | + logger.info("--> set next generation as pending in the cluster state"); |
| 190 | + final PlainActionFuture<Void> csUpdateFuture = PlainActionFuture.newFuture(); |
| 191 | + internalCluster().getCurrentMasterNodeInstance(ClusterService.class).submitStateUpdateTask("set pending generation", |
| 192 | + new ClusterStateUpdateTask() { |
| 193 | + @Override |
| 194 | + public ClusterState execute(ClusterState currentState) { |
| 195 | + return ClusterState.builder(currentState).metaData(MetaData.builder(currentState.getMetaData()) |
| 196 | + .putCustom(RepositoriesMetaData.TYPE, |
| 197 | + currentState.metaData().<RepositoriesMetaData>custom(RepositoriesMetaData.TYPE).withUpdatedGeneration( |
| 198 | + repository.getMetadata().name(), beforeMoveGen, beforeMoveGen + 1)).build()).build(); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void onFailure(String source, Exception e) { |
| 203 | + csUpdateFuture.onFailure(e); |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { |
| 208 | + csUpdateFuture.onResponse(null); |
| 209 | + } |
| 210 | + } |
| 211 | + ); |
| 212 | + csUpdateFuture.get(); |
| 213 | + |
| 214 | + logger.info("--> full cluster restart"); |
| 215 | + internalCluster().fullRestart(); |
| 216 | + ensureGreen(); |
| 217 | + |
| 218 | + Repository repositoryAfterRestart = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName); |
| 219 | + |
| 220 | + logger.info("--> verify index-N blob is found at the new location"); |
| 221 | + assertThat(getRepositoryData(repositoryAfterRestart).getGenId(), is(beforeMoveGen + 1)); |
| 222 | + |
| 223 | + logger.info("--> delete snapshot"); |
| 224 | + client().admin().cluster().prepareDeleteSnapshot(repoName, snapshot).get(); |
| 225 | + |
| 226 | + logger.info("--> verify index-N blob is found at the expected location"); |
| 227 | + assertThat(getRepositoryData(repositoryAfterRestart).getGenId(), is(beforeMoveGen + 2)); |
| 228 | + |
| 229 | + logger.info("--> make sure snapshot doesn't exist"); |
| 230 | + expectThrows(SnapshotMissingException.class, () -> client().admin().cluster().prepareGetSnapshots(repoName) |
| 231 | + .addSnapshots(snapshot).get()); |
| 232 | + } |
| 233 | + |
| 234 | + private void assertRepositoryBlocked(Client client, String repo, String existingSnapshot) { |
| 235 | + logger.info("--> try to delete snapshot"); |
| 236 | + final RepositoryException repositoryException3 = expectThrows(RepositoryException.class, |
| 237 | + () -> client.admin().cluster().prepareDeleteSnapshot(repo, existingSnapshot).execute().actionGet()); |
| 238 | + assertThat(repositoryException3.getMessage(), |
| 239 | + containsString("Could not read repository data because the contents of the repository do not match its expected state.")); |
| 240 | + |
| 241 | + logger.info("--> try to create snapshot"); |
| 242 | + final RepositoryException repositoryException4 = expectThrows(RepositoryException.class, |
| 243 | + () -> client.admin().cluster().prepareCreateSnapshot(repo, existingSnapshot).execute().actionGet()); |
| 244 | + assertThat(repositoryException4.getMessage(), |
| 245 | + containsString("Could not read repository data because the contents of the repository do not match its expected state.")); |
| 246 | + } |
| 247 | +} |
0 commit comments