Skip to content

ZSTAC-86788: set first created zone default#4579

Open
zstack-robot-2 wants to merge 1 commit into
5.4.12from
sync/hanyu.liang/fix-86788
Open

ZSTAC-86788: set first created zone default#4579
zstack-robot-2 wants to merge 1 commit into
5.4.12from
sync/hanyu.liang/fix-86788

Conversation

@zstack-robot-2

Copy link
Copy Markdown
Collaborator

Backport of ZSTAC-75952 to 5.4.12.

Source: zstack!10404 (5.5.28).

Sets the first zone created on a fresh installation as the default zone.

Validation: static diff and commit checks only; no case was run per backport requirement.

sync from gitlab !10548

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Failed to post review comments.

GitHub was unavailable or timed out while CodeRabbit was posting the review. Please request a new review later if the pull request still needs one. This happened while posting 1 inline comment. Use @coderabbitai full review to retry the review.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: fd41c26d-c4eb-4a3f-aec1-406a6415f79f

📥 Commits

Reviewing files that changed from the base of the PR and between 53153f1 and dd8dc50.

📒 Files selected for processing (3)
  • compute/src/main/java/org/zstack/compute/zone/ZoneManagerImpl.java
  • conf/db/upgrade/V5.4.12__schema.sql
  • test/src/test/groovy/org/zstack/test/integration/compute/ZoneCase.groovy
🧰 Additional context used
📓 Path-based instructions (2)
**/*.sql

⚙️ CodeRabbit configuration file

**/*.sql: Review the SQL code, make sure has no errors and confirm that:

  • Upgrading scene has been carefully handled
  • Do not use DEFAULT 0000-00-00 00:00:00 , use DEFAULT CURRENT_TIMESTAMP instead
  • When NOT NULL exists, must use stored procedure or other functions to process historical data, this is very very important

Files:

  • conf/db/upgrade/V5.4.12__schema.sql
**/*.java

⚙️ CodeRabbit configuration file

**/*.java: ## 1. API 设计要求

  • API 命名:
    • API 名称必须唯一,不能重复。
    • API 消息类需要继承 APIMessage;其返回类必须继承 APIReplyAPIEvent,并在注释中用 @RestResponse 进行标注。
    • API 消息上必须添加注解 @RestRequest,并满足如下规范:
      • path:
        • 针对资源使用复数形式。
        • 当 path 中引用消息类变量时,使用 {variableName} 格式。
      • HTTP 方法对应:
        • 查询操作 → HttpMethod.GET
        • 更新操作 → HttpMethod.PUT
        • 创建操作 → HttpMethod.POST
        • 删除操作 → HttpMethod.DELETE
    • API 类需要实现 __example__ 方法以便生成 API 文档,并确保生成对应的 Groovy API Template 与 API Markdown 文件。

2. 命名与格式规范

  • 类名:

    • 使用 UpperCamelCase 风格。
    • 特殊情况:
      • VO/AO/EO 类型类除外。
      • 抽象类采用 AbstractBase 前缀/后缀。
      • 异常类应以 Exception 结尾。
      • 测试类需要以 TestCase 结尾。
  • 方法名、参数名、成员变量和局部变量:

    • 使用 lowerCamelCase 风格。
  • 常量命名:

    • 全部大写,使用下划线分隔单词。
    • 要求表达清楚,避免使用含糊或不准确的名称。
  • 包名:

    • 统一使用小写,使用点分隔符,每个部分应是一个具有自然语义的英文单词(参考 Spring 框架的结构)。
  • 命名细节:

    • 避免在父子类或同一代码块中出现相同名字的成员或局部变量,防止混淆。
    • 命名缩写:
      • 不允许使用不必要的缩写,如:AbsSchedulerJobcondiFu 等。应使用完整单词提升可读性。

3. 编写自解释代码

  • 意图表达:

    • 避免使用布尔型参数造成含义不明确。例如:
      • 对于 stopAgent(boolean ignoreError),建议拆分为不同函数(如 stopAgentIgnoreError()),或使用枚举表达操作类型。
    • 命名应尽量用完整的单词组合表达意图,并在名称中体现数据类型或用途(例如在常量与变量名称中,将类型词放在末尾)。
  • 注释:

    • 代码应尽量做到自解释,对少于两行的说明可以直接写在代码中。
    • 对于较长的注释,需要仔细校对并随代码更新,确保内容正确。
    • 接口方法不应有多余的修饰符(例如 public),且必须配有有效的 Javadoc 注释。

