Skip to content

<fix>[cascade]: cleanup orphan templated vm and snapshot tree#3851

Open
MatheMatrix wants to merge 1 commit intozsv_5.0.0from
sync/tao.gan/ZSV-11769@@2
Open

<fix>[cascade]: cleanup orphan templated vm and snapshot tree#3851
MatheMatrix wants to merge 1 commit intozsv_5.0.0from
sync/tao.gan/ZSV-11769@@2

Conversation

@MatheMatrix
Copy link
Copy Markdown
Owner

Resolves: ZSV-11769

Change-Id: I616d676e647a656162756e666b7862747668736a

sync from gitlab !9723

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 23, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 355c9db5-2483-4a77-b900-032c7e05d91e

📥 Commits

Reviewing files that changed from the base of the PR and between 6d26ff1 and ad19409.

📒 Files selected for processing (2)
  • compute/src/main/java/org/zstack/compute/vm/VmCascadeExtension.java
  • storage/src/main/java/org/zstack/storage/snapshot/VolumeSnapshotCascadeExtension.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • compute/src/main/java/org/zstack/compute/vm/VmCascadeExtension.java

Walkthrough

在级联删除清理流程中补充孤立数据回溯删除:虚拟机删除新增对模板化虚拟机相关表的孤立一致性批量清理;卷快照删除路径补充了对 VolumeSnapshotTreeVO 的 EO 清理调用。

Changes

Cohort / File(s) Summary
模板化虚拟机清理
compute/src/main/java/org/zstack/compute/vm/VmCascadeExtension.java
在删除清理流程中新增一轮孤立一致性检查与批量 SQL 清理:先删除引用已不存在 VmInstanceEOTemplatedVmInstanceRefVO,若存在缺失的 TemplatedVmInstanceVO UUID,则对 TemplatedVmInstanceRefVOTemplatedVmInstanceCacheVOTemplatedVmInstanceVO 执行硬删除。
卷快照树清理
storage/src/main/java/org/zstack/storage/snapshot/VolumeSnapshotCascadeExtension.java
handleDeletionCleanup 的主分支与异常(NullPointerException)分支中都增加了 DatabaseFacade.eoCleanup(VolumeSnapshotTreeVO.class) 调用,确保卷快照树实体在各路径均被清理。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 我在代码田野里轻跳,翻起旧叶与尘嚣,

用小爪理清散落的线梢,
把孤影一一归巢,
铺成整齐的春耙,
喜欢这次安静的消消。

🚥 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 标题准确地反映了主要变更:清理孤立的模板化VM和快照树,与所有文件更改直接相关。
Description check ✅ Passed 描述提供了问题追踪号(ZSV-11769)和GitLab同步信息,与变更集相关且包含有意义的上下文。
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/tao.gan/ZSV-11769@@2

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

@MatheMatrix MatheMatrix force-pushed the sync/tao.gan/ZSV-11769@@2 branch from 5115097 to 6d26ff1 Compare April 23, 2026 14:22
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 the current code and only fix it if needed.

Inline comments:
In `@compute/src/main/java/org/zstack/compute/vm/VmCascadeExtension.java`:
- Around line 307-321: The current code loads all orphan UUIDs into memory via
sql(...).list() and then issues three hardDelete() calls with large IN
predicates (TemplatedVmInstanceRefVO, TemplatedVmInstanceCacheVO,
TemplatedVmInstanceVO), which can cause memory/SQL parameter issues; change each
delete to a single pure-SQL subquery delete that removes rows where
templatedVmInstanceUuid/uuid is in (select uuid from TemplatedVmInstanceVO t
where t.uuid not in (select vm.uuid from VmInstanceEO vm)) so you avoid building
the orphanUuids list and large IN parameter lists—update the delete logic in
VmCascadeExtension to run those three deletes as SQL subquery deletes
referencing TemplatedVmInstanceVO, TemplatedVmInstanceRefVO and
TemplatedVmInstanceCacheVO instead of using orphanUuids.
🪄 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: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 06bcec0e-794f-4304-b309-5c93e76c7c27

📥 Commits

Reviewing files that changed from the base of the PR and between 5115097 and 6d26ff1.

