-
Notifications
You must be signed in to change notification settings - Fork 129
fix: Make sure AiRoutes with fallback enabled are also synced after a provider is updated #633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: Make sure AiRoutes with fallback enabled are also synced after a provider is updated #633
Conversation
… provider is updated
修复AI路由在提供者更新时的同步问题并增强处理逻辑变更概述
变更文件
时序图sequenceDiagram
participant LPS as LlmProviderServiceImpl
participant ARS as AiRouteService
LPS->>LPS: update(provider)
alt Provider handlers updated
LPS->>LPS: register Qwen & Azure handlers
end
LPS->>LPS: syncRelatedAiRoutes(provider)
loop For each AiRoute
LPS->>LPS: check upstreams for provider
alt Match found or fallback enabled with match
LPS->>ARS: update(AiRoute)
end
end
💡 小贴士与 lingma-agents 交流的方式📜 直接回复评论
📜 在代码行处标记
📜 在讨论中提问
|
There was a problem hiding this 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 | 3 | 次要问题,酬情优化。例如:代码格式不规范或注释缺失。 |
总计: 3 个问题
📋 评审意见详情
💡 代码实现建议
以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
☕ backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ai/LlmProviderServiceImpl.java (3 💬)
- 添加对`AiUpstream`类的导入声明。 (L42)
- 初始化静态处理器列表时添加新的处理程序实例。 (L62)
- 当AI路由服务不可用时抛出异常。 (L300)
🚀 架构设计建议
以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍1. 同步逻辑中潜在的重复处理风险
在 syncRelatedAiRoutes 方法中,当 AI 路由配置了回退(fallback)机制时,如果主上游和回退上游都引用了同一个 LLM 提供商,则可能导致对该路由的不必要多次更新。这不仅影响性能,还可能引发并发问题或数据一致性问题。建议优化逻辑以避免重复处理同一 AI 路由。
📌 关键代码
if (hasProvider(aiRoute.getUpstreams(), providerName)
|| aiRoute.getFallbackConfig() != null && Boolean.TRUE.equals(aiRoute.getFallbackConfig().getEnabled())
&& hasProvider(aiRoute.getFallbackConfig().getUpstreams(), providerName)) {
aiRouteService.update(aiRoute);
}可能会导致不必要的资源消耗、增加系统负载,并有可能引起并发更新冲突或者状态不一致的问题。
🔍2. 新增 handler 的初始化顺序与可维护性
在静态初始化块中直接硬编码添加新的 LLM 提供商处理器(如 Qwen 和 Azure),虽然实现了功能需求,但降低了代码的灵活性和可扩展性。随着支持的提供商增多,这种写法会变得难以维护。推荐采用更灵活的注册机制来管理不同类型的处理器。
📌 关键代码
new QwenLlmProviderHandler(), new AzureLlmProviderHandler(),未来添加新提供商需要修改核心类,容易引入错误;不利于插件化架构的发展,限制系统的可拓展性和维护效率。
🔍3. hasProvider 方法缺乏通用性和复用性考量
当前实现的 hasProvider 静态方法仅用于判断给定列表中的 AiUpstream 是否包含指定的提供商名称,但由于其逻辑简单且特定于当前场景,在其他类似业务逻辑中无法被重用。考虑将其抽象为一个工具类方法以便提高代码复用率并增强整体设计的一致性。
📌 关键代码
private static boolean hasProvider(List<AiUpstream> upstreams, String providerName) {
if (CollectionUtils.isEmpty(upstreams)) {
return false;
}
for (AiUpstream upstream : upstreams) {
if (providerName.equals(upstream.getProvider())) {
return true;
}
}
return false;
}若将来有相似逻辑的需求出现,将不得不复制该段代码,造成冗余,违反 DRY 原则,同时也会增加后期重构的成本。
审查详情
📒 文件清单 (1 个文件)
📝 变更: 1 个文件
📝 变更文件:
backend/sdk/src/main/java/com/alibaba/higress/sdk/service/ai/LlmProviderServiceImpl.java
💡 小贴士
与 lingma-agents 交流的方式
📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:
-
在当前代码中添加详细的注释说明。
-
请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。
📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:
-
@lingma-agents 分析这个方法的性能瓶颈并提供优化建议。
-
@lingma-agents 对这个方法生成优化代码。
📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:
-
@lingma-agents 请总结上述讨论并提出解决方案。
-
@lingma-agents 请根据讨论内容生成优化代码。
| import com.alibaba.higress.sdk.model.WasmPluginInstance; | ||
| import com.alibaba.higress.sdk.model.WasmPluginInstanceScope; | ||
| import com.alibaba.higress.sdk.model.ai.AiRoute; | ||
| import com.alibaba.higress.sdk.model.ai.AiUpstream; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| new DefaultLlmProviderHandler(LlmProviderType.MOONSHOT, "api.moonshot.cn", 443, V1McpBridge.PROTOCOL_HTTPS), | ||
| new QwenLlmProviderHandler(), | ||
| new AzureLlmProviderHandler(), | ||
| new QwenLlmProviderHandler(), new AzureLlmProviderHandler(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| AiRouteService aiRouteService = this.aiRouteService; | ||
| if (aiRouteService == null) { | ||
| return; | ||
| throw new IllegalStateException("AiRouteService is not available when AI route syncing is needed."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ⅰ. Describe what this PR did
Make sure AiRoutes with fallback enabled are also synced after a provider is updated.
Ⅱ. Does this pull request fix one issue?
Ⅲ. Why don't you add test cases (unit test/integration test)?
Ⅳ. Describe how to verify it
Ⅴ. Special notes for reviews