| | |
| | | import com.zy.asrs.service.StationCycleCapacityService; |
| | | import com.zy.asrs.service.StationPathPolicyService; |
| | | import com.zy.core.News; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import com.zy.core.model.StationObjModel; |
| | | import com.zy.core.model.command.StationCommand; |
| | | import com.zy.core.enums.StationCommandType; |
| | |
| | | import java.util.LinkedHashMap; |
| | | |
| | | @Component |
| | | @Slf4j |
| | | public class NavigateUtils { |
| | | |
| | | private static final long STATION_PATH_SLOW_LOG_THRESHOLD_MS = 500L; |
| | |
| | | @Autowired |
| | | private StationTaskTraceRegistry stationTaskTraceRegistry; |
| | | |
| | | private final Object runtimeSnapshotLock = new Object(); |
| | | private volatile CachedStationPathRuntimeSnapshot cachedRuntimeSnapshot; |
| | | |
| | | private final Object loopMergeGuardLock = new Object(); |
| | | private volatile CachedLoopMergeGuardContext cachedLoopMergeGuardContext; |
| | | |
| | | @Scheduled(fixedDelay = 1500, initialDelay = 3000) |
| | | public void refreshStationPathCaches() { |
| | | try { |
| | | BaseRuntimeSnapshot snapshot = buildBaseRuntimeSnapshot(null); |
| | | cachedRuntimeSnapshot = new CachedStationPathRuntimeSnapshot(System.currentTimeMillis(), snapshot); |
| | | } catch (Exception ignore) { |
| | | } |
| | | try { |
| | | LoopMergeGuardContext context = buildLoopMergeGuardContext(); |
| | | cachedLoopMergeGuardContext = new CachedLoopMergeGuardContext(System.currentTimeMillis(), context); |
| | | } catch (Exception ignore) { |
| | | } |
| | | } |
| | | |
| | | public List<NavigateNode> calcOptimalPathByStationId(Integer startStationId, |
| | | Integer endStationId, |
| | |
| | | Integer endStationId, |
| | | Integer currentTaskNo, |
| | | Double pathLenFactor) { |
| | | StationPathResolvedPolicy resolvedPolicy = resolveStationPathPolicy(startStationId, endStationId); |
| | | StationPathRuntimeSnapshot runtimeSnapshot = loadStationPathRuntimeSnapshot(currentTaskNo); |
| | | StationPathSearchContext context = buildStationPathSearchContext( |
| | | startStationId, |
| | | endStationId, |
| | | resolvedPolicy, |
| | | StationPathCalcMode.REROUTE, |
| | | runtimeSnapshot |
| | | ); |
| | | if (context.allList.isEmpty()) { |
| | | return new ArrayList<>(); |
| | | } |
| | | long totalStartNs = System.nanoTime(); |
| | | log.info("站点路径候选计算入口,startStationId={},endStationId={},taskNo={},pathLenFactor={}", |
| | | startStationId, endStationId, currentTaskNo, pathLenFactor); |
| | | try { |
| | | StationPathResolvedPolicy resolvedPolicy = resolveStationPathPolicy(startStationId, endStationId); |
| | | log.info("站点路径候选策略解析完成,startStationId={},endStationId={},taskNo={},matchedRule={},ruleCode={},profileCode={},costMs={}", |
| | | startStationId, |
| | | endStationId, |
| | | currentTaskNo, |
| | | resolvedPolicy != null && resolvedPolicy.matchedRule(), |
| | | resolvedPolicy == null || resolvedPolicy.getRuleEntity() == null ? null : resolvedPolicy.getRuleEntity().getRuleCode(), |
| | | resolvedPolicy == null ? null : resolvedPolicy.getDefaultProfileCode(), |
| | | elapsedMillis(totalStartNs)); |
| | | StationPathRuntimeSnapshot runtimeSnapshot = loadStationPathRuntimeSnapshot(currentTaskNo); |
| | | List<NavigateNode> shortFallbackPath = buildRerouteShortFallbackPath( |
| | | startStationId, |
| | | endStationId, |
| | | currentTaskNo, |
| | | resolvedPolicy, |
| | | runtimeSnapshot |
| | | ); |
| | | StationPathSearchContext context = buildStationPathSearchContext( |
| | | startStationId, |
| | | endStationId, |
| | | resolvedPolicy, |
| | | StationPathCalcMode.REROUTE, |
| | | runtimeSnapshot |
| | | ); |
| | | if (context.allList.isEmpty()) { |
| | | List<List<NavigateNode>> fallbackCandidateList = normalizeCandidatePaths(Collections.singletonList(shortFallbackPath)); |
| | | log.warn("站点路径候选计算结束,原因=empty_search_context,startStationId={},endStationId={},taskNo={},fallbackCandidateCount={},fallbackPath={},totalCostMs={}", |
| | | startStationId, |
| | | endStationId, |
| | | currentTaskNo, |
| | | fallbackCandidateList.size(), |
| | | JSON.toJSONString(extractStationIdList(shortFallbackPath)), |
| | | elapsedMillis(totalStartNs)); |
| | | return fallbackCandidateList; |
| | | } |
| | | |
| | | List<List<NavigateNode>> orderedPathList = orderStationPathCandidates( |
| | | context.allList, |
| | | context.resolvedPolicy, |
| | | currentTaskNo, |
| | | pathLenFactor, |
| | | startStationId, |
| | | endStationId, |
| | | runtimeSnapshot |
| | | ); |
| | | return normalizeCandidatePaths(orderedPathList); |
| | | long orderStartNs = System.nanoTime(); |
| | | List<List<NavigateNode>> orderedPathList = orderStationPathCandidates( |
| | | context.allList, |
| | | context.resolvedPolicy, |
| | | currentTaskNo, |
| | | pathLenFactor, |
| | | startStationId, |
| | | endStationId, |
| | | runtimeSnapshot |
| | | ); |
| | | List<List<NavigateNode>> candidatePathList = appendShortFallbackCandidate(orderedPathList, shortFallbackPath); |
| | | List<List<NavigateNode>> normalizedPathList = normalizeCandidatePaths(candidatePathList); |
| | | log.info("站点路径候选计算结束,startStationId={},endStationId={},taskNo={},rawCandidateCount={},orderedPathCount={},fallbackPathLen={},normalizedPathCount={},orderCostMs={},totalCostMs={}", |
| | | startStationId, |
| | | endStationId, |
| | | currentTaskNo, |
| | | context.allList.size(), |
| | | orderedPathList == null ? null : orderedPathList.size(), |
| | | shortFallbackPath == null ? 0 : shortFallbackPath.size(), |
| | | normalizedPathList.size(), |
| | | elapsedMillis(orderStartNs), |
| | | elapsedMillis(totalStartNs)); |
| | | return normalizedPathList; |
| | | } catch (Exception e) { |
| | | log.error("站点路径候选计算异常,startStationId={},endStationId={},taskNo={},pathLenFactor={},costMs={}", |
| | | startStationId, endStationId, currentTaskNo, pathLenFactor, elapsedMillis(totalStartNs), e); |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | public Map<Integer, Set<Integer>> loadUndirectedStationGraphSnapshot() { |
| | |
| | | return new DirectPathSearchResult(degradedPath, includeWaypoint, false); |
| | | } |
| | | return DirectPathSearchResult.empty(includeWaypoint, includeSoftPreference); |
| | | } |
| | | |
| | | private List<NavigateNode> buildRerouteShortFallbackPath(Integer startStationId, |
| | | Integer endStationId, |
| | | Integer currentTaskNo, |
| | | StationPathResolvedPolicy resolvedPolicy, |
| | | StationPathRuntimeSnapshot runtimeSnapshot) { |
| | | long startNs = System.nanoTime(); |
| | | try { |
| | | List<NavigateNode> path = findStationDirectPath( |
| | | startStationId, |
| | | endStationId, |
| | | currentTaskNo, |
| | | 0.0d, |
| | | StationPathCalcMode.OPTIMAL, |
| | | resolvedPolicy, |
| | | runtimeSnapshot |
| | | ); |
| | | List<NavigateNode> normalizedPath = normalizeStationPath(path); |
| | | log.info("站点路径堵塞重规划短路径保底计算完成,startStationId={},endStationId={},taskNo={},pathLen={},path={},costMs={}", |
| | | startStationId, |
| | | endStationId, |
| | | currentTaskNo, |
| | | normalizedPath.size(), |
| | | JSON.toJSONString(extractStationIdList(normalizedPath)), |
| | | elapsedMillis(startNs)); |
| | | return normalizedPath; |
| | | } catch (Exception e) { |
| | | log.warn("站点路径堵塞重规划短路径保底计算失败,startStationId={},endStationId={},taskNo={},costMs={},error={}", |
| | | startStationId, |
| | | endStationId, |
| | | currentTaskNo, |
| | | elapsedMillis(startNs), |
| | | e.toString()); |
| | | return new ArrayList<>(); |
| | | } |
| | | } |
| | | |
| | | private List<List<NavigateNode>> appendShortFallbackCandidate(List<List<NavigateNode>> orderedPathList, |
| | | List<NavigateNode> shortFallbackPath) { |
| | | List<List<NavigateNode>> candidatePathList = new ArrayList<>(); |
| | | if (orderedPathList != null && !orderedPathList.isEmpty()) { |
| | | candidatePathList.addAll(orderedPathList); |
| | | } |
| | | if (shortFallbackPath != null && !shortFallbackPath.isEmpty()) { |
| | | candidatePathList.add(shortFallbackPath); |
| | | } |
| | | return candidatePathList; |
| | | } |
| | | |
| | | private List<NavigateNode> searchDirectPath(DirectStationPathContext context, |
| | |
| | | } |
| | | return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot(); |
| | | } |
| | | synchronized (runtimeSnapshotLock) { |
| | | cachedSnapshot = cachedRuntimeSnapshot; |
| | | if (cachedSnapshot != null && now - cachedSnapshot.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) { |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("baseSnapshotCacheHit", 0L); |
| | | } |
| | | return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot(); |
| | | } |
| | | long stepStartNs = System.nanoTime(); |
| | | BaseRuntimeSnapshot baseRuntimeSnapshot = buildBaseRuntimeSnapshot(stepCostMap); |
| | | // 缓存过期:用过期缓存兜底(@Scheduled 定时任务会很快刷新),避免多线程阻塞 |
| | | if (cachedSnapshot != null) { |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("buildBaseSnapshot", elapsedMillis(stepStartNs)); |
| | | stepCostMap.put("baseSnapshotStale", 0L); |
| | | } |
| | | cachedRuntimeSnapshot = new CachedStationPathRuntimeSnapshot(System.currentTimeMillis(), baseRuntimeSnapshot); |
| | | return baseRuntimeSnapshot; |
| | | return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot(); |
| | | } |
| | | // 极端情况:从未有过缓存(启动首次调用),同步构建 |
| | | long stepStartNs = System.nanoTime(); |
| | | BaseRuntimeSnapshot baseRuntimeSnapshot = buildBaseRuntimeSnapshot(stepCostMap); |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("buildBaseSnapshot", elapsedMillis(stepStartNs)); |
| | | } |
| | | cachedRuntimeSnapshot = new CachedStationPathRuntimeSnapshot(System.currentTimeMillis(), baseRuntimeSnapshot); |
| | | return baseRuntimeSnapshot; |
| | | } |
| | | |
| | | private BaseRuntimeSnapshot buildBaseRuntimeSnapshot(Map<String, Long> stepCostMap) { |
| | |
| | | if (cached != null && now - cached.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) { |
| | | return new LoopMergeGuardContext(cached.context); |
| | | } |
| | | synchronized (loopMergeGuardLock) { |
| | | cached = cachedLoopMergeGuardContext; |
| | | if (cached != null && now - cached.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) { |
| | | return new LoopMergeGuardContext(cached.context); |
| | | } |
| | | LoopMergeGuardContext context = buildLoopMergeGuardContext(); |
| | | cachedLoopMergeGuardContext = new CachedLoopMergeGuardContext(System.currentTimeMillis(), context); |
| | | return new LoopMergeGuardContext(context); |
| | | // 缓存过期:用过期缓存兜底(@Scheduled 定时任务会很快刷新) |
| | | if (cached != null) { |
| | | return new LoopMergeGuardContext(cached.context); |
| | | } |
| | | // 极端情况:从未有过缓存,同步构建 |
| | | LoopMergeGuardContext context = buildLoopMergeGuardContext(); |
| | | cachedLoopMergeGuardContext = new CachedLoopMergeGuardContext(System.currentTimeMillis(), context); |
| | | return new LoopMergeGuardContext(context); |
| | | } |
| | | |
| | | private LoopMergeGuardContext buildLoopMergeGuardContext() { |
| | |
| | | } |
| | | Integer lev = startStation.getStationLev(); |
| | | stepCostMap.put("loadStartStation", elapsedMillis(stepStartNs)); |
| | | log.info("站点路径搜索上下文加载起点完成,startStationId={},endStationId={},mode={},lev={},costMs={}", |
| | | startStationId, endStationId, calcMode, lev, stepCostMap.get("loadStartStation")); |
| | | |
| | | stepStartNs = System.nanoTime(); |
| | | NavigateSolution navigateSolution = new NavigateSolution(); |
| | |
| | | throw new CoolException("未找到该 终点 对应的节点"); |
| | | } |
| | | stepCostMap.put("loadStationMapAndNode", elapsedMillis(stepStartNs)); |
| | | log.info("站点路径搜索上下文加载地图完成,startStationId={},endStationId={},mode={},mapRows={},startNode=({},{}),endNode=({},{}),costMs={}", |
| | | startStationId, |
| | | endStationId, |
| | | calcMode, |
| | | stationMap.size(), |
| | | startNode.getX(), |
| | | startNode.getY(), |
| | | endNode.getX(), |
| | | endNode.getY(), |
| | | stepCostMap.get("loadStationMapAndNode")); |
| | | |
| | | stepStartNs = System.nanoTime(); |
| | | StationPathProfileConfig profileConfig = resolvedPolicy.getProfileConfig() == null |
| | |
| | | endStationId, |
| | | calcMode == null ? "" : calcMode.name()); |
| | | int calcMaxDepth = safeInt(profileConfig.getCalcMaxDepth(), 120); |
| | | int calcMaxPaths = safeInt(profileConfig.getCalcMaxPaths(), 500); |
| | | int calcMaxPaths = safeInt(profileConfig.getCalcMaxPaths(), 50); |
| | | int calcMaxCost = safeInt(profileConfig.getCalcMaxCost(), 300); |
| | | List<Integer> guideStationSequence = buildGuideStationSequence(startStationId, endStationId, resolvedPolicy.getRuleConfig()); |
| | | log.info("站点路径候选枚举开始,startStationId={},endStationId={},mode={},calcMaxDepth={},calcMaxPaths={},calcMaxCost={},guideStationSequence={}", |
| | | startStationId, |
| | | endStationId, |
| | | calcMode, |
| | | calcMaxDepth, |
| | | calcMaxPaths, |
| | | calcMaxCost, |
| | | JSON.toJSONString(guideStationSequence)); |
| | | stepStartNs = System.nanoTime(); |
| | | List<List<NavigateNode>> allList = navigateSolution.allSimplePaths( |
| | | stationMap, |
| | |
| | | ); |
| | | stepCostMap.put("allSimplePaths", elapsedMillis(stepStartNs)); |
| | | rawCandidateCount = allList.size(); |
| | | log.info("站点路径候选枚举完成,startStationId={},endStationId={},mode={},rawCandidateCount={},costMs={}", |
| | | startStationId, |
| | | endStationId, |
| | | calcMode, |
| | | rawCandidateCount, |
| | | stepCostMap.get("allSimplePaths")); |
| | | if (allList.isEmpty()) { |
| | | logStationPathSearchContextSlow(startStationId, endStationId, calcMode, lev, rawCandidateCount, filteredCandidateCount, stepCostMap, totalStartNs); |
| | | return StationPathSearchContext.empty(resolvedPolicy); |
| | |
| | | allList = filterNonAutoStationPaths(allList, statusMap); |
| | | stepCostMap.put("filterNonAutoStation", elapsedMillis(stepStartNs)); |
| | | filteredCandidateCount = allList.size(); |
| | | log.info("站点路径候选自动状态过滤完成,startStationId={},endStationId={},mode={},rawCandidateCount={},filteredCandidateCount={},statusCount={},costMs={}", |
| | | startStationId, |
| | | endStationId, |
| | | calcMode, |
| | | rawCandidateCount, |
| | | filteredCandidateCount, |
| | | statusMap.size(), |
| | | stepCostMap.get("filterNonAutoStation")); |
| | | if (allList.isEmpty()) { |
| | | News.info("[WCS Debug] 站点路径候选全部被过滤,存在非自动站点,startStationId={},endStationId={}", startStationId, endStationId); |
| | | logStationPathSearchContextSlow(startStationId, endStationId, calcMode, lev, rawCandidateCount, filteredCandidateCount, stepCostMap, totalStartNs); |