Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,36 @@ public void run(List<MessageReply> replies) {

private void handleDeletionCleanup(CascadeAction action, Completion completion) {
dbf.eoCleanup(VmInstanceVO.class);
cleanupOrphanTemplatedVmInstance();
completion.success();
}

private void cleanupOrphanTemplatedVmInstance() {
new SQLBatch() {
@Override
protected void scripts() {
sql("delete from TemplatedVmInstanceRefVO ref" +
" where ref.vmInstanceUuid not in (select vm.uuid from VmInstanceEO vm)").execute();

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();
Comment on lines +307 to +321
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.

}
}.execute();
}

protected List<DetachNicFromVmMsg> handleDeletionForIpRange(List<VmDeletionStruct> vminvs, List<IpRangeInventory> iprs) {
List<DetachNicFromVmMsg> msgs = new ArrayList<>();
List<String> uuids = iprs.stream().map(IpRangeInventory::getUuid).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ private void handleDeletionCleanup(CascadeAction action, Completion completion)
} else {
dbf.eoCleanup(VolumeSnapshotVO.class);
dbf.eoCleanup(VolumeSnapshotGroupVO.class);
dbf.eoCleanup(VolumeSnapshotTreeVO.class);
}
} catch (NullPointerException e) {
logger.warn(e.getLocalizedMessage());
dbf.eoCleanup(VolumeSnapshotVO.class);
dbf.eoCleanup(VolumeSnapshotGroupVO.class);
dbf.eoCleanup(VolumeSnapshotTreeVO.class);
} finally {
completion.success();
}
Expand Down