Skip to content

<chore>[header]: replace CollectionUtil.find by findOneOrNull#3792

Open
zstack-robot-1 wants to merge 1 commit intozsv_5.0.0from
sync/wenhao.zhang/zsv-0
Open

<chore>[header]: replace CollectionUtil.find by findOneOrNull#3792
zstack-robot-1 wants to merge 1 commit intozsv_5.0.0from
sync/wenhao.zhang/zsv-0

Conversation

@zstack-robot-1
Copy link
Copy Markdown
Collaborator

Related: ZSV-5936

Change-Id: I67756e797a647a6b6c6c796e766776747473616e

sync from gitlab !9661

Related: ZSV-5936

Change-Id: I67756e797a647a6b6c6c796e766776747473616e
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 19, 2026

预览

本次拉取请求将代码库中的多个文件从使用 CollectionUtils.find(...) 配合匿名 Function 类替换为使用 CollectionUtils.findOneOrNull(...) 配合 Java Lambda 谓词。这是一项系统性的重构,用现代化的 Lambda 表达式替代了旧式的函数式接口实现,同时保持原有逻辑不变。

变更清单

Cohort / File(s) Summary
虚拟机网卡管理
compute/src/main/java/org/zstack/compute/vm/VmDetachNicFlow.java, compute/src/main/java/org/zstack/compute/vm/VmDownloadIsoFlow.java, compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java, header/src/main/java/org/zstack/header/vm/VmInstanceInventory.java
将 NIC 选择逻辑从基于 Functionfind() 重构为 findOneOrNull() Lambda 谓词,涉及默认 L3 网络更新、ISO 备份存储引用查找、静态 IP 变更及 NIC 卸载时的网卡匹配。
网络服务扩展
network/src/main/java/org/zstack/network/service/DhcpExtension.java, plugin/eip/src/main/java/org/zstack/network/service/eip/EipExtension.java
将 DHCP 主机名和 EIP 目标网卡选择从 Function 回调替换为 findOneOrNull() Lambda 谓词,简化了 null 处理逻辑。
虚拟机设备管理
plugin/applianceVm/src/main/java/org/zstack/appliancevm/ApplianceVmDeployAgentFlow.java, plugin/applianceVm/src/main/java/org/zstack/appliancevm/ApplianceVmFirewallRuleVO.java, plugin/applianceVm/src/main/java/org/zstack/appliancevm/ApplianceVmManagementIpChecker.java
将管理网卡选择逻辑从 Functionfind() 替换为 findOneOrNull() Lambda 谓词,保持原有的 NIC 匹配行为。
平台网络数据服务
plugin/flatNetworkProvider/src/main/java/org/zstack/network/service/flat/FlatUserdataBackend.java, plugin/flatNetworkProvider/src/main/java/org/zstack/network/service/userdata/UserdataExtension.java, plugin/sugonSdnController/src/main/java/org/zstack/sugonSdnController/userdata/TfUserdataBackend.java
将 Userdata 和 Userdata GC 中的网卡及 IP 选择从 Function 回调替换为 findOneOrNull() Lambda,IP 地址派生逻辑更加直观。
存储后端
plugin/localstorage/src/main/java/org/zstack/storage/primary/local/LocalStorageKvmBackend.java, plugin/sftpBackupStorage/src/main/java/org/zstack/storage/backup/sftp/SftpBackupStorage.java, plugin/sharedMountPointPrimaryStorage/src/main/java/org/zstack/storage/primary/smp/KvmBackend.java
将备份存储引用查找从 Functionfind() 重构为 findOneOrNull() Lambda,用于镜像下载和同步操作。
虚拟路由器核心
plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/VirtualRouterManagerImpl.java, plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/lifecycle/VirtualRouterAssembleDecoratorFlow.java, plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/lifecycle/VirtualRouterDeployAgentFlow.java
将管理网卡选择从 Function 回调替换为 findOneOrNull() Lambda;VirtualRouterManagerImpl 包含格式化调整。
虚拟路由器网络服务
plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/dhcp/VirtualRouterDhcpBackend.java, plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/eip/VirtualRouterEipBackend.java, plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/eip/VirtualRouterSyncEipOnStartFlow.java, plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/portforwarding/VirtualRouterPortForwardingBackend.java
将 DHCP、EIP 及端口转发中的网卡和 MAC 地址选择从 Function 重构为 findOneOrNull() Lambda,null 安全检查更加清晰。
虚拟路由器 VyOS 后端
plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/vyos/VyosConfigSshFlow.java, plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/vyos/VyosDeployAgentFlow.java, plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/vyos/VyosGetVersionFlow.java
将目标网卡选择从 Function 回调替换为 findOneOrNull() Lambda;VyosGetVersionFlow 移除了多个未使用的导入。
存储卷管理
storage/src/main/java/org/zstack/storage/volume/VolumeManagerImpl.java, storage/src/main/java/org/zstack/storage/volume/VolumeUpgradeExtension.java
将镜像备份存储引用和卷版本查找从 Functionfind() 替换为 findOneOrNull() Lambda。
主存储分配
storage/src/main/java/org/zstack/storage/primary/PrimaryStorageTagAllocatorExtension.java
将按 UUID 的主存储查找从 Function 回调替换为 findOneOrNull() Lambda。
ZBS 存储控制器
plugin/zbs/src/main/java/org/zstack/storage/zbs/ZbsStorageController.java
CollectionUtils.transformToList(...) 更改为 CollectionUtils.transform(...)