📒 Files selected for processing (2)
  • compute/src/main/java/org/zstack/compute/vm/VmCascadeExtension.java
  • storage/src/main/java/org/zstack/storage/snapshot/VolumeSnapshotCascadeExtension.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • storage/src/main/java/org/zstack/storage/snapshot/VolumeSnapshotCascadeExtension.java

Comment on lines +307 to +321
List<String> orphanUuids = sql("select t.uuid from TemplatedVmInstanceVO t" +
" where t.uuid not in (select vm.uuid from VmInstanceEO vm)", String.class).list();
if (orphanUuids.isEmpty()) {
return;
}

sql(TemplatedVmInstanceRefVO.class)
.in(TemplatedVmInstanceRefVO_.templatedVmInstanceUuid, orphanUuids)
.hardDelete();
sql(TemplatedVmInstanceCacheVO.class)
.in(TemplatedVmInstanceCacheVO_.templatedVmInstanceUuid, orphanUuids)
.hardDelete();
sql(TemplatedVmInstanceVO.class)
.in(TemplatedVmInstanceVO_.uuid, orphanUuids)
.hardDelete();
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

避免先全量拉取 orphan UUID 再做 IN 批删。

Line 307-321 当前实现会把孤儿 UUID 全量加载到内存,并在 3 次删除中展开大 IN 条件。孤儿数据规模大时,容易触发内存压力或 SQL/参数长度上限,导致 cleanup 阶段超时或失败。

建议改为纯 SQL 子查询删除(避免全量 list + IN)
-                List<String> orphanUuids = sql("select t.uuid from TemplatedVmInstanceVO t" +
-                        " where t.uuid not in (select vm.uuid from VmInstanceEO vm)", String.class).list();
-                if (orphanUuids.isEmpty()) {
-                    return;
-                }
-
-                sql(TemplatedVmInstanceRefVO.class)
-                        .in(TemplatedVmInstanceRefVO_.templatedVmInstanceUuid, orphanUuids)
-                        .hardDelete();
-                sql(TemplatedVmInstanceCacheVO.class)
-                        .in(TemplatedVmInstanceCacheVO_.templatedVmInstanceUuid, orphanUuids)
-                        .hardDelete();
-                sql(TemplatedVmInstanceVO.class)
-                        .in(TemplatedVmInstanceVO_.uuid, orphanUuids)
-                        .hardDelete();
+                sql("delete from TemplatedVmInstanceRefVO ref" +
+                        " where ref.templatedVmInstanceUuid in (" +
+                        " select t.uuid from TemplatedVmInstanceVO t" +
+                        " where not exists (select 1 from VmInstanceEO vm where vm.uuid = t.uuid)" +
+                        " )").execute();
+
+                sql("delete from TemplatedVmInstanceCacheVO cache" +
+                        " where cache.templatedVmInstanceUuid in (" +
+                        " select t.uuid from TemplatedVmInstanceVO t" +
+                        " where not exists (select 1 from VmInstanceEO vm where vm.uuid = t.uuid)" +
+                        " )").execute();
+
+                sql("delete from TemplatedVmInstanceVO t" +
+                        " where not exists (select 1 from VmInstanceEO vm where vm.uuid = t.uuid)").execute();
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@compute/src/main/java/org/zstack/compute/vm/VmCascadeExtension.java` around
lines 307 - 321, The current code loads all orphan UUIDs into memory via
sql(...).list() and then issues three hardDelete() calls with large IN
predicates (TemplatedVmInstanceRefVO, TemplatedVmInstanceCacheVO,
TemplatedVmInstanceVO), which can cause memory/SQL parameter issues; change each
delete to a single pure-SQL subquery delete that removes rows where
templatedVmInstanceUuid/uuid is in (select uuid from TemplatedVmInstanceVO t
where t.uuid not in (select vm.uuid from VmInstanceEO vm)) so you avoid building
the orphanUuids list and large IN parameter lists—update the delete logic in
VmCascadeExtension to run those three deletes as SQL subquery deletes
referencing TemplatedVmInstanceVO, TemplatedVmInstanceRefVO and
TemplatedVmInstanceCacheVO instead of using orphanUuids.

Resolves: ZSV-11769

Change-Id: I616d676e647a656162756e666b7862747668736a
@MatheMatrix MatheMatrix force-pushed the sync/tao.gan/ZSV-11769@@2 branch from 6d26ff1 to ad19409 Compare April 24, 2026 02:59
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