perf(s3): drop unused hashes on S3Object creation (#3589)

* perf(s3): stop computing an unused SHA-256 for each stored object

The S3Object constructor hashed the whole body with SHA-256.
storeObjectInternal replaces that checksum before the object is published,
and delete markers never return a checksum, so nothing read the value.

* perf(s3): pass the composite ETag in when completing a multipart upload

CompleteMultipartUpload stored the object with an MD5 of the whole body
under the bucket lock, then replaced it with the composite ETag. Passing
the composite ETag in skips that hash.

The composite ETag is now also on the first write. WAL storage logs a
record when it is put, so the version record used to keep the plain MD5,
and a restart after a crash returned that MD5 for the version.

* test(s3): check the stored composite ETag without counting storage keys

The test pinned how many object store puts a completion makes. It now
checks that every stored ETag is the composite one, which still fails
when nothing was stored.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-14 00:18:00 -04:00
1 parent 3003f05953
commit a5ab1672f9
3 files changed
+46 -8

No files matched your search

@@ -484,24 +484,33 @@ public class S3Service implements Resettable, ResourceProvider {
private S3Object storeObject(String bucketName, String key, byte[] data,
String contentType, Map<String, String> metadata,
S3Checksum checksum, List<Part> parts, PutObjectOptions options) {
return storeObject(bucketName, key, data, contentType, metadata, checksum, parts, options, null);
}
private S3Object storeObject(String bucketName, String key, byte[] data,
String contentType, Map<String, String> metadata,
S3Checksum checksum, List<Part> parts, PutObjectOptions options, String eTag) {
Bucket bucket = resolveBucket(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
synchronized (bucket) {
return storeObjectInternal(bucket, bucketName, key, data, contentType, metadata, checksum, parts, options);
return storeObjectInternal(bucket, bucketName, key, data, contentType, metadata, checksum, parts, options,
eTag);
}
}
private S3Object storeObjectInternal(Bucket bucket, String bucketName, String key, byte[] data,
String contentType, Map<String, String> metadata,
S3Checksum checksum, List<Part> parts, PutObjectOptions options) {
S3Checksum checksum, List<Part> parts, PutObjectOptions options,
String eTag) {
PutObjectOptions effectiveOptions = options != null ? options : new PutObjectOptions();
String normalizedServerSideEncryption = normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption());
SseCustomerKey sseCustomerKey = validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(), effectiveOptions.getSseCustomerKey(), effectiveOptions.getSseCustomerKeyMd5());
rejectConflictingServerSideEncryption(normalizedServerSideEncryption, sseCustomerKey);
checkWritePreconditions(bucketName, key, effectiveOptions.getIfMatch(), effectiveOptions.getIfNoneMatch());
S3Object object = new S3Object(bucketName, key, data, contentType);
S3Object object = new S3Object(bucketName, key, data, contentType,
eTag != null ? eTag : computeETag(data));
if (metadata != null) {
object.getMetadata().putAll(metadata);
}
@@ -3166,13 +3175,12 @@ public class S3Service implements Resettable, ResourceProvider {
.withServerSideEncryption(upload.getServerSideEncryption())
.withSseKmsKeyId(upload.getSseKmsKeyId())
.withAcl(upload.getAcl())
.withTagging(upload.getTagging()));
.withTagging(upload.getTagging()),
compositeETag);
if (upload.getSseCustomerAlgorithm() != null) {
object.setSseCustomerAlgorithm(upload.getSseCustomerAlgorithm());
object.setSseCustomerKeyMd5(upload.getSseCustomerKeyMd5());
}
// Override the ETag with the composite multipart ETag
object.setETag(compositeETag);
objectStore.put(objectKey(bucket, key), object);
// Cleanup
@@ -61,17 +61,20 @@ public class S3Object {
}
public S3Object(String bucketName, String key, byte[] data, String contentType) {
this(bucketName, key, data, contentType, computeETag(data));
}
public S3Object(String bucketName, String key, byte[] data, String contentType, String eTag) {
this.bucketName = bucketName;
this.key = key;
this.data = data;
this.contentType = contentType != null ? contentType : "application/octet-stream";
this.size = data.length;
this.lastModified = Instant.now().truncatedTo(ChronoUnit.MILLIS);
this.eTag = computeETag(data);
this.eTag = eTag;
this.metadata = new HashMap<>();
this.storageClass = "STANDARD";
this.checksum = new S3Checksum();
this.checksum.setChecksumSHA256(S3Checksum.sha256Base64(data));
this.checksum.setChecksumType(ChecksumType.FULL_OBJECT);
this.parts = new ArrayList<>();
this.tags = new HashMap<>();
@@ -249,6 +249,33 @@ class S3MultipartServiceTest {
assertNotNull(result.getVersionId(), "Versioned bucket should produce a versionId");
}
@Test
void completeMultipartUploadStoresTheCompositeETagOnTheFirstWrite() {
// WAL storage logs a record when it is put, so an ETag changed after the put never reaches the log.
Map<String, String> firstStoredETags = new HashMap<>();
InMemoryStorage<String, S3Object> objectStore = new InMemoryStorage<>() {
@Override
public void put(String key, S3Object value) {
firstStoredETags.putIfAbsent(key, value.getETag());
super.put(key, value);
}
};
S3Service service = new S3Service(new InMemoryStorage<>(), objectStore, tempDir, true);
service.createBucket("versioned-bucket", "us-east-1");
service.putBucketVersioning("versioned-bucket", "Enabled");
MultipartUpload upload = service.initiateMultipartUpload("versioned-bucket", "file.bin", null);
service.uploadPart("versioned-bucket", "file.bin", upload.getUploadId(), 1,
"part1".getBytes(StandardCharsets.UTF_8));
service.uploadPart("versioned-bucket", "file.bin", upload.getUploadId(), 2,
"part2".getBytes(StandardCharsets.UTF_8));
S3Object result = service.completeMultipartUpload("versioned-bucket", "file.bin",
upload.getUploadId(), List.of(1, 2), null, null);
assertTrue(result.getETag().endsWith("-2\""), result.getETag());
assertEquals(Set.of(result.getETag()), Set.copyOf(firstStoredETags.values()));
}
@Test
void completeMultipartUploadCleansUp() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "file.bin", null);