预估代码审查工作量

🎯 3 (中等) | ⏱️ ~25 分钟

兔子的诗

🐰 从 Function 到 Lambda,代码更清扬,
谓词替代旧回调,现代化闪闪亮,
三十个文件齐改妆,逻辑同步不迷茫,
简洁优雅新气象,findOneOrNull 展风光! 🌟

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 1.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确地概括了主要变更内容:将 CollectionUtil.find 替换为 findOneOrNull,这反映了跨26个文件的核心重构工作。
Description check ✅ Passed 描述简洁但与变更相关,提及相关问题 ZSV-5936 和 gitlab 同步来源,与跨多个模块的重构工作相符。

✏️ 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/wenhao.zhang/zsv-0

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.1)
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: 2

🧹 Nitpick comments (1)
compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java (1)

5739-5747: 可选:避免同一 NIC 的重复查找

Line 5739 已拿到 vmNicVO,Line 5745-5747 再查一次同一对象是冗余的。可直接复用,减少一次遍历并提升可读性。

♻️ 建议修改
-        final VmNicInventory nic = VmNicInventory.valueOf(
-                CollectionUtils.findOneOrNull(
-                        self.getVmNics(),
-                        arg -> arg.getUuid().equals(nicUuid))
-        );
+        final VmNicInventory nic = VmNicInventory.valueOf(vmNicVO);
🤖 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/VmInstanceBase.java` around lines
5739 - 5747, You've already found the NIC as vmNicVO via
CollectionUtils.findOneOrNull(self.getVmNics(), arg ->
arg.getUuid().equals(nicUuid)); reuse that vmNicVO when creating the
VmNicInventory instead of performing a second find; replace the second
CollectionUtils.findOneOrNull(...) inside VmNicInventory.valueOf(...) with
vmNicVO so VmNicInventory.valueOf(vmNicVO) is used, eliminating the redundant
traversal and improving readability.
🤖 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/VmDetachNicFlow.java`:
- Around line 44-45: In VmDetachNicFlow, the predicate used to compute
`selected` currently matches the NIC being detached
(CollectionUtils.findOneOrNull(spec.getVmInventory().getVmNics(), arg ->
arg.getUuid().equals(nic.getUuid()))), which can cause `defaultL3NetworkUuid` to
be reset to the L3 of the NIC that is about to be removed; change the predicate
to select a different NIC (i.e., find any `arg` whose UUID is NOT equal to
`nic.getUuid()`) and use that NIC's L3 for `defaultL3NetworkUuid`, and if no
such remaining NIC exists set `defaultL3NetworkUuid` to null; apply the same
inverted-predicate fix for the other occurrence that sets
`defaultL3NetworkUuid`.

