<fix>[externalapiadapter]: fix VM NIC driver type fallback#3933
<fix>[externalapiadapter]: fix VM NIC driver type fallback#3933zstack-robot-1 wants to merge 1 commit into
Conversation
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml) Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (4)
Walkthrough此 PR 在 NIC 附加与初始化路径中:保留已有的 VmNicVO.driverType,只有为空时才基于 virtio 标签与平台类型计算并持久化;同时优化了 prepareDbInitialValue 的数据库查询与分组逻辑,并新增/扩展集成测试以覆盖这些行为。 更改内容NIC 驱动类型处理
🎯 3 (中等) | ⏱️ ~25 分钟
🚥 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.2)compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.javaComment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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 `@compute/src/main/java/org/zstack/compute/vm/VmNicManagerImpl.java`:
- Around line 146-147: The current vmDrivers.put call in VmNicManagerImpl
(inside prepareDbInitialValue) directly calls ImagePlatform.valueOf(vmPlatform)
which will throw on null/unknown platform; change this to safely parse the
platform string: first check if vmPlatform is null/blank, then attempt a guarded
parse (try-catch around valueOf or use a safe lookup method) and treat any
exception or unknown value as non-paravirtualized, falling back to
defaultNicDriver; ensure the put still uses virtioVmUuids.contains(vmUuid) ||
<isParaVirtualization> logic with the safe result, and log a warning/error
mentioning vmUuid and vmPlatform when falling back so startup repair tolerates
dirty/legacy data.
In
`@test/src/test/groovy/org/zstack/test/integration/kvm/nic/ChangeWindowsVmNicDriverCase.groovy`:
- Around line 116-124: After zeroing driverType with
SQL.New(VmNicVO.class)...set(VmNicVO_.driverType, null).update(), add an
immediate pre-condition assertion that Q.New(VmNicVO.class).eq(VmNicVO_.uuid,
newNic.uuid).select(VmNicVO_.driverType).findValue() == null before calling
bean(VmNicManagerImpl.class).prepareDbInitialValue(), so the test fails if the
SQL update didn't actually clear the field and avoids a false positive on the
later "virtio" assertions.
🪄 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: db2f81b5-3f6e-416b-88b0-7c67dc6ca0c2
📒 Files selected for processing (4)
compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.javacompute/src/main/java/org/zstack/compute/vm/VmNicManagerImpl.javatest/src/test/groovy/org/zstack/test/integration/kvm/nic/ChangeWindowsVmNicDriverCase.groovytest/src/test/groovy/org/zstack/test/integration/kvm/nic/VmNicBasicCase.groovy
| vmDrivers.put(vmUuid, virtioVmUuids.contains(vmUuid) || ImagePlatform.valueOf(vmPlatform).isParaVirtualization() ? | ||
| defaultPVNicDriver : defaultNicDriver); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "== 1) 检查 VmInstanceVO.platform 字段定义 =="
fd -i 'VmInstanceVO.java' | xargs -r rg -n -C3 'platform|@Column|nullable'
echo
echo "== 2) 检查 ImagePlatform 可接受取值 =="
fd -i 'ImagePlatform.java' | xargs -r rg -n -C2 'enum ImagePlatform|class ImagePlatform|\\b[A-Z0-9_]+\\b'
echo
echo "== 3) 检查平台字段历史写入/迁移路径(是否可能出现非枚举值或空值) =="
rg -n -C2 --iglob '*.{java,groovy,sql,xml}' 'vm_instance|VmInstanceVO_\\.platform|\\bplatform\\b\\s*=|insert\\s+into\\s+.*vm_instance'Repository: MatheMatrix/zstack
Length of output: 103
🏁 Script executed:
git ls-files | grep -i "VmNicManagerImpl\|VmInstanceVO\|ImagePlatform" | head -20Repository: MatheMatrix/zstack
Length of output: 418
🏁 Script executed:
sed -n '140,160p' compute/src/main/java/org/zstack/compute/vm/VmNicManagerImpl.javaRepository: MatheMatrix/zstack
Length of output: 1041
🏁 Script executed:
rg -n -C5 '@Column.*platform|private.*platform|public.*platform' header/src/main/java/org/zstack/header/vm/VmInstanceVO.java | head -30Repository: MatheMatrix/zstack
Length of output: 44
🏁 Script executed:
wc -l header/src/main/java/org/zstack/header/vm/VmInstanceVO.java && grep -n "platform" header/src/main/java/org/zstack/header/vm/VmInstanceVO.java | head -10Repository: MatheMatrix/zstack
Length of output: 126
🏁 Script executed:
cat -n header/src/main/java/org/zstack/header/vm/VmInstanceVO.javaRepository: MatheMatrix/zstack
Length of output: 5235
🏁 Script executed:
grep -r "class VmInstanceAO" --include="*.java" | head -5Repository: MatheMatrix/zstack
Length of output: 279
🏁 Script executed:
rg -n "platform" header/src/main/java/org/zstack/header/vm/VmInstanceAO.javaRepository: MatheMatrix/zstack
Length of output: 256
🏁 Script executed:
sed -n '75,95p' header/src/main/java/org/zstack/header/vm/VmInstanceAO.javaRepository: MatheMatrix/zstack
Length of output: 385
🏁 Script executed:
cat -n header/src/main/java/org/zstack/header/image/ImagePlatform.javaRepository: MatheMatrix/zstack
Length of output: 946
🏁 Script executed:
rg -n "prepareDbInitialValue" compute/src/main/java/org/zstack/compute/vm/VmNicManagerImpl.java -B5 -A15Repository: MatheMatrix/zstack
Length of output: 893
🏁 Script executed:
sed -n '120,170p' compute/src/main/java/org/zstack/compute/vm/VmNicManagerImpl.javaRepository: MatheMatrix/zstack
Length of output: 2231
避免在启动修复路径直接对平台字符串调用 valueOf,需要做安全解析
VmInstanceAO.platform 字段为可空(@Column 未设置 nullable=false),但 line 146 的 ImagePlatform.valueOf(vmPlatform) 在遇到 null 值或未知平台值时会抛 IllegalArgumentException,导致 prepareDbInitialValue() 启动修复被中断。需要安全解析:未知/空值平台降级为 defaultNicDriver 并记录告警。
建议修改(示例)
for (Tuple vmTuple : tupleList) {
String vmUuid = vmTuple.get(0, String.class);
String vmPlatform = vmTuple.get(1, String.class);
- vmDrivers.put(vmUuid, virtioVmUuids.contains(vmUuid) || ImagePlatform.valueOf(vmPlatform).isParaVirtualization() ?
- defaultPVNicDriver : defaultNicDriver);
+ Optional<ImagePlatform> platformOpt = Arrays.stream(ImagePlatform.values())
+ .filter(p -> p.toString().equals(vmPlatform))
+ .findFirst();
+ boolean isParaVirtualization = platformOpt.map(ImagePlatform::isParaVirtualization).orElse(false);
+ if (vmPlatform != null && !platformOpt.isPresent()) {
+ logger.warn(String.format("unknown vm platform[%s] for vm[uuid:%s], fallback to default nic driver",
+ vmPlatform, vmUuid));
+ }
+ vmDrivers.put(vmUuid, virtioVmUuids.contains(vmUuid) || isParaVirtualization
+ ? defaultPVNicDriver : defaultNicDriver);
}符合向后兼容原则:启动修复应容错处理历史脏数据,避免因个别异常记录导致全局启动失败。
🤖 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 `@compute/src/main/java/org/zstack/compute/vm/VmNicManagerImpl.java` around
lines 146 - 147, The current vmDrivers.put call in VmNicManagerImpl (inside
prepareDbInitialValue) directly calls ImagePlatform.valueOf(vmPlatform) which
will throw on null/unknown platform; change this to safely parse the platform
string: first check if vmPlatform is null/blank, then attempt a guarded parse
(try-catch around valueOf or use a safe lookup method) and treat any exception
or unknown value as non-paravirtualized, falling back to defaultNicDriver;
ensure the put still uses virtioVmUuids.contains(vmUuid) ||
<isParaVirtualization> logic with the safe result, and log a warning/error
mentioning vmUuid and vmPlatform when falling back so startup repair tolerates
dirty/legacy data.
Persist the resolved NIC driver type during AttachVmNicToVm and narrow prepareDbInitialValue to repair only null VNIC driverType records while respecting the VM virtio tag and platform, preventing existing virtio NICs from being overwritten as e1000 after management node restart. Resolves: ZSTAC-85160 Change-Id: I6868776c140529e5286d40c187fde99608127f7a
c68e521 to
046e4a7
Compare
Persist the resolved NIC driver type during AttachVmNicToVm and narrow prepareDbInitialValue to repair only null VNIC driverType records while respecting the VM virtio tag and platform, preventing existing virtio NICs from being overwritten as e1000 after management node restart.
Resolves: ZSTAC-85160
Change-Id: I6868776c140529e5286d40c187fde99608127f7a
sync from gitlab !9822