Skip to content

optimize: avoid redundant DB call when account matches calling account#11403

Closed
shwstppr wants to merge 2 commits into
apache:mainfrom
shapeblue:reduce-account-lookups
Closed

optimize: avoid redundant DB call when account matches calling account#11403
shwstppr wants to merge 2 commits into
apache:mainfrom
shapeblue:reduce-account-lookups

Conversation

@shwstppr
Copy link
Copy Markdown
Contributor

@shwstppr shwstppr commented Aug 6, 2025

Description

Use Account object from call context when possible

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • build/CI
  • test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

How did you try to break this feature and the system with this change?

Use Account object from call context when possible

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 6, 2025

Codecov Report

❌ Patch coverage is 33.33333% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 17.35%. Comparing base (d7b7bd5) to head (095a69e).
⚠️ Report is 666 commits behind head on main.

Files with missing lines Patch % Lines
...src/main/java/com/cloud/event/UsageEventUtils.java 0.00% 3 Missing ⚠️
...rc/main/java/com/cloud/event/ActionEventUtils.java 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #11403      +/-   ##
============================================
- Coverage     17.35%   17.35%   -0.01%     
+ Complexity    15231    15229       -2     
============================================
  Files          5885     5885              
  Lines        525611   525630      +19     
  Branches      64160    64164       +4     
============================================
- Hits          91224    91217       -7     
- Misses       424090   424115      +25     
- Partials      10297    10298       +1     
Flag Coverage Δ
uitests 3.63% <ø> (ø)
unittests 18.39% <33.33%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

@DaanHoogland DaanHoogland added this to the 4.21.1 milestone Aug 6, 2025
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
@shwstppr
Copy link
Copy Markdown
Contributor Author

shwstppr commented Aug 8, 2025

@blueorangutan package

@blueorangutan
Copy link
Copy Markdown

@shwstppr a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.

@blueorangutan
Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ debian ✔️ suse15. SL-JID 14579

@sureshanaparti sureshanaparti modified the milestones: 4.21.1, 4.22.0 Aug 8, 2025
@shwstppr
Copy link
Copy Markdown
Contributor Author

shwstppr commented Aug 8, 2025

@blueorangutan test

@blueorangutan
Copy link
Copy Markdown

@shwstppr a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@blueorangutan
Copy link
Copy Markdown

[SF] Trillian test result (tid-14047)
Environment: kvm-ol8 (x2), Advanced Networking with Mgmt server ol8
Total time taken: 49128 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr11403-t14047-kvm-ol8.zip
Smoke tests completed. 146 look OK, 0 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes event publishing by avoiding redundant database calls to fetch account objects when the account ID matches the calling account ID from CallContext. This improves performance by reusing the already-loaded account object from the call context instead of making an unnecessary database query.

Changes:

  • Added conditional logic to retrieve account from CallContext when accountId matches the calling account
  • Updated test setup to register CallContext once in setup/teardown instead of per-test
  • Removed unnecessary mock for accountDao.findById in tests since the optimization bypasses this call

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
server/src/main/java/com/cloud/event/ActionEventUtils.java Added optimization to use CallContext.current().getCallingAccount() when accountId matches calling account ID
engine/components-api/src/main/java/com/cloud/event/UsageEventUtils.java Added same optimization for usage event publishing
server/src/test/java/com/cloud/event/ActionEventUtilsTest.java Moved CallContext registration to setup/teardown and removed accountDao mock
server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java Removed accountDao mock but introduced duplicate CallContext registration bug

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -175,8 +175,6 @@ public EventVO answer(InvocationOnMock invocation) throws Throwable {
user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone",
UUID.randomUUID().toString(), User.Source.UNKNOWN);
CallContext.register(user, account);
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

CallContext is being registered twice - once here in setupCommonMocks() and once in the setup() method at line 143. Since setup() calls setupCommonMocks(), this results in duplicate registration. Remove this line and keep only the registration in the setup() method to maintain consistency with the ActionEventUtilsTest pattern.

Suggested change
CallContext.register(user, account);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants