Skip to content

Conversation

KomachiSion
Copy link
Collaborator

(cherry picked from commit bd7a1dd)

Please do not create a Pull Request without creating an issue first.

What is the purpose of the change

Fixes #13400

Brief changelog

XX

Verifying this change

XXXX

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.

@KomachiSion KomachiSion added this to the 2.5.2 milestone Jun 4, 2025
Copy link

github-actions bot commented Jun 4, 2025

Thanks for your this PR. 🙏
Please check again for your PR changes whether contains any usage/api/configuration change such as Add new API , Add new configuration, Change default value of configuration.
If so, please add or update documents(markdown type) in docs/next/ for repository nacos-group/nacos-group.github.io


感谢您提交的PR。 🙏
请再次查看您的PR内容,确认是否包含任何使用方式/API/配置参数的变更,如:新增API新增配置参数修改默认配置等操作。
如果是,请确保在提交之前,在仓库nacos-group/nacos-group.github.io中的docs/next/目录下添加或更新文档(markdown格式)。

Copy link

lingma-agents bot commented Jun 4, 2025

修复当连接的appName无效时从请求头获取应用名称的逻辑

变更文件

文件路径 变更说明
core/​src/​main/​java/​com/​alibaba/​nacos/​core/​remote/​grpc/​GrpcRequestAcceptor.java 新增`DEFAULT_APP_NAME`常量,当连接的appName为空或等于默认值时,从请求头`APP_FILED`获取应用名称,避免返回`-`占位符
core/​src/​test/​java/​com/​alibaba/​nacos/​core/​remote/​grpc/​GrpcRequestAcceptorTest.java 添加`testHandleRequestSuccessWithAppNameInHeader`等2个测试用例,补充`tearDown`方法清理上下文,以及引入`HttpHeaderConsts`等类依赖

时序图

sequenceDiagram
    participant G as GrpcRequestAcceptor
    participant C as ConnectionMeta
    participant H as HealthCheckRequest
    G->>C: 获取connection的appName
    alt appName无效(空或"-")
        G->>H: 从请求头获取APP_FILED值
        H-->>G: 返回appName
    else appName有效
        G-->>G: 直接使用connection的appName
    end
    G->>RequestContext: 设置最终的app值
Loading

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @Lingma-Agent 分析这个方法的性能瓶颈并提供优化建议。

  • @Lingma-Agent 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @Lingma-Agent 请总结上述讨论并提出解决方案。

  • @Lingma-Agent 请根据讨论内容生成优化代码。

Copy link

@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

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

🔍 代码评审报告

🎯 评审意见概览

严重度 数量 说明
🔴 Blocker 0 阻断性问题,需立即修复。例如:系统崩溃、关键功能不可用或严重安全漏洞。
🟠 Critical 0 严重问题,高优先级修复。例如:核心功能异常或性能瓶颈影响用户体验。
🟡 Major 0 主要问题,建议修复。例如:非核心功能缺陷或代码维护性较差。
🟢 Minor 1 次要问题,酌情优化。例如:代码格式不规范或注释缺失。

总计: 1 个问题


📋 评审意见详情

💡 单文件建议

以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
☕ core/src/test/java/com/alibaba/nacos/core/remote/grpc/GrpcRequestAcceptorTest.java (1 💬)

🚀 跨文件建议

以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍 1. 测试覆盖未全面覆盖所有边界条件

新增的appName处理逻辑未完全覆盖所有边界条件。例如,当连接的appName为'-'且请求头未设置APP_FILED时,应设置为'unknown',但现有测试未覆盖此情况。系统测试策略需要确保所有条件分支(如connection的appName为'-'但请求头为空)均被验证,以避免潜在逻辑漏洞。

⚠️ 潜在风险: 未覆盖的边界条件可能导致实际运行时appName未正确设置,引发后续业务逻辑错误(如权限校验失败、日志记录异常等)。

🔍 2. 代码逻辑复杂度增加,建议重构

GrpcRequestAcceptorprepareRequestContext方法中,新增的条件判断逻辑(检查appName是否为'-'或空)直接嵌入方法内,可能增加代码复杂度。建议将appName解析逻辑提取为独立方法或工具类,以提高可维护性和一致性,避免未来修改时遗漏条件。

📌 关键代码:

if (StringUtils.isBlank(app) || StringUtils.equals(DEFAULT_APP_NAME, app)) {
    app = request.getHeader(HttpHeaderConsts.APP_FILED, "unknown");
}

⚠️ 潜在风险: 代码复杂度增加可能导致后续维护困难,新增或修改条件时容易引发逻辑错误。

🔍 3. 系统行为变更未充分评估兼容性

改动改变了appName的解析逻辑(原仅检查空值,现扩展至检查'-'),可能影响其他依赖原有行为的组件(如日志模块、权限校验模块)。需确认系统内其他组件是否适配此变更,或是否存在未覆盖的依赖关系。

⚠️ 潜在风险: 未评估的兼容性问题可能导致系统其他模块因appName解析逻辑变化而出现不可预期的行为(如权限错误、日志不一致等)。


💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @Lingma-Agent 分析这个方法的性能瓶颈并提供优化建议。

  • @Lingma-Agent 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @Lingma-Agent 请总结上述讨论并提出解决方案。

  • @Lingma-Agent 请根据讨论内容生成优化代码。

@@ -309,6 +317,7 @@ public void onNext(Payload payload) {
System.out.println("Receive data from server: " + payload);
Object res = GrpcUtils.parse(payload);
assertTrue(res instanceof HealthCheckResponse);
assertEquals("unknown", RequestContextHolder.getContext().getBasicContext().getApp());
Copy link

Choose a reason for hiding this comment

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

测试用例未覆盖appName为-但请求头未设置的情况

🟢 Minor | 🧹 Code Smells

📋 问题详情

新增的测试用例testHandleRequestSuccessWithAppNameInHeader验证了当连接的appName为-且请求头存在时的行为,但未测试当连接appName为-且请求头无APP_FIELD字段时的逻辑。该场景应返回默认值unknown

💡 解决方案

新增测试用例覆盖以下场景:

+    @Test
+    void testHandleRequestSuccessWithDefaultAppName() {
+        ApplicationUtils.setStarted(true);
+        Mockito.when(requestHandlerRegistry.getByRequestType(Mockito.anyString())).thenReturn(mockHandler);
+        Mockito.when(connectionManager.checkValid(Mockito.any())).thenReturn(true);
+        ConnectionMeta connectionMeta = new ConnectionMeta("id", "ip", "ip", 8888, 9848, "GRPC", "", "-", new HashMap<>());
+        Mockito.when(connectionManager.getConnection(Mockito.any())).thenReturn(new GrpcConnection(connectionMeta, null, null));
+
+        RequestMeta metadata = new RequestMeta();
+        HealthCheckRequest mockRequest = new HealthCheckRequest();
+        Payload payload = GrpcUtils.convert(mockRequest, metadata);
+
+        StreamObserver<Payload> observer = new StreamObserver<Payload>() {
+            @Override
+            public void onNext(Payload p) {
+                assertEquals("unknown", RequestContextHolder.getContext().getBasicContext().getApp());
+            }
+            // ... 其他实现
+        };
+        streamStub.request(payload, observer);
+    }

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

@KomachiSion KomachiSion merged commit 79e3855 into alibaba:v2.x-develop Jun 4, 2025
6 checks passed
@KomachiSion KomachiSion deleted the v2.x-develop#13400 branch June 9, 2025 08:13
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