Skip to content

<fix>[kvm]: extend reconnect echo timeout after libvirtd restart#4479

Open
zstack-robot-2 wants to merge 1 commit into
5.4.10from
sync/haoyu.ding/fix-cherrypick5@@3
Open

<fix>[kvm]: extend reconnect echo timeout after libvirtd restart#4479
zstack-robot-2 wants to merge 1 commit into
5.4.10from
sync/haoyu.ding/fix-cherrypick5@@3

Conversation

@zstack-robot-2

Copy link
Copy Markdown
Collaborator

Resolves: ZSTAC-84691

Change-Id: I6668736975707575756d7665647867686e6b6b76

(cherry picked from commit e7851c6)

sync from gitlab !10441

Resolves: ZSTAC-84691

Change-Id: I6668736975707575756d7665647867686e6b6b76

(cherry picked from commit e7851c6)
@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

.coderabbit.yaml has a parsing error

The CodeRabbit configuration file in this repository has a parsing error and default settings were used instead. Please fix the error(s) in the configuration file. You can initialize chat with CodeRabbit to get help with the configuration file.

💥 Parsing errors (1)
Could not fetch remote config from http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml: TimeoutError: The operation was aborted due to timeout
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Walkthrough

本次变更为KVM主机连接流程新增libvirtd重启后echo等待超时自适应机制。KVMHostUtils新增常量与工具方法用于判断是否重启libvirtd、统计相关VM数量并计算扩展超时;KVMHost在echo-host流程中改用该动态超时。同时新增Java和Groovy测试覆盖相关逻辑。

Changes

Libvirtd重启echo超时自适应

Layer / File(s) Summary
超时计算工具方法与常量
plugin/kvm/src/main/java/org/zstack/kvm/KVMHostUtils.java
新增3个超时阈值常量及3个静态方法:判断部署期是否需重启libvirtd、统计host上非Stopped状态VM数、根据VM数计算受上限约束的动态毫秒级echo超时。
KVMHost连接流程集成动态超时
plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java
新增getLibvirtRestartedEchoTimeout()方法,连接流程状态中加入libvirtRestarted标记;部署完成后据此判定,echo-host及假死重启二次echo分支均改用动态超时替换固定的REST_FACADE_ECHO_TIMEOUT
单元测试与集成测试
test/src/test/java/org/zstack/test/kvm/KVMHostUtilsTest.java, test/src/test/groovy/org/zstack/test/integration/kvm/host/LibvirtRestartEchoTimeoutCase.groovy
新增单元测试覆盖shouldRestartLibvirtdDuringDeploycalculateLibvirtRestartEchoTimeoutMillis在阈值、超阈值、上限约束等场景下的行为;新增集成测试验证超时计算及VM计数排除Stopped虚拟机的逻辑。

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant KVMHost
  participant KVMHostUtils
  participant Database

  KVMHost->>KVMHostUtils: shouldRestartLibvirtdDuringDeploy(init, restartLibvirtd)
  KVMHostUtils-->>KVMHost: libvirtRestarted
  alt libvirtRestarted为true
    KVMHost->>KVMHostUtils: countVmsForLibvirtRestartEchoTimeout(hostUuid)
    KVMHostUtils->>Database: 查询非Stopped状态的VM数量
    Database-->>KVMHostUtils: vmCount
    KVMHostUtils->>KVMHostUtils: calculateLibvirtRestartEchoTimeoutMillis(vmCount)
    KVMHostUtils-->>KVMHost: echoTimeout(扩展)
  else libvirtRestarted为false
    KVMHost->>KVMHost: echoTimeout=REST_FACADE_ECHO_TIMEOUT
  end
  KVMHost->>KVMHost: restf.echo(echoPath, echoTimeout)
Loading

Poem

兔子敲键盘,超时算得妙 🐰
libvirtd重启莫慌张,
数一数虚拟机几只忙,
加点耐心加点秒,
echo稳稳到彼岸~

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确概括了本次改动:在 libvirtd 重启后延长 KVM 重连 echo 超时。
Description check ✅ Passed 描述虽简短,但与该补丁的来源、工单和同步信息一致,未偏离本次改动。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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/haoyu.ding/fix-cherrypick5@@3

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.44.1)
plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java

