Batch network lookups per NIC in NetworkOrchestrator to eliminate N+1 queries during migration#13354
Batch network lookups per NIC in NetworkOrchestrator to eliminate N+1 queries during migration#13354vishesh92 wants to merge 1 commit into
Conversation
|
@blueorangutan package |
|
@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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 18161 |
|
@blueorangutan test |
|
@vishesh92 a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests |
|
[SF] Trillian test result (tid-16258)
|
There was a problem hiding this comment.
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 inNetworkDaoImplusing anINsearch criteria. - Introduced a
batchLoadNetworksForNicshelper inNetworkOrchestratorand replaced per-NICfindByIdcalls at three migration-related call sites. - Updated the test mock
MockNetworkDaoImplto 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.
| @Override | ||
| public List<NetworkVO> listByIds(List<Long> ids) { | ||
| return null; | ||
| } |
| 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; | ||
| } |
| 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); |
nvazquez
left a comment
There was a problem hiding this comment.
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
81ca4c9 to
7acfc1a
Compare
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.
Types of changes
Feature/Enhancement Scale or Bug Severity
Feature/Enhancement Scale
Bug Severity
Screenshots (if appropriate):
How Has This Been Tested?
How did you try to break this feature and the system with this change?