Skip to content

Conversation

@ZStack-Robot
Copy link
Collaborator

Resolves: ZSTAC-77668

Change-Id: I6166696f686d6f677363766678646c7567746168

sync from gitlab !8437

Resolves: ZSTAC-77668

Change-Id: I6166696f686d6f677363766678646c7567746168
@coderabbitai
Copy link

coderabbitai bot commented Sep 18, 2025

Walkthrough

在会话过期检查中,新增在注销前调用插件注册表中的 LogoutExtensionPoint.beforeLogout(SessionInventory) 钩子;随后继续执行原有的注销并返回 INVALID_SESSION 错误。

Changes

Cohort / File(s) Change Summary
会话过期注销前扩展点
identity/src/main/java/org/zstack/identity/Session.java
在 checkSessionExpired 流程中,过期会话注销前迭代 PluginRegistry 中的 LogoutExtensionPoint 并调用 beforeLogout(s),然后执行 logout(s.getUuid()) 并返回 INVALID_SESSION。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Identity as Identity.Session
  participant Registry as PluginRegistry
  participant Ext as LogoutExtensionPoint(s)
  participant Auth as Logout Service

  Note over Identity: 检测到会话过期
  Identity->>Registry: 获取所有 LogoutExtensionPoint
  loop 对每个扩展点
    Identity->>Ext: beforeLogout(SessionInventory)
  end
  Identity->>Auth: logout(sessionUuid)
  Auth-->>Identity: 完成
  Identity-->>Identity: 返回 INVALID_SESSION
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

我在代码田里啃胡萝,耳朵随风摆摇曳
会话一过期,先敲插件门说声别
beforeLogout 轻轻落,扩展点里话未绝
随后注销如常去,INVALID 回声贴
小兔挥爪合上页,月下仓库更清洁

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed 标题与提交的会话登出相关改动有联系,但并不完全匹配提交内容;PR 的代码主要是在会话过期的登出流程中新增对 LogoutExtensionPoint.beforeLogout 的调用,而标题“validate session logout token”更像是在描述“验证登出令牌”的实现,未直接反映新增的扩展点或会话过期逻辑,因此应视为部分相关但略有误导。
Description Check ✅ Passed PR 描述包含关联的问题号、Change-Id 及同步来源,和本次变更是相关的但内容非常简略;它没有详细说明代码中新增的 beforeLogout 扩展点或具体受影响的文件,但根据宽松的检查标准,这个描述并非完全无关或离题,因此应判定为通过。
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sync/lin.ma/ZSTAC-77668

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

Copy link

@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: 1

🧹 Nitpick comments (3)
identity/src/main/java/org/zstack/identity/Session.java (3)

223-227: 并发情形下 beforeLogout 可能被多次触发,需确保扩展幂等

两个请求并发命中同一过期会话时,均会走到该分支并调用 beforeLogout,然后其中一个成功删除记录。建议在扩展实现侧保证幂等,或在调用前做一次快速存在性检查(非必需)。


223-227: 补充轻量可观测性日志

为定位扩展侧问题与调用耗时,建议在 debug/trace 级别记录扩展调用开始/结束与耗时(保持默认关闭,不影响性能)。


223-227: 统一 beforeLogout 回调:定时/批量删除会绕过扩展回调

验证结果:代码中有显式调用 Session.logout(...) 的路径(identity/.../Session.java:141、identity/.../login/LoginManagerImpl.java:257、identity/.../AccountManagerImpl.java:1145),且 Session.logout 内也会触发 SessionLogoutExtensionPoint 并对 SessionVO 做 hardDelete(约在 Session.java 行 177–180)。但存在直接批量删除会话的路径,会绕过 logout 回调:identity/.../Session.java:294–297 中执行了 "delete from SessionVO s where s.uuid in :uuids" 并直接 dq.executeUpdate()。结论:并非所有会话终结都会触发 beforeLogout,定时/批量清理会跳过回调。

建议(非破坏性改进):评估统一触发点——例如在 deleteSession()/批量删除前补齐 before 钩子,或将定时清理改为逐条调用 Session.logout,以保证扩展一致性;可先作为后续增强保留最小改动策略。

📜 Review details

Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9528b03 and 0b9eb65.

