From 50a309c2b89cb420051e217bd5db51efcae01e20 Mon Sep 17 00:00:00 2001 From: wangyu096 Date: Thu, 9 Nov 2023 09:46:49 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E4=B8=BB=E6=9C=BA=E4=BF=A1=E6=81=AF=E5=86=85=E9=83=A8=20API?= =?UTF-8?q?=E6=80=A7=E8=83=BD=20#2600?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/TaskExecuteServiceImpl.java | 92 ++++++++++++------- 1 file changed, 61 insertions(+), 31 deletions(-) diff --git a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskExecuteServiceImpl.java b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskExecuteServiceImpl.java index f5c92956cf..da297cb230 100644 --- a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskExecuteServiceImpl.java +++ b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskExecuteServiceImpl.java @@ -771,52 +771,71 @@ private AuthResult authFileTransfer(TaskInstanceDTO taskInstance, private ServiceListAppHostResultDTO acquireAndSetHosts(TaskInstanceDTO taskInstance, List stepInstances, Collection variables) { - long appId = taskInstance.getAppId(); + StopWatch watch = new StopWatch("AcquireAndSetHosts"); + try { + long appId = taskInstance.getAppId(); - // 提取动态分组/topo节点 - Set groups = new HashSet<>(); - Set topoNodes = new HashSet<>(); - stepInstances.forEach(stepInstance -> extractDynamicGroupsAndTopoNodes(stepInstance, groups, topoNodes)); - if (CollectionUtils.isNotEmpty(variables)) { - variables.forEach(variable -> { - if (TaskVariableTypeEnum.HOST_LIST.getType() == variable.getType()) { - extractDynamicGroupsAndTopoNodes(variable.getTargetServers(), groups, topoNodes); - } - }); - } + // 提取动态分组/topo节点 + Set groups = new HashSet<>(); + Set topoNodes = new HashSet<>(); + stepInstances.forEach(stepInstance -> extractDynamicGroupsAndTopoNodes(stepInstance, groups, topoNodes)); + if (CollectionUtils.isNotEmpty(variables)) { + variables.forEach(variable -> { + if (TaskVariableTypeEnum.HOST_LIST.getType() == variable.getType()) { + extractDynamicGroupsAndTopoNodes(variable.getTargetServers(), groups, topoNodes); + } + }); + } - // 获取动态分组的主机并设置 - fillDynamicGroupHosts(appId, groups, stepInstances, variables); + // 获取动态分组的主机并设置 + fillDynamicGroupHosts(watch, appId, groups, stepInstances, variables); - // 获取topo节点的主机并设置 - fillTopoNodeHosts(appId, topoNodes, stepInstances, variables); + // 获取topo节点的主机并设置 + fillTopoNodeHosts(watch, appId, topoNodes, stepInstances, variables); - // 提取作业包含的主机列表 - Set queryHosts = extractHosts(stepInstances, variables); + // 提取作业包含的主机列表 + watch.start("extractHosts"); + Set queryHosts = extractHosts(stepInstances, variables); + watch.stop(); - if (CollectionUtils.isEmpty(queryHosts)) { - return ServiceListAppHostResultDTO.EMPTY; - } + if (CollectionUtils.isEmpty(queryHosts)) { + return ServiceListAppHostResultDTO.EMPTY; + } - ServiceListAppHostResultDTO queryHostsResult = hostService.batchGetAppHosts(appId, queryHosts, - needRefreshHostBkAgentId(taskInstance)); + watch.start("batchGetAppHosts"); + ServiceListAppHostResultDTO queryHostsResult = hostService.batchGetAppHosts(appId, queryHosts, + needRefreshHostBkAgentId(taskInstance)); + watch.stop(); - if (CollectionUtils.isNotEmpty(queryHostsResult.getNotExistHosts())) { - // 如果主机在cmdb不存在,直接报错 - throwHostInvalidException(queryHostsResult.getNotExistHosts()); - } + if (CollectionUtils.isNotEmpty(queryHostsResult.getNotExistHosts())) { + // 如果主机在cmdb不存在,直接报错 + throwHostInvalidException(queryHostsResult.getNotExistHosts()); + } - fillTaskInstanceHostDetail(taskInstance, stepInstances, variables, queryHostsResult); + watch.start("fillTaskInstanceHostDetail"); + fillTaskInstanceHostDetail(taskInstance, stepInstances, variables, queryHostsResult); + watch.stop(); + + return queryHostsResult; + } finally { + if (watch.isRunning()) { + watch.stop(); + } + if (watch.getTotalTimeMillis() > 1000) { + log.warn("AcquireAndSetHosts slow, watch: {}", watch.prettyPrint()); + } + } - return queryHostsResult; } - private void fillDynamicGroupHosts(long appId, + private void fillDynamicGroupHosts(StopWatch watch, + long appId, Set groups, List stepInstances, Collection variables) { // 获取动态分组的主机并设置 if (CollectionUtils.isNotEmpty(groups)) { + watch.start("fillDynamicGroupHosts"); Map> dynamicGroupHosts = hostService.batchGetAndGroupHostsByDynamicGroup(appId, groups); stepInstances.forEach(stepInstance -> { @@ -839,14 +858,17 @@ private void fillDynamicGroupHosts(long appId, } }); } + watch.stop(); } } - private void fillTopoNodeHosts(long appId, + private void fillTopoNodeHosts(StopWatch watch, + long appId, Set topoNodes, List stepInstances, Collection variables) { if (CollectionUtils.isNotEmpty(topoNodes)) { + watch.start("fillTopoNodeHosts"); Map> topoNodeHosts = hostService.getAndGroupHostsByTopoNodes(appId, topoNodes); stepInstances.forEach(stepInstance -> { @@ -869,6 +891,7 @@ private void fillTopoNodeHosts(long appId, } }); } + watch.stop(); } } @@ -2217,6 +2240,8 @@ private void setAgentStatus(List hosts) { if (CollectionUtils.isEmpty(hosts)) { return; } + long start = System.currentTimeMillis(); + List hostAgentStateQueryList = new ArrayList<>(hosts.size()); Map hostAgentStateQueryMap = new HashMap<>(hosts.size()); hosts.forEach(hostDTO -> { @@ -2242,6 +2267,11 @@ private void setAgentStatus(List hosts) { host.setAlive(AgentAliveStatusEnum.NOT_ALIVE.getStatusValue()); } } + + long cost = System.currentTimeMillis() - start; + if (cost > 1000) { + log.warn("SetAgentStatus slow, hostSize: {}, cost:{} ms", hosts.size(), cost); + } } @Override