Skip to content

Commit c5c44c8

Browse files
committed
Ignore NoSuchKey exceptions when deleting container
1 parent 12d0fec commit c5c44c8

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobContainer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ public DeleteResult delete(OperationPurpose purpose) throws IOException {
412412
deletedBytes.addAndGet(s3Object.size());
413413
return s3Object.key();
414414
});
415-
blobStore.deleteBlobs(purpose, false, blobNameIterator);
415+
blobStore.deleteBlobs(purpose, blobNameIterator);
416416
prevListing = listObjectsResponse;
417417
}
418418
} catch (final SdkException e) {
@@ -423,7 +423,7 @@ public DeleteResult delete(OperationPurpose purpose) throws IOException {
423423

424424
@Override
425425
public void deleteBlobsIgnoringIfNotExists(OperationPurpose purpose, Iterator<String> blobNames) throws IOException {
426-
blobStore.deleteBlobs(purpose, true, Iterators.map(blobNames, this::buildKey));
426+
blobStore.deleteBlobs(purpose, Iterators.map(blobNames, this::buildKey));
427427
}
428428

429429
@Override

modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobStore.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,6 @@ public BlobContainer blobContainer(BlobPath path) {
306306
private static class DeletionExceptions {
307307
Exception exception = null;
308308
private int count = 0;
309-
final boolean ignoreNoSuchKey;
310-
311-
DeletionExceptions(boolean ignoreNoSuchKey) {
312-
this.ignoreNoSuchKey = ignoreNoSuchKey;
313-
}
314309

315310
void useOrMaybeSuppress(Exception e) {
316311
if (count < MAX_DELETE_EXCEPTIONS) {
@@ -320,15 +315,15 @@ void useOrMaybeSuppress(Exception e) {
320315
}
321316
}
322317

323-
void deleteBlobs(OperationPurpose purpose, boolean ignoreNoSuchKey, Iterator<String> blobNames) throws IOException {
318+
void deleteBlobs(OperationPurpose purpose, Iterator<String> blobNames) throws IOException {
324319
if (blobNames.hasNext() == false) {
325320
return;
326321
}
327322

328323
final List<ObjectIdentifier> partition = new ArrayList<>();
329324
try {
330325
// S3 API only allows 1k blobs per delete so we split up the given blobs into requests of max. 1k deletes
331-
final var deletionExceptions = new DeletionExceptions(ignoreNoSuchKey);
326+
final var deletionExceptions = new DeletionExceptions();
332327
blobNames.forEachRemaining(key -> {
333328
partition.add(ObjectIdentifier.builder().key(key).build());
334329
if (partition.size() == bulkDeletionBatchSize) {
@@ -360,7 +355,7 @@ private void deletePartition(OperationPurpose purpose, List<ObjectIdentifier> pa
360355
while (true) {
361356
try (AmazonS3Reference clientReference = clientReference()) {
362357
final var response = clientReference.client().deleteObjects(bulkDelete(purpose, this, partition));
363-
if (maybeRecordDeleteErrors(response, deletionExceptions) == false) {
358+
if (maybeRecordDeleteErrors(purpose, response, deletionExceptions) == false) {
364359
s3RepositoriesMetrics.retryDeletesHistogram().record(retryCounter);
365360
}
366361
return;
@@ -384,7 +379,11 @@ private void deletePartition(OperationPurpose purpose, List<ObjectIdentifier> pa
384379
}
385380
}
386381

387-
private static boolean maybeRecordDeleteErrors(DeleteObjectsResponse response, DeletionExceptions deletionExceptions) {
382+
private static boolean maybeRecordDeleteErrors(
383+
OperationPurpose purpose,
384+
DeleteObjectsResponse response,
385+
DeletionExceptions deletionExceptions
386+
) {
388387
if (response.hasErrors() == false) {
389388
return false;
390389
}
@@ -394,8 +393,9 @@ private static boolean maybeRecordDeleteErrors(DeleteObjectsResponse response, D
394393
StringBuilder sb = null;
395394

396395
for (final var err : errors) {
397-
if (deletionExceptions.ignoreNoSuchKey && "NoSuchKey".equals(err.code())) {
396+
if (purpose != OperationPurpose.REPOSITORY_ANALYSIS && "NoSuchKey".equals(err.code())) {
398397
// The blob does not exist, which is what we wanted, so let's count that as a win
398+
// (except for repo analysis where we can be certain that the blobs being deleted do all exist)
399399
continue;
400400
}
401401

0 commit comments

Comments
 (0)