ast-grep timed out on this file


Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
test/src/test/groovy/org/zstack/test/integration/kvm/host/LibvirtRestartEchoTimeoutCase.groovy (1)

45-59: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

testCalculateLibvirtRestartEchoTimeout与单元测试内容重复

该方法的断言与KVMHostUtilsTest.java中4个测试方法完全重复,且不依赖env环境(host/vm),放在集成测试中会不必要地增加该用例套件的执行开销。可考虑仅保留testCountVmsForLibvirtRestartEchoTimeoutExcludesStoppedVm(真正需要DB/环境支持的用例),将纯逻辑断言留给Java单元测试即可。

🤖 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
`@test/src/test/groovy/org/zstack/test/integration/kvm/host/LibvirtRestartEchoTimeoutCase.groovy`
around lines 45 - 59, Remove the pure logic coverage from
LibvirtRestartEchoTimeoutCase by deleting
testCalculateLibvirtRestartEchoTimeout, since its assertions are already covered
in KVMHostUtilsTest and it does not need env-dependent integration setup. Keep
only the env/DB-backed test that validates VM counting behavior, and leave the
timeout calculation checks in the existing unit tests around
KVMHostUtils.calculateLibvirtRestartEchoTimeoutMillis.
plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java (1)

5903-5949: 🧹 Nitpick | 🔵 Trivial

大规模主机上echo超时延长可能推迟"假死重启"检测

libvirtRestarted为真且主机VM数量较多时,首次restf.echo的等待时间最长可达180s(或更高,取决于REST_FACADE_ECHO_TIMEOUT基础值)。若此时kvmagent确实处于假死状态(而非仅仅因libvirt初始化慢导致响应慢),"假死重启"逻辑的触发会因此被推迟,从而延长整体主机重连耗时。这是该特性的合理权衡,建议在验收/监控层面关注大规模VM主机重连耗时的变化。

🤖 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 `@plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java` around lines 5903 -
5949, The `restf.echo` retry path in `KVMHost` can delay fake-dead detection too
long when `libvirtRestarted` is true and the host is slow, so adjust the timeout
strategy in this block to keep the initial echo check short enough to trigger
`AnsibleRunner.restartAgent` promptly. Use `libvirtRestarted`,
`getLibvirtRestartedEchoTimeout()`, and the `restf.echo(..., Completion)` fail
branch to separate “restart verification” from the longer post-restart
stabilization wait, so the fake-dead restart decision is not postponed by the
extended echo timeout.
🤖 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 `@plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java`:
- Around line 5903-5949: The `restf.echo` retry path in `KVMHost` can delay
fake-dead detection too long when `libvirtRestarted` is true and the host is
slow, so adjust the timeout strategy in this block to keep the initial echo
check short enough to trigger `AnsibleRunner.restartAgent` promptly. Use
`libvirtRestarted`, `getLibvirtRestartedEchoTimeout()`, and the `restf.echo(...,
Completion)` fail branch to separate “restart verification” from the longer
post-restart stabilization wait, so the fake-dead restart decision is not
postponed by the extended echo timeout.

In
`@test/src/test/groovy/org/zstack/test/integration/kvm/host/LibvirtRestartEchoTimeoutCase.groovy`:
- Around line 45-59: Remove the pure logic coverage from
LibvirtRestartEchoTimeoutCase by deleting
testCalculateLibvirtRestartEchoTimeout, since its assertions are already covered
in KVMHostUtilsTest and it does not need env-dependent integration setup. Keep
only the env/DB-backed test that validates VM counting behavior, and leave the
timeout calculation checks in the existing unit tests around
KVMHostUtils.calculateLibvirtRestartEchoTimeoutMillis.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9e391a83-fca8-410c-81a1-5a9c00c76c50

📥 Commits

Reviewing files that changed from the base of the PR and between e51bb82 and 1869d02.

📒 Files selected for processing (4)
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMHostUtils.java
  • test/src/test/groovy/org/zstack/test/integration/kvm/host/LibvirtRestartEchoTimeoutCase.groovy
  • test/src/test/java/org/zstack/test/kvm/KVMHostUtilsTest.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