mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): promote a version on delete without reading its body (#5064)
Deleting the current version in a versioned bucket made the previous version current by reading its whole file into a byte array and writing it out again as the current file, under the bucket lock. A version over 2 GiB could not be read that way, and because the new current version's metadata was published before the file, the failure left the key pointing at the promoted version while its current file still held the deleted version's bytes. The current file is now a hard link to the promoted version's file, the way a versioned multipart upload already stores its current file, so a version of any size is promoted without being read; in memory both entries share the version's array, as they already did. The body is also put in place before the metadata is published, the order PutObject writes in, so a concurrent GET can no longer pair the promoted version with the deleted version's bytes.
This commit is contained in:
1 parent
fe296fda69
commit
d8728ac4d4
3 files changed
+71
-19
No files matched your search
@@ -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<Path> objectFiles;
|
||||
try (Stream<Path> 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 {
|
||||
|
||||
Reference in new issue
Block a user