Junjie
1 天以前 852664df1caf38831793b341edcada9dd7b6c22a
src/main/java/com/zy/core/thread/impl/station/StationSegmentExecutor.java
@@ -6,6 +6,7 @@
import com.zy.asrs.domain.vo.StationTaskTraceSegmentVo;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
@@ -15,6 +16,8 @@
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.move.StationMoveCoordinator;
import com.zy.core.trace.StationTaskTraceRegistry;
import com.zy.core.thread.impl.v5.StationV5RuntimeConfigProvider;
import com.zy.core.thread.support.StationTaskLocationRegistry;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
@@ -24,11 +27,13 @@
import java.util.Map;
import java.util.function.Function;
@Slf4j
public class StationSegmentExecutor {
    private static final String CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = "stationCommandSegmentAdvanceRatio";
    private static final double DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = 0.3d;
    private static final long CURRENT_STATION_TIMEOUT_MS = 1000L * 60L;
    private static final long TASK_LOCATION_STALE_MS = 2_000L;
    private final DeviceConfig deviceConfig;
    private final RedisUtil redisUtil;
@@ -236,15 +241,19 @@
                                         Integer currentStationId) {
        // 在下发新分段前检查路由版本是否仍然有效,避免在路由版本已更新的情况下下发旧版本命令
        if (!isRouteDispatchable(command == null ? null : command.getTaskNo(), command == null ? null : command.getRouteVersion())) {
            if (traceRegistry != null && command != null) {
                traceRegistry.markCancelled(command.getTaskNo(), traceVersion, currentStationId,
                        buildDetails("reason", "route_version_replaced_before_segment_send", "routeVersion", command.getRouteVersion()));
            // 首次校验失败可能是 Redis 写入延迟导致的,短暂等待后重试一次。
            sleepQuietly(50L);
            if (!isRouteDispatchable(command == null ? null : command.getTaskNo(), command == null ? null : command.getRouteVersion())) {
                if (traceRegistry != null && command != null) {
                    traceRegistry.markCancelled(command.getTaskNo(), traceVersion, currentStationId,
                            buildDetails("reason", "route_version_replaced_before_segment_send", "routeVersion", command.getRouteVersion()));
                }
                markCancelled(command == null ? null : command.getTaskNo(),
                        command == null ? null : command.getRouteVersion(),
                        currentStationId,
                        "route_version_replaced_before_segment_send");
                return false;
            }
            markCancelled(command == null ? null : command.getTaskNo(),
                    command == null ? null : command.getRouteVersion(),
                    currentStationId,
                    "route_version_replaced_before_segment_send");
            return false;
        }
        
        while (true) {
@@ -310,6 +319,13 @@
    }
    private double loadSegmentAdvanceRatio() {
        if (isV5ThreadImpl()) {
            StationV5RuntimeConfigProvider configProvider = SpringUtils.getBean(StationV5RuntimeConfigProvider.class);
            if (configProvider != null) {
                return configProvider.getSegmentAdvanceRatio();
            }
            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
        }
        try {
            ConfigService configService = SpringUtils.getBean(ConfigService.class);
            if (configService == null) {
@@ -363,6 +379,9 @@
    }
    private StationProtocol findCurrentStationByTask(Integer taskNo) {
        if (isV5ThreadImpl()) {
            return findCurrentStationByTaskFromRegistry(taskNo);
        }
        try {
            com.zy.asrs.service.DeviceConfigService deviceConfigService = SpringUtils.getBean(com.zy.asrs.service.DeviceConfigService.class);
            if (deviceConfigService == null) {
@@ -389,6 +408,27 @@
            return null;
        }
        return null;
    }
    private StationProtocol findCurrentStationByTaskFromRegistry(Integer taskNo) {
        StationTaskLocationRegistry registry = SpringUtils.getBean(StationTaskLocationRegistry.class);
        if (registry == null) {
            return null;
        }
        StationTaskLocationRegistry.TaskLocationSnapshot snapshot = registry.findActive(taskNo, TASK_LOCATION_STALE_MS);
        if (snapshot == null || !snapshot.isLoading()) {
            return null;
        }
        StationProtocol stationProtocol = new StationProtocol();
        stationProtocol.setTaskNo(snapshot.getTaskNo());
        stationProtocol.setStationId(snapshot.getStationId());
        stationProtocol.setRunBlock(snapshot.isRunBlock());
        stationProtocol.setLoading(true);
        return stationProtocol;
    }
    private boolean isV5ThreadImpl() {
        return deviceConfig != null && "ZyStationV5Thread".equals(deviceConfig.getThreadImpl());
    }
    private List<StationTaskTraceSegmentVo> buildTraceSegments(List<StationCommand> segmentCommands) {
@@ -492,7 +532,15 @@
            return true;
        }
        StationMoveCoordinator moveCoordinator = loadMoveCoordinator();
        return moveCoordinator == null || moveCoordinator.canDispatchRoute(taskNo, routeVersion);
        if (moveCoordinator == null) {
            return true;
        }
        boolean dispatchable = moveCoordinator.canDispatchRoute(taskNo, routeVersion);
        if (!dispatchable) {
            log.warn("isRouteDispatchable rejected, taskNo={}, routeVersion={}, threadImpl={}",
                    taskNo, routeVersion, deviceConfig == null ? null : deviceConfig.getThreadImpl());
        }
        return dispatchable;
    }
    private StationMoveCoordinator loadMoveCoordinator() {