Skip to content

Rebuild each account once when adding a batch of containers - #3324

Merged
mayur040993 merged 3 commits into
linkedin:masterfrom
Qian1900:qianwang/account-cache-batch-container-update
Dec 3, 2026
Merged

mayur040993 merged 3 commits into
linkedin:masterfrom
Qian1900:qianwang/account-cache-batch-container-update

Conversation

@Qian1900

@Qian1900 Qian1900 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

AccountInfoMap.addOrUpdateContainers rebuilt the parent Account once per container. Each rebuild copies the account's existing containers into an AccountBuilder, iterates them in build(), and re-validates them in the Account constructor. Adding N containers to one account was therefore O(N²).

This matters on a full sync, which CachedAccountService does at every service start (lastModifiedTime = 0). In a LinkedIn frontend with 805 accounts and 64,186 containers, the MySQL fetch took ~1 s and the cache update took ~222 s on the main thread. That is about a third of the frontend's ~10 min startup.

This change groups the containers by parent account and rebuilds each account once.

  • Last write wins: a later update of the same container id in the batch still wins, because AccountBuilder.addOrUpdateContainer keys by id and LinkedHashMap keeps input order.
  • Validation: duplicate name/id checks run once on the final state, so any batch that passed before still passes.
  • All-or-nothing: every affected account is rebuilt before any is published. If a container's parent account is unknown, or an account fails validation, the method throws IllegalArgumentException and the map is unchanged. Before, the batch was partially applied up to the failing container.

The only caller is CachedAccountService.updateContainersInCache, which holds the infoMapLock write lock.

Testing Done

  • New AccountInfoMapTest.testAddOrUpdateContainersLargeAccount adds 30,000 containers to one account with a 20 s timeout. It passes in ~0.04 s with this change and times out on master.
  • New tests cover batches spanning several accounts, last-write-wins for a repeated container, and an unknown parent account (asserting the map is unchanged).
  • ./gradlew :ambry-account:test --tests AccountInfoMapTest --tests MySqlAccountServiceTest: 32 tests pass.

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>
@codecov-commenter

codecov-commenter commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 15.59%. Comparing base (52ba813) to head (bc3bd33).
⚠️ Report is 436 commits behind head on master.

Files with missing lines Patch % Lines
.../java/com/github/ambry/account/AccountInfoMap.java 0.00% 16 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (52ba813) and HEAD (bc3bd33). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (52ba813) HEAD (bc3bd33)
3 1
Additional details and impacted files
@@              Coverage Diff              @@
##             master    #3324       +/-   ##
=============================================
- Coverage     64.24%   15.59%   -48.65%     
+ Complexity    10398     3256     -7142     
=============================================
  Files           840      941      +101     
  Lines         71755    81376     +9621     
  Branches       8611     9879     +1268     
=============================================
- Hits          46099    12694    -33405     
- Misses        23004    67716    +44712     
+ Partials       2652      966     -1686     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@mayur040993 mayur040993 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mayur040993
mayur040993 merged commit 1e07760 into linkedin:master Dec 3, 2026
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants