fix(storage): preserve account namespaces during legacy fallback (#8251)

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-13 23:39:06 -04:00
1 parent 486f0e4604
commit e201709363
5 files changed
+430 -5

No files matched your search

@@ -32,7 +32,8 @@ import java.util.stream.Collectors;
*
* <p>Backward compatibility: on a {@link #get} miss for the prefixed key, the un-prefixed
* key is tried and the entry is migrated on read. This covers existing persistent/WAL data
* created before multi-account support was added.
* created before multi-account support was added. A key already carrying an account prefix
* is only read inside the caller's explicit namespace, never as a raw legacy key.
*/
public class AccountAwareStorageBackend<V> implements StorageBackend<String, V> {
@@ -98,7 +99,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
public Optional<V> get(String key) {
String prefixedKey = prefixed(key);
Optional<V> result = delegate.get(prefixedKey);
if (result.isPresent()) {
if (result.isPresent() || hasAccountPrefix(key)) {
return result;
}
// Backward-compat: try un-prefixed key (pre-multi-account data) and migrate on read. The
@@ -349,12 +350,13 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
*
* <p>The caller-supplied predicate must establish that the legacy value belongs to the
* destination account. This opt-in path avoids {@link #get(String)}'s request-context fallback,
* which cannot validate ownership before migrating old data.
* which cannot validate ownership before migrating old data. Account-prefixed logical
* keys are only eligible for the explicit scoped lookup, not the raw legacy fallback.
*/
public synchronized Optional<V> getForAccountMigratingLegacy(
String accountId, String key, Predicate<V> legacyOwner) {
Optional<V> scoped = getForAccount(accountId, key);
if (scoped.isPresent()) {
if (scoped.isPresent() || hasAccountPrefix(key)) {
return scoped;
}
Optional<V> legacy = delegate.get(key).filter(legacyOwner);
@@ -661,6 +663,11 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
return prefix();
}
/** The configured owner of data written before account namespaces were introduced. */
public String defaultAccountId() {
return defaultAccountId;
}
private RequestContext requestContext() {
return requestContextProxy.get();
}
@@ -7773,6 +7773,11 @@ public class S3Service implements Resettable, ResourceProvider {
* {@code globalBucketNamespace} is enabled, otherwise account-scoped.
*/
private Optional<S3Object> resolveObject(String storeKey) {
String numericBucket = numericBucketName(storeKey);
if (numericBucket != null) {
return resolveBucketEntry(numericBucket)
.flatMap(bucket -> resolveObjectForAccount(bucket.account(), storeKey));
}
if (globalBucketNamespace && objectStore instanceof AccountAwareStorageBackend<?> aware) {
@SuppressWarnings("unchecked")
AccountAwareStorageBackend<S3Object> typed = (AccountAwareStorageBackend<S3Object>) aware;
@@ -7786,11 +7791,46 @@ public class S3Service implements Resettable, ResourceProvider {
@SuppressWarnings("unchecked")
AccountAwareStorageBackend<S3Object> typed = (AccountAwareStorageBackend<S3Object>) aware;
return typed.getForAccount(accountId, storeKey)
.or(() -> typed.getLegacyForAccount(accountId, storeKey));
.or(() -> typed.getLegacyForAccount(accountId, storeKey))
.or(() -> migrateNumericBucketObject(typed, accountId, storeKey));
}
return objectStore.get(storeKey);
}
private Optional<S3Object> migrateNumericBucketObject(
AccountAwareStorageBackend<S3Object> objects, String accountId, String storeKey) {
String bucketName = numericBucketName(storeKey);
if (bucketName == null || !accountId.equals(objects.defaultAccountId())) {
return Optional.empty();
}
AccountAwareStorageBackend.OwnedEntry<Bucket> bucket = resolveBucketEntry(bucketName).orElse(null);
if (bucket == null || !accountId.equals(bucket.account())
|| !bucketName.equals(bucket.value().getName())) {
return Optional.empty();
}
synchronized (bucket.value()) {
// A numeric bucket can look like an account prefix. Only the stored S3 identity
// distinguishes its legacy object from another account's unrelated scoped entry.
return objects.getForAccountMigratingLegacyKeys(accountId, storeKey, List.of(),
object -> matchesStoredObjectKey(bucketName, storeKey, object));
}
}
private static String numericBucketName(String storeKey) {
if (!AccountAwareStorageBackend.hasAccountPrefix(storeKey)) {
return null;
}
return storeKey.substring(0, 12);
}
private boolean matchesStoredObjectKey(String bucketName, String storeKey, S3Object object) {
if (!bucketName.equals(object.getBucketName()) || object.getKey() == null) {
return false;
}
return storeKey.equals(objectKey(bucketName, object.getKey()))
|| storeKey.equals(versionedKey(bucketName, object.getKey(), storedVersionId(object)));
}
private void putObjectForAccount(String accountId, String storeKey, S3Object object) {
if (objectStore instanceof AccountAwareStorageBackend<?> aware) {
@SuppressWarnings("unchecked")