#
Junjie
9 小时以前 1116b5a1c3feb85959d9b0b03e1c14693271aa8a
#
3个文件已修改
525 ■■■■■ 已修改文件
src/main/java/com/zy/common/utils/NavigateUtils.java 325 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/enums/RedisKeyType.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java 199 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/common/utils/NavigateUtils.java
@@ -11,17 +11,22 @@
import com.zy.asrs.domain.path.StationPathProfileConfig;
import com.zy.asrs.domain.path.StationPathResolvedPolicy;
import com.zy.asrs.domain.path.StationPathRuleConfig;
import com.zy.asrs.domain.vo.StationTaskTraceSegmentVo;
import com.zy.asrs.entity.BasStationOpt;
import com.zy.asrs.domain.vo.StationTaskTraceVo;
import com.zy.asrs.domain.vo.StationCycleCapacityVo;
import com.zy.asrs.domain.vo.StationCycleLoopVo;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.BasStation;
import com.zy.asrs.service.BasStationOptService;
import com.zy.asrs.service.BasDevpService;
import com.zy.asrs.service.BasStationService;
import com.zy.asrs.service.StationCycleCapacityService;
import com.zy.asrs.service.StationPathPolicyService;
import com.zy.core.News;
import com.zy.core.model.StationObjModel;
import com.zy.core.model.command.StationCommand;
import com.zy.core.enums.StationCommandType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -39,6 +44,9 @@
import com.zy.core.thread.StationThread;
import com.zy.core.trace.StationTaskTraceRegistry;
import java.util.Date;
import java.util.LinkedHashMap;
@Component
public class NavigateUtils {
@@ -54,6 +62,8 @@
    @Autowired
    private BasStationService basStationService;
    @Autowired
    private BasStationOptService basStationOptService;
    @Autowired
    private StationPathPolicyService stationPathPolicyService;
    @Autowired
@@ -945,7 +955,7 @@
            }
        }
        for (StationTaskTraceVo traceVo : loadActiveTraceList(currentTaskNo)) {
        for (StationTaskTraceVo traceVo : loadActiveTraceList(currentTaskNo, statusMap)) {
            if (traceVo == null) {
                continue;
            }
@@ -1017,21 +1027,12 @@
        return snapshot;
    }
    private List<StationTaskTraceVo> loadActiveTraceList(Integer currentTaskNo) {
        if (stationTaskTraceRegistry == null) {
            return Collections.emptyList();
        }
        List<StationTaskTraceVo> traceList;
    private List<StationTaskTraceVo> loadActiveTraceList(Integer currentTaskNo, Map<Integer, StationProtocol> statusMap) {
        Map<Integer, StationTaskTraceVo> traceMap = new LinkedHashMap<>();
        if (stationTaskTraceRegistry != null) {
        try {
            traceList = stationTaskTraceRegistry.listLatestTraces();
        } catch (Exception ignore) {
            return Collections.emptyList();
        }
        if (traceList == null || traceList.isEmpty()) {
            return Collections.emptyList();
        }
        List<StationTaskTraceVo> result = new ArrayList<>();
                List<StationTaskTraceVo> traceList = stationTaskTraceRegistry.listLatestTraces();
                if (traceList != null) {
        for (StationTaskTraceVo traceVo : traceList) {
            if (!isPlanningActiveTrace(traceVo)) {
                continue;
@@ -1039,9 +1040,19 @@
            if (currentTaskNo != null && currentTaskNo.equals(traceVo.getTaskNo())) {
                continue;
            }
            result.add(traceVo);
                        if (traceVo.getTaskNo() != null) {
                            traceMap.put(traceVo.getTaskNo(), traceVo);
        }
        return result;
                    }
                }
            } catch (Exception ignore) {
            }
        }
        Map<Integer, StationTaskTraceVo> fallbackTraceMap = loadFallbackActiveTraceMap(currentTaskNo, statusMap, traceMap.keySet());
        if (!fallbackTraceMap.isEmpty()) {
            traceMap.putAll(fallbackTraceMap);
        }
        return new ArrayList<>(traceMap.values());
    }
    private boolean isPlanningActiveTrace(StationTaskTraceVo traceVo) {
@@ -1052,6 +1063,275 @@
        return StationTaskTraceRegistry.STATUS_WAITING.equals(status)
                || StationTaskTraceRegistry.STATUS_RUNNING.equals(status)
                || StationTaskTraceRegistry.STATUS_REROUTED.equals(status);
    }
    private Map<Integer, StationTaskTraceVo> loadFallbackActiveTraceMap(Integer currentTaskNo,
                                                                        Map<Integer, StationProtocol> statusMap,
                                                                        Set<Integer> existingTaskNoSet) {
        if (basStationOptService == null || statusMap == null || statusMap.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<Integer, StationProtocol> activeTaskProtocolMap = new LinkedHashMap<>();
        for (StationProtocol protocol : statusMap.values()) {
            if (protocol == null || protocol.getTaskNo() == null || protocol.getTaskNo() <= 0) {
                continue;
            }
            if (!Boolean.TRUE.equals(protocol.isLoading())) {
                continue;
            }
            if (currentTaskNo != null && currentTaskNo.equals(protocol.getTaskNo())) {
                continue;
            }
            if (existingTaskNoSet != null && existingTaskNoSet.contains(protocol.getTaskNo())) {
                continue;
            }
            activeTaskProtocolMap.putIfAbsent(protocol.getTaskNo(), protocol);
        }
        if (activeTaskProtocolMap.isEmpty()) {
            return Collections.emptyMap();
        }
        List<Integer> taskNoList = new ArrayList<>(activeTaskProtocolMap.keySet());
        int limit = Math.max(50, taskNoList.size() * 8);
        List<BasStationOpt> optList;
        try {
            optList = basStationOptService.list(new QueryWrapper<BasStationOpt>()
                    .select("id", "task_no", "send_time", "command", "mode", "send", "target_station_id")
                    .in("task_no", taskNoList)
                    .eq("send", 1)
                    .orderByDesc("send_time")
                    .last("limit " + limit));
        } catch (Exception ignore) {
            return Collections.emptyMap();
        }
        if (optList == null || optList.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<Integer, List<FallbackMoveCommand>> fallbackCommandMap = new LinkedHashMap<>();
        for (BasStationOpt opt : optList) {
            FallbackMoveCommand moveCommand = parseFallbackMoveCommand(opt);
            if (moveCommand == null || moveCommand.taskNo == null) {
                continue;
            }
            if (!activeTaskProtocolMap.containsKey(moveCommand.taskNo)) {
                continue;
            }
            fallbackCommandMap.computeIfAbsent(moveCommand.taskNo, key -> new ArrayList<>()).add(moveCommand);
        }
        Map<Integer, StationTaskTraceVo> result = new LinkedHashMap<>();
        for (Map.Entry<Integer, StationProtocol> entry : activeTaskProtocolMap.entrySet()) {
            Integer taskNo = entry.getKey();
            StationProtocol protocol = entry.getValue();
            StationTaskTraceVo fallbackTrace = buildFallbackTraceVo(taskNo, protocol, fallbackCommandMap.get(taskNo));
            if (fallbackTrace != null) {
                result.put(taskNo, fallbackTrace);
            }
        }
        return result;
    }
    private FallbackMoveCommand parseFallbackMoveCommand(BasStationOpt opt) {
        if (opt == null || opt.getTaskNo() == null || opt.getTaskNo() <= 0) {
            return null;
        }
        try {
            StationCommand command = JSON.parseObject(opt.getCommand(), StationCommand.class);
            if (command == null || command.getCommandType() != StationCommandType.MOVE) {
                return null;
            }
            List<Integer> navigatePath = distinctPositiveStationIds(command.getNavigatePath());
            if (navigatePath.isEmpty()) {
                return null;
            }
            FallbackMoveCommand item = new FallbackMoveCommand();
            item.taskNo = opt.getTaskNo();
            item.traceVersion = command.getTraceVersion();
            item.segmentNo = command.getSegmentNo();
            item.segmentCount = command.getSegmentCount();
            item.stationId = command.getStationId();
            item.targetStaNo = command.getTargetStaNo();
            item.navigatePath = navigatePath;
            item.sendTime = opt.getSendTime();
            return item;
        } catch (Exception ignore) {
            return null;
        }
    }
    private StationTaskTraceVo buildFallbackTraceVo(Integer taskNo,
                                                    StationProtocol protocol,
                                                    List<FallbackMoveCommand> commandList) {
        if (taskNo == null || protocol == null || commandList == null || commandList.isEmpty()) {
            return null;
        }
        Integer latestTraceVersion = null;
        long latestTimestamp = 0L;
        for (FallbackMoveCommand item : commandList) {
            if (item == null) {
                continue;
            }
            if (item.traceVersion != null && (latestTraceVersion == null || item.traceVersion > latestTraceVersion)) {
                latestTraceVersion = item.traceVersion;
            }
            long ts = item.sendTime == null ? 0L : item.sendTime.getTime();
            if (ts > latestTimestamp) {
                latestTimestamp = ts;
            }
        }
        List<FallbackMoveCommand> sameTraceCommandList = new ArrayList<>();
        for (FallbackMoveCommand item : commandList) {
            if (item == null) {
                continue;
            }
            if (latestTraceVersion == null || latestTraceVersion.equals(item.traceVersion)) {
                sameTraceCommandList.add(item);
            }
        }
        sameTraceCommandList.sort((a, b) -> {
            int av = a.segmentNo == null ? Integer.MAX_VALUE : a.segmentNo;
            int bv = b.segmentNo == null ? Integer.MAX_VALUE : b.segmentNo;
            if (av != bv) {
                return Integer.compare(av, bv);
            }
            long at = a.sendTime == null ? 0L : a.sendTime.getTime();
            long bt = b.sendTime == null ? 0L : b.sendTime.getTime();
            return Long.compare(at, bt);
        });
        List<Integer> issuedPath = new ArrayList<>();
        List<StationTaskTraceSegmentVo> segmentList = new ArrayList<>();
        FallbackMoveCommand latestCommand = null;
        for (FallbackMoveCommand item : sameTraceCommandList) {
            if (item == null || item.navigatePath == null || item.navigatePath.isEmpty()) {
                continue;
            }
            appendMergedPath(issuedPath, item.navigatePath);
            segmentList.add(toTraceSegment(item));
            if (latestCommand == null || compareFallbackCommand(item, latestCommand) > 0) {
                latestCommand = item;
            }
        }
        if (issuedPath.isEmpty()) {
            return null;
        }
        Integer currentStationId = protocol.getStationId();
        List<Integer> passedStationIds = new ArrayList<>();
        List<Integer> pendingStationIds = new ArrayList<>();
        if (currentStationId != null) {
            int currentIndex = issuedPath.indexOf(currentStationId);
            if (currentIndex >= 0) {
                passedStationIds = copyIntegerSubList(issuedPath, 0, currentIndex);
                pendingStationIds = copyIntegerSubList(issuedPath, currentIndex + 1, issuedPath.size());
            } else {
                pendingStationIds = new ArrayList<>(issuedPath);
            }
        } else {
            pendingStationIds = new ArrayList<>(issuedPath);
        }
        StationTaskTraceVo vo = new StationTaskTraceVo();
        vo.setTaskNo(taskNo);
        vo.setThreadImpl("DB_FALLBACK");
        vo.setStatus(StationTaskTraceRegistry.STATUS_RUNNING);
        vo.setTraceVersion(latestTraceVersion == null ? 1 : latestTraceVersion);
        vo.setStartStationId(issuedPath.isEmpty() ? null : issuedPath.get(0));
        vo.setCurrentStationId(currentStationId);
        vo.setFinalTargetStationId(protocol.getTargetStaNo() != null ? protocol.getTargetStaNo()
                : latestCommand == null ? null : latestCommand.targetStaNo);
        vo.setBlockedStationId(null);
        vo.setFullPathStationIds(new ArrayList<>(issuedPath));
        vo.setIssuedStationIds(new ArrayList<>(issuedPath));
        vo.setPassedStationIds(passedStationIds);
        vo.setPendingStationIds(pendingStationIds);
        vo.setLatestIssuedSegmentPath(latestCommand == null ? Collections.emptyList() : new ArrayList<>(latestCommand.navigatePath));
        vo.setSegmentList(segmentList);
        vo.setIssuedSegmentCount(segmentList.size());
        vo.setTotalSegmentCount(latestCommand == null || latestCommand.segmentCount == null
                ? segmentList.size()
                : latestCommand.segmentCount);
        vo.setUpdatedAt(latestTimestamp > 0L ? latestTimestamp : System.currentTimeMillis());
        vo.setEvents(Collections.emptyList());
        return vo;
    }
    private int compareFallbackCommand(FallbackMoveCommand a, FallbackMoveCommand b) {
        int av = a == null || a.segmentNo == null ? Integer.MIN_VALUE : a.segmentNo;
        int bv = b == null || b.segmentNo == null ? Integer.MIN_VALUE : b.segmentNo;
        if (av != bv) {
            return Integer.compare(av, bv);
        }
        long at = a == null || a.sendTime == null ? 0L : a.sendTime.getTime();
        long bt = b == null || b.sendTime == null ? 0L : b.sendTime.getTime();
        return Long.compare(at, bt);
    }
    private StationTaskTraceSegmentVo toTraceSegment(FallbackMoveCommand item) {
        StationTaskTraceSegmentVo segmentVo = new StationTaskTraceSegmentVo();
        if (item == null) {
            return segmentVo;
        }
        segmentVo.setSegmentNo(item.segmentNo);
        segmentVo.setSegmentCount(item.segmentCount);
        segmentVo.setStationId(item.stationId);
        segmentVo.setTargetStationId(item.targetStaNo);
        segmentVo.setSegmentPath(item.navigatePath == null ? Collections.emptyList() : new ArrayList<>(item.navigatePath));
        segmentVo.setIssued(Boolean.TRUE);
        return segmentVo;
    }
    private void appendMergedPath(List<Integer> target, List<Integer> source) {
        if (target == null || source == null || source.isEmpty()) {
            return;
        }
        if (target.isEmpty()) {
            target.addAll(source);
            return;
        }
        int overlap = 0;
        int maxOverlap = Math.min(target.size(), source.size());
        for (int size = maxOverlap; size >= 1; size--) {
            boolean matched = true;
            for (int i = 0; i < size; i++) {
                Integer left = target.get(target.size() - size + i);
                Integer right = source.get(i);
                if (left == null || !left.equals(right)) {
                    matched = false;
                    break;
                }
            }
            if (matched) {
                overlap = size;
                break;
            }
        }
        for (int i = overlap; i < source.size(); i++) {
            Integer stationId = source.get(i);
            if (stationId != null) {
                target.add(stationId);
            }
        }
    }
    private List<Integer> copyIntegerSubList(List<Integer> source, int fromIndex, int toIndex) {
        if (source == null || source.isEmpty()) {
            return new ArrayList<>();
        }
        int from = Math.max(0, fromIndex);
        int to = Math.min(source.size(), Math.max(from, toIndex));
        List<Integer> result = new ArrayList<>();
        for (int i = from; i < to; i++) {
            Integer value = source.get(i);
            if (value != null) {
                result.add(value);
            }
        }
        return result;
    }
    private List<Integer> distinctPositiveStationIds(List<Integer> stationIdList) {
@@ -1215,6 +1495,17 @@
        private double sequentialRisk;
    }
    private static class FallbackMoveCommand {
        private Integer taskNo;
        private Integer traceVersion;
        private Integer segmentNo;
        private Integer segmentCount;
        private Integer stationId;
        private Integer targetStaNo;
        private List<Integer> navigatePath = Collections.emptyList();
        private Date sendTime;
    }
    private static class PathGlobalPolicy {
        private double lenWeightFactor = 1.0d;
        private double congWeightFactor = 1.0d;
src/main/java/com/zy/core/enums/RedisKeyType.java
@@ -58,6 +58,7 @@
    WATCH_CIRCLE_STATION_("watch_circle_station_"),
    WATCH_STATION_COLOR_CONFIG("watch_station_color_config"),
    STATION_CYCLE_LOAD_RESERVE("station_cycle_load_reserve"),
    STATION_TASK_TRACE_REGISTRY("station_task_trace_registry"),
    CURRENT_CIRCLE_TASK_CRN_NO("current_circle_task_crn_no_"),
    ASYNC_WMS_IN_TASK_REQUEST("async_wms_in_task_request_"),
src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java
@@ -1,10 +1,14 @@
package com.zy.core.trace;
import com.alibaba.fastjson.JSON;
import com.zy.asrs.domain.vo.StationTaskTraceEventVo;
import com.zy.asrs.domain.vo.StationTaskTraceSegmentVo;
import com.zy.asrs.domain.vo.StationTaskTraceVo;
import com.zy.common.utils.RedisUtil;
import com.zy.core.model.command.StationCommand;
import com.zy.core.enums.RedisKeyType;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@@ -20,6 +24,7 @@
    private static final int MAX_EVENT_COUNT = 200;
    private static final long TERMINAL_KEEP_MS = 30L * 60L * 1000L;
    private static final String TRACE_CACHE_KEY = RedisKeyType.STATION_TASK_TRACE_REGISTRY.key;
    public static final String STATUS_WAITING = "WAITING";
    public static final String STATUS_RUNNING = "RUNNING";
@@ -29,7 +34,11 @@
    public static final String STATUS_FINISHED = "FINISHED";
    public static final String STATUS_REROUTED = "REROUTED";
    @Autowired
    private RedisUtil redisUtil;
    private final Map<Integer, TraceTaskState> taskStateMap = new ConcurrentHashMap<>();
    private volatile boolean loadedFromRedis = false;
    public TraceRegistration registerPlan(Integer taskNo,
                                          String threadImpl,
@@ -42,10 +51,13 @@
            return new TraceRegistration();
        }
        ensureCacheLoaded();
        cleanupExpired();
        TraceTaskState taskState = taskStateMap.computeIfAbsent(taskNo, TraceTaskState::new);
        return taskState.registerPlan(threadImpl, startStationId, currentStationId, finalTargetStationId,
        TraceRegistration registration = taskState.registerPlan(threadImpl, startStationId, currentStationId, finalTargetStationId,
                copyIntegerList(localPathStationIds), copySegmentList(localSegmentList));
        persistState(taskState);
        return registration;
    }
    public void markSegmentIssued(Integer taskNo,
@@ -54,11 +66,13 @@
                                  String eventType,
                                  String message,
                                  Map<String, Object> details) {
        ensureCacheLoaded();
        TraceTaskState state = taskStateMap.get(taskNo);
        if (state == null) {
            return;
        }
        state.markSegmentIssued(traceVersion, command, eventType, message, details);
        persistState(state);
    }
    public void updateProgress(Integer taskNo,
@@ -67,62 +81,73 @@
                               String eventType,
                               String message,
                               Map<String, Object> details) {
        ensureCacheLoaded();
        TraceTaskState state = taskStateMap.get(taskNo);
        if (state == null) {
            return;
        }
        state.updateProgress(traceVersion, currentStationId, eventType, message, details);
        persistState(state);
    }
    public void markBlocked(Integer taskNo,
                            Integer traceVersion,
                            Integer currentStationId,
                            Map<String, Object> details) {
        ensureCacheLoaded();
        TraceTaskState state = taskStateMap.get(taskNo);
        if (state == null) {
            return;
        }
        state.markTerminal(traceVersion, STATUS_BLOCKED, currentStationId, currentStationId,
                "BLOCKED", "输送任务运行堵塞", details);
        persistState(state);
    }
    public void markCancelled(Integer taskNo,
                              Integer traceVersion,
                              Integer currentStationId,
                              Map<String, Object> details) {
        ensureCacheLoaded();
        TraceTaskState state = taskStateMap.get(taskNo);
        if (state == null) {
            return;
        }
        state.markTerminal(traceVersion, STATUS_CANCELLED, currentStationId, null,
                "CANCELLED", "输送任务收到取消信号", details);
        persistState(state);
    }
    public void markTimeout(Integer taskNo,
                            Integer traceVersion,
                            Integer currentStationId,
                            Map<String, Object> details) {
        ensureCacheLoaded();
        TraceTaskState state = taskStateMap.get(taskNo);
        if (state == null) {
            return;
        }
        state.markTerminal(traceVersion, STATUS_TIMEOUT, currentStationId, null,
                "TIMEOUT", "输送任务长时间无法定位当前位置", details);
        persistState(state);
    }
    public void markFinished(Integer taskNo,
                             Integer traceVersion,
                             Integer currentStationId,
                             Map<String, Object> details) {
        ensureCacheLoaded();
        TraceTaskState state = taskStateMap.get(taskNo);
        if (state == null) {
            return;
        }
        state.markTerminal(traceVersion, STATUS_FINISHED, currentStationId, null,
                "FINISHED", "输送任务轨迹完成", details);
        persistState(state);
    }
    public List<StationTaskTraceVo> listLatestTraces() {
        ensureCacheLoaded();
        cleanupExpired();
        List<StationTaskTraceVo> result = new ArrayList<>();
        for (TraceTaskState state : taskStateMap.values()) {
@@ -147,7 +172,79 @@
            TraceTaskState state = entry.getValue();
            if (state != null && state.shouldRemove(now)) {
                taskStateMap.remove(entry.getKey(), state);
                removePersistedState(entry.getKey());
            }
        }
    }
    private void ensureCacheLoaded() {
        if (loadedFromRedis) {
            return;
        }
        synchronized (this) {
            if (loadedFromRedis) {
                return;
            }
            Map<Object, Object> cacheMap = redisUtil == null ? null : redisUtil.hmget(TRACE_CACHE_KEY);
            if (cacheMap != null && !cacheMap.isEmpty()) {
                long now = System.currentTimeMillis();
                for (Map.Entry<Object, Object> entry : cacheMap.entrySet()) {
                    Integer taskNo = parseTaskNo(entry.getKey());
                    TraceSnapshot snapshot = parseSnapshot(entry.getValue(), taskNo);
                    if (snapshot == null || snapshot.getTaskNo() == null) {
                        if (taskNo != null) {
                            removePersistedState(taskNo);
                        }
                        continue;
                    }
                    if (snapshot.isExpired(now)) {
                        removePersistedState(snapshot.getTaskNo());
                        continue;
                    }
                    taskStateMap.put(snapshot.getTaskNo(), TraceTaskState.fromSnapshot(snapshot));
                }
            }
            loadedFromRedis = true;
        }
    }
    private void persistState(TraceTaskState state) {
        if (state == null || state.taskNo == null || redisUtil == null) {
            return;
        }
        redisUtil.hset(TRACE_CACHE_KEY, String.valueOf(state.taskNo), JSON.toJSONString(state.toSnapshot()));
    }
    private void removePersistedState(Integer taskNo) {
        if (taskNo == null || redisUtil == null) {
            return;
        }
        redisUtil.hdel(TRACE_CACHE_KEY, String.valueOf(taskNo));
    }
    private Integer parseTaskNo(Object value) {
        if (value == null) {
            return null;
        }
        try {
            return Integer.parseInt(String.valueOf(value));
        } catch (Exception e) {
            return null;
        }
    }
    private TraceSnapshot parseSnapshot(Object cacheValue, Integer fallbackTaskNo) {
        if (cacheValue == null) {
            return null;
        }
        try {
            TraceSnapshot snapshot = JSON.parseObject(String.valueOf(cacheValue), TraceSnapshot.class);
            if (snapshot != null && snapshot.getTaskNo() == null) {
                snapshot.setTaskNo(fallbackTaskNo);
            }
            return snapshot;
        } catch (Exception e) {
            return null;
        }
    }
@@ -205,6 +302,29 @@
        return result;
    }
    private static List<StationTaskTraceEventVo> copyEventList(List<StationTaskTraceEventVo> source) {
        List<StationTaskTraceEventVo> result = new ArrayList<>();
        if (source == null) {
            return result;
        }
        for (StationTaskTraceEventVo item : source) {
            if (item == null) {
                continue;
            }
            StationTaskTraceEventVo copy = new StationTaskTraceEventVo();
            copy.setTimestamp(item.getTimestamp());
            copy.setEventType(item.getEventType());
            copy.setMessage(item.getMessage());
            copy.setStatus(item.getStatus());
            copy.setCurrentStationId(item.getCurrentStationId());
            copy.setTargetStationId(item.getTargetStationId());
            copy.setTraceVersion(item.getTraceVersion());
            copy.setDetails(copyDetails(item.getDetails()));
            result.add(copy);
        }
        return result;
    }
    private static boolean isTerminalStatus(String status) {
        return STATUS_BLOCKED.equals(status)
                || STATUS_CANCELLED.equals(status)
@@ -218,6 +338,33 @@
        private Integer pathOffset;
        private List<Integer> fullPathStationIds = new ArrayList<>();
        private boolean rerouted;
    }
    @Data
    private static class TraceSnapshot {
        private Integer taskNo;
        private String threadImpl;
        private String status;
        private Integer traceVersion;
        private Integer startStationId;
        private Integer currentStationId;
        private Integer finalTargetStationId;
        private Integer blockedStationId;
        private List<Integer> fullPathStationIds = new ArrayList<>();
        private List<Integer> issuedStationIds = new ArrayList<>();
        private List<Integer> passedStationIds = new ArrayList<>();
        private List<Integer> pendingStationIds = new ArrayList<>();
        private List<Integer> latestIssuedSegmentPath = new ArrayList<>();
        private List<StationTaskTraceSegmentVo> segmentList = new ArrayList<>();
        private Integer issuedSegmentCount;
        private Integer totalSegmentCount;
        private Long updatedAt;
        private Long terminalExpireAt;
        private List<StationTaskTraceEventVo> events = new ArrayList<>();
        private boolean isExpired(long now) {
            return terminalExpireAt != null && terminalExpireAt <= now;
        }
    }
    private static class TraceTaskState {
@@ -401,10 +548,58 @@
            vo.setIssuedSegmentCount(issuedSegmentCount);
            vo.setTotalSegmentCount(totalSegmentCount);
            vo.setUpdatedAt(updatedAt);
            vo.setEvents(new ArrayList<>(events));
            vo.setEvents(copyEventList(events));
            return vo;
        }
        private synchronized TraceSnapshot toSnapshot() {
            TraceSnapshot snapshot = new TraceSnapshot();
            snapshot.setTaskNo(taskNo);
            snapshot.setThreadImpl(threadImpl);
            snapshot.setStatus(status);
            snapshot.setTraceVersion(traceVersion);
            snapshot.setStartStationId(startStationId);
            snapshot.setCurrentStationId(currentStationId);
            snapshot.setFinalTargetStationId(finalTargetStationId);
            snapshot.setBlockedStationId(blockedStationId);
            snapshot.setFullPathStationIds(copyIntegerList(fullPathStationIds));
            snapshot.setIssuedStationIds(copyIntegerList(issuedStationIds));
            snapshot.setPassedStationIds(copyIntegerList(passedStationIds));
            snapshot.setPendingStationIds(copyIntegerList(pendingStationIds));
            snapshot.setLatestIssuedSegmentPath(copyIntegerList(latestIssuedSegmentPath));
            snapshot.setSegmentList(copySegmentList(segmentList));
            snapshot.setIssuedSegmentCount(issuedSegmentCount);
            snapshot.setTotalSegmentCount(totalSegmentCount);
            snapshot.setUpdatedAt(updatedAt);
            snapshot.setTerminalExpireAt(terminalExpireAt);
            snapshot.setEvents(copyEventList(events));
            return snapshot;
        }
        private static TraceTaskState fromSnapshot(TraceSnapshot snapshot) {
            TraceTaskState state = new TraceTaskState(snapshot.getTaskNo());
            state.threadImpl = snapshot.getThreadImpl();
            state.status = snapshot.getStatus() == null ? STATUS_WAITING : snapshot.getStatus();
            state.traceVersion = snapshot.getTraceVersion() == null ? 0 : snapshot.getTraceVersion();
            state.startStationId = snapshot.getStartStationId();
            state.currentStationId = snapshot.getCurrentStationId();
            state.finalTargetStationId = snapshot.getFinalTargetStationId();
            state.blockedStationId = snapshot.getBlockedStationId();
            state.fullPathStationIds = copyIntegerList(snapshot.getFullPathStationIds());
            state.issuedStationIds = copyIntegerList(snapshot.getIssuedStationIds());
            state.passedStationIds = copyIntegerList(snapshot.getPassedStationIds());
            state.pendingStationIds = copyIntegerList(snapshot.getPendingStationIds());
            state.latestIssuedSegmentPath = copyIntegerList(snapshot.getLatestIssuedSegmentPath());
            state.segmentList = copySegmentList(snapshot.getSegmentList());
            state.issuedSegmentCount = snapshot.getIssuedSegmentCount() == null ? 0 : snapshot.getIssuedSegmentCount();
            state.totalSegmentCount = snapshot.getTotalSegmentCount() == null ? state.segmentList.size() : snapshot.getTotalSegmentCount();
            state.updatedAt = snapshot.getUpdatedAt() == null ? System.currentTimeMillis() : snapshot.getUpdatedAt();
            state.terminalExpireAt = snapshot.getTerminalExpireAt();
            state.events.clear();
            state.events.addAll(copyEventList(snapshot.getEvents()));
            return state;
        }
        private List<Integer> buildFullPathForRegistration(boolean rerouted,
                                                           Integer planCurrentStationId,
                                                           List<Integer> localPathStationIds) {