@@ -1578,6 +1578,12 @@ public void restore() throws IOException {
1578
1578
index .totalRecoverFiles (), new ByteSizeValue (index .totalRecoverBytes ()), index .reusedFileCount (), new ByteSizeValue (index .reusedFileCount ()));
1579
1579
}
1580
1580
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
1581
1587
for (final BlobStoreIndexShardSnapshot .FileInfo fileToRecover : filesToRecover ) {
1582
1588
logger .trace ("[{}] [{}] restoring file [{}]" , shardId , snapshotId , fileToRecover .name ());
1583
1589
restoreFile (fileToRecover , store );
@@ -1638,15 +1644,6 @@ private void restoreFile(final BlobStoreIndexShardSnapshot.FileInfo fileInfo, fi
1638
1644
stream = new RateLimitingInputStream (partSliceStream , restoreRateLimiter , restoreRateLimitingTimeInNanos ::inc );
1639
1645
}
1640
1646
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
-
1650
1647
try (final IndexOutput indexOutput = store .createVerifyingOutput (fileInfo .physicalName (), fileInfo .metadata (), IOContext .DEFAULT )) {
1651
1648
final byte [] buffer = new byte [BUFFER_SIZE ];
1652
1649
int length ;
0 commit comments