<fix>[cloud]: emit vm and usedip delete events#4137
Conversation
1. Why? ZCF incremental sync depends on Cloud resource notifications. Some VM and UsedIp lifecycle paths changed rows through direct SQL hard delete or update operations, bypassing DB lifecycle extensions and leaving ZCF with stale resources. 2. How? Use DBFacade-aware delete/update paths for Created VM DB-only deletion, root volume deletion, UsedIp release, and UsedIp vmNic binding updates so ResNotify receives lifecycle callbacks. Keep EO cleanup for VM/root volume after the delete notifications are generated. 3. Side effects? Only notification-visible persistence paths change; the resources are still removed or updated in the same lifecycle flow. # Summary of changes (by module): - compute - network Resolves: ZCF-3961 Change-Id: I9a53cb036e11e742d1c420fa01444554f3b09285
Walkthrough此 PR 统一重构了跨计算和网络模块的数据库删除和更新模式。VM 实例删除改用软删除并显式调度 EO 清理操作;IP 管理的删除和重分配改用 SQL 查询删除和实体更新方式,提高操作的显式性和可追踪性。 Changes删除与更新模式重构
Estimated Code Review Effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ast-grep (0.42.3)compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.javaComment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@network/src/main/java/org/zstack/network/l3/L3NetworkManagerImpl.java`:
- Around line 997-1001: In L3NetworkManagerImpl replace the silent skip when
usedIpVO == null with an immediate failure that triggers rollback: locate the
block that fetches UsedIpVO (usedIpVO = dbf.findByUuid(usedIp.getUuid(),
UsedIpVO.class)) and instead of doing nothing when usedIpVO is null, throw a
runtime/operation exception (including usedIp.getUuid() and nicVO.getUuid() in
the message) so the transaction fails and the newly allocated IP is released;
keep the existing branch that updates usedIpVO.setVmNicUuid(nicVO.getUuid()) and
dbf.update(usedIpVO) unchanged for the non-null case.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)
Review profile: CHILL
Plan: Pro
Run ID: ce11bb9b-2434-4f17-b9e9-26ee491fc188
📒 Files selected for processing (3)
compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.javanetwork/src/main/java/org/zstack/network/l3/L3BasicNetwork.javanetwork/src/main/java/org/zstack/network/l3/L3NetworkManagerImpl.java
| UsedIpVO usedIpVO = dbf.findByUuid(usedIp.getUuid(), UsedIpVO.class); | ||
| if (usedIpVO != null) { | ||
| usedIpVO.setVmNicUuid(nicVO.getUuid()); | ||
| dbf.update(usedIpVO); | ||
| } |
There was a problem hiding this comment.
避免静默跳过缺失的 UsedIp,改为失败回滚。
Line [997]-Line [1001] 在 usedIpVO == null 时直接跳过,会让 NIC 与 UsedIp 绑定进入部分成功状态。这里应立即失败,让流程走回滚并释放新分配 IP。
🔧 建议修改
for (UsedIpInventory usedIp : newIps) {
/* update usedIpVo */
UsedIpVO usedIpVO = dbf.findByUuid(usedIp.getUuid(), UsedIpVO.class);
- if (usedIpVO != null) {
- usedIpVO.setVmNicUuid(nicVO.getUuid());
- dbf.update(usedIpVO);
- }
+ if (usedIpVO == null) {
+ trigger.fail(errf.instantiateErrorCode(
+ SysErrors.RESOURCE_NOT_FOUND,
+ String.format("UsedIp[uuid:%s] not found during NIC IP reallocation", usedIp.getUuid())
+ ));
+ return;
+ }
+ usedIpVO.setVmNicUuid(nicVO.getUuid());
+ dbf.update(usedIpVO);
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| UsedIpVO usedIpVO = dbf.findByUuid(usedIp.getUuid(), UsedIpVO.class); | |
| if (usedIpVO != null) { | |
| usedIpVO.setVmNicUuid(nicVO.getUuid()); | |
| dbf.update(usedIpVO); | |
| } | |
| UsedIpVO usedIpVO = dbf.findByUuid(usedIp.getUuid(), UsedIpVO.class); | |
| if (usedIpVO == null) { | |
| trigger.fail(errf.instantiateErrorCode( | |
| SysErrors.RESOURCE_NOT_FOUND, | |
| String.format("UsedIp[uuid:%s] not found during NIC IP reallocation", usedIp.getUuid()) | |
| )); | |
| return; | |
| } | |
| usedIpVO.setVmNicUuid(nicVO.getUuid()); | |
| dbf.update(usedIpVO); |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@network/src/main/java/org/zstack/network/l3/L3NetworkManagerImpl.java` around
lines 997 - 1001, In L3NetworkManagerImpl replace the silent skip when usedIpVO
== null with an immediate failure that triggers rollback: locate the block that
fetches UsedIpVO (usedIpVO = dbf.findByUuid(usedIp.getUuid(), UsedIpVO.class))
and instead of doing nothing when usedIpVO is null, throw a runtime/operation
exception (including usedIp.getUuid() and nicVO.getUuid() in the message) so the
transaction fails and the newly allocated IP is released; keep the existing
branch that updates usedIpVO.setVmNicUuid(nicVO.getUuid()) and
dbf.update(usedIpVO) unchanged for the non-null case.
Resolves: ZCF-3961 Change-Id: I59e60ae7cb9ffbe8e6195de730675c5d00138356
Summary
Test
mvn -pl header -DskipTests installmvn -pl compute -DskipTests packagemvn -pl network -DskipTests packageroot@172.26.21.56.sync from gitlab !10041