101
101
import java .io .FilterInputStream ;
102
102
import java .io .IOException ;
103
103
import java .io .InputStream ;
104
- import java .nio .file .DirectoryNotEmptyException ;
105
104
import java .nio .file .FileAlreadyExistsException ;
106
105
import java .nio .file .NoSuchFileException ;
107
106
import java .util .ArrayList ;
@@ -466,22 +465,16 @@ public void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId) {
466
465
final Collection <IndexId > indicesToCleanUp = Sets .newHashSet (repositoryData .getIndices ().values ());
467
466
indicesToCleanUp .removeAll (updatedRepositoryData .getIndices ().values ());
468
467
final BlobContainer indicesBlobContainer = blobStore ().blobContainer (basePath ().add ("indices" ));
469
- for (final IndexId indexId : indicesToCleanUp ) {
470
468
try {
471
- indicesBlobContainer .deleteBlobIgnoringIfNotExists (indexId .getId ());
472
- } catch (DirectoryNotEmptyException dnee ) {
473
- // if the directory isn't empty for some reason, it will fail to clean up;
474
- // we'll ignore that and accept that cleanup didn't fully succeed.
475
- // since we are using UUIDs for path names, this won't be an issue for
476
- // snapshotting indices of the same name
477
- logger .warn (() -> new ParameterizedMessage ("[{}] index [{}] no longer part of any snapshots in the repository, " +
478
- "but failed to clean up its index folder due to the directory not being empty." , metadata .name (), indexId ), dnee );
469
+ indicesBlobContainer .deleteBlobsIgnoringIfNotExists (
470
+ indicesToCleanUp .stream ().map (IndexId ::getId ).collect (Collectors .toList ()));
479
471
} catch (IOException ioe ) {
480
472
// a different IOException occurred while trying to delete - will just log the issue for now
481
- logger .warn (() -> new ParameterizedMessage ("[{}] index [{}] no longer part of any snapshots in the repository, " +
482
- "but failed to clean up its index folder." , metadata .name (), indexId ), ioe );
473
+ logger .warn (() ->
474
+ new ParameterizedMessage (
475
+ "[{}] indices {} are no longer part of any snapshots in the repository, " +
476
+ "but failed to clean up their index folders." , metadata .name (), indicesToCleanUp ), ioe );
483
477
}
484
- }
485
478
} catch (IOException | ResourceNotFoundException ex ) {
486
479
throw new RepositoryException (metadata .name (), "failed to delete snapshot [" + snapshotId + "]" , ex );
487
480
}
@@ -1018,16 +1011,14 @@ protected void finalize(final List<SnapshotFiles> snapshots,
1018
1011
try {
1019
1012
// Delete temporary index files first, as we might otherwise fail in the next step creating the new index file if an earlier
1020
1013
// attempt to write an index file with this generation failed mid-way after creating the temporary file.
1021
- for (final String blobName : blobs .keySet ()) {
1022
- if (FsBlobContainer .isTempBlobName (blobName )) {
1023
- try {
1024
- blobContainer .deleteBlobIgnoringIfNotExists (blobName );
1025
- } catch (IOException e ) {
1026
- logger .warn (() -> new ParameterizedMessage ("[{}][{}] failed to delete index blob [{}] during finalization" ,
1027
- snapshotId , shardId , blobName ), e );
1028
- throw e ;
1029
- }
1030
- }
1014
+ final List <String > blobNames =
1015
+ blobs .keySet ().stream ().filter (FsBlobContainer ::isTempBlobName ).collect (Collectors .toList ());
1016
+ try {
1017
+ blobContainer .deleteBlobsIgnoringIfNotExists (blobNames );
1018
+ } catch (IOException e ) {
1019
+ logger .warn (() -> new ParameterizedMessage ("[{}][{}] failed to delete index blobs {} during finalization" ,
1020
+ snapshotId , shardId , blobNames ), e );
1021
+ throw e ;
1031
1022
}
1032
1023
1033
1024
// If we deleted all snapshots, we don't need to create a new index file
@@ -1036,28 +1027,26 @@ protected void finalize(final List<SnapshotFiles> snapshots,
1036
1027
}
1037
1028
1038
1029
// Delete old index files
1039
- for (final String blobName : blobs .keySet ()) {
1040
- if (blobName .startsWith (SNAPSHOT_INDEX_PREFIX )) {
1041
- try {
1042
- blobContainer .deleteBlobIgnoringIfNotExists (blobName );
1043
- } catch (IOException e ) {
1044
- logger .warn (() -> new ParameterizedMessage ("[{}][{}] failed to delete index blob [{}] during finalization" ,
1045
- snapshotId , shardId , blobName ), e );
1046
- throw e ;
1047
- }
1048
- }
1030
+ final List <String > indexBlobs =
1031
+ blobs .keySet ().stream ().filter (blob -> blob .startsWith (SNAPSHOT_INDEX_PREFIX )).collect (Collectors .toList ());
1032
+ try {
1033
+ blobContainer .deleteBlobsIgnoringIfNotExists (indexBlobs );
1034
+ } catch (IOException e ) {
1035
+ logger .warn (() -> new ParameterizedMessage ("[{}][{}] failed to delete index blobs {} during finalization" ,
1036
+ snapshotId , shardId , indexBlobs ), e );
1037
+ throw e ;
1049
1038
}
1050
1039
1051
1040
// Delete all blobs that don't exist in a snapshot
1052
- for ( final String blobName : blobs .keySet ()) {
1053
- if ( blobName . startsWith ( DATA_BLOB_PREFIX ) && ( updatedSnapshots . findNameFile ( canonicalName ( blobName )) == null )) {
1054
- try {
1055
- blobContainer . deleteBlobIgnoringIfNotExists ( blobName );
1056
- } catch ( IOException e ) {
1057
- logger . warn (() -> new ParameterizedMessage ( "[{}][{}] failed to delete data blob [{}] during finalization" ,
1058
- snapshotId , shardId , blobName ), e );
1059
- }
1060
- }
1041
+ final List < String > orphanedBlobs = blobs .keySet (). stream ()
1042
+ . filter ( blobName ->
1043
+ blobName . startsWith ( DATA_BLOB_PREFIX ) && updatedSnapshots . findNameFile ( canonicalName ( blobName )) == null )
1044
+ . collect ( Collectors . toList () );
1045
+ try {
1046
+ blobContainer . deleteBlobsIgnoringIfNotExists ( orphanedBlobs );
1047
+ } catch ( IOException e ) {
1048
+ logger . warn (() -> new ParameterizedMessage ( "[{}][{}] failed to delete data blobs {} during finalization" ,
1049
+ snapshotId , shardId , orphanedBlobs ), e );
1061
1050
}
1062
1051
} catch (IOException e ) {
1063
1052
String message = "Failed to finalize " + reason + " with shard index [" + currentIndexGen + "]" ;
0 commit comments