Commit abdafdf1 by 杨卫

fix(strategy): 修复策略删除时任务中心状态校验问题

- 在删除策略时增加对任务中心实际状态的校验,避免误删已完成任务
- 添加 verifyTaskCenterNotFinished 方法查询任务中心获取准确的任务状态
- 当任务中心状态为 FINISHED 时阻止删除操作并返回相应错误提示
- 更新异常日志记录,增加 strategyId 参数便于问题追踪
- 引入 TASK_CENTER_FINISHED_STATUS 常量定义任务完成状态值
- 添加相关 TaskDTO 和 GetTaskDetailRequest 类的导入声明
1 parent 608bb6dc
......@@ -88,10 +88,12 @@ public class StrategyInfoRemoteImpl implements StrategyInfoRemote {
try {
return RpcResponse.success(strategyInfoService.delete(strategyInfo));
} catch (RpcServiceException e) {
log.error(e.getMessage(), e);
log.error("删除策略 occur RpcServiceException, strategyId={}",
strategyInfo != null ? strategyInfo.getId() : null, e);
return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) {
log.error(e.getMessage(), e);
log.error("删除策略 occur Exception, strategyId={}",
strategyInfo != null ? strategyInfo.getId() : null, e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage());
}
}
......
......@@ -4,7 +4,9 @@ import cn.com.glsx.dj.strategy.enums.StrategyErrorEnum;
import cn.com.glsx.dj.strategy.enums.StrategyStatusEnum;
import cn.com.glsx.dj.strategy.enums.StrategyTypeEnum;
import cn.com.glsx.dj.strategy.model.StrategyInfo;
import cn.com.glsx.dj.taskcenter.dto.taskcenter.TaskDTO;
import cn.com.glsx.dj.taskcenter.dto.taskcenter.request.DeleteTaskRequest;
import cn.com.glsx.dj.taskcenter.dto.taskcenter.request.GetTaskDetailRequest;
import cn.com.glsx.dj.taskcenter.remote.TaskCenterRemote;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
......@@ -34,6 +36,9 @@ public class StrategyDeleteDelegate {
@Resource
private TaskCenterRemote taskCenterRemote;
/** 任务中心任务状态:已完成 */
private static final String TASK_CENTER_FINISHED_STATUS = "FINISHED";
/**
* 校验策略是否可删除。不可删时抛出 RpcServiceException。
*
......@@ -46,6 +51,8 @@ public class StrategyDeleteDelegate {
if (!StrategyStatusEnum.UNFINISHED.getValue().equals(info.getPushFlag())) {
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "该类型策略仅在未完成状态可删除");
}
// 任务类状态由 MQ 定时同步,本地 push_flag 可能滞后于任务中心,删除前需核对任务中心实际状态
verifyTaskCenterNotFinished(info, typeEnum);
} else {
if (!StrategyStatusEnum.PENDING.getValue().equals(info.getPushFlag())) {
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "策略已下发,不允许删除");
......@@ -79,6 +86,44 @@ public class StrategyDeleteDelegate {
// ==================== 私有方法 ====================
/**
* 校验任务中心任务是否仍为未完成。
*
* <p>任务类策略状态由 MQ 定时同步,本地 {@code push_flag} 可能滞后于任务中心实际状态。
* 删除前查询任务中心任务详情,若任务中心已 FINISHED(本地尚未同步为已完成),
* 则不允许删除,避免误删已完成任务。</p>
*
* @param info 数据库中的策略记录
* @param typeEnum 策略类型枚举(提供 bizCode)
* @throws RpcServiceException 任务中心查询失败或任务已完成
*/
private void verifyTaskCenterNotFinished(StrategyInfo info, StrategyTypeEnum typeEnum) {
if (taskCenterRemote == null) {
return;
}
try {
GetTaskDetailRequest request = new GetTaskDetailRequest();
request.setBizType(typeEnum.getBizCode());
request.setBizInstanceId(String.valueOf(info.getId()));
RpcResponse<TaskDTO> resp = taskCenterRemote.getTaskDetail(request);
if (resp == null || !resp.isSuccess()) {
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "查询任务中心状态失败,请稍后重试");
}
if (resp.getResult() == null) {
// 任务中心无此任务记录,视为未完成,允许删除
return;
}
if (TASK_CENTER_FINISHED_STATUS.equals(resp.getResult().getStatus())) {
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "任务已在任务中心完成,不允许删除");
}
} catch (RpcServiceException e) {
throw e;
} catch (Exception e) {
log.error("[DeleteDelegate] 查询任务中心任务状态异常, strategyId={}", info.getId(), e);
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "查询任务中心状态失败,请稍后重试");
}
}
private void deleteTaskCenterRecord(StrategyInfo info, String updatedBy, StrategyTypeEnum typeEnum) {
if (taskCenterRemote == null) {
return;
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(Top 3)

    1. 缺失 RPC 调用失败的详细日志记录(可维护性与功能健壮性)

    • 问题描述:在 StrategyDeleteDelegate.java 的 verifyTaskCenterNotFinished 方法中,当 !resp.isSuccess() 时,代码直接抛出了 RpcServiceException,但没有记录任务中心返回的具体错误码或错误信息。
    • 影响分析:当任务中心接口报错(如参数校验失败、内部异常等)时,调用方只能看到笼统的“查询任务中心状态失败,请稍后重试”,排查线上问题时将无法得知下游服务的具体报错原因,增加排查成本。
    • 优化建议:在抛出异常前,将下游 RPC 返回的错误信息(resp.getMessage() 或 resp.getCode())打印到 error 日志中。
    • 代码示例: java RpcResponse<TaskDTO> resp = taskCenterRemote.getTaskDetail(request); if (resp == null || !resp.isSuccess()) { // 增加错误日志记录下游真实报错信息 log.error("[DeleteDelegate] 查询任务中心失败, strategyId={}, response={}", info.getId(), resp != null ? resp.getMessage() : "null"); throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "查询任务中心状态失败,请稍后重试"); }

    2. 依赖注入对象空指针判断的逻辑隐患(功能正确性)

    • 问题描述:verifyTaskCenterNotFinished 方法开头包含了 if (taskCenterRemote == null) { return; }。
    • 影响分析:如果 taskCenterRemote 是强依赖(通常由 Spring @Resource 注入),正常情况下不应为空。如果因为配置问题导致注入失败,该判断会静默跳过任务中心的状态校验,直接允许删除。这违背了新增此校验方法的业务初衷,可能导致严重的“误删已完成任务”的业务故障。
    • 优化建议:除非该 RPC 服务明确设计为“可选依赖”(Optional),否则不应静默 return。建议移除该 null 判断让框架在启动时暴露问题,或者在为空时抛出明确的系统异常。
    • 代码示例: java if (taskCenterRemote == null) { log.error("taskCenterRemote is null, 无法执行任务中心状态校验"); throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "内部服务配置异常"); }

    3. 对未知 Exception 直接抛出 e.getMessage() 的隐患(最佳实践)

    • 问题描述:在 StrategyInfoRemoteImpl.java 中,捕获 Exception e 后,返回了 RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage());。
    • 影响分析:对于非业务自定义的运行时异常(如 NullPointerException、IndexOutOfBoundsException 等),e.getMessage() 的值通常为 null 或包含底层的代码细节。这会导致返回给调用方的错误提示为 null,且可能暴露内部实现细节。
    • 优化建议:对于兜底的 Exception,建议返回统一的、友好的系统异常提示文案,而不是直接使用 e.getMessage()。
    • 代码示例: java } catch (Exception e) { log.error("删除策略 occur Exception, strategyId={}", strategyInfo != null ? strategyInfo.getId() : null, e); // 建议不要直接使用 e.getMessage() return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, "系统内部异常,请稍后重试"); }

    二、评分明细

    • 功能实现的正确性与健壮性(36/40分): 核心业务逻辑正确,成功引入了任务中心状态的前置校验,有效防止了状态不同步导致的误删问题。未发现违规的子查询或 Dubbo 接口参数变更。扣分项在于 RPC 依赖为空时的静默跳过逻辑存在业务风险。

    • 安全性与潜在风险(30/30分): 代码变更未引入 SQL 注入、XSS、越权等常见的安全漏洞。日志中打印的 strategyId 不属于敏感隐私信息,无信息泄露风险。

    • 最佳实践与可维护性(16/20分): 常量抽取(TASK_CENTER_FINISHED_STATUS)和方法拆分符合规范,日志增加了关键的上下文 ID(strategyId),值得肯定。扣分项在于 RPC 失败时缺乏下游错误日志的保留,以及兜底异常处理不够优雅。

    • 性能与资源利用(4/5分): 在删除操作中增加了一次同步的 RPC 查询,会略微增加接口响应时间,但属于保证数据一致性的必要开销。无明显的性能瓶颈或资源浪费。

    • 提交信息质量(5/5分): Commit message 格式规范(使用了 fix(strategy):),清晰地列出了变更的目的、具体改动点以及新增的类和常量,可追溯性极强,是非常优秀的提交记录。

    总分:91分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!