Skip to content

[ZSTAC-83890][ha] create pre-fence tag after HA state update#4027

Open
zstack-robot-2 wants to merge 1 commit into
5.5.22from
sync/yingzhe.hu/fix/ZSTAC-83890-pre-fence-tag-5.5.22
Open

[ZSTAC-83890][ha] create pre-fence tag after HA state update#4027
zstack-robot-2 wants to merge 1 commit into
5.5.22from
sync/yingzhe.hu/fix/ZSTAC-83890-pre-fence-tag-5.5.22

Conversation

@zstack-robot-2
Copy link
Copy Markdown
Collaborator

背景

ZSTAC-83890 review 要求 createPreFencePendingTag 放到 HaStartVmInstanceMsg 的 handle 流程中,并且位于 VM state/hostUuid 数据库更新之后,保证后续 pre-fence 逻辑读取到的 tag 与数据库状态一致。

改动

  • VmSystemTags 中定义 HA_PRE_FENCE_PENDING,使 core 可以直接创建该 tag。
  • VmInstanceBase.handle(HaStartVmInstanceMsg) 中,sql.update(); refreshVO(); 后创建 pre-fence pending tag,再进入 startVm(...)

验证

  • git diff --check
  • mvn -pl compute -am -DskipTests compile
  • mvn -pl compute -am -DskipTests install

sync from gitlab !9922

Move HA pre-fence pending tag creation into HaStartVmInstanceMsg handling after the VM state update, so later pre-fence execution observes the same database state.

Resolves: ZSTAC-83890

Change-Id: I7f046732e0245319ed4cf188d1342b65dad11767
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

Review Change Stack

概览

在虚拟机 HA 启动流程中新增预围栏待处理系统标签机制。定义了标签格式和 token 常量,并在 HA 启动时根据怀疑主机和可访问对等主机信息自动创建标签。

变更

HA 预围栏待处理标签系统

层级 / 文件 摘要
HA 预围栏标签定义和常量
compute/src/main/java/org/zstack/compute/vm/VmSystemTags.java
新增 HA_PRE_FENCE_SUSPECT_HOST_UUID_TOKENHA_PRE_FENCE_ACCESSIBLE_PEER_HOST_UUID_TOKEN 常量和 HA_PRE_FENCE_PENDING 系统标签定义,模式为 haPreFencePending::{suspectHostUuid}::{accessiblePeerHostUuid}
HA 启动流程中的标签创建
compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java
在 HA 启动时刷新虚拟机对象后调用 createPreFencePendingTag() 创建预围栏标签;新增私有方法从 softAvoidHostUuids 推导怀疑主机 UUID(缺失则用 lastHostUuid),从 accessiblePeerHostUuid 推导对等主机,两者存在时创建带 token 的系统标签。

🎯 2 (Simple) | ⏱️ ~12 分钟

🐰 灾难准备新标签来,
预围栏之心已安排,
怀疑与对等相配对,
HA 启动更周全,
标签系统展开怀!


Important

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 error, 1 warning)

Check name Status Explanation Resolution
Title check ❌ Error 标题格式不符合要求,未遵循 '[scope]: ' 格式,缺少冒号。 将标题改为符合格式的形式,例如 '[fix][ha]: Create pre-fence tag after HA state update',确保冒号在方括号后。
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 (3 passed)
Check name Status Explanation
Description check ✅ Passed PR 描述清晰地说明了背景、改动和验证步骤,与代码变更相关且内容详尽。
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.
✨ 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/yingzhe.hu/fix/ZSTAC-83890-pre-fence-tag-5.5.22

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.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.

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

1059-1061: ⚡ Quick win

建议优先选取 softAvoidHostUuids 中首个非空白值,避免因首元素空白导致误跳过打标签。

当前只取 get(0),若首元素是空白字符串而后续元素有效,会直接走“缺失”分支并跳过创建 pre-fence tag。建议先过滤空白值再回退到 lastHostUuid

可参考修改
-        String suspectHostUuid = StringUtils.trimToNull(CollectionUtils.isEmpty(msg.getSoftAvoidHostUuids()) ?
-                self.getLastHostUuid() : msg.getSoftAvoidHostUuids().get(0));
+        String suspectHostUuid = Optional.ofNullable(msg.getSoftAvoidHostUuids())
+                .orElse(Collections.emptyList())
+                .stream()
+                .map(StringUtils::trimToNull)
+                .filter(Objects::nonNull)
+                .findFirst()
+                .orElse(StringUtils.trimToNull(self.getLastHostUuid()));

As per coding guidelines: “注意检查来自 Message 的参数是否做过 trim,用户可能在浏览器上复制粘贴的数据带有空格、换行符等”。

🤖 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/VmInstanceBase.java` around lines
1059 - 1061, 在 VmInstanceBase 中计算 suspectHostUuid 时不要直接使用
msg.getSoftAvoidHostUuids().get(0);应先对 msg.getSoftAvoidHostUuids() 做过滤并对每个元素
trim 后取第一个非空值(例如通过 stream/filter + StringUtils.trimToNull),如果没有可用元素再回退到
self.getLastHostUuid(); 同样确保对 msg.getAccessiblePeerHostUuid() 使用
StringUtils.trimToNull 来去除空白并赋给 peerHostUuid,避免因首元素为空白而跳过创建 pre-fence tag。
🤖 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.

Nitpick comments:
In `@compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java`:
- Around line 1059-1061: 在 VmInstanceBase 中计算 suspectHostUuid 时不要直接使用
msg.getSoftAvoidHostUuids().get(0);应先对 msg.getSoftAvoidHostUuids() 做过滤并对每个元素
trim 后取第一个非空值(例如通过 stream/filter + StringUtils.trimToNull),如果没有可用元素再回退到
self.getLastHostUuid(); 同样确保对 msg.getAccessiblePeerHostUuid() 使用
StringUtils.trimToNull 来去除空白并赋给 peerHostUuid,避免因首元素为空白而跳过创建 pre-fence tag。

ℹ️ 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: 0ecc1e51-8c5c-4399-8251-24a13b6f9e8a

📥 Commits

Reviewing files that changed from the base of the PR and between b6a3b24 and 7cfb204.

📒 Files selected for processing (2)
  • compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java
  • compute/src/main/java/org/zstack/compute/vm/VmSystemTags.java

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