Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: 在CMDB跨业务转移主机后从ESB接口立即使用报主机无效 #1909 #1912

Merged
merged 3 commits into from
Apr 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ public List<HostSimpleDTO> findStatusChangedHosts(List<HostSimpleDTO> hostList)
queryAgentStatusClient.batchGetAgentStatus(cloudIpList);
for (HostSimpleDTO host : hostList) {
QueryAgentStatusClient.AgentStatus agentStatus = agentStatusMap.get(host.getCloudIp());
if(host.getGseAgentAlive() != agentStatus.status){
if (host.getGseAgentAlive() != agentStatus.status) {
host.setGseAgentAlive(agentStatus.status);
statusChangedHosts.add(host);
}
Expand Down Expand Up @@ -1436,7 +1436,8 @@ public int updateHostsStatus(List<HostSimpleDTO> simpleHostList) {
.collect(Collectors.groupingBy(HostSimpleDTO::getGseAgentAlive,
Collectors.mapping(HostSimpleDTO::getHostId, Collectors.toList())));
for (Integer status : statusGroupMap.keySet()) {
updateCount += applicationHostDAO.batchUpdateHostStatusByHostIds(status, statusGroupMap.get(status));
updateCount += applicationHostDAO.batchUpdateHostStatusByHostIds(status,
statusGroupMap.get(status));
}
start += batchSize;
} while (end < size);
Expand Down Expand Up @@ -1522,6 +1523,11 @@ public ServiceListAppHostResultDTO listAppHosts(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);
Expand All @@ -1534,9 +1540,75 @@ public ServiceListAppHostResultDTO listAppHosts(Long appId,
return result;
}

private Pair<List<HostDTO>, List<BasicAppHost>> listHostsFromCacheOrCmdb(Collection<HostDTO> hosts) {
List<BasicAppHost> appHosts = new ArrayList<>();
/**
* 对于判定为其他业务下的主机,可能是缓存数据不准确导致,根据CMDB实时数据进行二次判定
*
* @param notInAppHosts 前期判定为在其他业务下的主机,在该方法中数据可能被修改
* @param notExistHosts 前期判定为不存在的主机,在该方法中数据可能被修改
* @param validHosts 前期判定为在业务下的主机,在该方法中数据可能被修改
* @param appId Job内业务ID
* @param includeBizIds Job内业务ID可能对应的多个CMDB业务ID列表
*/
private void reConfirmNotInAppHostsByCmdb(List<HostDTO> notInAppHosts,
List<HostDTO> notExistHosts,
List<HostDTO> validHosts,
Long appId,
List<Long> includeBizIds) {
Pair<List<HostDTO>, List<BasicAppHost>> cmdbHostsPair = listHostsFromCmdb(notInAppHosts);
if (CollectionUtils.isNotEmpty(cmdbHostsPair.getLeft())) {
notExistHosts.addAll(cmdbHostsPair.getLeft());
}
List<BasicAppHost> cmdbExistHosts = cmdbHostsPair.getRight();
if (CollectionUtils.isNotEmpty(cmdbExistHosts)) {
notInAppHosts.clear();
List<BasicAppHost> 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 Pair<List<HostDTO>, List<BasicAppHost>> listHostsFromCmdb(Collection<HostDTO> hosts) {
List<HostDTO> notExistHosts = new ArrayList<>();
List<BasicAppHost> appHosts = new ArrayList<>();
Pair<List<Long>, List<String>> pair = separateByHostIdOrCloudIp(hosts);
List<Long> hostIds = pair.getLeft();
List<String> cloudIps = pair.getRight();
if (CollectionUtils.isNotEmpty(hostIds)) {
Pair<List<Long>, List<BasicAppHost>> 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<String>, List<BasicAppHost>> 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<Long>, List<String>> separateByHostIdOrCloudIp(Collection<HostDTO> hosts) {
List<Long> hostIds = new ArrayList<>();
List<String> cloudIps = new ArrayList<>();
hosts.forEach(host -> {
Expand All @@ -1546,6 +1618,15 @@ private Pair<List<HostDTO>, List<BasicAppHost>> listHostsFromCacheOrCmdb(Collect
cloudIps.add(host.toCloudIp());
}
});
return Pair.of(hostIds, cloudIps);
}

private Pair<List<HostDTO>, List<BasicAppHost>> listHostsFromCacheOrCmdb(Collection<HostDTO> hosts) {
List<BasicAppHost> appHosts = new ArrayList<>();
List<HostDTO> notExistHosts = new ArrayList<>();
Pair<List<Long>, List<String>> pair = separateByHostIdOrCloudIp(hosts);
List<Long> hostIds = pair.getLeft();
List<String> cloudIps = pair.getRight();
if (CollectionUtils.isNotEmpty(hostIds)) {
Pair<List<Long>, List<BasicAppHost>> result = listHostsByStrategy(hostIds, new ListHostByHostIdsStrategy());
appHosts.addAll(result.getRight());
Expand Down