Skip to content

Commit 8fa0629

Browse files
authored
Do not check for object existence when deleting repository index files (#31680)
Before deleting a repository index generation file, BlobStoreRepository checks for the existence of the file and then deletes it. We can save a request here by using BlobContainer.deleteBlobIgnoringIfNotExists() which ignores error when deleting a file that does not exist. Since there is no way with S3 to know if a non versioned file existed before being deleted, this pull request also changes S3BlobContainer so that it now implements deleteBlobIgnoringIfNotExists(). It will now save one more request (blobExist?) when appropriate. The tests and fixture have been modified to conform the S3 API that always returns a 204/NO CONTENT HTTP response on deletions.
1 parent d8b3f33 commit 8fa0629

File tree

5 files changed

+12
-34
lines changed

5 files changed

+12
-34
lines changed

plugins/repository-s3/qa/amazon-s3/src/test/java/org/elasticsearch/repositories/s3/AmazonS3Fixture.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,8 @@ private static PathTrie<RequestHandler> defaultHandlers(final Map<String, Bucket
174174
}
175175

176176
final String objectName = objectName(request.getParameters());
177-
if (bucket.objects.remove(objectName) != null) {
178-
return new Response(RestStatus.OK.getStatus(), TEXT_PLAIN_CONTENT_TYPE, EMPTY_BYTE);
179-
}
180-
return newObjectNotFoundError(request.getId(), objectName);
177+
bucket.objects.remove(objectName);
178+
return new Response(RestStatus.NO_CONTENT.getStatus(), TEXT_PLAIN_CONTENT_TYPE, EMPTY_BYTE);
181179
})
182180
);
183181

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,13 @@ public void deleteBlob(String blobName) throws IOException {
107107
if (blobExists(blobName) == false) {
108108
throw new NoSuchFileException("Blob [" + blobName + "] does not exist");
109109
}
110+
deleteBlobIgnoringIfNotExists(blobName);
111+
}
110112

113+
@Override
114+
public void deleteBlobIgnoringIfNotExists(String blobName) throws IOException {
111115
try (AmazonS3Reference clientReference = blobStore.clientReference()) {
116+
// There is no way to know if an non-versioned object existed before the deletion
112117
SocketAccess.doPrivilegedVoid(() -> clientReference.client().deleteObject(blobStore.bucket(), buildKey(blobName)));
113118
} catch (final AmazonClientException e) {
114119
throw new IOException("Exception when deleting blob [" + blobName + "]", e);

plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/MockAmazonS3.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,9 @@ public ObjectListing listObjects(final ListObjectsRequest request) throws Amazon
149149
@Override
150150
public void deleteObject(final DeleteObjectRequest request) throws AmazonClientException {
151151
assertThat(request.getBucketName(), equalTo(bucket));
152-
153-
final String blobName = request.getKey();
154-
if (blobs.remove(blobName) == null) {
155-
AmazonS3Exception exception = new AmazonS3Exception("[" + blobName + "] does not exist.");
156-
exception.setStatusCode(404);
157-
throw exception;
158-
}
152+
blobs.remove(request.getKey());
159153
}
160-
154+
161155
@Override
162156
public void shutdown() {
163157
// TODO check close

server/src/main/java/org/elasticsearch/index/snapshots/blobstore/BlobStoreIndexShardSnapshot.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,6 @@ public BlobStoreIndexShardSnapshot(String snapshot, long indexVersion, List<File
389389
this.incrementalSize = incrementalSize;
390390
}
391391

392-
/**
393-
* Special constructor for the prototype
394-
*/
395-
private BlobStoreIndexShardSnapshot() {
396-
this.snapshot = "";
397-
this.indexVersion = 0;
398-
this.indexFiles = Collections.emptyList();
399-
this.startTime = 0;
400-
this.time = 0;
401-
this.incrementalFileCount = 0;
402-
this.incrementalSize = 0;
403-
}
404-
405392
/**
406393
* Returns index version
407394
*

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,7 @@ protected void writeIndexGen(final RepositoryData repositoryData, final long rep
668668
// delete the N-2 index file if it exists, keep the previous one around as a backup
669669
if (isReadOnly() == false && newGen - 2 >= 0) {
670670
final String oldSnapshotIndexFile = INDEX_FILE_PREFIX + Long.toString(newGen - 2);
671-
if (snapshotsBlobContainer.blobExists(oldSnapshotIndexFile)) {
672-
snapshotsBlobContainer.deleteBlob(oldSnapshotIndexFile);
673-
}
671+
snapshotsBlobContainer.deleteBlobIgnoringIfNotExists(oldSnapshotIndexFile);
674672
}
675673

676674
// write the current generation to the index-latest file
@@ -679,9 +677,7 @@ protected void writeIndexGen(final RepositoryData repositoryData, final long rep
679677
bStream.writeLong(newGen);
680678
genBytes = bStream.bytes();
681679
}
682-
if (snapshotsBlobContainer.blobExists(INDEX_LATEST_BLOB)) {
683-
snapshotsBlobContainer.deleteBlob(INDEX_LATEST_BLOB);
684-
}
680+
snapshotsBlobContainer.deleteBlobIgnoringIfNotExists(INDEX_LATEST_BLOB);
685681
logger.debug("Repository [{}] updating index.latest with generation [{}]", metadata.name(), newGen);
686682
writeAtomic(INDEX_LATEST_BLOB, genBytes);
687683
}
@@ -702,9 +698,7 @@ void writeIncompatibleSnapshots(RepositoryData repositoryData) throws IOExceptio
702698
}
703699
bytes = bStream.bytes();
704700
}
705-
if (snapshotsBlobContainer.blobExists(INCOMPATIBLE_SNAPSHOTS_BLOB)) {
706-
snapshotsBlobContainer.deleteBlob(INCOMPATIBLE_SNAPSHOTS_BLOB);
707-
}
701+
snapshotsBlobContainer.deleteBlobIgnoringIfNotExists(INCOMPATIBLE_SNAPSHOTS_BLOB);
708702
// write the incompatible snapshots blob
709703
writeAtomic(INCOMPATIBLE_SNAPSHOTS_BLOB, bytes);
710704
}

0 commit comments

Comments
 (0)