Skip to content

Commit 62579b0

Browse files
authored
Fix unhandled exception when blobstore repository contains unexpected file (#93914) (#97113)
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 6cd68fc commit 62579b0

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
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.repositories.ShardGeneration;
3131
import org.elasticsearch.repositories.ShardGenerations;
3232
import org.elasticsearch.repositories.blobstore.BlobStoreRepository;
33+
import org.elasticsearch.repositories.fs.FsRepository;
3334
import org.elasticsearch.threadpool.ThreadPool;
3435
import org.elasticsearch.xcontent.XContentFactory;
3536

@@ -844,6 +845,19 @@ public void testSnapshotWithMissingShardLevelIndexFile() throws Exception {
844845
assertEquals(0, restoreSnapshotResponse.getRestoreInfo().failedShards());
845846
}
846847

848+
public void testDeletesWithUnexpectedIndexBlob() throws Exception {
849+
Path repo = randomRepoPath();
850+
final String repoName = "test-repo";
851+
createRepository(repoName, FsRepository.TYPE, repo);
852+
final String snapshot = "first-snapshot";
853+
createFullSnapshot(repoName, snapshot);
854+
// create extra file with the index prefix
855+
final Path extraFile = Files.createFile(repo.resolve(BlobStoreRepository.INDEX_FILE_PREFIX + randomAlphaOfLength(5)));
856+
assertAcked(startDeleteSnapshot(repoName, snapshot).get());
857+
// delete file again so repo consistency checks pass
858+
Files.delete(extraFile);
859+
}
860+
847861
private void assertRepositoryBlocked(Client client, String repo, String existingSnapshot) {
848862
logger.info("--> try to delete snapshot");
849863
final RepositoryException repositoryException3 = expectThrows(

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,12 @@ private static List<String> staleRootBlobs(RepositoryData repositoryData, Set<St
12891289
return allSnapshotIds.contains(foundUUID) == false;
12901290
} else if (blob.startsWith(INDEX_FILE_PREFIX)) {
12911291
// TODO: Include the current generation here once we remove keeping index-(N-1) around from #writeIndexGen
1292-
return repositoryData.getGenId() > Long.parseLong(blob.substring(INDEX_FILE_PREFIX.length()));
1292+
try {
1293+
return repositoryData.getGenId() > Long.parseLong(blob.substring(INDEX_FILE_PREFIX.length()));
1294+
} catch (NumberFormatException nfe) {
1295+
// odd case of an extra file with the index- prefix that we can't identify
1296+
return false;
1297+
}
12931298
}
12941299
return false;
12951300
}).collect(Collectors.toList());

0 commit comments

Comments
 (0)