Skip to content

Commit a132405

Browse files
author
Ali Beyad
authored
Ensures that during the restore process, if a file in the snapshot (#20220)
already has a file of the same name in the Store, but is different in content (different checksum/length), then those files are first deleted before restoring the files in question.
1 parent 55b91cd commit a132405

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,12 @@ public void restore() throws IOException {
15781578
index.totalRecoverFiles(), new ByteSizeValue(index.totalRecoverBytes()), index.reusedFileCount(), new ByteSizeValue(index.reusedFileCount()));
15791579
}
15801580
try {
1581+
// first, delete pre-existing files in the store that have the same name but are
1582+
// different (i.e. different length/checksum) from those being restored in the snapshot
1583+
for (final StoreFileMetaData storeFileMetaData : diff.different) {
1584+
IOUtils.deleteFiles(store.directory(), storeFileMetaData.name());
1585+
}
1586+
// restore the files from the snapshot to the Lucene store
15811587
for (final BlobStoreIndexShardSnapshot.FileInfo fileToRecover : filesToRecover) {
15821588
logger.trace("[{}] [{}] restoring file [{}]", shardId, snapshotId, fileToRecover.name());
15831589
restoreFile(fileToRecover, store);
@@ -1638,15 +1644,6 @@ private void restoreFile(final BlobStoreIndexShardSnapshot.FileInfo fileInfo, fi
16381644
stream = new RateLimitingInputStream(partSliceStream, restoreRateLimiter, restoreRateLimitingTimeInNanos::inc);
16391645
}
16401646

1641-
// A restore could possibly overwrite existing segment files due to any number of reasons,
1642-
// for example if the primary was snapshotted and then the replica was promoted to primary
1643-
// with different segment files. In this case, the goal of the restore is to forget about
1644-
// what is currently in the index and just restore the state to whatever is in the snapshot.
1645-
// Hence, we are deleting files here if they already exist before writing to them. A better
1646-
// long term solution would be to use recovery for restoring, so we have more robust restoring
1647-
// of files (copying to temporary files first and then moving them over).
1648-
IOUtils.deleteFilesIgnoringExceptions(store.directory(), fileInfo.physicalName());
1649-
16501647
try (final IndexOutput indexOutput = store.createVerifyingOutput(fileInfo.physicalName(), fileInfo.metadata(), IOContext.DEFAULT)) {
16511648
final byte[] buffer = new byte[BUFFER_SIZE];
16521649
int length;

0 commit comments

Comments
 (0)