| | |
| | | } |
| | | |
| | | private StationPathRuntimeSnapshot loadStationPathRuntimeSnapshot(Integer currentTaskNo) { |
| | | BaseRuntimeSnapshot baseRuntimeSnapshot = loadBaseRuntimeSnapshot(); |
| | | long totalStartNs = System.nanoTime(); |
| | | Map<String, Long> stepCostMap = new LinkedHashMap<>(); |
| | | |
| | | long stepStartNs = System.nanoTime(); |
| | | BaseRuntimeSnapshot baseRuntimeSnapshot = loadBaseRuntimeSnapshot(stepCostMap); |
| | | stepCostMap.put("loadBaseSnapshot", elapsedMillis(stepStartNs)); |
| | | if (baseRuntimeSnapshot == null) { |
| | | logRuntimeSnapshotSlow(currentTaskNo, false, stepCostMap, totalStartNs); |
| | | return StationPathRuntimeSnapshot.empty(); |
| | | } |
| | | |
| | | stepStartNs = System.nanoTime(); |
| | | StationTrafficSnapshot trafficSnapshot = buildStationTrafficSnapshot( |
| | | baseRuntimeSnapshot.statusMap, |
| | | currentTaskNo, |
| | | baseRuntimeSnapshot.activeTraceList |
| | | ); |
| | | stepCostMap.put("buildTrafficSnapshot", elapsedMillis(stepStartNs)); |
| | | logRuntimeSnapshotSlow(currentTaskNo, baseRuntimeSnapshot.cacheHit, stepCostMap, totalStartNs); |
| | | return new StationPathRuntimeSnapshot( |
| | | baseRuntimeSnapshot.statusMap, |
| | | baseRuntimeSnapshot.stationLoopLoadMap, |
| | |
| | | ); |
| | | } |
| | | |
| | | private BaseRuntimeSnapshot loadBaseRuntimeSnapshot() { |
| | | private BaseRuntimeSnapshot loadBaseRuntimeSnapshot(Map<String, Long> stepCostMap) { |
| | | long now = System.currentTimeMillis(); |
| | | CachedStationPathRuntimeSnapshot cachedSnapshot = cachedRuntimeSnapshot; |
| | | if (cachedSnapshot != null && now - cachedSnapshot.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) { |
| | | return cachedSnapshot.baseRuntimeSnapshot; |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("baseSnapshotCacheHit", 0L); |
| | | } |
| | | return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot(); |
| | | } |
| | | synchronized (runtimeSnapshotLock) { |
| | | cachedSnapshot = cachedRuntimeSnapshot; |
| | | if (cachedSnapshot != null && now - cachedSnapshot.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) { |
| | | return cachedSnapshot.baseRuntimeSnapshot; |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("baseSnapshotCacheHit", 0L); |
| | | } |
| | | return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot(); |
| | | } |
| | | BaseRuntimeSnapshot baseRuntimeSnapshot = buildBaseRuntimeSnapshot(); |
| | | long stepStartNs = System.nanoTime(); |
| | | BaseRuntimeSnapshot baseRuntimeSnapshot = buildBaseRuntimeSnapshot(stepCostMap); |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("buildBaseSnapshot", elapsedMillis(stepStartNs)); |
| | | } |
| | | cachedRuntimeSnapshot = new CachedStationPathRuntimeSnapshot(now, baseRuntimeSnapshot); |
| | | return baseRuntimeSnapshot; |
| | | } |
| | | } |
| | | |
| | | private BaseRuntimeSnapshot buildBaseRuntimeSnapshot() { |
| | | private BaseRuntimeSnapshot buildBaseRuntimeSnapshot(Map<String, Long> stepCostMap) { |
| | | long stepStartNs = System.nanoTime(); |
| | | Map<Integer, StationProtocol> statusMap = Collections.unmodifiableMap(new LinkedHashMap<>(loadStationStatusMap())); |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("loadStatusMap", elapsedMillis(stepStartNs)); |
| | | } |
| | | |
| | | stepStartNs = System.nanoTime(); |
| | | Map<Integer, Double> stationLoopLoadMap = Collections.unmodifiableMap(new LinkedHashMap<>(loadStationLoopLoadMap())); |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("loadLoopLoadMap", elapsedMillis(stepStartNs)); |
| | | } |
| | | |
| | | stepStartNs = System.nanoTime(); |
| | | LoopMergeGuardContext loopMergeGuardContext = loadLoopMergeGuardContext(); |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("loadLoopMergeGuard", elapsedMillis(stepStartNs)); |
| | | } |
| | | |
| | | stepStartNs = System.nanoTime(); |
| | | Set<Integer> outStationIdSet = Collections.unmodifiableSet(new LinkedHashSet<>(loadAllOutStationIdSet())); |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("loadOutStationSet", elapsedMillis(stepStartNs)); |
| | | } |
| | | |
| | | stepStartNs = System.nanoTime(); |
| | | List<StationTaskTraceVo> activeTraceList = Collections.unmodifiableList(new ArrayList<>(loadPlanningActiveTraceList(statusMap))); |
| | | return new BaseRuntimeSnapshot(statusMap, stationLoopLoadMap, loopMergeGuardContext, outStationIdSet, activeTraceList); |
| | | if (stepCostMap != null) { |
| | | stepCostMap.put("loadActiveTraceList", elapsedMillis(stepStartNs)); |
| | | } |
| | | return new BaseRuntimeSnapshot(false, statusMap, stationLoopLoadMap, loopMergeGuardContext, outStationIdSet, activeTraceList); |
| | | } |
| | | |
| | | private Map<Integer, Double> loadStationLoopLoadMap() { |
| | |
| | | Map<Integer, StationTaskTraceVo> traceMap = new LinkedHashMap<>(); |
| | | if (stationTaskTraceRegistry != null) { |
| | | try { |
| | | List<StationTaskTraceVo> traceList = stationTaskTraceRegistry.listLatestTraces(); |
| | | List<StationTaskTraceVo> traceList = stationTaskTraceRegistry.listPlanningActiveTraceSnapshots(); |
| | | if (traceList != null) { |
| | | for (StationTaskTraceVo traceVo : traceList) { |
| | | if (!isPlanningActiveTrace(traceVo)) { |
| | |
| | | totalCostMs); |
| | | } |
| | | |
| | | private void logRuntimeSnapshotSlow(Integer currentTaskNo, |
| | | boolean cacheHit, |
| | | Map<String, Long> stepCostMap, |
| | | long totalStartNs) { |
| | | long totalCostMs = elapsedMillis(totalStartNs); |
| | | if (totalCostMs < STATION_PATH_SLOW_LOG_THRESHOLD_MS) { |
| | | return; |
| | | } |
| | | News.warn("站点路径运行时快照加载耗时较长,taskNo={},cacheHit={},stepCosts={},totalCost={}ms", |
| | | currentTaskNo, |
| | | cacheHit, |
| | | JSON.toJSONString(stepCostMap), |
| | | totalCostMs); |
| | | } |
| | | |
| | | private long elapsedMillis(long startNs) { |
| | | long elapsedNs = System.nanoTime() - startNs; |
| | | return elapsedNs <= 0L ? 0L : elapsedNs / 1_000_000L; |
| | |
| | | } |
| | | |
| | | private static class BaseRuntimeSnapshot { |
| | | private final boolean cacheHit; |
| | | private final Map<Integer, StationProtocol> statusMap; |
| | | private final Map<Integer, Double> stationLoopLoadMap; |
| | | private final LoopMergeGuardContext loopMergeGuardContext; |
| | | private final Set<Integer> outStationIdSet; |
| | | private final List<StationTaskTraceVo> activeTraceList; |
| | | |
| | | private BaseRuntimeSnapshot(Map<Integer, StationProtocol> statusMap, |
| | | private BaseRuntimeSnapshot(boolean cacheHit, |
| | | Map<Integer, StationProtocol> statusMap, |
| | | Map<Integer, Double> stationLoopLoadMap, |
| | | LoopMergeGuardContext loopMergeGuardContext, |
| | | Set<Integer> outStationIdSet, |
| | | List<StationTaskTraceVo> activeTraceList) { |
| | | this.cacheHit = cacheHit; |
| | | this.statusMap = statusMap == null ? Collections.emptyMap() : statusMap; |
| | | this.stationLoopLoadMap = stationLoopLoadMap == null ? Collections.emptyMap() : stationLoopLoadMap; |
| | | this.loopMergeGuardContext = loopMergeGuardContext == null ? new LoopMergeGuardContext() : loopMergeGuardContext; |
| | | this.outStationIdSet = outStationIdSet == null ? Collections.emptySet() : outStationIdSet; |
| | | this.activeTraceList = activeTraceList == null ? Collections.emptyList() : activeTraceList; |
| | | } |
| | | |
| | | private BaseRuntimeSnapshot toCacheHitSnapshot() { |
| | | return new BaseRuntimeSnapshot(true, |
| | | statusMap, |
| | | stationLoopLoadMap, |
| | | loopMergeGuardContext, |
| | | outStationIdSet, |
| | | activeTraceList); |
| | | } |
| | | } |
| | | |
| | | private static class CachedStationPathRuntimeSnapshot { |