Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

perf(iam): find sessions across accounts without scanning every session (#3881)

TATSUNO “Taz” Yasuhirodf3ce4a1985b
diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java b/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java
index 1affab8e49..f66bdc95be 100644
--- a/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java
+++ b/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java
@@ -12,6 +12,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -43,6 +44,13 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
private final Instance<RequestContext> requestContextInstance;
private final String defaultAccountId;
+ // Accounts that own keys here, so findAnyAccountEntry does one get per account instead of a scan.
+ // Tracking starts on the first cross-account lookup, so other stores pay one volatile read per write.
+ // Accounts are never removed. A stale one only costs a missed get.
+ private final Set<String> ownerAccounts = ConcurrentHashMap.newKeySet();
+ private volatile boolean trackingOwners;
+ private volatile boolean ownersComplete;
+
public AccountAwareStorageBackend(StorageBackend<String, V> delegate,
Instance<RequestContext> requestContextInstance,
String defaultAccountId) {
@@ -61,7 +69,9 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
@Override
public void put(String key, V value) {
- delegate.put(prefixed(key), value);
+ String account = prefix();
+ delegate.put(account + "/" + key, value);
+ trackOwner(account);
}
@Override
@@ -84,6 +94,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
result = delegate.get(key);
if (result.isPresent()) {
delegate.put(prefixedKey, result.get());
+ trackOwner(prefix());
delegate.delete(key);
}
return result;
@@ -118,6 +129,9 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
@Override
public void load() {
delegate.load();
+ if (trackingOwners) {
+ addOwnersFromKeys();
+ }
}
@Override
@@ -212,6 +226,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
if (!destination.equals(rawKey)) {
if (delegate.get(destination).isEmpty()) {
delegate.put(destination, value.get());
+ trackOwner(accountId);
} else {
LOG.warnv("Legacy storage migration skipped stale value at {0}; destination {1} already exists",
rawKey, destination);
@@ -289,6 +304,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
return Optional.empty();
}
delegate.put(accountId + "/" + key, legacy.get());
+ trackOwner(accountId);
delegate.delete(key);
return legacy;
}
@@ -356,6 +372,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
continue;
}
delegate.put(destinationKey, legacy.get());
+ trackOwner(accountId);
delegate.delete(candidateKey);
return legacy;
}
@@ -364,6 +381,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
public void putForAccount(String accountId, String key, V value) {
delegate.put(accountId + "/" + key, value);
+ trackOwner(accountId);
}
public void deleteForAccount(String accountId, String key) {
@@ -411,33 +429,58 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
* Like {@link #findAnyAccount}, but also reports the owning account so a cross-account
* <em>mutation</em> can write the value back to its owner's partition (via
* {@link #putForAccount}) instead of forking a phantom copy into the caller's partition.
- * The caller's own hit is owned by the current account context; a scanned hit's owner is
- * the account segment of its raw key (or {@code defaultAccountId} for pre-multi-account,
- * un-prefixed data).
+ * The caller's own hit, including un-prefixed data that {@link #get} migrates, is owned by the
+ * current account context; any other hit is owned by the account whose partition holds it.
*/
public Optional<OwnedEntry<V>> findAnyAccountEntry(String key) {
- Optional<V> own = get(key);
+ String callerAccount = prefix();
+ Optional<V> own = delegate.get(callerAccount + "/" + key);
+ // get locks the store on a miss, so call it only when there is legacy data to migrate
+ if (own.isEmpty() && delegate.get(key).isPresent()) {
+ own = get(key);
+ }
if (own.isPresent()) {
- return Optional.of(new OwnedEntry<>(prefix(), own.get()));
+ return Optional.of(new OwnedEntry<>(callerAccount, own.get()));
}
- String suffix = "/" + key;
- for (String rawKey : delegate.keys()) {
- if (rawKey.equals(key)) {
- Optional<V> value = delegate.get(rawKey);
- if (value.isPresent()) {
- return Optional.of(new OwnedEntry<>(defaultAccountId, value.get()));
- }
- } else if (rawKey.endsWith(suffix)) {
- Optional<V> value = delegate.get(rawKey);
- if (value.isPresent()) {
- String account = rawKey.substring(0, rawKey.length() - suffix.length());
- return Optional.of(new OwnedEntry<>(account, value.get()));
- }
+ if (!ownersComplete) {
+ startTrackingOwners();
+ }
+ for (String account : ownerAccounts) {
+ Optional<V> value = delegate.get(account + "/" + key);
+ if (value.isPresent()) {
+ return Optional.of(new OwnedEntry<>(account, value.get()));
}
}
return Optional.empty();
}
+ private void trackOwner(String accountId) {
+ if (trackingOwners && !ownerAccounts.contains(accountId)) {
+ ownerAccounts.add(accountId);
+ }
+ }
+
+ private void startTrackingOwners() {
+ synchronized (ownerAccounts) {
+ if (ownersComplete) {
+ return;
+ }
+ // A write that reads the flag as unset stored its key before the flag was set, so the scan sees it
+ trackingOwners = true;
+ addOwnersFromKeys();
+ ownersComplete = true;
+ }
+ }
+
+ private void addOwnersFromKeys() {
+ for (String rawKey : delegate.keys()) {
+ int slash = rawKey.indexOf('/');
+ if (slash > 0) {
+ ownerAccounts.add(rawKey.substring(0, slash));
+ }
+ }
+ }
+
// ---
/** Returns the account ID that keys are currently being prefixed with. */
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java
index 3bff7656f8..ffcd07c000 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java
@@ -2129,15 +2129,15 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
*
* <p>Sessions are keyed by a globally-unique access key (e.g. {@code ASIA...}) but stored in
* the minting account's namespace. Account routing must resolve the session <em>before</em> the
- * request's account is known, so a normal account-scoped {@code get} would miss it. This scans
- * across all accounts; the access key's global uniqueness keeps the result unambiguous.
+ * request's account is known, so a normal account-scoped {@code get} would miss it. The
+ * lookup spans all accounts; the access key's global uniqueness keeps the result unambiguous.
*/
private Optional<SessionCredential> findSessionAnyAccount(String accessKeyId) {
if (!isTemporaryAccessKey(accessKeyId)) {
return Optional.empty();
}
if (sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {
- return Optional.ofNullable(aware.scanAllAccountsAsMap().get(accessKeyId));
+ return aware.findAnyAccount(accessKeyId);
}
return sessions.get(accessKeyId);
}
diff --git a/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java b/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java
index 8eb475361a..afaffa62ce 100644
--- a/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java
@@ -18,6 +18,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.clearInvocations;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
class AccountAwareStorageBackendTest {
@@ -353,4 +359,141 @@ class AccountAwareStorageBackendTest {
.count();
assertEquals(1, copies, "the entry landed in exactly one account partition, not forked into both");
}
+
+ @Test
+ void findAnyAccountEntryUsesTrackedAccountsWithoutScanning() {
+ InMemoryStorage<String, String> raw = spy(new InMemoryStorage<String, String>());
+ AccountAwareStorageBackend<String> storage =
+ new AccountAwareStorageBackend<>(raw, null, "111111111111");
+ storage.findAnyAccountEntry("MISSING");
+ clearInvocations(raw);
+ storage.putForAccount("222222222222", "ASIAKEY", "session");
+
+ for (int i = 0; i < 2; i++) {
+ AccountAwareStorageBackend.OwnedEntry<String> entry = storage.findAnyAccountEntry("ASIAKEY").orElseThrow();
+ assertEquals("222222222222", entry.account());
+ assertEquals("session", entry.value());
+ }
+ assertTrue(storage.findAnyAccountEntry("MISSING").isEmpty());
+
+ verify(raw, never()).keys();
+ }
+
+ @Test
+ void findAnyAccountEntryScansOnceForLoadedData() {
+ InMemoryStorage<String, String> raw = spy(new InMemoryStorage<String, String>());
+ raw.put("222222222222/ASIAKEY", "session");
+ AccountAwareStorageBackend<String> storage =
+ new AccountAwareStorageBackend<>(raw, null, "111111111111");
+
+ assertEquals("222222222222", storage.findAnyAccountEntry("ASIAKEY").orElseThrow().account());
+ assertEquals("222222222222", storage.findAnyAccountEntry("ASIAKEY").orElseThrow().account());
+
+ verify(raw, times(1)).keys();
+ }
+
+ @Test
+ void writesBeforeTheFirstCrossAccountLookupAreFoundByTheInitialScan() {
+ InMemoryStorage<String, String> raw = spy(new InMemoryStorage<String, String>());
+ AccountAwareStorageBackend<String> storage =
+ new AccountAwareStorageBackend<>(raw, null, "111111111111");
+ storage.putForAccount("222222222222", "ASIAKEY", "session");
+
+ assertEquals("222222222222", storage.findAnyAccountEntry("ASIAKEY").orElseThrow().account());
+ assertEquals("222222222222", storage.findAnyAccountEntry("ASIAKEY").orElseThrow().account());
+
+ verify(raw, times(1)).keys();
+ }
+
+ @Test
+ void writeDuringTheInitialScanIsStillTracked() {
+ InMemoryStorage<String, String> raw = spy(new InMemoryStorage<String, String>());
+ AccountAwareStorageBackend<String> storage =
+ new AccountAwareStorageBackend<>(raw, null, "111111111111");
+ doAnswer(invocation -> {
+ Set<?> seenByScan = Set.copyOf((Set<?>) invocation.callRealMethod());
+ storage.putForAccount("333333333333", "LATE", "value");
+ return seenByScan;
+ }).when(raw).keys();
+
+ assertTrue(storage.findAnyAccountEntry("MISSING").isEmpty());
+
+ assertEquals("333333333333", storage.findAnyAccountEntry("LATE").orElseThrow().account());
+ }
+
+ @Test
+ void loadAfterTheFirstCrossAccountLookupTracksLoadedAccounts() {
+ InMemoryStorage<String, String> raw = spy(new InMemoryStorage<String, String>());
+ AccountAwareStorageBackend<String> storage =
+ new AccountAwareStorageBackend<>(raw, null, "111111111111");
+ doAnswer(invocation -> {
+ raw.put("333333333333/LOADED", "value");
+ return null;
+ }).when(raw).load();
+ storage.findAnyAccountEntry("MISSING");
+
+ storage.load();
+
+ assertEquals("333333333333", storage.findAnyAccountEntry("LOADED").orElseThrow().account());
+ }
+
+ @Test
+ void legacyMigrationIntoAnotherAccountIsTracked() {
+ InMemoryStorage<String, String> raw = new InMemoryStorage<>();
+ AccountAwareStorageBackend<String> storage =
+ new AccountAwareStorageBackend<>(raw, null, "111111111111");
+ storage.findAnyAccountEntry("MISSING");
+ raw.put("LEGACY", "value");
+
+ storage.getForAccountMigratingLegacy("222222222222", "LEGACY", value -> true);
+
+ assertEquals("222222222222", storage.findAnyAccountEntry("LEGACY").orElseThrow().account());
+ }
+
+ @Test
+ void deleteAndClearHideEntriesFromCrossAccountLookup() {
+ InMemoryStorage<String, String> raw = new InMemoryStorage<>();
+ AccountAwareStorageBackend<String> storage =
+ new AccountAwareStorageBackend<>(raw, null, "111111111111");
+ storage.findAnyAccountEntry("MISSING");
+ storage.putForAccount("222222222222", "ASIAKEY", "session");
+ storage.deleteForAccount("222222222222", "ASIAKEY");
+ assertTrue(storage.findAnyAccountEntry("ASIAKEY").isEmpty());
+
+ storage.putForAccount("222222222222", "ASIAKEY", "session");
+ storage.clear();
+ assertTrue(storage.findAnyAccountEntry("ASIAKEY").isEmpty());
+ storage.putForAccount("333333333333", "ASIAKEY", "fresh");
+ assertEquals("333333333333", storage.findAnyAccountEntry("ASIAKEY").orElseThrow().account());
+ }
+
+ @Test
+ void findAnyAccountEntryMigratesUnprefixedDataIntoTheCallerAccount() {
+ InMemoryStorage<String, String> raw = new InMemoryStorage<>();
+ raw.put("LEGACY", "value");
+ AccountAwareStorageBackend<String> storage =
+ new AccountAwareStorageBackend<>(raw, null, "111111111111");
+
+ AccountAwareStorageBackend.OwnedEntry<String> entry = storage.findAnyAccountEntry("LEGACY").orElseThrow();
+
+ assertEquals("111111111111", entry.account());
+ assertEquals("value", entry.value());
+ assertEquals(Optional.of("value"), raw.get("111111111111/LEGACY"));
+ assertTrue(raw.get("LEGACY").isEmpty());
+ }
+
+ @Test
+ void crossAccountLookupDoesNotWaitForTheStoreMonitor() throws Exception {
+ AccountAwareStorageBackend<String> storage = AccountAwareStorageBackend.inMemory("111111111111");
+ storage.putForAccount("222222222222", "ASIAKEY", "session");
+ ExecutorService pool = Executors.newSingleThreadExecutor();
+ try {
+ synchronized (storage) {
+ Future<Optional<String>> lookup = pool.submit(() -> storage.findAnyAccount("ASIAKEY"));
+ assertEquals(Optional.of("session"), lookup.get(5, TimeUnit.SECONDS));
+ }
+ } finally {
+ pool.shutdownNow();
+ }
+ }
}