| | |
| | | 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; |
| | |
| | | @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, |
| | |
| | | } |
| | | return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot(); |
| | | } |
| | | synchronized (runtimeSnapshotLock) { |
| | | cachedSnapshot = cachedRuntimeSnapshot; |
| | | if (cachedSnapshot != null && now - cachedSnapshot.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) { |
| | | // 缓存过期:用过期缓存兜底(@Scheduled 定时任务会很快刷新),避免多线程阻塞 |
| | | if (cachedSnapshot != null) { |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("baseSnapshotCacheHit", 0L); |
| | | stepCostMap.put("baseSnapshotStale", 0L); |
| | | } |
| | | return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot(); |
| | | } |
| | | // 极端情况:从未有过缓存(启动首次调用),同步构建 |
| | | long stepStartNs = System.nanoTime(); |
| | | BaseRuntimeSnapshot baseRuntimeSnapshot = buildBaseRuntimeSnapshot(stepCostMap); |
| | | if (stepCostMap != null) { |
| | |
| | | } |
| | | 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) { |
| | | // 缓存过期:用过期缓存兜底(@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() { |