Skip to content

network: look up only the requested vnets in findOverlappingVnets (in cs-4.23.0.0+mdc-3.3.1) - #28

Open
calvix wants to merge 1 commit into
mdc/productionfrom
fix/vnet-overlap-lookup-4.23
Open

calvix wants to merge 1 commit into
mdc/productionfrom
fix/vnet-overlap-lookup-4.23

Conversation

@calvix

@calvix calvix commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Record of the fix shipped as cs-4.23.0.0+mdc-3.3.1 (already on mdc/production as 303c3cc2ff, cherry-pick of this branch's 6a1a2de40b), kept here with the full analysis for the upstream PR. Upstream main still has the same code; DataCenterVnetDaoImpl.java is identical between 4.23.0.0 and apache/cloudstack main (8eaff972a0, 2026-12-01), so the commit applies there unchanged.

Symptom (cloudstack-production, 2026-12-01)

  • createNetwork with an explicit VLAN/VXLAN tag (here: the VMware migrator's per-NIC dry-run sandbox networks, L2 offering, VXLAN physical network) took 30-100 s.
  • While one ran, every network create and delete in the cloud waited: ResourceCountDaoImpl failed with Lock wait timeout exceeded; try restarting transaction (HTTP 530 "Unable to find on DB"), NetworkOrchestrator logged "Failed to delete network", API clients timed out.
  • Side effects: a timed-out create still completed later, so networks were left behind (11 orphans removed by hand), and the resource_count network counters drifted below the real counts (cid-7814 0 vs 3, cid-6284 3 vs 4, cid-6756 1 vs 2) while increments failed or were rolled back.

Root cause

  • NetworkServiceImpl.commitNetwork creates the network in one transaction. Inside it NetworkOrchestrator.createGuestNetwork calls DataCenterVnetDao.findVnet(...) (NetworkOrchestrator.java:2835/2840/2857).
  • DataCenterVnetDaoImpl.findOverlappingVnets then ran listBy(dc[, physical network]) with no vnet condition: it loaded every op_dc_vnet_alloc row of the zone (or physical network) into DataCenterVnetVO objects and searched for the requested vnet in Java.
  • With a VXLAN physical network that is one row per VNI of its range: production has 1,800,000 rows per VXLAN physnet (200000-2000000 and 2000001-3800000, 3.6 M in the zone; the findVnet(dcId, vnet) overload without a physical network scans all of them).
  • The transaction already holds the resource_count rows for the network type of the account, its domains and ROOT (ResourceLimitManagerImpl.lockAccountAndOwnerDomainRows, SELECT ... FOR UPDATE), so every network create/delete in the whole cloud queued behind the scan; waiters past innodb_lock_wait_timeout (50 s) failed.
  • The callers that check a range one number at a time (NetworkServiceImpl.java:443 and :4912) paid the full scan per number.

Evidence

  • Live on the PXC writer at 16:34 UTC: transaction opened at 16:33:10 (the second the migrator created csmig-sandbox-mig-98bf0fb2aeeff382), idle on the DB side (Sleep) while holding the network resource_count rows of cid-6756, its domain and ROOT for ~95 s; two resource_count statements waiting behind it.
  • JVM thread dump taken by a lock watcher at the next Lock wait timeout (MS1, 16:40:42 UTC): thread qtp...-7864, RUNNABLE, in GenericDaoBase.toEntityBean ← DataCenterVnetDaoImpl.findOverlappingVnets ← findVnet ← NetworkOrchestrator.createGuestNetwork ← NetworkServiceImpl$4.doInTransaction (commitNetwork) ← CreateNetworkCmd.execute.
  • async_job: the migrator's DeleteNetworkCmd took 22-51 s in the incident windows (two failed).
  • op_dc_vnet_alloc on production: physical_network_id 203: 1800001 rows, 205: 1800000 rows.

Fix

findOverlappingVnets asks only for the requested vnets: vnet IN (...) with data_center_id and the optional physical_network_id (new search builder VnetsInDcSearch), served by the existing unique index i_op_dc_vnet_alloc__vnet__data_center_id (vnet, physical_network_id, data_center_id) (EXPLAIN on production: range, 2 rows instead of 1.8 M). A tag range is looked up in batches of 1000. CloudStack writes the vnet column as Integer.toString, so the string match finds the same rows as the old numeric comparison (production has no other format). No schema change, no interface change.

Tests

  • New DataCenterVnetDaoImplTest: one query for one vnet, one without a physical network, three for a 2500-vnet range, none for an empty one.
  • mvn -pl engine/schema test on mdc/production 3.3.0 + the commit: 388 tests, 0 failures, Checkstyle clean.
  • Package (git archive of the tag, ubuntu:24.04): file lists identical to 3.3.0; the shipped DataCenterVnetDaoImpl.class has the new search and no NumbersUtil scan.
  • On dev, stable and production after rollout: a tag inside the VXLAN range is still refused ("already being used for dynamic vlan allocation"), a free tag is created in 0.24 s (dev, 290k rows) / 0.30 s (stable, 190k) / 0.31 s (production, 1.8 M; was 30-100 s), a duplicate tag is refused.
  • Production after the rollout (19:12 UTC) under parallel migrator dry-runs: 0 lock timeouts, 0 lock waits, DeleteNetworkCmd 0.1 s average.

For the upstream PR

  • Base: main (same code; applies unchanged). Probably also worth offering for the 4.20/4.22 LTS branches.
  • Upstream issue to file first: "createNetwork with an explicit VLAN/VXLAN tag scans op_dc_vnet_alloc and holds resource_count rows (incl. ROOT) - cloud-wide lock waits with large VXLAN ranges".
  • Mention: the range loops in NetworkServiceImpl (:443, :4912) benefit too; a follow-up could replace them with one range query.

DataCenterVnetDaoImpl.findOverlappingVnets loaded every op_dc_vnet_alloc row
of the zone (or of the physical network) into entity objects and then looked
for the requested vnet in Java. With a VXLAN physical network that is one row
per VNI of its range - 1.8 million rows per physical network in our production
zone - so every createNetwork with an explicit VLAN/VXLAN tag spent tens of
seconds in it (NetworkOrchestrator.createGuestNetwork, inside the
commitNetwork transaction). That transaction already holds the resource_count
rows of the account, its domains and ROOT, so every network create and delete
in the cloud waited behind it and failed after innodb_lock_wait_timeout
("Lock wait timeout exceeded", ResourceCountDaoImpl), and the network counts
drifted. The callers that check a range one number at a time
(NetworkServiceImpl) paid the full scan per number.

Query only the requested vnets: vnet IN (...) with data_center_id and the
optional physical_network_id, which the existing unique index
i_op_dc_vnet_alloc__vnet__data_center_id (vnet, physical_network_id,
data_center_id) serves; ranges are looked up in batches of 1000. CloudStack
writes the vnet column as Integer.toString, so the string match finds the
same rows as the previous numeric comparison. No schema change.

Upstream: none yet - same code in apache/cloudstack main, to offer
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.

1 participant