From 5ead288d4d7c9e9dc2c07bd41ea586f18ef4d744 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Tue, 27 Jul 2021 16:34:07 +0200 Subject: [PATCH] Enhance Shard Level Metdata check in BlobStoreTestUtil Adding check that shard level index metadata actually contains the snapshots it's supposed to contain. This would have caught a number of recent bugs. --- .../snapshots/CorruptedBlobStoreRepositoryIT.java | 2 ++ .../elasticsearch/repositories/IndexSnapshotsService.java | 2 +- .../repositories/blobstore/BlobStoreRepository.java | 5 ++--- .../repositories/blobstore/BlobStoreTestUtil.java | 5 +++++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/CorruptedBlobStoreRepositoryIT.java b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/CorruptedBlobStoreRepositoryIT.java index 4bc78736c2e59..fbd7cf4b78a77 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/CorruptedBlobStoreRepositoryIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/CorruptedBlobStoreRepositoryIT.java @@ -453,6 +453,8 @@ public void testRepairBrokenShardGenerations() throws Exception { * Tests that a shard snapshot with a corrupted shard index file can still be used for restore and incremental snapshots. */ public void testSnapshotWithCorruptedShardIndexFile() throws Exception { + disableRepoConsistencyCheck("This test intentionally corrupts the repository contents"); + final Client client = client(); final Path repo = randomRepoPath(); final String indexName = "test-idx"; diff --git a/server/src/main/java/org/elasticsearch/repositories/IndexSnapshotsService.java b/server/src/main/java/org/elasticsearch/repositories/IndexSnapshotsService.java index 5d16ad60ec40c..3df7fbc461150 100644 --- a/server/src/main/java/org/elasticsearch/repositories/IndexSnapshotsService.java +++ b/server/src/main/java/org/elasticsearch/repositories/IndexSnapshotsService.java @@ -162,7 +162,7 @@ private String getIndexMetadataId() throws IOException { private BlobStoreIndexShardSnapshots getBlobStoreIndexShardSnapshots() throws IOException { BlobStoreRepository blobStoreRepository = (BlobStoreRepository) repository; final String shardGen = repositoryData.shardGenerations().getShardGen(indexId, shardId.getId()); - return blobStoreRepository.getBlobStoreIndexShardSnapshots(indexId, shardId, shardGen); + return blobStoreRepository.getBlobStoreIndexShardSnapshots(indexId, shardId.getId(), shardGen); } private ShardSnapshotInfo createIndexShardSnapshotInfo(String indexMetadataId, SnapshotFiles snapshotFiles) { diff --git a/server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java b/server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java index aa431cd1becff..6c500dc40d1b8 100644 --- a/server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java +++ b/server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java @@ -3238,10 +3238,9 @@ public BlobStoreIndexShardSnapshot loadShardSnapshot(BlobContainer shardContaine * Loads all available snapshots in the repository using the given {@code generation} for a shard. When {@code shardGen} * is null it tries to load it using the BwC mode, listing the available index- blobs in the shard container. */ - public BlobStoreIndexShardSnapshots getBlobStoreIndexShardSnapshots(IndexId indexId, ShardId shardId, @Nullable String shardGen) + public BlobStoreIndexShardSnapshots getBlobStoreIndexShardSnapshots(IndexId indexId, int shardId, @Nullable String shardGen) throws IOException { - final int shard = shardId.getId(); - final BlobContainer shardContainer = shardContainer(indexId, shard); + final BlobContainer shardContainer = shardContainer(indexId, shardId); Set blobs = Collections.emptySet(); if (shardGen == null) { diff --git a/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java b/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java index b60e22b7ca3fb..68ccc647d841c 100644 --- a/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java +++ b/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java @@ -34,6 +34,7 @@ import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshots; import org.elasticsearch.repositories.GetSnapshotInfoContext; import org.elasticsearch.repositories.IndexId; import org.elasticsearch.repositories.RepositoryData; @@ -300,6 +301,10 @@ private static void assertSnapshotInfosConsistency(BlobStoreRepository repositor hasKey(String.format(Locale.ROOT, BlobStoreRepository.SNAPSHOT_NAME_FORMAT, snapshotId.getUUID()))); assertThat(shardPathContents.keySet().stream() .filter(name -> name.startsWith(BlobStoreRepository.INDEX_FILE_PREFIX)).count(), lessThanOrEqualTo(2L)); + final BlobStoreIndexShardSnapshots blobStoreIndexShardSnapshots = repository.getBlobStoreIndexShardSnapshots( + indexId, shardId, repositoryData.shardGenerations().getShardGen(indexId, shardId)); + assertTrue(blobStoreIndexShardSnapshots.snapshots().stream() + .anyMatch(snapshotFiles -> snapshotFiles.snapshot().equals(snapshotId.getName()))); } } }