Skip to content

Commit

Permalink
Snapshot/Restore: make it possible to delete snapshots with missing m…
Browse files Browse the repository at this point in the history
…etadata file

Fixes #7980
  • Loading branch information
imotov committed Oct 7, 2014
1 parent 18a1f5f commit 76efdf8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
Expand Up @@ -41,6 +41,7 @@
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.*;
import org.elasticsearch.index.shard.IndexShardException;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.snapshots.IndexShardRepository;
import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardRepository;
Expand Down Expand Up @@ -259,7 +260,12 @@ public void initializeSnapshot(SnapshotId snapshotId, ImmutableList<String> indi
@Override
public void deleteSnapshot(SnapshotId snapshotId) {
Snapshot snapshot = readSnapshot(snapshotId);
MetaData metaData = readSnapshotMetaData(snapshotId, snapshot.indices(), true);
MetaData metaData = null;
try {
metaData = readSnapshotMetaData(snapshotId, snapshot.indices(), true);
} catch (SnapshotException ex) {
logger.warn("cannot read metadata for snapshot [{}]", ex, snapshotId);
}
try {
String blobName = snapshotBlobName(snapshotId);
// Delete snapshot file first so we wouldn't end up with partially deleted snapshot that looks OK
Expand All @@ -286,10 +292,17 @@ public void deleteSnapshot(SnapshotId snapshotId) {
} catch (IOException ex) {
logger.warn("[{}] failed to delete metadata for index [{}]", ex, snapshotId, index);
}
IndexMetaData indexMetaData = metaData.index(index);
if (indexMetaData != null) {
for (int i = 0; i < indexMetaData.getNumberOfShards(); i++) {
indexShardRepository.delete(snapshotId, new ShardId(index, i));
if (metaData != null) {
IndexMetaData indexMetaData = metaData.index(index);
if (indexMetaData != null) {
for (int i = 0; i < indexMetaData.getNumberOfShards(); i++) {
ShardId shardId = new ShardId(index, i);
try {
indexShardRepository.delete(snapshotId, shardId);
} catch (IndexShardException | SnapshotException ex) {
logger.warn("[{}] failed to delete shard data for shard [{}]", ex, snapshotId, shardId);
}
}
}
}
}
Expand Down
Expand Up @@ -717,6 +717,41 @@ public void deleteSnapshotWithMissingIndexAndShardMetadataTest() throws Exceptio
assertThrows(client.admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap-1"), SnapshotMissingException.class);
}

@Test
public void deleteSnapshotWithMissingMetadataTest() throws Exception {
Client client = client();

File repo = newTempDir(LifecycleScope.SUITE);
logger.info("--> creating repository at " + repo.getAbsolutePath());
assertAcked(client.admin().cluster().preparePutRepository("test-repo")
.setType("fs").setSettings(ImmutableSettings.settingsBuilder()
.put("location", repo)
.put("compress", false)
.put("chunk_size", randomIntBetween(100, 1000))));

createIndex("test-idx-1", "test-idx-2");
ensureYellow();
logger.info("--> indexing some data");
indexRandom(true,
client().prepareIndex("test-idx-1", "doc").setSource("foo", "bar"),
client().prepareIndex("test-idx-2", "doc").setSource("foo", "bar"));

logger.info("--> creating snapshot");
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-1").setWaitForCompletion(true).setIndices("test-idx-*").get();
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));

logger.info("--> delete index metadata and shard metadata");
File metadata = new File(repo, "metadata-test-snap-1");
assertThat(metadata.delete(), equalTo(true));

logger.info("--> delete snapshot");
client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap-1").get();

logger.info("--> make sure snapshot doesn't exist");
assertThrows(client.admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap-1"), SnapshotMissingException.class);
}

@Test
@TestLogging("snapshots:TRACE")
public void snapshotClosedIndexTest() throws Exception {
Expand Down

0 comments on commit 76efdf8

Please sign in to comment.