fix(s3): journal the object index under persistent storage (#4642)

* fix(s3): journal the object index under persistent storage

Under persistent mode every PutObject, CopyObject and DeleteObject
rewrote s3-objects.json in full, and that file holds the metadata of
every object in every bucket, so the cost of one write grew with the
total object count (two rewrites per write on a versioned bucket).

Create the object store with WriteProfile.APPEND_HEAVY, as CloudWatch
Logs events already are: a change is appended to s3-objects.wal and the
JSON file is rewritten on the WAL compaction interval and at shutdown.
An existing s3-objects.json is read as the first snapshot.

* fix(s3): journal both index entries of a current version's metadata update

A versioned put indexes the new object under its version key and the latest
key. Tagging, retention, legal hold, ACL and the SSE-C fields set on multipart
completion wrote back only one of the two. With the object index journaled,
each entry replays on its own, so a restart before compaction brought the
other entry back with the old metadata.

Write both entries when the update targets the current version. The restart
tests now shut down every factory and replay a copy of the stopped process's
directory, so no two writers share a journal.

* test(lambda): stub the WriteProfile overload for S3 stores in the stream poller test

* fix(s3): write object metadata under the bucket lock and the owner account

A metadata update checked whether the latest key still pointed at the object
and then wrote it without holding the bucket lock that puts and deletes take,
so a concurrent delete marker or overwrite could be replaced by the object it
had superseded. The check and both writes now run under that lock, and the
latest key is written only while it still refers to the updated object.

Multipart completion wrote the SSE-C fields back through the caller's account
partition, so with the global bucket namespace a cross-account completion
left a shadow latest entry and version in the caller's partition. It now goes
through the same helper with the bucket owner's account.
This commit is contained in:
Abhigya authored and GitHub committed 2026-09-29 19:18:51 -05:00
1 parent 600797db17
commit cea6d3e99f
9 files changed
+316 -25

No files matched your search

@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.core.storage.WriteProfile;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
import io.github.hectorvent.floci.services.iam.IamService;
@@ -174,7 +175,7 @@ public class S3Service implements Resettable, ResourceProvider {
}),
storageFactory.create("s3", "s3-objects.json",
new TypeReference<Map<String, S3Object>>() {
}),
}, WriteProfile.APPEND_HEAVY),
storageFactory.create("s3", "s3-annotations.json",
new TypeReference<Map<String, ObjectAnnotation>>() {
}),
@@ -2083,7 +2084,7 @@ public class S3Service implements Resettable, ResourceProvider {
getStoredObjectEntry(bucketName, key, null);
S3Object obj = ownedObject.value();
obj.setTags(tags != null ? tags : new java.util.HashMap<>());
putObjectForAccount(ownedObject.account(), objectKey(bucketName, key), obj);
putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);
LOG.debugv("Put tags on object: {0}/{1}", bucketName, key);
}
@@ -2102,7 +2103,7 @@ public class S3Service implements Resettable, ResourceProvider {
getStoredObjectEntry(bucketName, key, null);
S3Object obj = ownedObject.value();
obj.setTags(new java.util.HashMap<>());
putObjectForAccount(ownedObject.account(), objectKey(bucketName, key), obj);
putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);
LOG.debugv("Deleted tags from object: {0}/{1}", bucketName, key);
}
@@ -3098,9 +3099,6 @@ public class S3Service implements Resettable, ResourceProvider {
String mode, Instant retainUntil, boolean bypassGovernance) {
AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =
getStoredObjectEntry(bucketName, key, versionId);
String storeKey = versionId != null
? versionedKey(bucketName, key, versionId)
: objectKey(bucketName, key);
S3Object obj = ownedObject.value();
boolean activeComplianceRetention = "COMPLIANCE".equals(obj.getObjectLockMode())
@@ -3135,7 +3133,7 @@ public class S3Service implements Resettable, ResourceProvider {
obj.setObjectLockMode(mode);
obj.setRetainUntilDate(retainUntil);
putObjectForAccount(ownedObject.account(), storeKey, obj);
putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);
LOG.debugv("Set retention on {0}/{1}: mode={2}, until={3}", bucketName, key, mode, retainUntil);
}
@@ -3146,12 +3144,9 @@ public class S3Service implements Resettable, ResourceProvider {
public void putObjectLegalHold(String bucketName, String key, String versionId, String status) {
AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =
getStoredObjectEntry(bucketName, key, versionId);
String storeKey = versionId != null
? versionedKey(bucketName, key, versionId)
: objectKey(bucketName, key);
S3Object obj = ownedObject.value();
obj.setLegalHoldStatus(status);
putObjectForAccount(ownedObject.account(), storeKey, obj);
putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);
LOG.debugv("Set legal hold on {0}/{1}: {2}", bucketName, key, status);
}
@@ -3425,7 +3420,11 @@ public class S3Service implements Resettable, ResourceProvider {
object.setSseCustomerAlgorithm(upload.getSseCustomerAlgorithm());
object.setSseCustomerKeyMd5(upload.getSseCustomerKeyMd5());
}
objectStore.put(objectKey(bucket, key), object);
String bucketOwnerAccount = resolveBucketEntry(bucket)
.map(AccountAwareStorageBackend.OwnedEntry::account)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
putObjectMetadataForAccount(bucketOwnerAccount, bucket, key, object);
// Cleanup
cleanupMultipart(uploadId);
@@ -3782,8 +3781,7 @@ public class S3Service implements Resettable, ResourceProvider {
String newAcl = resolvedAcl != null ? resolvedAcl : (bodyAcl.isBlank() ? null : bodyAcl);
rejectPublicAclWhenBlocked(bucketName, newAcl);
obj.setAcl(newAcl);
String storeKey = (versionId != null) ? versionedKey(bucketName, key, versionId) : objectKey(bucketName, key);
putObjectForAccount(ownedObject.account(), storeKey, obj);
putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);
}
/**
@@ -4865,6 +4863,34 @@ public class S3Service implements Resettable, ResourceProvider {
objectStore.put(storeKey, object);
}
/**
* Persists a metadata change to one stored object. The current version of a versioned object
* is indexed under both its version key and the latest key, so both entries are written: the
* journaled object store replays each entry on its own, and writing one would bring the
* other back with the old metadata after a restart. The latest key is written only while it
* still refers to this object, checked under the bucket lock that puts and deletes hold, so a
* concurrent overwrite or delete marker is never replaced by the object it superseded.
*/
private void putObjectMetadataForAccount(String accountId, String bucketName, String key, S3Object object) {
Bucket bucket = resolveBucket(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
synchronized (bucket) {
String latestKey = objectKey(bucketName, key);
String versionId = object.getVersionId();
S3Object latest = resolveObjectForAccount(accountId, latestKey).orElse(null);
boolean isCurrent = latest == object
|| (versionId != null && latest != null && !latest.isDeleteMarker()
&& versionId.equals(latest.getVersionId()));
if (versionId != null) {
putObjectForAccount(accountId, versionedKey(bucketName, key, versionId), object);
}
if (isCurrent) {
putObjectForAccount(accountId, latestKey, object);
}
}
}
private String objectKey(String bucketName, String key) {
return bucketName + "/" + key;
}