| | |
| | | import com.zy.core.model.command.StationCommand; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | |
| | | import java.util.Objects; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class StationTaskTraceRegistry { |
| | | |
| | |
| | | return result; |
| | | } |
| | | |
| | | public List<StationTaskTraceVo> listPlanningActiveTraceSnapshots() { |
| | | ensureCacheLoaded(); |
| | | cleanupExpired(); |
| | | List<StationTaskTraceVo> result = new ArrayList<>(); |
| | | for (TraceTaskState state : taskStateMap.values()) { |
| | | if (state == null) { |
| | | continue; |
| | | } |
| | | StationTaskTraceVo traceVo = state.toPlanningVo(); |
| | | if (traceVo == null) { |
| | | continue; |
| | | } |
| | | result.add(traceVo); |
| | | } |
| | | result.sort(new Comparator<StationTaskTraceVo>() { |
| | | @Override |
| | | public int compare(StationTaskTraceVo a, StationTaskTraceVo b) { |
| | | long av = a.getUpdatedAt() == null ? 0L : a.getUpdatedAt(); |
| | | long bv = b.getUpdatedAt() == null ? 0L : b.getUpdatedAt(); |
| | | return Long.compare(bv, av); |
| | | } |
| | | }); |
| | | return result; |
| | | } |
| | | |
| | | private void cleanupExpired() { |
| | | long now = System.currentTimeMillis(); |
| | | for (Map.Entry<Integer, TraceTaskState> entry : taskStateMap.entrySet()) { |
| | |
| | | if (loadedFromRedis) { |
| | | return; |
| | | } |
| | | long redisStartNs = System.nanoTime(); |
| | | Map<Object, Object> cacheMap = redisUtil == null ? null : redisUtil.hmget(TRACE_CACHE_KEY); |
| | | long redisCostMs = (System.nanoTime() - redisStartNs) / 1_000_000L; |
| | | if (redisCostMs > 100) { |
| | | log.info("StationTaskTraceRegistry Redis加载耗时={}ms, entries={}", redisCostMs, |
| | | cacheMap == null ? 0 : cacheMap.size()); |
| | | } |
| | | if (cacheMap != null && !cacheMap.isEmpty()) { |
| | | long now = System.currentTimeMillis(); |
| | | for (Map.Entry<Object, Object> entry : cacheMap.entrySet()) { |
| | |
| | | return vo; |
| | | } |
| | | |
| | | private synchronized StationTaskTraceVo toPlanningVo() { |
| | | if (isTerminalStatus(this.status)) { |
| | | return null; |
| | | } |
| | | StationTaskTraceVo vo = new StationTaskTraceVo(); |
| | | vo.setTaskNo(taskNo); |
| | | vo.setThreadImpl(threadImpl); |
| | | vo.setStatus(status); |
| | | vo.setTraceVersion(traceVersion); |
| | | vo.setStartStationId(startStationId); |
| | | vo.setCurrentStationId(currentStationId); |
| | | vo.setFinalTargetStationId(finalTargetStationId); |
| | | vo.setPendingStationIds(copyIntegerList(pendingStationIds)); |
| | | vo.setLatestIssuedSegmentPath(copyIntegerList(latestIssuedSegmentPath)); |
| | | vo.setUpdatedAt(updatedAt); |
| | | return vo; |
| | | } |
| | | |
| | | private synchronized TraceSnapshot toSnapshot() { |
| | | TraceSnapshot snapshot = new TraceSnapshot(); |
| | | snapshot.setTaskNo(taskNo); |