perf(s3): assemble multipart uploads without doubling the object in heap (#4227)

* perf(s3): assemble multipart uploads without doubling the object in heap

CompleteMultipartUpload read every part with Files.readAllBytes into a growing
ByteArrayOutputStream and then copied it again with toByteArray(), so a
disk-backed upload briefly held several full-size copies of the object.

Size one array from the part lengths and read each part straight into its
slot, failing if a part changes size during assembly rather than producing a
corrupt object. The composite ETag and full-object checksum are unchanged.

* fix(s3): fail multipart assembly when a part grows after being measured

The size check caught a part that shrank between being measured and
being copied, but not one that grew: the read stopped at the measured
size and the extra bytes were dropped. Disk parts are now checked for
end of file after the measured bytes, and in-memory parts are copied
from the arrays that were measured, whose length cannot change.

* chore: rerun CI
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-23 13:56:29 -04:00
1 parent 4ca0e085cd
commit 0e432dd41b
2 files changed
+237 -9

No files matched your search

@@ -30,7 +30,6 @@ import jakarta.inject.Inject;
import jakarta.ws.rs.core.MultivaluedHashMap;
import org.jboss.logging.Logger;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -3319,24 +3318,18 @@ public class S3Service implements Resettable, ResourceProvider {
// Concatenate parts in order
try {
ByteArrayOutputStream combined = new ByteArrayOutputStream();
MessageDigest md = MessageDigest.getInstance("MD5");
for (int num : partNumbers) {
byte[] partData = inMemory
? memoryMultipartStore.get(uploadId).get(num)
: Files.readAllBytes(dataRoot.resolve(".multipart").resolve(uploadId).resolve(String.valueOf(num)));
combined.write(partData);
// A part ETag is the MD5 of that part, so the composite hashes it without rehashing the data
String partETag = stripSurroundingQuotes(upload.getParts().get(num).getETag());
md.update(HexFormat.of().parseHex(partETag));
}
byte[] allData = combined.toByteArray();
// Composite ETag: MD5 of concatenated part MD5s, suffixed with part count
String compositeETag = "\"" + bytesToHex(md.digest()) + "-" + partNumbers.size() + "\"";
byte[] allData = concatenateParts(uploadId, partNumbers);
List<Part> completedParts = partNumbers.stream()
.map(num -> copyPart(upload.getParts().get(num)))
.toList();
@@ -3376,6 +3369,55 @@ public class S3Service implements Resettable, ResourceProvider {
}
}
/**
* Copies the parts, in order, into one array sized to the total upload. Disk parts are read
* straight into their slot, so assembly never holds a second full-size copy of the object.
* In-memory parts are copied from the arrays that were measured, so their sizes cannot change
* in between.
*/
private byte[] concatenateParts(String uploadId, List<Integer> partNumbers) throws IOException {
Path partsDir = dataRoot.resolve(".multipart").resolve(uploadId);
Map<Integer, byte[]> memoryParts = inMemory ? memoryMultipartStore.get(uploadId) : null;
byte[][] memoryData = inMemory ? new byte[partNumbers.size()][] : null;
long[] partSizes = new long[partNumbers.size()];
long totalSize = 0;
for (int i = 0; i < partNumbers.size(); i++) {
int num = partNumbers.get(i);
if (inMemory) {
memoryData[i] = memoryParts.get(num);
partSizes[i] = memoryData[i].length;
} else {
partSizes[i] = Files.size(partsDir.resolve(String.valueOf(num)));
}
totalSize += partSizes[i];
}
byte[] allData = new byte[Math.toIntExact(totalSize)];
int offset = 0;
for (int i = 0; i < partNumbers.size(); i++) {
int size = (int) partSizes[i];
if (inMemory) {
System.arraycopy(memoryData[i], 0, allData, offset, size);
} else {
int num = partNumbers.get(i);
try (InputStream in = Files.newInputStream(partsDir.resolve(String.valueOf(num)))) {
readPart(in, allData, offset, size, num);
}
}
offset += size;
}
return allData;
}
/**
* Reads exactly {@code size} bytes of part {@code partNumber} into {@code dest} at {@code offset},
* failing if the part holds a different number of bytes than was measured before assembly.
*/
static void readPart(InputStream in, byte[] dest, int offset, int size, int partNumber) throws IOException {
if (in.readNBytes(dest, offset, size) != size || in.read() != -1) {
throw new IOException("Part " + partNumber + " changed size during assembly");
}
}
private boolean etagsMatch(String storedETag, String submittedETag) {
return stripSurroundingQuotes(storedETag).equals(stripSurroundingQuotes(submittedETag));
}