4. 流程控制和结构优化

  • if...else 的使用:

    • 应尽量减少 if...else 结构的使用,建议:
      • 限制嵌套层级最多为两层,且内层不应再出现 else 分支。
      • 尽早返回(Early Return),将条件判断中的处理逻辑提前结束或抽成独立方法。
      • 使用 Java Stream 或 Lambda 表达式代替冗长的循环与条件判断。
  • 条件判断:

    • if 条件表达不宜过长或过于复杂,必要时可以将条件抽成 boolean 变量描述。
  • 代码块长度:

    • 单个 if 代码块不宜超过一屏显示,以提高可读性和后续维护性。

5. 异常处理与日志

  • 捕获异常的原则:
    • 对于可以通过预检查避免的 RuntimeException(如 NullPointerExceptionIndexOutOfBoundsException 等),不建议使用 try-catch 来进行处理。
    • 捕获异常应仅用于处理真正的意外情况,不应将异常逻辑当作正常流程控制。
    • 在必要时,应继续抛出异常,使上层业务处理者可以转换为用户友好的错误提示。
    • 使用 try-with-resources 语法管理资源,确保在 finally 块中正确关闭资源,并避免在 finally 中返回值。
      ...

Files:

  • compute/src/main/java/org/zstack/compute/zone/ZoneManagerImpl.java
🧠 Learnings (6)
📚 Learning: 2026-03-05T02:13:23.339Z
Learnt from: zstack-robot-2
Repo: MatheMatrix/zstack PR: 3416
File: conf/db/upgrade/V5.5.12__schema.sql:24-35
Timestamp: 2026-03-05T02:13:23.339Z
Learning: In ZStack, the database schema name is fixed as 'zstack'. For SQL upgrade scripts under conf/db/upgrade, continue using TABLE_SCHEMA = 'zstack' and reference tables as zstack.`UsedIpVO` (i.e., qualify with the schema). Do not replace with DATABASE() or remove the schema qualifier for portability. This pattern can be assumed across upgrade scripts in this directory.

Applied to files:

  • conf/db/upgrade/V5.4.12__schema.sql
