Skip to content

Commit

Permalink
Merge pull request #2184 from jsonwan/github_fix/sync
Browse files Browse the repository at this point in the history
fix: 主机AgentId更新后,小概率出现使用旧AgentId下发任务 #2142
  • Loading branch information
wangyu096 authored Jul 4, 2023
2 parents 9e823e0 + 343d7c7 commit ad8b27d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/backend/commons/common-otel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ dependencies {
}
api 'org.springframework.cloud:spring-cloud-sleuth-otel-autoconfigure'
api "io.opentelemetry.instrumentation:opentelemetry-jdbc"
api "io.opentelemetry:opentelemetry-exporter-otlp"
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.tencent.bk.job.manage.dao;

import com.tencent.bk.job.manage.model.dto.HostTopoDTO;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -80,4 +81,12 @@ public interface HostTopoDAO {
* @return 模块ID列表
*/
List<Long> listModuleIdByHostId(Long hostId);

/**
* 根据业务ID批量查询主机Id与模块Id信息
*
* @param bizId 业务ID
* @return <主机ID,模块ID>列表
*/
List<Pair<Long, Long>> listHostIdAndModuleIdByBizId(Long bizId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.jooq.BatchBindStep;
import org.jooq.Condition;
import org.jooq.DSLContext;
Expand Down Expand Up @@ -378,6 +379,23 @@ public List<Long> listModuleIdByHostId(Long hostId) {
return query.fetch().map(record -> record.get(defaultTable.MODULE_ID, Long.class));
}

@Override
public List<Pair<Long, Long>> listHostIdAndModuleIdByBizId(Long bizId) {
List<Condition> conditions = new ArrayList<>();
if (bizId != null) {
conditions.add(defaultTable.APP_ID.eq(JooqDataTypeUtil.buildULong(bizId)));
}
val query = defaultContext.select(
defaultTable.HOST_ID,
defaultTable.MODULE_ID
).from(defaultTable)
.where(conditions);
return query.fetch().map(record -> Pair.of(
record.get(defaultTable.HOST_ID, Long.class),
record.get(defaultTable.MODULE_ID, Long.class)
));
}

private HostTopoDTO convertRecordToDto(HostTopoRecord record) {
return new HostTopoDTO(
record.getHostId().longValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,28 +687,54 @@ public void fillHostInfo(Long bizId, CcTopologyNodeVO topologyTree, boolean upda
}
//填充云区域名称
fillCloudAreaName(dbHosts);
List<HostInfoVO> hostInfoVOList = dbHosts.stream()
.map(ApplicationHostDTO::toVO).collect(Collectors.toList());
//将主机挂载到topo树
setHostsToTopoTree(bizId, dbHosts, topologyTree, map, watch);
}

private void setHostsToTopoTree(Long bizId,
List<ApplicationHostDTO> dbHosts,
CcTopologyNodeVO topologyTree,
Map<Long, CcTopologyNodeVO> map,
StopWatch watch) {
List<HostInfoVO> hostInfoVOList = new ArrayList<>();
for (ApplicationHostDTO dbHost : dbHosts) {
hostInfoVOList.add(dbHost.toVO());
}
watch.start("setToTopoTree");
List<Pair<Long, Long>> hostModuleIdPairList = hostTopoDAO.listHostIdAndModuleIdByBizId(bizId);
Map<Long, List<Long>> hostIdModuleMap = new HashMap<>();
hostModuleIdPairList.forEach(pair -> {
List<Long> moduleIdList = hostIdModuleMap.computeIfAbsent(pair.getLeft(), aLong -> new ArrayList<>());
moduleIdList.add(pair.getRight());
});
for (int i = 0; i < hostInfoVOList.size(); i++) {
ApplicationHostDTO host = dbHosts.get(i);
HostInfoVO hostInfoVO = hostInfoVOList.get(i);
List<Long> moduleIdList = hostTopoDAO.listModuleIdByHostId(host.getHostId());
moduleIdList.forEach(moduleId -> {
CcTopologyNodeVO moduleNode = map.get(moduleId);
if (moduleNode == null) {
log.warn("cannot find moduleNode in topoTree, cache may expire, ignore this moduleNode");
} else {
moduleNode.getIpListStatus().add(hostInfoVO);
}
});
List<Long> moduleIdList = hostIdModuleMap.get(host.getHostId());
if (CollectionUtils.isNotEmpty(moduleIdList)) {
moduleIdList.forEach(moduleId -> {
CcTopologyNodeVO moduleNode = map.get(moduleId);
if (moduleNode == null) {
log.warn("cannot find moduleNode in topoTree, cache may expire, ignore this moduleNode");
} else {
moduleNode.getIpListStatus().add(hostInfoVO);
}
});
} else {
log.info("No moduleId found for host:{}, ignore", host);
}
}
watch.stop();
watch.start("countHosts");
countHosts(topologyTree);
watch.stop();
log.debug(watch.toString());
if (watch.getTotalTimeMillis() > 4000) {
log.warn("PERF:SLOW:fillHostInfo: {}", watch.toString());
} else {
if (log.isDebugEnabled()) {
log.debug("fillHostInfo: {}", watch.toString());
}
}
}

@Override
Expand Down

0 comments on commit ad8b27d

Please sign in to comment.