From 4cfb92d2992cbf2dcbff68991acdb5f0c12d9823 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Mon, 25 Nov 2024 16:29:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Redis=E4=B8=AD=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=95=B0=E6=8D=AE=E5=81=B6=E7=8E=B0=E4=B8=8D?= =?UTF-8?q?=E4=B8=80=E8=87=B4=20#3295?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.从CMDB查询最新主机数据后,不仅将主机数据更新到DB,也更新缓存; 2.主机事件数据中的lastTime更旧时,打印出当前DB中的主机lastTime进行比较。 --- .../job/manage/service/host/HostService.java | 2 ++ .../service/host/impl/HostServiceImpl.java | 22 ++++++++++++++++--- .../service/impl/sync/HostEventHandler.java | 4 ++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/HostService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/HostService.java index 54c6c5373b..e415fcb20f 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/HostService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/HostService.java @@ -84,6 +84,8 @@ public interface HostService { */ int deleteHostBeforeOrEqualLastTime(ApplicationHostDTO hostInfoDTO); + void updateDbHostToCache(Long hostId); + long countHostsByOsType(String osType); Map groupHostByOsType(); 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 15a9d551e9..187a7d652c 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 @@ -240,6 +240,22 @@ public Pair 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; @@ -256,7 +272,7 @@ public Pair createOrUpdateHostBeforeLastTime(ApplicationHostDT int affectedNum = applicationHostDAO.syncHostTopo(hostInfoDTO.getHostId()); log.info("hostTopo synced: hostId={}, affectedNum={}", hostInfoDTO.getHostId(), affectedNum); // 更新缓存 - updateHostCache(hostInfoDTO); + updateDbHostToCache(hostInfoDTO.getHostId()); } } @@ -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())) { diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventHandler.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventHandler.java index 4d616eca3b..f163efad4a 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventHandler.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventHandler.java @@ -128,6 +128,10 @@ private void handleOneEventIndeed(ResourceEvent 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);