Skip to content

<fix>[cloud]: emit vm and usedip delete events#4137

Open
zstack-robot-1 wants to merge 2 commits into
5.5.22from
sync/hanyu.liang/zcf-3961
Open

<fix>[cloud]: emit vm and usedip delete events#4137
zstack-robot-1 wants to merge 2 commits into
5.5.22from
sync/hanyu.liang/zcf-3961

Conversation

@zstack-robot-1
Copy link
Copy Markdown
Collaborator

Summary

  • Use DBFacade-visible delete/update paths for Created VM deletion and UsedIp lifecycle changes.
  • Preserve VM/root volume EO cleanup after generating delete notifications.
  • Ensure UsedIp binding/release changes can be observed by ResNotify.

Test

  • mvn -pl header -DskipTests install
  • mvn -pl compute -DskipTests package
  • mvn -pl network -DskipTests package
  • Real env: VM, volume, used_ip, load_balancer, vrouter lifecycle webhook paths verified on root@172.26.21.56.

sync from gitlab !10041

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
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 28, 2026

Review Change Stack

Walkthrough

此 PR 统一重构了跨计算和网络模块的数据库删除和更新模式。VM 实例删除改用软删除并显式调度 EO 清理操作;IP 管理的删除和重分配改用 SQL 查询删除和实体更新方式,提高操作的显式性和可追踪性。

Changes

删除与更新模式重构

Layer / File(s) Summary
VM 实例删除清理逻辑调整
compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java
doDestroy 方法的 DBOnly/KeepVolume 分支中,VmInstanceVO 删除方式从 hardDelete() 改为 delete()eoCleanup 调用从 SQLBatch 内隐式执行改为 execute() 后显式执行,并根据 root volume 存在性追加清理操作。
网络 IP 删除和重新分配模式调整
network/src/main/java/org/zstack/network/l3/L3BasicNetwork.java, network/src/main/java/org/zstack/network/l3/L3NetworkManagerImpl.java
ReturnIpMsg 中将 UsedIpVO 删除改用 SQL.New().delete() 替代 hardDelete()reAllocateNicIp 改为通过 dbf.findByUuid 获取实体后执行 dbf.update() 更新,取代直接的 SQL 批量更新。

Estimated Code Review Effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 删除的模式换新颜,
SQL 查询显意图;
软删除、显式清理,
数据流清晰如初。
跨模块齐步重构,
代码韧性更强劲。

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed PR标题完全符合要求的格式,包含type、scope和description,且长度46字符在72字符限制内。标题准确反映了主要变更内容。
Description check ✅ Passed PR描述清晰地解释了变更目的、测试方法和相关模块,与changeset中关于DBFacade感知删除/更新路径和生命周期事件的变更直接相关。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sync/hanyu.liang/zcf-3961

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 113a77a and 85d6b15.

📒 Files selected for processing (3)
  • compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java
  • network/src/main/java/org/zstack/network/l3/L3BasicNetwork.java
  • network/src/main/java/org/zstack/network/l3/L3NetworkManagerImpl.java

Comment on lines +997 to +1001
UsedIpVO usedIpVO = dbf.findByUuid(usedIp.getUuid(), UsedIpVO.class);
if (usedIpVO != null) {
usedIpVO.setVmNicUuid(nicVO.getUuid());
dbf.update(usedIpVO);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

避免静默跳过缺失的 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.

Suggested change
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
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.

2 participants