📒 Files selected for processing (1)
  • identity/src/main/java/org/zstack/identity/Session.java (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.*

⚙️ CodeRabbit configuration file

**/*.*: - 代码里不应当有有中文,包括报错、注释等都应当使用正确的、无拼写错误的英文来写

Files:

  • identity/src/main/java/org/zstack/identity/Session.java
**/*.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()),或使用枚举表达操作类型。
    • 命名应尽量用完整的单词组合表达意图,并在名称中体现数据类型或用途(例如在常量与变量名称中,将类型词放在末尾)。
    • 避免使用魔法值(Magic Value):
      • 直接使用未经定义的数值或字符串(如 if (status == 5))应替换为枚举或常量。

      • 示例:

      • // 错误示例:魔法值

      • if (user.getStatus() == 5) { ... }

      • // 正确示例:常量或枚举

      • public static final int STATUS_ACTIVE = 5;

      • if (user.getStatus() == STATUS_ACTIVE) { ... }

      • // 或使用枚举

      • enum UserStatus { ACTIVE, INACTIVE }

  • 注释:

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

4. 流程控制和结构优化

  • if...else 的使用:

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

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

Files:

  • identity/src/main/java/org/zstack/identity/Session.java
🧠 Learnings (1)
📓 Common learnings
Learnt from: zstack-robot-1
PR: MatheMatrix/zstack#2360
File: network/src/main/java/org/zstack/network/l3/L3BasicNetwork.java:449-490
Timestamp: 2025-08-04T04:48:19.103Z
Learning: ZStack项目在cherry-pick操作中,即使发现了性能优化机会(如IP地址批量保存的内存优化),也严格遵循不做额外修改的政策,优先保证cherry-pick的完整性和一致性。
Learnt from: ZStack-Robot
PR: MatheMatrix/zstack#2489
File: storage/src/main/java/org/zstack/storage/snapshot/VolumeTree.java:471-545
Timestamp: 2025-08-22T05:36:40.467Z
Learning: In ZStack项目中,对于核心逻辑相关的代码,团队倾向于暂时不进行重构,即使代码复杂度较高,优先保证核心功能的稳定性。
Learnt from: ZStack-Robot
PR: MatheMatrix/zstack#2325
File: compute/src/main/java/org/zstack/compute/vm/VmMigrateCallExtensionFlow.java:29-29
Timestamp: 2025-07-24T05:53:10.246Z
Learning: 在ZStack项目的重构过程中,团队采用务实的优先级策略:优先完成影响运行时功能的代码重构,对于注释中的旧引用等非功能性问题可以延后处理,因为这些不会影响系统的实际运行。

Comment on lines +223 to +227
PluginRegistry pluginRgty = getComponentLoader().getComponent(PluginRegistry.class);

for (LogoutExtensionPoint ext : pluginRgty.getExtensionList(LogoutExtensionPoint.class)) {
ext.beforeLogout(s);
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

在过期注销前缺少故障隔离:单个扩展抛异常会阻断注销与错误返回

beforeLogout() 位于请求链路的过期校验路径上,一旦任一扩展抛出异常,将导致后续 logout() 未执行、请求拿不到期望的 INVALID_SESSION。建议捕获并记录,继续执行注销,避免扩展影响核心行为。

可按以下方式加护栏(保持最小改动):

-PluginRegistry pluginRgty = getComponentLoader().getComponent(PluginRegistry.class);
-for (LogoutExtensionPoint ext : pluginRgty.getExtensionList(LogoutExtensionPoint.class)) {
-    ext.beforeLogout(s);
-}
+PluginRegistry pluginRgty = getComponentLoader().getComponent(PluginRegistry.class);
+for (LogoutExtensionPoint ext : pluginRgty.getExtensionList(LogoutExtensionPoint.class)) {
+    try {
+        ext.beforeLogout(s);
+    } catch (Throwable t) {
+        logger.warn(String.format(
+                "ignore error from %s.beforeLogout on session[uuid:%s]",
+                ext.getClass().getSimpleName(), s.getUuid()), t);
+    }
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
PluginRegistry pluginRgty = getComponentLoader().getComponent(PluginRegistry.class);
for (LogoutExtensionPoint ext : pluginRgty.getExtensionList(LogoutExtensionPoint.class)) {
ext.beforeLogout(s);
}
PluginRegistry pluginRgty = getComponentLoader().getComponent(PluginRegistry.class);
for (LogoutExtensionPoint ext : pluginRgty.getExtensionList(LogoutExtensionPoint.class)) {
try {
ext.beforeLogout(s);
} catch (Throwable t) {
logger.warn(String.format(
"ignore error from %s.beforeLogout on session[uuid:%s]",
ext.getClass().getSimpleName(), s.getUuid()), t);
}
}
🤖 Prompt for AI Agents
In identity/src/main/java/org/zstack/identity/Session.java around lines 223 to
227, the loop calling each LogoutExtensionPoint.beforeLogout(s) lacks fault
isolation so any extension throwing an exception aborts logout and prevents
returning INVALID_SESSION; wrap each ext.beforeLogout(s) invocation in a
try-catch that catches Throwable (or Exception), log the exception with context
(which extension and session info) and continue to the next extension so logout
proceeds regardless of individual extension failures.

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