Skip to content

Commit

Permalink
Merge pull request #3317 from jsonwan/3.9.x
Browse files Browse the repository at this point in the history
fix: Redis中主机缓存数据偶现不一致 #3295
  • Loading branch information
jsonwan authored Dec 3, 2024
2 parents 4bc6e94 + 4cfb92d commit 714ee04
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public interface HostService {
*/
int deleteHostBeforeOrEqualLastTime(ApplicationHostDTO hostInfoDTO);

void updateDbHostToCache(Long hostId);

long countHostsByOsType(String osType);

Map<String, Integer> groupHostByOsType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ public Pair<Boolean, Integer> createOrUpdateHostBeforeLastTime(ApplicationHostDT
if (applicationHostDAO.existAppHostInfoByHostId(hostInfoDTO.getHostId())) {
// 只更新事件中的主机属性与agent状态
int affectedNum = applicationHostDAO.updateHostAttrsBeforeLastTime(hostInfoDTO);
if (affectedNum == 0) {
ApplicationHostDTO hostInDB = applicationHostDAO.getHostById(hostInfoDTO.getHostId());
if (hostInDB != null) {
log.info(
"Not update host, hostId={}, dbHostLastTime={}, currentHostLastTime={}",
hostInDB.getHostId(),
hostInDB.getLastTime(),
hostInfoDTO.getLastTime()
);
} else {
log.warn(
"Not update host, hostId={}, hostInDB not exists",
hostInfoDTO.getHostId()
);
}
}
return Pair.of(needToCreate, affectedNum);
} else {
needToCreate = true;
Expand All @@ -256,7 +272,7 @@ public Pair<Boolean, Integer> createOrUpdateHostBeforeLastTime(ApplicationHostDT
int affectedNum = applicationHostDAO.syncHostTopo(hostInfoDTO.getHostId());
log.info("hostTopo synced: hostId={}, affectedNum={}", hostInfoDTO.getHostId(), affectedNum);
// 更新缓存
updateHostCache(hostInfoDTO);
updateDbHostToCache(hostInfoDTO.getHostId());
}
}

Expand Down Expand Up @@ -287,8 +303,8 @@ public int deleteHostBeforeOrEqualLastTime(ApplicationHostDTO hostInfoDTO) {
return affectedRowNum;
}

private void updateHostCache(ApplicationHostDTO hostInfoDTO) {
hostInfoDTO = applicationHostDAO.getHostById(hostInfoDTO.getHostId());
public void updateDbHostToCache(Long hostId) {
ApplicationHostDTO hostInfoDTO = applicationHostDAO.getHostById(hostId);
if (hostInfoDTO.getBizId() != null && hostInfoDTO.getBizId() > 0) {
// 只更新常规业务的主机到缓存
if (applicationService.existBiz(hostInfoDTO.getBizId())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ private void handleOneEventIndeed(ResourceEvent<HostEventDetail> event) {
hostInfoDTO.setGseAgentStatus(agentStatus);
affectedNum = hostService.updateHostAttrsByHostId(hostInfoDTO);
log.info("update host attrs:{}, affectedNum={}", hostInfoDTO, affectedNum);
// 更新缓存
if (affectedNum > 0) {
hostService.updateDbHostToCache(hostInfoDTO.getHostId());
}
} else {
// 机器在CMDB中已不存在,忽略
log.info("host not exist in cmdb:{}, ignore", hostInfoDTO);
Expand Down

0 comments on commit 714ee04

Please sign in to comment.