@@ -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