diff --git a/docs/services/s3.md b/docs/services/s3.md index dbe211db07..532b634d9e 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -398,9 +398,7 @@ copy, until the parts are removed. Multipart uploads in progress do not survive reset, and their files are removed when either happens. `UploadPartCopy` reads only the copied range from its source, so it can copy any part of a source of any size, but one copied part can hold at most about 2 GiB; a larger range fails with `EntityTooLarge`. `CopyObject` and S3 Select -still read the whole source object into memory in every mode, so they need a source under 2 GiB, -and deleting the current version of a key in a versioned bucket reads the version that takes its -place the same way. +still read the whole source object into memory in every mode, so they need a source under 2 GiB. ## Examples diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java index 3bc0ca79b2..9dc923544e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java @@ -1920,19 +1920,18 @@ public class S3Service implements Resettable, ResourceProvider { S3Object newLatest = remaining.stream() .max(Comparator.comparing(S3Object::getLastModified)) .orElseThrow(); + // The promoted version's body becomes current before its metadata is + // published, the order storeObjectInternal writes in, so a concurrent GET + // never pairs the promoted version with the deleted version's bytes. + // Delete markers have no versioned file. + if (!newLatest.isDeleteMarker()) { + promoteVersionedFile(bucketName, key, newLatest.getVersionId()); + } newLatest.setLatest(true); objectStore.put(versionedKey(bucketName, key, newLatest.getVersionId()), newLatest); objectStore.put(latestKey, newLatest); - // Delete markers have no versioned file — readVersionedFile throws in persistent mode. if (newLatest.isDeleteMarker()) { deleteFile(bucketName, key); - } else { - byte[] promotedData = readVersionedFile(bucketName, key, newLatest.getVersionId()); - if (promotedData != null) { - writeFile(bucketName, key, promotedData); - } else { - deleteFile(bucketName, key); - } } } } @@ -5763,10 +5762,6 @@ public class S3Service implements Resettable, ResourceProvider { } } - private byte[] readVersionedFile(String bucketName, String key, String versionId) { - return readVersionedFile(ownerId(), bucketName, key, versionId); - } - private byte[] readVersionedFile(String accountId, String bucketName, String key, String versionId) { if (inMemory) { return memoryDataStore.get(physicalVersionedKey(accountId, bucketName, key, versionId)); @@ -5850,9 +5845,9 @@ public class S3Service implements Resettable, ResourceProvider { } /** - * Gives {@code target} the file already stored at {@code source} through a hard link, so a - * versioned multipart object is kept once instead of being copied under the bucket lock. - * Sharing the file is safe because no object file is ever modified in place: every write + * Gives {@code target} the file already stored at {@code source} through a hard link, so a body + * a versioned object already has on disk is shared instead of being copied under the bucket + * lock. Sharing the file is safe because no object file is ever modified in place: every write * replaces its path with a rename, which leaves the file under the other name untouched. * A filesystem without hard links gets a copy instead. */ @@ -5872,11 +5867,29 @@ public class S3Service implements Resettable, ResourceProvider { } catch (IOException e) { throw new UncheckedIOException("Failed to link S3 object file into place", e); } finally { - deleteQuietly(tmp, "temporary link of a versioned multipart object that was not published"); + deleteQuietly(tmp, "temporary link of an S3 object file that was not put in place"); lock.unlock(); } } + /** + * Makes the stored body of {@code versionId} the key's current body without reading it, so a + * version of any size can be promoted: on disk the current file becomes a hard link to the + * version's file, and in memory both entries share the version's array. + */ + private void promoteVersionedFile(String bucketName, String key, String versionId) { + if (inMemory) { + byte[] data = memoryDataStore.get(physicalVersionedKey(bucketName, key, versionId)); + if (data != null) { + memoryDataStore.put(physicalKey(bucketName, key), data); + } else { + deleteFile(bucketName, key); + } + return; + } + linkIntoPlace(resolveVersionedPathForRead(bucketName, key, versionId), resolveObjectPath(bucketName, key)); + } + private byte[] readFile(String bucketName, String key) { return readFile(ownerId(), bucketName, key); } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceTest.java index 589a94667a..97ca39ce5a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceTest.java @@ -34,6 +34,7 @@ import java.nio.file.Path; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; @@ -52,6 +53,46 @@ class S3ServiceTest { s3Service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), dataRoot, false); } + @Test + void deletingTheLatestVersionPromotesThePreviousVersionsFile() throws IOException { + s3Service.createBucket("promotion-bucket", "us-east-1"); + s3Service.putBucketVersioning("promotion-bucket", "Enabled"); + S3Object first = s3Service.putObject("promotion-bucket", "key", + "first".getBytes(StandardCharsets.UTF_8), "text/plain", Map.of()); + S3Object second = s3Service.putObject("promotion-bucket", "key", + "second".getBytes(StandardCharsets.UTF_8), "text/plain", Map.of()); + + s3Service.deleteObject("promotion-bucket", "key", second.getVersionId()); + + S3Object current = s3Service.getObject("promotion-bucket", "key"); + assertEquals(first.getVersionId(), current.getVersionId()); + assertArrayEquals("first".getBytes(StandardCharsets.UTF_8), current.getData()); + List objectFiles; + try (Stream files = Files.walk(tempDir.resolve("s3"))) { + objectFiles = files.filter(path -> path.getFileName().toString().endsWith(".s3data")).toList(); + } + assertEquals(2, objectFiles.size(), "the promoted version's file and the current file: " + objectFiles); + // Without hard links the promotion copies the file instead, which is still correct. + if (hardLinksSupported(tempDir)) { + assertTrue(Files.isSameFile(objectFiles.get(0), objectFiles.get(1)), + "the current file should be the promoted version's file, linked rather than read and copied"); + } + } + + private static boolean hardLinksSupported(Path dir) throws IOException { + Path probe = Files.createTempFile(dir, "link-probe", null); + Path link = probe.resolveSibling(probe.getFileName() + ".link"); + try { + Files.createLink(link, probe); + return true; + } catch (UnsupportedOperationException | IOException unsupported) { + return false; + } finally { + Files.deleteIfExists(link); + Files.deleteIfExists(probe); + } + } + @Test @org.junit.jupiter.api.condition.DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS) void constructorThrowsWhenDataRootUncreatable() throws Exception {