From 1a9e611efcf629d85bc413b89c5680c9f99a6709 Mon Sep 17 00:00:00 2001 From: Qian Wang <38326034+Qian1900@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:14:54 -0800 Subject: [PATCH 1/3] Rebuild each account once when adding a batch of containers AccountInfoMap.addOrUpdateContainers rebuilt the parent Account once per container. Each rebuild copies and validates every container already in the account, so loading N containers into one account is O(N^2). On a full sync (service startup) with ~64k containers this took ~220 s on a single thread. Group the containers by parent account and rebuild each account once. A later update of the same container in the batch still wins. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../github/ambry/account/AccountInfoMap.java | 20 +++++- .../ambry/account/AccountInfoMapTest.java | 64 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java b/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java index c1e42f31c8..c4fcd1ad76 100644 --- a/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java +++ b/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java @@ -16,9 +16,11 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.json.JSONObject; @@ -290,11 +292,27 @@ void addOrUpdateAccounts(Collection accounts) { /** * Adds or updates the {@code AccountInfoMap} with the input {@link Collection} of {@link Container}s. + * Containers are grouped by parent account so that each {@link Account} is rebuilt once. Rebuilding an account copies + * and validates all of its containers, so rebuilding once per container is quadratic in the account's container + * count, which made the initial full sync of large accounts take minutes. * @param containers collection of {@link Container}s to be added. + * @throws IllegalArgumentException if the parent {@link Account} of any container doesn't exist. */ void addOrUpdateContainers(Collection containers) { + Map> containersByAccountId = new LinkedHashMap<>(); for (Container container : containers) { - addOrUpdateContainer(container.getParentAccountId(), container); + containersByAccountId.computeIfAbsent(container.getParentAccountId(), k -> new ArrayList<>()).add(container); + } + for (Map.Entry> entry : containersByAccountId.entrySet()) { + Account account = idToAccountMap.get(entry.getKey()); + if (account == null) { + throw new IllegalArgumentException("Account with ID " + entry.getKey() + "doesn't exist"); + } + AccountBuilder accountBuilder = new AccountBuilder(account); + entry.getValue().forEach(accountBuilder::addOrUpdateContainer); + account = accountBuilder.build(); + idToAccountMap.put(account.getId(), account); + nameToAccountMap.put(account.getName(), account); } } diff --git a/ambry-account/src/test/java/com/github/ambry/account/AccountInfoMapTest.java b/ambry-account/src/test/java/com/github/ambry/account/AccountInfoMapTest.java index 05fe473e4b..2fcbbcdc0e 100644 --- a/ambry-account/src/test/java/com/github/ambry/account/AccountInfoMapTest.java +++ b/ambry-account/src/test/java/com/github/ambry/account/AccountInfoMapTest.java @@ -17,6 +17,7 @@ import com.github.ambry.quota.QuotaResourceType; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -78,4 +79,67 @@ public void testRepeatedContainerReplayPreservesSerializableMetadata() throws IO assertEquals(expectedAccounts, BackupFileManager.deserializeAccounts(BackupFileManager.serializeAccounts(infoMap.getAccounts()).array())); } + + /** + * A full sync loads every container at once. Each account must be rebuilt once per batch, not once per container, + * otherwise loading a large account is quadratic and takes minutes. + */ + @Test(timeout = 20_000) + public void testAddOrUpdateContainersLargeAccount() { + Account original = new AccountBuilder((short) 1, "account", Account.AccountStatus.ACTIVE).build(); + AccountInfoMap infoMap = new AccountInfoMap(Collections.singletonList(original)); + int numContainers = 30_000; + List containers = new ArrayList<>(numContainers); + for (short id = 1; id <= numContainers; id++) { + containers.add(new ContainerBuilder(id, "container-" + id, Container.ContainerStatus.ACTIVE, "description", + original.getId()).build()); + } + infoMap.addOrUpdateContainers(containers); + Account updated = infoMap.getAccountById(original.getId()); + assertEquals("All containers should be added", numContainers, updated.getAllContainers().size()); + assertSame("Both indexes should reference the rebuilt account", updated, + infoMap.getAccountByName(original.getName())); + } + + /** + * Containers for several accounts in one batch are applied to the right accounts, and a later update of the same + * container in the batch wins. + */ + @Test + public void testAddOrUpdateContainersMultipleAccounts() { + Container existing = + new ContainerBuilder((short) 1, "existing", Container.ContainerStatus.ACTIVE, "old", (short) 1).build(); + Account account1 = + new AccountBuilder((short) 1, "account1", Account.AccountStatus.ACTIVE).addOrUpdateContainer(existing).build(); + Account account2 = new AccountBuilder((short) 2, "account2", Account.AccountStatus.ACTIVE).build(); + AccountInfoMap infoMap = new AccountInfoMap(Arrays.asList(account1, account2)); + + Container firstUpdate = + new ContainerBuilder((short) 1, "existing", Container.ContainerStatus.ACTIVE, "first", (short) 1).build(); + Container added1 = + new ContainerBuilder((short) 2, "added1", Container.ContainerStatus.ACTIVE, "description", (short) 1).build(); + Container added2 = + new ContainerBuilder((short) 1, "added2", Container.ContainerStatus.ACTIVE, "description", (short) 2).build(); + Container lastUpdate = + new ContainerBuilder((short) 1, "existing", Container.ContainerStatus.INACTIVE, "last", (short) 1).build(); + infoMap.addOrUpdateContainers(Arrays.asList(firstUpdate, added1, added2, lastUpdate)); + + Account updated1 = infoMap.getAccountById((short) 1); + assertEquals(2, updated1.getAllContainers().size()); + assertEquals("Last update of a container should win", lastUpdate, updated1.getContainerById((short) 1)); + assertEquals(added1, updated1.getContainerById((short) 2)); + assertSame(updated1, infoMap.getAccountByName("account1")); + Account updated2 = infoMap.getAccountById((short) 2); + assertEquals(Collections.singletonList(added2), new ArrayList<>(updated2.getAllContainers())); + assertSame(updated2, infoMap.getAccountByName("account2")); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddOrUpdateContainersUnknownAccount() { + AccountInfoMap infoMap = new AccountInfoMap( + Collections.singletonList(new AccountBuilder((short) 1, "account", Account.AccountStatus.ACTIVE).build())); + infoMap.addOrUpdateContainers(Collections.singletonList( + new ContainerBuilder((short) 1, "container", Container.ContainerStatus.ACTIVE, "description", (short) 2) + .build())); + } } From db1f1d463d95913ed3420a7da2d0246852ad7df4 Mon Sep 17 00:00:00 2001 From: Qian Wang <38326034+Qian1900@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:32:43 -0800 Subject: [PATCH 2/3] Make addOrUpdateContainers all-or-nothing Rebuild every affected account before publishing any of them, so a batch with an unknown parent account (or one that fails account validation) leaves the map unchanged instead of partially applied. Exercise migrationConfigs in the large-account test. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../github/ambry/account/AccountInfoMap.java | 8 +++-- .../ambry/account/AccountInfoMapTest.java | 29 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java b/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java index c4fcd1ad76..6e37e8526a 100644 --- a/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java +++ b/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java @@ -294,7 +294,8 @@ void addOrUpdateAccounts(Collection accounts) { * Adds or updates the {@code AccountInfoMap} with the input {@link Collection} of {@link Container}s. * Containers are grouped by parent account so that each {@link Account} is rebuilt once. Rebuilding an account copies * and validates all of its containers, so rebuilding once per container is quadratic in the account's container - * count, which made the initial full sync of large accounts take minutes. + * count, which made the initial full sync of large accounts take minutes. All accounts are rebuilt before any is + * published, so if any container is rejected the map is left unchanged. * @param containers collection of {@link Container}s to be added. * @throws IllegalArgumentException if the parent {@link Account} of any container doesn't exist. */ @@ -303,6 +304,7 @@ void addOrUpdateContainers(Collection containers) { for (Container container : containers) { containersByAccountId.computeIfAbsent(container.getParentAccountId(), k -> new ArrayList<>()).add(container); } + List updatedAccounts = new ArrayList<>(containersByAccountId.size()); for (Map.Entry> entry : containersByAccountId.entrySet()) { Account account = idToAccountMap.get(entry.getKey()); if (account == null) { @@ -310,7 +312,9 @@ void addOrUpdateContainers(Collection containers) { } AccountBuilder accountBuilder = new AccountBuilder(account); entry.getValue().forEach(accountBuilder::addOrUpdateContainer); - account = accountBuilder.build(); + updatedAccounts.add(accountBuilder.build()); + } + for (Account account : updatedAccounts) { idToAccountMap.put(account.getId(), account); nameToAccountMap.put(account.getName(), account); } diff --git a/ambry-account/src/test/java/com/github/ambry/account/AccountInfoMapTest.java b/ambry-account/src/test/java/com/github/ambry/account/AccountInfoMapTest.java index 2fcbbcdc0e..089c49eb2f 100644 --- a/ambry-account/src/test/java/com/github/ambry/account/AccountInfoMapTest.java +++ b/ambry-account/src/test/java/com/github/ambry/account/AccountInfoMapTest.java @@ -86,7 +86,9 @@ public void testRepeatedContainerReplayPreservesSerializableMetadata() throws IO */ @Test(timeout = 20_000) public void testAddOrUpdateContainersLargeAccount() { - Account original = new AccountBuilder((short) 1, "account", Account.AccountStatus.ACTIVE).build(); + Account original = + new AccountBuilder((short) 1, "account", Account.AccountStatus.ACTIVE).migrationConfigs(migrationConfigs) + .build(); AccountInfoMap infoMap = new AccountInfoMap(Collections.singletonList(original)); int numContainers = 30_000; List containers = new ArrayList<>(numContainers); @@ -97,6 +99,7 @@ public void testAddOrUpdateContainersLargeAccount() { infoMap.addOrUpdateContainers(containers); Account updated = infoMap.getAccountById(original.getId()); assertEquals("All containers should be added", numContainers, updated.getAllContainers().size()); + assertEquals("Migration configs should be preserved", migrationConfigs, updated.getMigrationConfigs()); assertSame("Both indexes should reference the rebuilt account", updated, infoMap.getAccountByName(original.getName())); } @@ -134,12 +137,24 @@ public void testAddOrUpdateContainersMultipleAccounts() { assertSame(updated2, infoMap.getAccountByName("account2")); } - @Test(expected = IllegalArgumentException.class) + /** + * If any container's parent account is unknown, the whole batch is rejected and no account is changed. + */ + @Test public void testAddOrUpdateContainersUnknownAccount() { - AccountInfoMap infoMap = new AccountInfoMap( - Collections.singletonList(new AccountBuilder((short) 1, "account", Account.AccountStatus.ACTIVE).build())); - infoMap.addOrUpdateContainers(Collections.singletonList( - new ContainerBuilder((short) 1, "container", Container.ContainerStatus.ACTIVE, "description", (short) 2) - .build())); + Account original = new AccountBuilder((short) 1, "account", Account.AccountStatus.ACTIVE).build(); + AccountInfoMap infoMap = new AccountInfoMap(Collections.singletonList(original)); + Container valid = + new ContainerBuilder((short) 1, "valid", Container.ContainerStatus.ACTIVE, "description", (short) 1).build(); + Container orphan = + new ContainerBuilder((short) 1, "orphan", Container.ContainerStatus.ACTIVE, "description", (short) 2).build(); + try { + infoMap.addOrUpdateContainers(Arrays.asList(valid, orphan)); + fail("Should fail for a container whose parent account doesn't exist"); + } catch (IllegalArgumentException e) { + // expected + } + assertSame("Account should be unchanged", original, infoMap.getAccountById((short) 1)); + assertSame(original, infoMap.getAccountByName("account")); } } From bc3bd332d31a6f23858cef39033d5b064a0ac266 Mon Sep 17 00:00:00 2001 From: Qian Wang <38326034+Qian1900@users.noreply.github.com> Date: Thu, 3 Dec 2026 15:03:28 -0800 Subject: [PATCH 3/3] Add missing space in unknown-account error message Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../main/java/com/github/ambry/account/AccountInfoMap.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java b/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java index 6e37e8526a..6a119e0a3b 100644 --- a/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java +++ b/ambry-account/src/main/java/com/github/ambry/account/AccountInfoMap.java @@ -308,7 +308,7 @@ void addOrUpdateContainers(Collection containers) { for (Map.Entry> entry : containersByAccountId.entrySet()) { Account account = idToAccountMap.get(entry.getKey()); if (account == null) { - throw new IllegalArgumentException("Account with ID " + entry.getKey() + "doesn't exist"); + throw new IllegalArgumentException("Account with ID " + entry.getKey() + " doesn't exist"); } AccountBuilder accountBuilder = new AccountBuilder(account); entry.getValue().forEach(accountBuilder::addOrUpdateContainer); @@ -329,7 +329,7 @@ void addOrUpdateContainers(Collection containers) { void addOrUpdateContainer(short accountId, Container container) { Account account = idToAccountMap.get(accountId); if (account == null) { - throw new IllegalArgumentException("Account with ID " + accountId + "doesn't exist"); + throw new IllegalArgumentException("Account with ID " + accountId + " doesn't exist"); } AccountBuilder accountBuilder = new AccountBuilder(account).addOrUpdateContainer(container); account = accountBuilder.build();