mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -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
+1
-3
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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