mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): stream UploadPartCopy ranges into the part (#5162)
UploadPartCopy read the copied range into a byte array and stored it through the byte-array UploadPart path, so one copied part was capped near 2 GiB with EntityTooLarge, while S3 allows a part of up to 5 GiB. The range is now read from the source stream as it is needed, by a new CopyRangeInputStream that skips to the range on its first read and ends after its last byte, and written straight into the part through the streaming UploadPart path, so neither the source nor the part has to fit in memory in the disk-backed modes. A range over S3's 5 GiB part limit is rejected with EntityTooLarge, as the S3 API Reference error table words it, before anything is copied. Memory mode keeps its limit of about 2 GiB.
This commit is contained in:
1 parent
9c0102559d
commit
8fee66ae61
6 files changed
+197
-35
No files matched your search
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.services.s3.S3Service.CopySourceRange;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -91,10 +92,10 @@ class S3CopySourceRangeTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void readRangeSkipsToAnOffsetPastTwoGibibytes() throws IOException {
|
||||
void aRangeSkipsToAnOffsetPastTwoGibibytes() throws IOException {
|
||||
CopySourceRange range = new CopySourceRange(2_500_000_000L, 2_500_000_015L);
|
||||
|
||||
byte[] data = S3Service.readRange(patternStream(3L * 1024 * 1024 * 1024), range);
|
||||
byte[] data = new CopyRangeInputStream(patternStream(3L * 1024 * 1024 * 1024), range).readAllBytes();
|
||||
|
||||
assertEquals(16, data.length);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
@@ -103,7 +104,7 @@ class S3CopySourceRangeTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void readRangeSkipsPastTwoGibibytesInTheFileStreamDiskObjectsAreReadThrough() throws IOException {
|
||||
void aRangeSkipsPastTwoGibibytesInTheFileStreamDiskObjectsAreReadThrough() throws IOException {
|
||||
long offset = 2_500_000_000L;
|
||||
byte[] written = new byte[16];
|
||||
for (int i = 0; i < written.length; i++) {
|
||||
@@ -118,16 +119,40 @@ class S3CopySourceRangeTest {
|
||||
|
||||
byte[] data;
|
||||
try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ)) {
|
||||
data = S3Service.readRange(Channels.newInputStream(channel),
|
||||
new CopySourceRange(offset, offset + written.length - 1));
|
||||
data = new CopyRangeInputStream(Channels.newInputStream(channel),
|
||||
new CopySourceRange(offset, offset + written.length - 1)).readAllBytes();
|
||||
}
|
||||
|
||||
assertArrayEquals(written, data);
|
||||
}
|
||||
|
||||
@Test
|
||||
void readRangeFailsWhenTheSourceEndsBeforeTheRange() {
|
||||
assertThrows(IOException.class, () -> S3Service.readRange(patternStream(10), new CopySourceRange(5, 14)));
|
||||
void aRangeFailsWhenTheSourceEndsBeforeIt() {
|
||||
assertThrows(IOException.class,
|
||||
() -> new CopyRangeInputStream(patternStream(10), new CopySourceRange(5, 14)).readAllBytes());
|
||||
assertThrows(IOException.class,
|
||||
() -> new CopyRangeInputStream(patternStream(10), new CopySourceRange(12, 14)).readAllBytes(),
|
||||
"a source that ends before the range starts");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRangeEndsAfterItsLastByteWhateverTheReadSize() throws IOException {
|
||||
CopySourceRange range = new CopySourceRange(3, 1002);
|
||||
for (int readSize : List.of(1, 7, 1000, 4096)) {
|
||||
try (InputStream in = new CopyRangeInputStream(patternStream(5000), range)) {
|
||||
ByteArrayOutputStream copied = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[readSize];
|
||||
for (int read = in.read(buffer); read >= 0; read = in.read(buffer)) {
|
||||
copied.write(buffer, 0, read);
|
||||
}
|
||||
byte[] data = copied.toByteArray();
|
||||
assertEquals(range.length(), data.length, "read size " + readSize);
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
assertEquals(patternByte(range.first() + i), data[i], "byte " + i + ", read size " + readSize);
|
||||
}
|
||||
assertEquals(-1, in.read(), "the range stays at its end");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static byte patternByte(long position) {
|
||||
|
||||
@@ -17,14 +17,19 @@ import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -42,12 +47,14 @@ class S3StreamedUploadTest {
|
||||
Path tempDir;
|
||||
|
||||
private S3Service s3Service;
|
||||
private InMemoryStorage<String, S3Object> objectStore;
|
||||
private Path dataRoot;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
dataRoot = tempDir.resolve("s3");
|
||||
s3Service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), dataRoot, false);
|
||||
objectStore = new InMemoryStorage<>();
|
||||
s3Service = new S3Service(new InMemoryStorage<>(), objectStore, dataRoot, false);
|
||||
s3Service.createBucket("bucket", "us-east-1");
|
||||
}
|
||||
|
||||
@@ -221,6 +228,82 @@ class S3StreamedUploadTest {
|
||||
assertNothingStaged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUploadPartCopyStreamsItsRangeIntoThePart() {
|
||||
byte[] source = new byte[300_000];
|
||||
new Random(5034).nextBytes(source);
|
||||
s3Service.putObject("bucket", "source.bin", source, "application/octet-stream", Map.of());
|
||||
MultipartUpload upload = s3Service.initiateMultipartUpload("bucket", "copy.bin", null);
|
||||
|
||||
String rangeETag = s3Service.uploadPartCopy("bucket", "copy.bin", upload.getUploadId(), 1,
|
||||
"bucket", "source.bin", null, "bytes=1000-200999");
|
||||
String wholeETag = s3Service.uploadPartCopy("bucket", "copy.bin", upload.getUploadId(), 2,
|
||||
"bucket", "source.bin", null, null);
|
||||
|
||||
byte[] range = Arrays.copyOfRange(source, 1000, 201_000);
|
||||
assertEquals(S3Object.computeETag(range), rangeETag);
|
||||
assertEquals(S3Object.computeETag(source), wholeETag);
|
||||
s3Service.completeMultipartUpload("bucket", "copy.bin", upload.getUploadId(), List.of(1, 2), null, null);
|
||||
byte[] whole = new byte[range.length + source.length];
|
||||
System.arraycopy(range, 0, whole, 0, range.length);
|
||||
System.arraycopy(source, 0, whole, range.length, source.length);
|
||||
assertArrayEquals(whole, s3Service.getObject("bucket", "copy.bin").getData());
|
||||
assertNothingStaged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUploadPartCopyIntoAMissingUploadFails() {
|
||||
s3Service.putObject("bucket", "source.bin", BODY, "text/plain", Map.of());
|
||||
|
||||
assertEquals("NoSuchUpload", assertThrows(AwsException.class, () -> s3Service.uploadPartCopy("bucket",
|
||||
"copy.bin", "no-such-upload", 1, "bucket", "source.bin", null, "bytes=0-9")).getErrorCode());
|
||||
assertNothingStaged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUploadPartCopyOverFiveGibibytesIsEntityTooLarge() throws IOException {
|
||||
assertEquals(5_368_709_120L, S3Service.MAX_PART_SIZE);
|
||||
long overLimit = S3Service.MAX_PART_SIZE + 1;
|
||||
sparseSource("source.bin", overLimit);
|
||||
MultipartUpload upload = s3Service.initiateMultipartUpload("bucket", "copy.bin", null);
|
||||
|
||||
for (String range : Arrays.asList(null, "bytes=0-" + S3Service.MAX_PART_SIZE)) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> s3Service.uploadPartCopy("bucket",
|
||||
"copy.bin", upload.getUploadId(), 1, "bucket", "source.bin", null, range), "range " + range);
|
||||
|
||||
assertEquals("EntityTooLarge", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals("Your proposed upload exceeds the maximum allowed object size.", error.getMessage());
|
||||
}
|
||||
assertTrue(s3Service.getMultipartUpload("bucket", "copy.bin", upload.getUploadId()).getParts().isEmpty());
|
||||
assertNothingStaged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUploadPartCopyOfExactlyFiveGibibytesIsWithinTheLimit() throws IOException {
|
||||
sparseSource("source.bin", S3Service.MAX_PART_SIZE + 1);
|
||||
|
||||
// A missing upload is only found once the size passed, and before any of the source is read.
|
||||
assertEquals("NoSuchUpload", assertThrows(AwsException.class, () -> s3Service.uploadPartCopy("bucket",
|
||||
"copy.bin", "no-such-upload", 1, "bucket", "source.bin", null,
|
||||
"bytes=1-" + S3Service.MAX_PART_SIZE)).getErrorCode());
|
||||
}
|
||||
|
||||
/** A stored object of {@code size} bytes whose file is sparse, so it takes almost no disk. */
|
||||
private void sparseSource(String key, long size) throws IOException {
|
||||
s3Service.putObject("bucket", key, new byte[] {1}, "application/octet-stream", Map.of());
|
||||
objectStore.scan(storeKey -> true).stream()
|
||||
.filter(object -> "bucket".equals(object.getBucketName()) && key.equals(object.getKey()))
|
||||
.forEach(object -> object.setSize(size));
|
||||
try (Stream<Path> files = Files.walk(dataRoot)) {
|
||||
for (Path file : files.filter(path -> path.getFileName().toString().equals(key + ".s3data")).toList()) {
|
||||
try (FileChannel channel = FileChannel.open(file, StandardOpenOption.WRITE)) {
|
||||
channel.write(ByteBuffer.wrap(new byte[] {0}), size - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void memoryModeReadsTheStreamIntoTheBytePaths() {
|
||||
S3Service memory = memoryService();
|
||||
|
||||
Reference in new issue
Block a user