Junjie
2026-04-14 cab0ccaa5f3c639cee16eea02421bbfa1cdbe09a
src/main/java/com/zy/common/utils/NavigateUtils.java
@@ -27,6 +27,7 @@
import com.zy.asrs.service.StationCycleCapacityService;
import com.zy.asrs.service.StationPathPolicyService;
import com.zy.core.News;
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;
@@ -77,8 +78,23 @@
    @Autowired
    private StationTaskTraceRegistry stationTaskTraceRegistry;
    private final Object runtimeSnapshotLock = new Object();
    private volatile CachedStationPathRuntimeSnapshot cachedRuntimeSnapshot;
    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,
@@ -1688,22 +1704,21 @@
            }
            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) {
@@ -1766,6 +1781,22 @@
    }
    private LoopMergeGuardContext loadLoopMergeGuardContext() {
        long now = System.currentTimeMillis();
        CachedLoopMergeGuardContext cached = cachedLoopMergeGuardContext;
        if (cached != null && now - cached.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) {
            return new LoopMergeGuardContext(cached.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() {
        LoopMergeGuardContext context = new LoopMergeGuardContext();
        try {
            if (stationCycleCapacityService == null) {
@@ -3083,6 +3114,16 @@
        }
    }
    private static class CachedLoopMergeGuardContext {
        private final long cacheAtMs;
        private final LoopMergeGuardContext context;
        private CachedLoopMergeGuardContext(long cacheAtMs, LoopMergeGuardContext context) {
            this.cacheAtMs = cacheAtMs;
            this.context = context;
        }
    }
    private static class PathCandidateMetrics {
        private List<NavigateNode> path;
        private int pathLen;
@@ -3136,6 +3177,16 @@
    private static class LoopMergeGuardContext {
        private final Set<Integer> loopStationIdSet = new HashSet<>();
        private final Map<Integer, Set<Integer>> loopNeighborMap = new HashMap<>();
        private LoopMergeGuardContext() {
        }
        private LoopMergeGuardContext(LoopMergeGuardContext source) {
            this.loopStationIdSet.addAll(source.loopStationIdSet);
            for (Map.Entry<Integer, Set<Integer>> entry : source.loopNeighborMap.entrySet()) {
                this.loopNeighborMap.put(entry.getKey(), new LinkedHashSet<>(entry.getValue()));
            }
        }
    }
    private static class LoopMergeEntry {