Repository navigation
Serialize NIC add/remove on the target VM's work job queue - #14139
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 4.22 #14139 +/- ##
============================================
+ Coverage 17.94% 18.01% +0.06%
- Complexity 16145 16221 +76
============================================
Files 5928 5936 +8
Lines 535174 535718 +544
Branches 65494 65597 +103
============================================
+ Hits 96017 96484 +467
- Misses 428228 428236 +8
- Partials 10929 10998 +69
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:
|
8fae08a to
f734aa0
Compare
a4f03f6 to
74fc025
Compare
74fc025 to
7564538
Compare
|
@vladimirpetrov @sureshanaparti could you take a look at this one? We'd like this fix to make it into the upcoming 4.22.2 release. |
createNicForVm() reads the next free device id and persists the nic in two
separate steps:
int deviceId = _nicDao.getFreeDeviceId(vm.getId()); // reads existing nics
...
allocateNic(..., deviceId, ...); // persists much later
getFreeDeviceId() has no lock, and the nic row it is choosing an id for is not
written until the end of allocateNic(). Two nics added to the same VM at once
therefore both read the same free id and land on the same ethN.
This happens when several tiers of a VPC are brought up in parallel: each tier
attaches the *shared* redundant VR, so N concurrent createNicForVm() calls run
against one router. The result is two guest nics on one ethN (one network
blackholed) and, because the two routers of a redundant VPC end up with
different device -> network maps, divergent keepalived configs -> both routers
go PRIMARY and the VPC is dead. (issue apache#11710)
Fix: compute the free device id and persist the nic under the vm_instance row
lock, so device id assignment is atomic per VM. This is the same shape as the
existing persistNicAfterRaceCheck() IPv4 race guard and needs no change to job
dispatch. Serializing per router is what the non-concurrent path already does
implicitly (one tier at a time).
Fixes: apache#11710
Signed-off-by: Brad House <bhouse@nexthop.ai>
7564538 to
c65369e
Compare
The previous commit held the vm_instance row lock in a transaction that spanned the whole of allocateNic(). That made the device id atomic, but it also pulled persistNicAfterRaceCheck() into the outer transaction, so a new nic was no longer committed when it was written but only once the rest of the allocation had finished. Until then its guest ip is invisible to other vms in the network, both to the guru picking a free ip and to the race check that is meant to catch two nics taking the same one. The vm row lock does nothing for that, as it only serialises nics of one vm. Choose the device id where the nic is written instead. persistNic() runs the ip race check, takes the vm row lock, reads the free device id and persists the nic in one short transaction, as persistNicAfterRaceCheck() did before, so the nic is committed as soon as it exists. allocateNicWithFreeDeviceId() asks for that with FREE_DEVICE_ID and no longer opens a transaction of its own. Signed-off-by: Brad House <bhouse@nexthop.ai>
Damans227
left a comment
There was a problem hiding this comment.
thanks @bhouse-nexthop, fix looks right. LGTM
|
@DaanHoogland this has your and @Damans227's approvals and CI is green, but it hasn't had a Trillian run yet. Could you kick one and add it to the 4.22.2 milestone? I understand 4.22.2 is due out late this week or early next, and we'd like this fix in it. |
|
@blueorangutan package |
|
@DaanHoogland 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. |
|
Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 19685 |
|
@blueorangutan test |
|
@DaanHoogland a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests |
|
[SF] Trillian test result (tid-17223)
|
Test report: parallel tier bring-up on a redundant VPCEnvironment
Method. I ran three rounds. Each round:
After the three rounds I kept two tiers with a VM each and ran 1. Duplicate device ids: none This query returned no rows after every round, before the restart, and after it: SELECT n.instance_id, n.device_id, COUNT(*) FROM nics n JOIN vm_instance v ON v.id=n.instance_id
WHERE n.removed IS NULL AND v.type='DomainRouter' AND v.vpc_id=<rvpc> GROUP BY 1,2 HAVING COUNT(*)>1;No two guest NICs shared an 2. Recovery with
So the corruption this PR targets did not appear: two networks on one 3. A separate failure mode, not fixed by this PR Not every parallel deploy succeeded:
All 12 failures have the same cause: The agent of the router's host logged the answer to the
So a deploy can report success while one router never got its tier's config. That explains the VIP sets in rounds 1 and 2. There, the missing gateway belonged to a tier whose deploy was Running. I think this is the "partial application" mentioned under How Has This Been Tested. It comes from the agent, not from storage. It needs its own fix: serialise aggregated applies per router, or key the aggregation by job rather than by router name. Result: pass for this PR's change.
|
|
@weizhouapache @RosiKyu this fixes NICs added to the same VM at the same time getting the same device id. On a redundant VPC, bringing up tiers in parallel breaks both routers' NIC layout and VRRP. It has two code approvals (DaanHoogland, Damans227), but still needs an independent test review before it can be merged. Could one of you test it, or kick a fresh Trillian run? It's on 4.22, so if it gets in before the 24.0 freeze this week it would also land in 24.0 through the merge forward. |
jmsperu
left a comment
There was a problem hiding this comment.
Tested the PR head (a6762e9) on 4.22.1.1 (KVM, Ubuntu 24.04, two hosts), with this PR applied on the management server and #14640 on the agents. The PR applies cleanly on the 4.22.1.1 tag and its unit tests pass (NetworkOrchestratorTest, 62 run, 0 failures).
Scenario: create a VPC, then create 6 tiers in parallel and deploy one instance in each, so that all tiers are plugged into the VPC router at the same time. After each run I compared the device IDs of the router NICs in the database and the gateways configured inside the router.
- Without the PR (control, same day): 2 of 3 runs gave the router two NICs with the same device ID, and in both the router had 5 of 6 tier gateways with one unprocessed guest_network.json left in /var/cache/cloud, while every tier and instance was reported as Implemented and Running. Yesterday the same control gave 4 bad runs of 6.
- With the PR: 8 of 8 runs clean. Device IDs 0 to 7 with no duplicates, 6 of 6 gateways, nothing left unprocessed, 6 of 6 instances Running, no NullPointerException in the agent log.
LGTM from a functional test. Not tested: VMware or XenServer, adding and removing NICs on user instances by hand, redundant VPC routers.
|
merging based on approvals, thanks @bhouse-nexthop @jmsperu |
Description
On a redundant VPC, bringing up several tiers with VMs in parallel corrupts both virtual routers' NIC layout. Their keepalived configs diverge and VRRP breaks — the routers end up FAULT or both PRIMARY, and the VPC is unusable until restarted. Root-cause analysis in #11710.
Cause — a lockless read-then-persist in device id allocation
NetworkOrchestrator.createNicForVm()picks the device id and persists the nic in two separate steps:getFreeDeviceId()takes no lock, and the row it is choosing an id for is not written until the end ofallocateNic(). Two nics added to the same VM concurrently both read the same free id and land on the sameethN.This is triggered when several VPC tiers are created in parallel: each tier attaches the shared redundant VR, so N concurrent
createNicForVm()calls run against one router. Observednicsrows for one router during reproduction (all live at once):Two guest nics share one
ethN(one network blackholed), and because the two routers of a redundant VPC end up with differentdevice_id → networkmaps, their keepalivedvirtual_ipaddresssets diverge → both go PRIMARY.Fix
Compute the free device id and persist the nic under the
vm_instancerow lock, so device id assignment is atomic per VM:persistNicAfterRaceCheck()IPv4 race guard.getFreeDeviceId()'s only caller iscreateNicForVm(), so this covers the whole device-id race surface — the user-VM add-nic path and the VPC tier attach to the shared VR alike — not just the reported case. (The initial deploy path assigns device ids from a single precomputed set, so it was never exposed to this race.)Types of changes
Feature/Enhancement Scale or Bug Severity
Bug Severity
How Has This Been Tested?
Unit test —
NetworkOrchestratorTest.testAllocateNicWithFreeDeviceIdLocksVmRowBeforeReadingFreeDeviceIdasserts (viaInOrder) that thevm_instancerow is locked beforegetFreeDeviceId()is read, and thatallocateNic()runs with that id. Full suite (59 tests) green.Live — 4.22.1.0, KVM, two management servers, VXLAN, patch deployed on both. Redundant VPC ("VPC HA"), then 8 tiers each with a VM created in parallel via the API (mimics
terraform apply):device_ids on a router (two guest NICs on oneethN, table above), routers FAULT/PRIMARY.device_ids on every run — the persistent corruption is gone. Restarting the VPC (the recovery the issue documents) now re-applies both routers to byte-identical keepalived configs and a clean BACKUP + PRIMARY election, because there is no longer any duplicated device id to re-render.Full disclosure on the test environment: its secondary storage was flaky enough that a pristine 8/8 parallel bring-up could not be captured — a couple of VM deploys timed out, and the resulting partial application left one router's keepalived missing some gateways, which by itself can still cause a transient dual-PRIMARY on first bring-up. That is a distinct failure mode (a per-router network-apply failure being swallowed while the VPC still reports success — see #11710 discussion) from the device-id race this PR fixes, and is worth a follow-up. What this change is verified to remove is the duplicate-
device_idcorruption that put two networks on oneethN.How did you try to break this feature and the system with this change?
vm_instancerow of the VM getting the nic (the router). It is held acrossallocateNic(), which acquires only leaf locks (IP address rows) — consistentvm → ipordering, no inversion.implementNetwork()'s network op-lock is taken after allocation, outside this transaction. The router being attached is already running, so it is never being started concurrently under its own row lock.allocateNic'sInsufficientCapacity/ConcurrentOperationexceptions propagate as before (the callback'sthrowswidens toInsufficientCapacityException, already declared bycreateNicForVm).