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..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 @@ -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,31 @@ 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. 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. */ 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); + } + List updatedAccounts = new ArrayList<>(containersByAccountId.size()); + 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); + updatedAccounts.add(accountBuilder.build()); + } + for (Account account : updatedAccounts) { + idToAccountMap.put(account.getId(), account); + nameToAccountMap.put(account.getName(), account); } } @@ -307,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(); 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..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 @@ -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,82 @@ 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).migrationConfigs(migrationConfigs) + .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()); + assertEquals("Migration configs should be preserved", migrationConfigs, updated.getMigrationConfigs()); + 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")); + } + + /** + * If any container's parent account is unknown, the whole batch is rejected and no account is changed. + */ + @Test + public void testAddOrUpdateContainersUnknownAccount() { + 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")); + } }