Skip to content

Commit

Permalink
feature: 原子操作能力支持滚动执行 TencentBlueKing#446
Browse files Browse the repository at this point in the history
修复执行详情-结果分组和主机数量不正确的问题
  • Loading branch information
wangyu096 committed Jul 14, 2022
1 parent b3509a2 commit f2b5cf9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public AgentTaskResultGroupBaseDTO(Integer status, String tag) {
this.tag = tag;
}

public AgentTaskResultGroupBaseDTO(AgentTaskResultGroupBaseDTO baseResultGroup) {
this.status = baseResultGroup.getStatus();
this.tag = baseResultGroup.getTag();
this.totalAgentTasks = baseResultGroup.getTotalAgentTasks();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ public AgentTaskResultGroupDTO(Integer status, String tag) {
super(status, tag);
}

public AgentTaskResultGroupDTO(AgentTaskResultGroupBaseDTO baseResultGroup) {
super(baseResultGroup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.tencent.bk.job.common.constant.Order;
import com.tencent.bk.job.execute.model.AgentTaskDTO;
import com.tencent.bk.job.execute.model.AgentTaskDetailDTO;
import com.tencent.bk.job.execute.model.AgentTaskResultGroupBaseDTO;
import com.tencent.bk.job.execute.model.AgentTaskResultGroupDTO;
import com.tencent.bk.job.execute.model.StepInstanceBaseDTO;

Expand Down Expand Up @@ -94,6 +95,18 @@ List<AgentTaskResultGroupDTO> listAndGroupAgentTasks(StepInstanceBaseDTO stepIns
int executeCount,
Integer batch);

/**
* 获取任务结果分组
*
* @param stepInstanceId 步骤实例ID
* @param executeCount 执行次数
* @param batch 滚动执行批次;传入null或者0将忽略该参数
* @return Agent任务
*/
List<AgentTaskResultGroupBaseDTO> listResultGroups(long stepInstanceId,
int executeCount,
Integer batch);

/**
* 根据执行结果查询Agent任务详情(排序、限制返回数量) - 包含主机详情
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.tencent.bk.job.execute.dao.GseTaskIpLogDAO;
import com.tencent.bk.job.execute.model.AgentTaskDTO;
import com.tencent.bk.job.execute.model.AgentTaskDetailDTO;
import com.tencent.bk.job.execute.model.AgentTaskResultGroupBaseDTO;
import com.tencent.bk.job.execute.model.AgentTaskResultGroupDTO;
import com.tencent.bk.job.execute.model.FileSourceDTO;
import com.tencent.bk.job.execute.model.StepInstanceBaseDTO;
Expand Down Expand Up @@ -114,6 +115,19 @@ public List<AgentTaskResultGroupDTO> listAndGroupAgentTasks(StepInstanceBaseDTO
return resultGroups.stream().sorted().collect(Collectors.toList());
}

@Override
public List<AgentTaskResultGroupBaseDTO> listResultGroups(long stepInstanceId,
int executeCount,
Integer batch) {
List<AgentTaskResultGroupBaseDTO> resultGroups;
resultGroups = fileAgentTaskDAO.listResultGroups(stepInstanceId, executeCount, batch);
if (CollectionUtils.isEmpty(resultGroups)) {
// 兼容历史数据
resultGroups = gseTaskIpLogDAO.listResultGroups(stepInstanceId, executeCount);
}
return resultGroups;
}

@Override
public List<AgentTaskDetailDTO> listAgentTaskDetailByResultGroup(StepInstanceBaseDTO stepInstance,
Integer executeCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.tencent.bk.job.execute.dao.ScriptAgentTaskDAO;
import com.tencent.bk.job.execute.model.AgentTaskDTO;
import com.tencent.bk.job.execute.model.AgentTaskDetailDTO;
import com.tencent.bk.job.execute.model.AgentTaskResultGroupBaseDTO;
import com.tencent.bk.job.execute.model.AgentTaskResultGroupDTO;
import com.tencent.bk.job.execute.model.StepInstanceBaseDTO;
import com.tencent.bk.job.execute.service.HostService;
Expand Down Expand Up @@ -159,6 +160,19 @@ public List<AgentTaskResultGroupDTO> listAndGroupAgentTasks(StepInstanceBaseDTO
return resultGroups.stream().sorted().collect(Collectors.toList());
}

@Override
public List<AgentTaskResultGroupBaseDTO> listResultGroups(long stepInstanceId,
int executeCount,
Integer batch) {
List<AgentTaskResultGroupBaseDTO> resultGroups;
resultGroups = scriptAgentTaskDAO.listResultGroups(stepInstanceId, executeCount, batch);
if (CollectionUtils.isEmpty(resultGroups)) {
// 兼容历史数据
resultGroups = gseTaskIpLogDAO.listResultGroups(stepInstanceId, executeCount);
}
return resultGroups;
}

@Override
public List<AgentTaskDetailDTO> listAgentTaskDetailByResultGroup(StepInstanceBaseDTO stepInstance,
Integer executeCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.tencent.bk.job.execute.engine.consts.AgentTaskStatus;
import com.tencent.bk.job.execute.model.AgentTaskDTO;
import com.tencent.bk.job.execute.model.AgentTaskDetailDTO;
import com.tencent.bk.job.execute.model.AgentTaskResultGroupBaseDTO;
import com.tencent.bk.job.execute.model.AgentTaskResultGroupDTO;
import com.tencent.bk.job.execute.model.ConfirmStepInstanceDTO;
import com.tencent.bk.job.execute.model.FileSourceTaskLogDTO;
Expand Down Expand Up @@ -551,7 +552,7 @@ private StepExecutionDetailDTO loadAllTasksFromDBAndBuildExecutionResultInMemory

watch.start("loadAllTasksFromDbAndGroup");
List<AgentTaskResultGroupDTO> resultGroups = listAndGroupAgentTasks(stepInstance,
query.getStepInstanceId(), query.getExecuteCount(), query.getBatch());
query.getExecuteCount(), query.getBatch());

if (CollectionUtils.isNotEmpty(query.getMatchHostIds())) {
filterAgentTasksByMatchIp(resultGroups, query.getMatchHostIds());
Expand Down Expand Up @@ -579,7 +580,6 @@ private StepExecutionDetailDTO loadAllTasksFromDBAndBuildExecutionResultInMemory
}

private List<AgentTaskResultGroupDTO> listAndGroupAgentTasks(StepInstanceBaseDTO stepInstance,
long stepInstanceId,
int executeCount,
Integer batch) {
List<AgentTaskResultGroupDTO> resultGroups = null;
Expand Down Expand Up @@ -671,7 +671,7 @@ private Set<Long> getMatchHostIdsByIp(StepInstanceBaseDTO stepInstance, StepExec

private StepExecutionDetailDTO buildExecutionDetailWhenTaskAreEmpty(StepInstanceBaseDTO stepInstance,
Integer batch) {
List<AgentTaskResultGroupDTO> resultGroups = listAndGroupAgentTasks(stepInstance, stepInstance.getId(),
List<AgentTaskResultGroupDTO> resultGroups = listAndGroupAgentTasks(stepInstance,
stepInstance.getExecuteCount(), batch);
StepExecutionDetailDTO executeDetail = new StepExecutionDetailDTO(stepInstance);
executeDetail.setResultGroups(resultGroups);
Expand Down Expand Up @@ -713,7 +713,6 @@ private StepExecutionDetailDTO filterAndSortExecutionResultInDB(StopWatch watch,
StepInstanceBaseDTO stepInstance,
StepExecutionResultQuery query) {
query.transformOrderFieldToDbField();
long stepInstanceId = query.getStepInstanceId();
int queryExecuteCount = query.getExecuteCount();
Integer status = query.getStatus();
String tag = query.getTag();
Expand All @@ -722,11 +721,14 @@ private StepExecutionDetailDTO filterAndSortExecutionResultInDB(StopWatch watch,
executeDetail.setExecuteCount(queryExecuteCount);

watch.start("getBaseResultGroups");
List<AgentTaskResultGroupDTO> resultGroups = listAndGroupAgentTasks(stepInstance, stepInstanceId,
List<AgentTaskResultGroupBaseDTO> baseResultGroups = listBaseResultGroups(stepInstance,
queryExecuteCount, query.getBatch());
watch.stop();

watch.start("setAgentTasks");
List<AgentTaskResultGroupDTO> resultGroups = baseResultGroups.stream()
.map(AgentTaskResultGroupDTO::new)
.collect(Collectors.toList());
if (status != null) {
List<AgentTaskDetailDTO> tasks = listAgentTaskByResultGroup(stepInstance, queryExecuteCount,
query.getBatch(), status, tag, query.getMaxAgentTasksForResultGroup(), query.getOrderField(),
Expand All @@ -748,6 +750,18 @@ private StepExecutionDetailDTO filterAndSortExecutionResultInDB(StopWatch watch,
return executeDetail;
}

private List<AgentTaskResultGroupBaseDTO> listBaseResultGroups(StepInstanceBaseDTO stepInstance,
int executeCount,
Integer batch) {
List<AgentTaskResultGroupBaseDTO> resultGroups = null;
if (stepInstance.isScriptStep()) {
resultGroups = scriptAgentTaskService.listResultGroups(stepInstance.getId(), executeCount, batch);
} else if (stepInstance.isFileStep()) {
resultGroups = fileAgentTaskService.listResultGroups(stepInstance.getId(), executeCount, batch);
}
return resultGroups;
}

private List<AgentTaskDetailDTO> listAgentTaskByResultGroup(StepInstanceBaseDTO stepInstance,
int queryExecuteCount,
Integer batch,
Expand Down

0 comments on commit f2b5cf9

Please sign in to comment.