Skip to content

Batch network lookups per NIC in NetworkOrchestrator to eliminate N+1 queries during migration - #13354

Open
vishesh92 wants to merge 1 commit into
apache:4.22from
shapeblue:batch-network-lookups
Open

Batch network lookups per NIC in NetworkOrchestrator to eliminate N+1 queries during migration#13354
vishesh92 wants to merge 1 commit into
apache:4.22from
shapeblue:batch-network-lookups

Conversation

@vishesh92

Copy link
Copy Markdown
Member

Description

Three methods called _networksDao.findById(nic.getNetworkId()) per NIC in a loop: setHypervisorHostname, prepareNicForMigration, prepareAllNicsForMigration. Now batch-loads all networks for a VM's NICs in a single WHERE id IN (...) query.

  • Add NetworkDao.listByIds(List) for batch ID lookup
  • Add getNetworkMapForNics helper returning Map<Long, NetworkVO>
  • Replace per-NIC findById with map lookup at 3 sites
  • Add null guard for deleted networks (pre-existing NPE risk on all 3 sites)

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?

@vishesh92

Copy link
Copy Markdown
Member Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@vishesh92 a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

@codecov

codecov Bot commented Jun 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 40 lines in your changes missing coverage. Please review.
✅ Project coverage is 19.41%. Comparing base (a951ac6) to head (7acfc1a).
⚠️ Report is 78 commits behind head on 4.22.

Files with missing lines Patch % Lines
...tack/engine/orchestration/NetworkOrchestrator.java 0.00% 30 Missing ⚠️
...ain/java/com/cloud/network/dao/NetworkDaoImpl.java 0.00% 10 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               4.22   #13354      +/-   ##
============================================
+ Coverage     17.67%   19.41%   +1.74%     
- Complexity    15797    19155    +3358     
============================================
  Files          5923     6284     +361     
  Lines        533349   564880   +31531     
  Branches      65248    68921    +3673     
============================================
+ Hits          94253   109685   +15432     
- Misses       428437   443294   +14857     
- Partials      10659    11901    +1242     
Flag Coverage Δ
uitests 3.46% <ø> (-0.24%) ⬇️
unittests 20.67% <0.00%> (+1.92%) ⬆️

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

☔ 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.

@blueorangutan

Copy link
Copy Markdown

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

@vishesh92

Copy link
Copy Markdown
Member Author

@blueorangutan test

@blueorangutan

Copy link
Copy Markdown

@vishesh92 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-16258)
Environment: kvm-ol8 (x2), zone: Advanced Networking with Mgmt server ol8
Total time taken: 49633 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr13354-t16258-kvm-ol8.zip
Smoke tests completed. 151 look OK, 0 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 reduces N+1 database lookups in NetworkOrchestrator during VM migration by batch-loading NetworkVO records for a VM’s NICs (via a new NetworkDao.listByIds method) and then using an in-memory map for per-NIC network access, with added null-guards when a referenced network no longer exists.

Changes:

  • Added NetworkDao.listByIds(List<Long>) and implemented it in NetworkDaoImpl using an IN search criteria.
  • Introduced a batchLoadNetworksForNics helper in NetworkOrchestrator and replaced per-NIC findById calls at three migration-related call sites.
  • Updated the test mock MockNetworkDaoImpl to implement the new DAO method.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
server/src/test/java/com/cloud/vpc/dao/MockNetworkDaoImpl.java Adds listByIds to satisfy the updated NetworkDao interface.
engine/schema/src/main/java/com/cloud/network/dao/NetworkDaoImpl.java Implements batch network lookup via id IN (...).
engine/schema/src/main/java/com/cloud/network/dao/NetworkDao.java Extends the DAO contract with listByIds(List<Long>).
engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java Uses a batch-loaded map to avoid per-NIC network DAO calls during migration prep/hostname update.

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

Comment on lines +284 to +287
@Override
public List<NetworkVO> listByIds(List<Long> ids) {
return null;
}
Comment on lines +2088 to +2100
private Map<Long, NetworkVO> batchLoadNetworksForNics(List<NicVO> nics) {
List<Long> networkIds = new ArrayList<>(nics.size());
for (NicVO nic : nics) {
networkIds.add(nic.getNetworkId());
}
Map<Long, NetworkVO> result = new HashMap<>(networkIds.size());
if (!networkIds.isEmpty()) {
for (NetworkVO network : _networksDao.listByIds(networkIds)) {
result.put(network.getId(), network);
}
}
return result;
}
Comment on lines +567 to +576
public List<NetworkVO> listByIds(List<Long> ids) {
if (ids == null || ids.isEmpty()) {
return new ArrayList<>();
}
SearchBuilder<NetworkVO> sb = createSearchBuilder();
sb.and("id", sb.entity().getId(), Op.IN);
sb.done();
SearchCriteria<NetworkVO> sc = sb.create();
sc.setParameters("id", ids.toArray());
return listBy(sc);
@sureshanaparti
sureshanaparti marked this pull request as ready for review July 7, 2026 10:44
@sureshanaparti
sureshanaparti requested a review from nvazquez July 7, 2026 10:46

@nvazquez nvazquez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @vishesh92 - can you please rebase to branch 4.22 and check if Copilot comments are applicable?

… queries during migration

Three methods called _networksDao.findById(nic.getNetworkId()) per NIC in a loop:
setHypervisorHostname, prepareNicForMigration, prepareAllNicsForMigration.
Now batch-loads all networks for a VM's NICs in a single WHERE id IN (...) query.

- Add NetworkDao.listByIds(List<Long>) for batch ID lookup
- Add getNetworkMapForNics helper returning Map<Long, NetworkVO>
- Replace per-NIC findById with map lookup at 3 sites
- Add null guard for deleted networks (pre-existing NPE risk on all 3 sites)
- Site 2 (prepare) left unchanged — findById only on error path, not N+1
@vishesh92
vishesh92 force-pushed the batch-network-lookups branch from 81ca4c9 to 7acfc1a Compare July 7, 2026 12:02
@vishesh92
vishesh92 changed the base branch from main to 4.22 July 7, 2026 12:03
@DaanHoogland DaanHoogland moved this from Backlog to Ready in CloudStack Testing Sep 1, 2026
@abh1sar abh1sar self-assigned this Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

9 participants