In
`@plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/portforwarding/VirtualRouterPortForwardingBackend.java`:
- Around line 82-85: The code silently sets publicMac to null when no VM NIC
matches vip L3 network (selected from vr.getVmNics() by comparing
arg.getL3NetworkUuid() to struct.getVipL3Network().getUuid()), which defers
failure; change this to fail-fast like privateMac by asserting or throwing an
exception if selected is null (i.e., when computing publicMac) so the method
(VirtualRouterPortForwardingBackend logic around publicMac) aborts immediately
with a clear error rather than proceeding with a null MAC.

---

Nitpick comments:
In `@compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java`:
- Around line 5739-5747: You've already found the NIC as vmNicVO via
CollectionUtils.findOneOrNull(self.getVmNics(), arg ->
arg.getUuid().equals(nicUuid)); reuse that vmNicVO when creating the
VmNicInventory instead of performing a second find; replace the second
CollectionUtils.findOneOrNull(...) inside VmNicInventory.valueOf(...) with
vmNicVO so VmNicInventory.valueOf(vmNicVO) is used, eliminating the redundant
traversal and improving readability.
🪄 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: cdb0d97d-22ed-4df9-8274-a9c9190791ae

📥 Commits

Reviewing files that changed from the base of the PR and between 7609252 and fe08cda.

📒 Files selected for processing (29)
  • compute/src/main/java/org/zstack/compute/vm/VmDetachNicFlow.java
  • compute/src/main/java/org/zstack/compute/vm/VmDownloadIsoFlow.java
  • compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java
  • header/src/main/java/org/zstack/header/vm/VmInstanceInventory.java
  • network/src/main/java/org/zstack/network/service/DhcpExtension.java
  • plugin/applianceVm/src/main/java/org/zstack/appliancevm/ApplianceVmDeployAgentFlow.java
  • plugin/applianceVm/src/main/java/org/zstack/appliancevm/ApplianceVmFirewallRuleVO.java
  • plugin/applianceVm/src/main/java/org/zstack/appliancevm/ApplianceVmManagementIpChecker.java
  • plugin/eip/src/main/java/org/zstack/network/service/eip/EipExtension.java
  • plugin/flatNetworkProvider/src/main/java/org/zstack/network/service/flat/FlatUserdataBackend.java
  • plugin/flatNetworkProvider/src/main/java/org/zstack/network/service/userdata/UserdataExtension.java
  • plugin/localstorage/src/main/java/org/zstack/storage/primary/local/LocalStorageKvmBackend.java
  • plugin/sftpBackupStorage/src/main/java/org/zstack/storage/backup/sftp/SftpBackupStorage.java
  • plugin/sharedMountPointPrimaryStorage/src/main/java/org/zstack/storage/primary/smp/KvmBackend.java
  • plugin/sugonSdnController/src/main/java/org/zstack/sugonSdnController/userdata/TfUserdataBackend.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/VirtualRouterManagerImpl.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/dhcp/VirtualRouterDhcpBackend.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/eip/VirtualRouterEipBackend.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/eip/VirtualRouterSyncEipOnStartFlow.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/lifecycle/VirtualRouterAssembleDecoratorFlow.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/lifecycle/VirtualRouterDeployAgentFlow.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/portforwarding/VirtualRouterPortForwardingBackend.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/vyos/VyosConfigSshFlow.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/vyos/VyosDeployAgentFlow.java
  • plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/vyos/VyosGetVersionFlow.java
  • plugin/zbs/src/main/java/org/zstack/storage/zbs/ZbsStorageController.java
  • storage/src/main/java/org/zstack/storage/primary/PrimaryStorageTagAllocatorExtension.java
  • storage/src/main/java/org/zstack/storage/volume/VolumeManagerImpl.java
  • storage/src/main/java/org/zstack/storage/volume/VolumeUpgradeExtension.java

