From b02eabaffc18484792a5a08252f8dd76dd26f1f3 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Thu, 6 Apr 2023 10:31:12 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E5=9C=A8CMDB=E8=B7=A8=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E8=BD=AC=E7=A7=BB=E4=B8=BB=E6=9C=BA=E5=90=8E=E4=BB=8E?= =?UTF-8?q?ESB=E6=8E=A5=E5=8F=A3=E7=AB=8B=E5=8D=B3=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=8A=A5=E4=B8=BB=E6=9C=BA=E6=97=A0=E6=95=88=20#1909?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在其他业务下的主机根据CMDB实时数据进行二次判定 --- .../service/host/impl/HostServiceImpl.java | 87 ++++++++++++++++++- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/HostServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/HostServiceImpl.java index e174c18582..1cb4fe48c7 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/HostServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/HostServiceImpl.java @@ -1366,6 +1366,11 @@ public ServiceListAppHostResultDTO listAppHostsPreferCache(Long appId, } }); + // 对于判定为其他业务下的主机,可能是缓存数据不准确导致,需要根据CMDB实时数据进行二次判定 + if (CollectionUtils.isNotEmpty(notInAppHosts)) { + reConfirmNotInAppHostsByCmdb(notInAppHosts, notExistHosts, validHosts, appId, includeBizIds); + } + if (CollectionUtils.isNotEmpty(notExistHosts) || CollectionUtils.isNotEmpty(notInAppHosts)) { log.info("Contains invalid hosts, appId: {}, notExistHosts: {}, hostsInOtherApp: {}", appId, notExistHosts, notInAppHosts); @@ -1378,6 +1383,47 @@ public ServiceListAppHostResultDTO listAppHostsPreferCache(Long appId, return result; } + /** + * 对于判定为其他业务下的主机,可能是缓存数据不准确导致,根据CMDB实时数据进行二次判定 + * + * @param notInAppHosts 前期判定为在其他业务下的主机,在该方法中数据可能被修改 + * @param notExistHosts 前期判定为不存在的主机,在该方法中数据可能被修改 + * @param validHosts 前期判定为在业务下的主机,在该方法中数据可能被修改 + * @param appId Job内业务ID + * @param includeBizIds Job内业务ID可能对应的多个CMDB业务ID列表 + */ + private void reConfirmNotInAppHostsByCmdb(List notInAppHosts, + List notExistHosts, + List validHosts, + Long appId, + List includeBizIds) { + Pair, List> cmdbHostsPair = listHostsFromCmdb(notInAppHosts); + if (CollectionUtils.isNotEmpty(cmdbHostsPair.getLeft())) { + notExistHosts.addAll(cmdbHostsPair.getLeft()); + } + List cmdbExistHosts = cmdbHostsPair.getRight(); + if (CollectionUtils.isNotEmpty(cmdbExistHosts)) { + notInAppHosts.clear(); + List cmdbValidHosts = new ArrayList<>(); + cmdbExistHosts.forEach(existHost -> { + if (includeBizIds.contains(existHost.getBizId())) { + validHosts.add(existHost.toHostDTO()); + cmdbValidHosts.add(existHost); + } else { + notInAppHosts.add(existHost.toHostDTO()); + } + }); + if (!cmdbValidHosts.isEmpty()) { + log.info( + "{} hosts belong to appId {} after check in cmdb, cmdbValidHosts={}", + cmdbValidHosts.size(), + appId, + cmdbValidHosts + ); + } + } + } + private void refreshHostAgentIdIfNeed(boolean needRefreshAgentId, List hosts) { // 如果Job缓存的主机中没有agentId,那么需要从cmdb实时获取(解决一些特殊场景,比如节点管理Agent插件安装,bk_agent_id更新事件还没被处理的场景 if (!needRefreshAgentId || CollectionUtils.isEmpty(hosts)) { @@ -1409,19 +1455,54 @@ private void refreshHostAgentIdIfNeed(boolean needRefreshAgentId, List, List> listHostsFromCacheOrCmdb(Collection hosts) { - List appHosts = new ArrayList<>(); + private Pair, List> listHostsFromCmdb(Collection hosts) { List notExistHosts = new ArrayList<>(); + List appHosts = new ArrayList<>(); + Pair, List> pair = separateByHostIdOrCloudIp(hosts); + List hostIds = pair.getLeft(); + List cloudIps = pair.getRight(); + if (CollectionUtils.isNotEmpty(hostIds)) { + Pair, List> result = + new ListHostByHostIdsStrategy().listHostsFromCmdb(hostIds); + appHosts.addAll(result.getRight()); + if (CollectionUtils.isNotEmpty(result.getLeft())) { + result.getLeft().forEach(notExistHostId -> { + notExistHosts.add(HostDTO.fromHostId(notExistHostId)); + }); + } + } + if (CollectionUtils.isNotEmpty(cloudIps)) { + Pair, List> result = + new ListHostByIpsStrategy().listHostsFromCmdb(cloudIps); + appHosts.addAll(result.getRight()); + if (CollectionUtils.isNotEmpty(result.getLeft())) { + result.getLeft().forEach(notExistCloudIp -> { + notExistHosts.add(HostDTO.fromCloudIp(notExistCloudIp)); + }); + } + } + return Pair.of(notExistHosts, appHosts); + } + + private Pair, List> separateByHostIdOrCloudIp(Collection hosts) { List hostIds = new ArrayList<>(); List cloudIps = new ArrayList<>(); hosts.forEach(host -> { if (host.getHostId() != null) { hostIds.add(host.getHostId()); } else { - // 兼容使用云区域+ip来代表主机的场景 cloudIps.add(host.toCloudIp()); } }); + return Pair.of(hostIds, cloudIps); + } + + private Pair, List> listHostsFromCacheOrCmdb(Collection hosts) { + List appHosts = new ArrayList<>(); + List notExistHosts = new ArrayList<>(); + Pair, List> pair = separateByHostIdOrCloudIp(hosts); + List hostIds = pair.getLeft(); + List cloudIps = pair.getRight(); if (CollectionUtils.isNotEmpty(hostIds)) { Pair, List> result = listHostsByStrategy(hostIds, new ListHostByHostIdsStrategy());