Junjie
昨天 a4f07b2a0ddb6c210e05afbbb491feeb466203e7
src/main/java/com/zy/ai/service/LlmRoutingService.java
@@ -1,6 +1,6 @@
package com.zy.ai.service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.ai.entity.LlmRouteConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -12,6 +12,7 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
@@ -29,6 +30,14 @@
    private volatile List<LlmRouteConfig> allRouteCache = Collections.emptyList();
    private volatile long cacheExpireAt = 0L;
    private static final Comparator<LlmRouteConfig> ROUTE_ORDER = (a, b) -> {
        int pa = a == null || a.getPriority() == null ? Integer.MAX_VALUE : a.getPriority();
        int pb = b == null || b.getPriority() == null ? Integer.MAX_VALUE : b.getPriority();
        if (pa != pb) return Integer.compare(pa, pb);
        long ia = a == null || a.getId() == null ? Long.MAX_VALUE : a.getId();
        long ib = b == null || b.getId() == null ? Long.MAX_VALUE : b.getId();
        return Long.compare(ia, ib);
    };
    public void evictCache() {
        cacheExpireAt = 0L;
@@ -63,9 +72,11 @@
        }
        if (result.isEmpty() && !coolingRoutes.isEmpty()) {
            // 避免所有路由都处于冷却时系统完全不可用,降级允许使用冷却路由
            coolingRoutes.sort(ROUTE_ORDER);
            log.warn("LLM 路由均处于冷却,降级启用冷却路由。cooling={}, total={}", coolingRoutes.size(), total);
            return coolingRoutes;
        }
        result.sort(ROUTE_ORDER);
        if (result.isEmpty()) {
            log.warn("未找到可用 LLM 路由。total={}, disabled={}, invalid={}", total, disabled, invalid);
        }
@@ -75,7 +86,7 @@
    public void markSuccess(Long routeId) {
        if (routeId == null) return;
        try {
            LlmRouteConfig db = llmRouteConfigService.selectById(routeId);
            LlmRouteConfig db = llmRouteConfigService.getById(routeId);
            if (db == null) return;
            db.setSuccessCount(nvl(db.getSuccessCount()) + 1);
            db.setConsecutiveFailCount(0);
@@ -91,7 +102,7 @@
    public void markFailure(Long routeId, String errorText, boolean enterCooldown, Integer cooldownSeconds) {
        if (routeId == null) return;
        try {
            LlmRouteConfig db = llmRouteConfigService.selectById(routeId);
            LlmRouteConfig db = llmRouteConfigService.getById(routeId);
            if (db == null) return;
            Date now = new Date();
            db.setFailCount(nvl(db.getFailCount()) + 1);
@@ -144,10 +155,15 @@
            if (now < cacheExpireAt && allRouteCache != null) {
                return allRouteCache;
            }
            EntityWrapper<LlmRouteConfig> wrapper = new EntityWrapper<>();
            wrapper.orderBy("priority", true).orderBy("id", true);
            List<LlmRouteConfig> list = llmRouteConfigService.selectList(wrapper);
            allRouteCache = list == null ? Collections.emptyList() : list;
            QueryWrapper<LlmRouteConfig> wrapper = new QueryWrapper<>();
            wrapper.orderBy(true, true, "priority").orderBy(true, true, "id");
            List<LlmRouteConfig> list = llmRouteConfigService.list(wrapper);
            if (list == null) {
                allRouteCache = Collections.emptyList();
            } else {
                list.sort(ROUTE_ORDER);
                allRouteCache = list;
            }
            cacheExpireAt = System.currentTimeMillis() + CACHE_TTL_MS;
            return allRouteCache;
        }