Comment on lines +44 to +45
VmNicInventory selected = CollectionUtils.findOneOrNull(
spec.getVmInventory().getVmNics(), arg -> arg.getUuid().equals(nic.getUuid()));
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 | 🔴 Critical

默认 L3 重置条件写反,导致可能引用已解绑 NIC 的 L3。

Line 44-45 当前匹配的是被解绑 NIC 本身,Line 48 因而可能把 defaultL3NetworkUuid 设回即将移除的 L3。这里应选择“非当前解绑 NIC”的剩余网卡;不存在时再置 null

🔧 建议修复
-            VmNicInventory selected = CollectionUtils.findOneOrNull(
-                    spec.getVmInventory().getVmNics(), arg -> arg.getUuid().equals(nic.getUuid()));
+            VmNicInventory selected = CollectionUtils.findOneOrNull(
+                    spec.getVmInventory().getVmNics(), arg -> !arg.getUuid().equals(nic.getUuid()));

             VmInstanceVO vm = dbf.findByUuid(spec.getVmInventory().getUuid(), VmInstanceVO.class);
             vm.setDefaultL3NetworkUuid(selected == null ? null : selected.getL3NetworkUuid());

Also applies to: 48-48

🤖 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/VmDetachNicFlow.java` around
lines 44 - 45, In VmDetachNicFlow, the predicate used to compute `selected`
currently matches the NIC being detached
(CollectionUtils.findOneOrNull(spec.getVmInventory().getVmNics(), arg ->
arg.getUuid().equals(nic.getUuid()))), which can cause `defaultL3NetworkUuid` to
be reset to the L3 of the NIC that is about to be removed; change the predicate
to select a different NIC (i.e., find any `arg` whose UUID is NOT equal to
`nic.getUuid()`) and use that NIC's L3 for `defaultL3NetworkUuid`, and if no
such remaining NIC exists set `defaultL3NetworkUuid` to null; apply the same
inverted-predicate fix for the other occurrence that sets
`defaultL3NetworkUuid`.

Comment on lines +82 to 85
selected = CollectionUtils.findOneOrNull(vr.getVmNics(),
arg -> arg.getL3NetworkUuid().equals(struct.getVipL3Network().getUuid()));
String publicMac = selected == null ? null : selected.getMac();

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

publicMac 变为静默 null 会弱化失败语义。

当前 privateMac 会显式断言,但 publicMac 缺失时会继续执行并写入 null,这会把错误延后到下游调用,增加排障成本。建议与 privateMac 保持一致,缺失即快速失败。

建议修复
         selected = CollectionUtils.findOneOrNull(vr.getVmNics(),
                 arg -> arg.getL3NetworkUuid().equals(struct.getVipL3Network().getUuid()));
         String publicMac = selected == null ? null : selected.getMac();
+        DebugUtils.Assert(publicMac != null, String.format("cannot find vip nic[l3NetworkUuid:%s] on virtual router[uuid:%s, name:%s]",
+                struct.getVipL3Network().getUuid(), vr.getUuid(), vr.getName()));
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@plugin/virtualRouterProvider/src/main/java/org/zstack/network/service/virtualrouter/portforwarding/VirtualRouterPortForwardingBackend.java`
around lines 82 - 85, The code silently sets publicMac to null when no VM NIC
matches vip L3 network (selected from vr.getVmNics() by comparing
arg.getL3NetworkUuid() to struct.getVipL3Network().getUuid()), which defers
failure; change this to fail-fast like privateMac by asserting or throwing an
exception if selected is null (i.e., when computing publicMac) so the method
(VirtualRouterPortForwardingBackend logic around publicMac) aborts immediately
with a clear error rather than proceeding with a null MAC.

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.

1 participant