<fix>[vm]: prune stale device metadata on startVm#3836
Open
zstack-robot-2 wants to merge 2 commits intozsv_5.0.0from
Open
<fix>[vm]: prune stale device metadata on startVm#3836zstack-robot-2 wants to merge 2 commits intozsv_5.0.0from
zstack-robot-2 wants to merge 2 commits intozsv_5.0.0from
Conversation
afterReceiveVmDeviceInfoResponse was upsert-only, leaving VmInstanceResourceMetadataVO rows for devices (e.g. cdrom) detached while the VM was stopped. Add pruneStaleDeviceMetadata and invoke it after each start/sync so DB matches the libvirt domain. Rows with non-null metadataClass are preserved. Resolves: ZSV-11374 Change-Id: I687463656770616d6967746b6270767366756a64
1. hasAuthoritativeDeviceInfo now tracks actually valid entries observed from the agent response instead of raw list non-emptiness, preventing a reply with all-invalid VirtualDeviceInfo from triggering prune with only vmUuid in surviving. 2. When rsp.nicInfos is null, populate surviving with every known nic uuid from spec/vm so the prune step cannot drop their address rows as collateral damage. 3. Extract ALWAYS_KEEP_UUIDS to a static final Set. 4. Remove stray uncompilable line left in prune method. Resolves: ZSV-11374 Change-Id: Ieb86e65494b0d957ef00267f4013605170f35adb
Walkthrough通过在VmInstanceResourceMetadataManager中新增 Changes
Sequence Diagram(s)sequenceDiagram
participant Agent as KVM Agent
participant Extension as VirtualPciDeviceKvmExtensionPoint
participant Manager as VmInstanceResourceMetadataManager
participant DB as Database
Agent->>Extension: afterReceiveVmDeviceInfoResponse(rsp)
Extension->>Extension: 从响应构建surviving集合<br/>(vmUuid, 有效设备, NIC, 气球)
Extension->>Extension: 设置hasAuthoritativeDeviceInfo标志<br/>(基于有效设备数据)
alt hasAuthoritativeDeviceInfo为true
Extension->>Extension: 更新气球设备元数据
Extension->>Manager: pruneStaleDeviceMetadata(vmUuid,<br/>surviving)
Manager->>DB: 加载所有元数据行
Manager->>Manager: 计算待删除列表<br/>(排除白名单、vmInstanceUuid、<br/>存活资源)
Manager->>DB: 硬删除陈旧元数据
Manager-->>Extension: 返回删除数量
Extension->>Extension: 日志记录清理结果
else hasAuthoritativeDeviceInfo为false
Extension->>Extension: 跳过元数据清理<br/>(无权威设备信息)
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 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)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
plugin/kvm/src/main/java/org/zstack/kvm/VirtualPciDeviceKvmExtensionPoint.java (1)
132-139: 简化 memBalloonInfo 的空值检查。当前检查链中的第三个条件是冗余的。
DeviceAddress.toString()调用JSONObjectUtil.toJsonString(this),而toJsonString()使用 Gson 序列化对象,对于非空对象永远不会返回空字符串(至少会返回"{}")。因此,一旦getDeviceAddress() != null通过,toString()不可能为空。♻️ 建议简化
- if (rsp.getMemBalloonInfo() != null - && rsp.getMemBalloonInfo().getDeviceAddress() != null - && !StringUtils.isEmpty(rsp.getMemBalloonInfo().getDeviceAddress().toString())) { + if (rsp.getMemBalloonInfo() != null + && rsp.getMemBalloonInfo().getDeviceAddress() != null) {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@plugin/kvm/src/main/java/org/zstack/kvm/VirtualPciDeviceKvmExtensionPoint.java` around lines 132 - 139, The null/empty check chain in VirtualPciDeviceKvmExtensionPoint is redundant: once rsp.getMemBalloonInfo().getDeviceAddress() is non-null, its toString() (which delegates to JSONObjectUtil/Gson) cannot be an empty string. Simplify the if condition to only test rsp.getMemBalloonInfo() != null && rsp.getMemBalloonInfo().getDeviceAddress() != null, then proceed to call vidManager.createOrUpdateVmResourceMetadata(... DeviceAddress.fromString(...)), add vidManager.MEM_BALLOON_UUID to surviving, and set hasAuthoritativeDeviceInfo = true.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@plugin/kvm/src/main/java/org/zstack/kvm/VirtualPciDeviceKvmExtensionPoint.java`:
- Around line 132-139: The null/empty check chain in
VirtualPciDeviceKvmExtensionPoint is redundant: once
rsp.getMemBalloonInfo().getDeviceAddress() is non-null, its toString() (which
delegates to JSONObjectUtil/Gson) cannot be an empty string. Simplify the if
condition to only test rsp.getMemBalloonInfo() != null &&
rsp.getMemBalloonInfo().getDeviceAddress() != null, then proceed to call
vidManager.createOrUpdateVmResourceMetadata(... DeviceAddress.fromString(...)),
add vidManager.MEM_BALLOON_UUID to surviving, and set hasAuthoritativeDeviceInfo
= true.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f3bab009-a40d-4959-b2fb-957b44ffbe96
📒 Files selected for processing (3)
compute/src/main/java/org/zstack/compute/vm/devices/VmInstanceResourceMetadataManagerImpl.javaheader/src/main/java/org/zstack/header/vm/devices/VmInstanceResourceMetadataManager.javaplugin/kvm/src/main/java/org/zstack/kvm/VirtualPciDeviceKvmExtensionPoint.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
afterReceiveVmDeviceInfoResponse was upsert-only, leaving
VmInstanceResourceMetadataVO rows for devices (e.g. cdrom)
detached while the VM was stopped. Add pruneStaleDeviceMetadata
and invoke it after each start/sync so DB matches the libvirt
domain. Rows with non-null metadataClass are preserved.
Resolves: ZSV-11374
Change-Id: I687463656770616d6967746b6270767366756a64
sync from gitlab !9708