Skip to content

Commit 77988a8

Browse files
original-brownbearcarlosdelest
authored andcommitted
Fix unhandled exception when blobstore repository contains unexpected file (elastic#93914)
If there's any file with the `index-` prefix but not a number after that at the repo root we must not throw here. If we do, we will end up throwing an unexpected exception that is not properly handled by `org.elasticsearch.snapshots.SnapshotsService#failAllListenersOnMasterFailOver`, leading to the repository generation not getting correctly set in the cluster state down the line.
1 parent ebe8a98 commit 77988a8

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

docs/changelog/93914.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 93914
2+
summary: Fix unhandled exception when blobstore repository contains unexpected file
3+
area: Snapshot/Restore
4+
type: bug
5+
issues: []

server/src/internalClusterTest/java/org/elasticsearch/snapshots/CorruptedBlobStoreRepositoryIT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.repositories.ShardGeneration;
3232
import org.elasticsearch.repositories.ShardGenerations;
3333
import org.elasticsearch.repositories.blobstore.BlobStoreRepository;
34+
import org.elasticsearch.repositories.fs.FsRepository;
3435
import org.elasticsearch.threadpool.ThreadPool;
3536
import org.elasticsearch.xcontent.XContentFactory;
3637

@@ -814,6 +815,19 @@ public void testSnapshotWithMissingShardLevelIndexFile() throws Exception {
814815
assertEquals(0, restoreSnapshotResponse.getRestoreInfo().failedShards());
815816
}
816817

818+
public void testDeletesWithUnexpectedIndexBlob() throws Exception {
819+
Path repo = randomRepoPath();
820+
final String repoName = "test-repo";
821+
createRepository(repoName, FsRepository.TYPE, repo);
822+
final String snapshot = "first-snapshot";
823+
createFullSnapshot(repoName, snapshot);
824+
// create extra file with the index prefix
825+
final var extraFile = Files.createFile(repo.resolve(BlobStoreRepository.INDEX_FILE_PREFIX + randomAlphaOfLength(5)));
826+
assertAcked(startDeleteSnapshot(repoName, snapshot).get());
827+
// delete file again so repo consistency checks pass
828+
Files.delete(extraFile);
829+
}
830+
817831
private void assertRepositoryBlocked(String repo, String existingSnapshot) {
818832
if (getRepositoryMetadata(repo).generation() == RepositoryData.CORRUPTED_REPO_GEN) return;
819833

server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,12 @@ private static List<String> staleRootBlobs(RepositoryData repositoryData, Set<St
12981298
return allSnapshotIds.contains(foundUUID) == false;
12991299
} else if (blob.startsWith(INDEX_FILE_PREFIX)) {
13001300
// TODO: Include the current generation here once we remove keeping index-(N-1) around from #writeIndexGen
1301-
return repositoryData.getGenId() > Long.parseLong(blob.substring(INDEX_FILE_PREFIX.length()));
1301+
try {
1302+
return repositoryData.getGenId() > Long.parseLong(blob.substring(INDEX_FILE_PREFIX.length()));
1303+
} catch (NumberFormatException nfe) {
1304+
// odd case of an extra file with the index- prefix that we can't identify
1305+
return false;
1306+
}
13021307
}
13031308
return false;
13041309
}).toList();

0 commit comments

Comments
 (0)