📚 Learning: 2026-05-07T09:44:01.429Z
Learnt from: zstack-robot-2
Repo: MatheMatrix/zstack PR: 3891
File: conf/db/upgrade/V5.5.22__schema.sql:28-31
Timestamp: 2026-05-07T09:44:01.429Z
Learning: In ZStack MySQL upgrade scripts under conf/db/upgrade/*.sql, if a table already contains a TIMESTAMP column defined with ON UPDATE CURRENT_TIMESTAMP (e.g., lastOpDate), then any second TIMESTAMP column (e.g., createDate) must NOT use DEFAULT CURRENT_TIMESTAMP. This repository’s CI/validated environment uses MySQL 5.7 with explicit_defaults_for_timestamp=OFF, which can trigger MySQL Error 1293 (only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT/ON UPDATE). Use a non-CURRENT_TIMESTAMP sentinel default such as '2000-01-01 00:00:00' (and typically keep it NOT NULL), and rely on ModelServiceRefVO.PrePersist in Java to set the real creation timestamp for new JPA-managed rows.

Applied to files:

  • conf/db/upgrade/V5.4.12__schema.sql
📚 Learning: 2026-04-22T04:38:00.864Z
Learnt from: ZStack-Robot
Repo: MatheMatrix/zstack PR: 3826
File: compute/src/main/java/org/zstack/compute/allocator/HostAllocatorChain.java:261-265
Timestamp: 2026-04-22T04:38:00.864Z
Learning: 在 ZStack 的 `Platform.err()` / `Platform.toRawString()` 调用中,传入用于格式化的 `%s` 参数若实现了 `org.zstack.header.core.I18nMessage`,框架会自动通过 `getDetails()` 取得原始文本、并通过 `getI18nDetails()` 取得国际化文本。因此,在代码审查中不应将“直接把实现了 `I18nMessage` 的对象作为 `%s` 参数传入而可能打印出对象地址/不安全”当作问题;此用法是正确且应当被视为安全的(例如将 `HostCandidate.RejectedCandidate` 这类实现了 `I18nMessage` 的对象作为 `%s` 参数传入)。

Applied to files:

  • compute/src/main/java/org/zstack/compute/zone/ZoneManagerImpl.java
📚 Learning: 2026-04-22T14:27:42.894Z
Learnt from: ZStack-Robot
Repo: MatheMatrix/zstack PR: 3837
File: compute/src/main/java/org/zstack/compute/vm/VmInstanceBase.java:8789-8791
Timestamp: 2026-04-22T14:27:42.894Z
Learning: In ZStack compute code, calls to VmInstanceResourceMetadataManager.deleteVmResourceMetadata(...) should generally be treated as best-effort: do not fail/block the primary VM operation when deletion fails (for example, during detach/swap flows such as VmDetachNicFlow, VolumeManagerImpl, KVMHostFactory, and VmInstanceBase when swapping the default CD-ROM). Prefer logging the returned ErrorCode (and relevant context) rather than enforcing strict failure, unless maintainers explicitly request strict enforcement for that specific flow.

Applied to files:

  • compute/src/main/java/org/zstack/compute/zone/ZoneManagerImpl.java
📚 Learning: 2026-05-09T02:01:09.283Z
Learnt from: zstack-robot-1
Repo: MatheMatrix/zstack PR: 3906
File: compute/src/main/java/org/zstack/compute/zone/ZoneManagerImpl.java:162-164
Timestamp: 2026-05-09T02:01:09.283Z
Learning: In ZStack compute code, when invoking post-commit extension point hooks (for example `ZoneExtensionPointEmitter.afterCreate`, `ClusterExtensionPointEmitter.afterCreate`, and other `*ExtensionPointEmitter.after*` hooks), treat extension implementations as fail-loud by convention. Do not wrap these hook invocations in `try/catch` to suppress or convert exceptions—let exceptions thrown by the extension propagate to the caller so they are detected and handled. If you must do local cleanup, use `finally` for resource cleanup, but do not swallow or mask the thrown exception.

Applied to files:

  • compute/src/main/java/org/zstack/compute/zone/ZoneManagerImpl.java
📚 Learning: 2026-03-17T02:08:46.892Z
Learnt from: zstack-robot-2
Repo: MatheMatrix/zstack PR: 3513
File: test/src/test/groovy/org/zstack/test/integration/networkservice/provider/flat/FlatChangeVmIpOutsideCidrCase.groovy:371-371
Timestamp: 2026-03-17T02:08:46.892Z
Learning: In ZStack Groovy integration tests, avoid adding size checks before indexing results (e.g., using [0] on queryVmInstance results). This behavior is intentional in tests like FlatChangeVmIpOutsideCidrCase.groovy; do not suggest or enforce size assertions prior to indexing query results in these tests. If applying this guidance broadly, it should cover all Groovy integration tests under test/src/test/groovy/org/zstack/test/integration/ with similar patterns.

Applied to files:

  • test/src/test/groovy/org/zstack/test/integration/compute/ZoneCase.groovy

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

调整 Zone 创建时的默认标记条件,新增升级脚本回填缺失的默认 Zone,并在集成测试中验证首个 Zone 会成为唯一默认 Zone。

Changes

默认 Zone 行为

Layer / File(s) Summary
Zone 创建默认值逻辑
compute/src/main/java/org/zstack/compute/zone/ZoneManagerImpl.java
仅当请求未指定 default 且不存在 Zone 记录时自动设置默认标记;default=null 请求进入链式处理流程。
升级脚本回填默认 Zone
conf/db/upgrade/V5.4.12__schema.sql
新增并执行临时存储过程,在缺少默认 Zone 时选择最早创建的 Zone 进行回填,随后删除该过程。
首个 Zone 默认行为验证
test/src/test/groovy/org/zstack/test/integration/compute/ZoneCase.groovy
新增测试并加入执行链,验证首个 Zone 及数据库记录被标记为默认,且默认 Zone 数量为 1。

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

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant ZoneManagerImpl
  participant Database
  participant ZoneCase
  Client->>ZoneManagerImpl: 创建首个 Zone
  ZoneManagerImpl->>Database: 查询并保存默认标记
  Database-->>ZoneManagerImpl: 返回 Zone 记录
  ZoneManagerImpl-->>Client: 返回创建结果
  ZoneCase->>Database: 查询默认 Zone
  Database-->>ZoneCase: 返回唯一默认 Zone
Loading

Poem

小兔挥爪改 Zone,
首个区域戴王冠。
数据库里补旧缺,
测试蹦跳数一番。
默认旗帜稳稳安。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确概括了首个创建的 zone 被设为默认的核心变更。
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/hanyu.liang/fix-86788

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

DBImpact

Resolves: ZSTAC-86788

Change-Id: I746dc070d672e8191a0bf4bf995584ca5f5b8266
@MatheMatrix
MatheMatrix force-pushed the sync/hanyu.liang/fix-86788 branch from 2d6b51e to dd8dc50 Compare July 16, 2026 09:56
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