diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebHostResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebHostResourceImpl.java index f722286efc..733cb81585 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebHostResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebHostResourceImpl.java @@ -263,6 +263,10 @@ public Response> listHostIdByBizTopologyNodes(String us req.getNodeList(), req.getSearchContent(), req.getAlive(), + req.getIpKeyList(), + req.getIpv6KeyList(), + req.getHostNameKeyList(), + req.getOsNameKeyList(), pagePair.getLeft(), pagePair.getRight() ); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationHostDAO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationHostDAO.java index 50b262a289..08a48478a9 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationHostDAO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationHostDAO.java @@ -171,7 +171,8 @@ Long countHostInfoByMultiKeys(Collection bizIds, /** * 批量更新主机状态 - * @param status 主机状态 + * + * @param status 主机状态 * @param hostIdList 主机id列表 * @return 成功更新的条数 */ @@ -233,8 +234,10 @@ Long countHostInfoByMultiKeys(Collection bizIds, * @return 删除的主机数量 */ int deleteBizHostInfoByBizId(long bizId); + /** * 根据业务id统计主机状态数量 + * * @param bizIds 业务id * @return 状态数量 */ diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationHostDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationHostDAOImpl.java index 4e0979653b..0f5355005a 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationHostDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationHostDAOImpl.java @@ -53,6 +53,7 @@ import org.jooq.BatchBindStep; import org.jooq.Condition; import org.jooq.DSLContext; +import org.jooq.Field; import org.jooq.Query; import org.jooq.Record; import org.jooq.Record1; @@ -381,11 +382,20 @@ public List listHostInfoByHostNames(Collection hostN } @Override - public Long countHostInfoBySearchContents(Collection bizIds, Collection moduleIds, - Collection cloudAreaIds, List searchContents, + public Long countHostInfoBySearchContents(Collection bizIds, + Collection moduleIds, + Collection cloudAreaIds, + List searchContents, Integer agentStatus) { - List hostIdList = getHostIdListBySearchContents(bizIds, moduleIds, cloudAreaIds, searchContents, - agentStatus, null, null); + List hostIdList = getHostIdListBySearchContents( + bizIds, + moduleIds, + cloudAreaIds, + searchContents, + agentStatus, + null, + null + ); return (long) (hostIdList.size()); } @@ -550,6 +560,18 @@ private List buildSearchContentsConditions(Collection bizIds, return conditions; } + private void addFieldMultiLikeCondition(List conditions, Field field, Collection keys) { + if (CollectionUtils.isNotEmpty(keys)) { + List keyList = new ArrayList<>(keys); + String firstContent = keyList.get(0); + Condition condition = field.like("%" + firstContent + "%"); + for (int i = 1; i < keyList.size(); i++) { + condition = condition.or(field.like("%" + keyList.get(i) + "%")); + } + conditions.add(condition); + } + } + private List buildMultiKeysConditions(Collection bizIds, Collection moduleIds, Collection cloudAreaIds, @@ -563,18 +585,10 @@ private List buildMultiKeysConditions(Collection bizIds, if (cloudAreaIds != null) { conditions.add(tHost.CLOUD_AREA_ID.in(cloudAreaIds)); } - if (ipKeys != null) { - conditions.add(tHost.IP.in(ipKeys)); - } - if (ipv6Keys != null) { - conditions.add(tHost.IP_V6.in(ipv6Keys)); - } - if (hostNameKeys != null) { - conditions.add(tHost.IP_DESC.in(hostNameKeys)); - } - if (osNameKeys != null) { - conditions.add(tHost.OS.in(osNameKeys)); - } + addFieldMultiLikeCondition(conditions, tHost.IP, ipKeys); + addFieldMultiLikeCondition(conditions, tHost.IP_V6, ipv6Keys); + addFieldMultiLikeCondition(conditions, tHost.IP_DESC, hostNameKeys); + addFieldMultiLikeCondition(conditions, tHost.OS, osNameKeys); return conditions; } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/query/HostQuery.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/query/HostQuery.java new file mode 100644 index 0000000000..a96cfcfcd8 --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/query/HostQuery.java @@ -0,0 +1,87 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.model.query; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +import java.util.Collection; +import java.util.List; + +/** + * 主机查询 + */ +@Getter +@Setter +@ToString +@Builder +public class HostQuery { + /** + * 主机所在的业务ID + */ + private Collection bizIds; + /** + * 主机所在的业务模块ID + */ + private Collection moduleIds; + /** + * 主机云区域ID + */ + private Collection cloudAreaIds; + /** + * 模糊搜索key,匹配字段:IP、IP_V6、IP_DESC、OS + */ + private List searchContents; + /** + * 主机Agent状态 + */ + private Integer agentAlive; + /** + * 主机IP模糊搜索key列表,任意一个key匹配即命中 + */ + private List ipKeyList; + /** + * 主机IPv6模糊搜索key列表,任意一个key匹配即命中 + */ + private List ipv6KeyList; + /** + * 主机名称模糊搜索key列表,任意一个key匹配即命中 + */ + private List hostNameKeyList; + /** + * 主机系统名称模糊搜索key列表,任意一个key匹配即命中 + */ + private List osNameKeyList; + /** + * 起始位置 + */ + private Long start; + /** + * 记录数量 + */ + private Long limit; +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/BizHostService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/BizHostService.java index 8b1b72d12b..333b80fc4c 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/BizHostService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/BizHostService.java @@ -26,6 +26,7 @@ import com.tencent.bk.job.common.model.PageData; import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; +import com.tencent.bk.job.manage.model.query.HostQuery; import java.util.Collection; import java.util.List; @@ -128,50 +129,18 @@ List getHostsByBizAndHostNames(Collection bizIds, /** * 根据条件查询主机ID * - * @param bizIds 业务ID集合 - * @param moduleIds 模块ID集合 - * @param cloudAreaIds 云区域ID集合 - * @param searchContents 搜索关键字列表 - * @param agentAlive agent是否正常 - * @param start 数据起始位置 - * @param limit 查询数据条数 + * @param hostQuery 主机查询条件 * @return 主机列表 */ - PageData pageListHostId(Collection bizIds, - Collection moduleIds, - Collection cloudAreaIds, - List searchContents, - Integer agentAlive, - Long start, - Long limit); + PageData pageListHostId(HostQuery hostQuery); /** * 根据条件查询主机 * - * @param bizIds 业务ID集合 - * @param moduleIds 模块ID集合 - * @param cloudAreaIds 云区域ID集合 - * @param searchContents 搜索关键字列表 - * @param agentAlive agent是否正常 - * @param ipKeyList IP关键字列表 - * @param ipv6KeyList IPv6关键字列表 - * @param hostNameKeyList 主机名关键字列表 - * @param osNameKeyList 系统名关键字列表 - * @param start 数据起始位置 - * @param limit 查询数据条数 + * @param hostQuery 主机查询条件 * @return 主机列表 */ - PageData pageListHost(Collection bizIds, - Collection moduleIds, - Collection cloudAreaIds, - List searchContents, - Integer agentAlive, - List ipKeyList, - List ipv6KeyList, - List hostNameKeyList, - List osNameKeyList, - Long start, - Long limit); + PageData pageListHost(HostQuery hostQuery); /** * 根据 moduleId 集合查询主机信息 diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/ScopeHostService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/ScopeHostService.java index 567e34ac40..9278a43f36 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/ScopeHostService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/ScopeHostService.java @@ -94,6 +94,10 @@ List getScopeHostsByHostNames(AppResourceScope appResourceSc * @param appTopoNodeList 拓扑节点列表 * @param searchContent 模糊搜索关键字(同时对主机IP/主机名/操作系统/云区域名称进行模糊搜索) * @param agentAlive 筛选条件:agentAlive:0为异常,1为正常 + * @param ipKeyList IP关键字列表 + * @param ipv6KeyList IPv6关键字列表 + * @param hostNameKeyList 主机名称关键字列表 + * @param osNameKeyList 操作系统名称关键字列表 * @param start 数据起始位置 * @param pageSize 拉取数量 * @return hostId列表 @@ -102,6 +106,10 @@ PageData listHostIdByBizTopologyNodes(AppResourceScope appResourceScope, List appTopoNodeList, String searchContent, Integer agentAlive, + List ipKeyList, + List ipv6KeyList, + List hostNameKeyList, + List osNameKeyList, Long start, Long pageSize); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/BizHostServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/BizHostServiceImpl.java index 3bff95765f..e6ac2a0e12 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/BizHostServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/BizHostServiceImpl.java @@ -30,6 +30,7 @@ import com.tencent.bk.job.manage.dao.ApplicationHostDAO; import com.tencent.bk.job.manage.dao.HostTopoDAO; import com.tencent.bk.job.manage.model.dto.HostTopoDTO; +import com.tencent.bk.job.manage.model.query.HostQuery; import com.tencent.bk.job.manage.service.host.BizHostService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; @@ -127,93 +128,113 @@ public List getHostsByBizAndHostNames(Collection bizId } @Override - public PageData pageListHostId(Collection bizIds, - Collection moduleIds, - Collection cloudAreaIds, - List searchContents, - Integer agentAlive, - Long start, - Long limit) { + public PageData pageListHostId(HostQuery hostQuery) { + List hostIdList; + Long count; StopWatch watch = new StopWatch("pageListHostId"); - watch.start("listHostInfoBySearchContents"); - List hostIdList = applicationHostDAO.getHostIdListBySearchContents( - bizIds, - moduleIds, - cloudAreaIds, - searchContents, - agentAlive, - start, - limit - ); - watch.stop(); - watch.start("countHostInfoBySearchContents"); - Long count = applicationHostDAO.countHostInfoBySearchContents( - bizIds, - moduleIds, - cloudAreaIds, - searchContents, - agentAlive - ); - watch.stop(); - return new PageData<>(start.intValue(), limit.intValue(), count, hostIdList); + List searchContents = hostQuery.getSearchContents(); + if (searchContents != null) { + watch.start("getHostIdListBySearchContents"); + hostIdList = applicationHostDAO.getHostIdListBySearchContents( + hostQuery.getBizIds(), + hostQuery.getModuleIds(), + hostQuery.getCloudAreaIds(), + searchContents, + hostQuery.getAgentAlive(), + hostQuery.getStart(), + hostQuery.getLimit() + ); + watch.stop(); + watch.start("countHostInfoBySearchContents"); + count = applicationHostDAO.countHostInfoBySearchContents( + hostQuery.getBizIds(), + hostQuery.getModuleIds(), + hostQuery.getCloudAreaIds(), + searchContents, + hostQuery.getAgentAlive() + ); + watch.stop(); + } else { + watch.start("getHostIdListByMultiKeys"); + hostIdList = applicationHostDAO.getHostIdListByMultiKeys( + hostQuery.getBizIds(), + hostQuery.getModuleIds(), + hostQuery.getCloudAreaIds(), + hostQuery.getIpKeyList(), + hostQuery.getIpv6KeyList(), + hostQuery.getHostNameKeyList(), + hostQuery.getOsNameKeyList(), + hostQuery.getAgentAlive(), + hostQuery.getStart(), + hostQuery.getLimit() + ); + watch.stop(); + watch.start("countHostInfoByMultiKeys"); + count = applicationHostDAO.countHostInfoByMultiKeys( + hostQuery.getBizIds(), + hostQuery.getModuleIds(), + hostQuery.getCloudAreaIds(), + hostQuery.getIpKeyList(), + hostQuery.getIpv6KeyList(), + hostQuery.getHostNameKeyList(), + hostQuery.getOsNameKeyList(), + hostQuery.getAgentAlive() + ); + watch.stop(); + } + if (watch.getTotalTimeMillis() > 3000) { + log.warn("pageListHostId slow:" + watch.prettyPrint()); + } + return new PageData<>(hostQuery.getStart().intValue(), hostQuery.getLimit().intValue(), count, hostIdList); } @Override - public PageData pageListHost(Collection bizIds, - Collection moduleIds, - Collection cloudAreaIds, - List searchContents, - Integer agentAlive, - List ipKeyList, - List ipv6KeyList, - List hostNameKeyList, - List osNameKeyList, - Long start, - Long limit) { + public PageData pageListHost(HostQuery hostQuery) { List hostList; Long count; + List searchContents = hostQuery.getSearchContents(); if (searchContents != null) { hostList = applicationHostDAO.listHostInfoBySearchContents( - bizIds, - moduleIds, - cloudAreaIds, + hostQuery.getBizIds(), + hostQuery.getModuleIds(), + hostQuery.getCloudAreaIds(), searchContents, - agentAlive, - start, - limit + hostQuery.getAgentAlive(), + hostQuery.getStart(), + hostQuery.getLimit() ); count = applicationHostDAO.countHostInfoBySearchContents( - bizIds, - moduleIds, - cloudAreaIds, + hostQuery.getBizIds(), + hostQuery.getModuleIds(), + hostQuery.getCloudAreaIds(), searchContents, - agentAlive + hostQuery.getAgentAlive() ); } else { hostList = applicationHostDAO.listHostInfoByMultiKeys( - bizIds, - moduleIds, - cloudAreaIds, - ipKeyList, - ipv6KeyList, - hostNameKeyList, - osNameKeyList, - agentAlive, - start, - limit + hostQuery.getBizIds(), + hostQuery.getModuleIds(), + hostQuery.getCloudAreaIds(), + hostQuery.getIpKeyList(), + hostQuery.getIpv6KeyList(), + hostQuery.getHostNameKeyList(), + hostQuery.getOsNameKeyList(), + hostQuery.getAgentAlive(), + hostQuery.getStart(), + hostQuery.getLimit() ); count = applicationHostDAO.countHostInfoByMultiKeys( - bizIds, - moduleIds, - cloudAreaIds, - ipKeyList, - ipv6KeyList, - hostNameKeyList, - osNameKeyList, - agentAlive + hostQuery.getBizIds(), + hostQuery.getModuleIds(), + hostQuery.getCloudAreaIds(), + hostQuery.getIpKeyList(), + hostQuery.getIpv6KeyList(), + hostQuery.getHostNameKeyList(), + hostQuery.getOsNameKeyList(), + hostQuery.getAgentAlive() ); } - return new PageData<>(start.intValue(), limit.intValue(), count, hostList); + return new PageData<>(hostQuery.getStart().intValue(), hostQuery.getLimit().intValue(), count, hostList); } @Override diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/ScopeHostServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/ScopeHostServiceImpl.java index 675135e2fa..159bf7fdf5 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/ScopeHostServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/host/impl/ScopeHostServiceImpl.java @@ -30,6 +30,7 @@ import com.tencent.bk.job.common.model.dto.ApplicationDTO; import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; import com.tencent.bk.job.common.util.StringUtil; +import com.tencent.bk.job.manage.model.query.HostQuery; import com.tencent.bk.job.manage.model.web.request.ipchooser.BizTopoNode; import com.tencent.bk.job.manage.service.ApplicationService; import com.tencent.bk.job.manage.service.host.BizHostService; @@ -164,6 +165,10 @@ public PageData listHostIdByBizTopologyNodes(AppResourceScope appResourceS List appTopoNodeList, String searchContent, Integer agentAlive, + List ipKeyList, + List ipv6KeyList, + List hostNameKeyList, + List osNameKeyList, Long start, Long pageSize) { StopWatch watch = new StopWatch("listHostByBizTopologyNodes"); @@ -174,16 +179,26 @@ public PageData listHostIdByBizTopologyNodes(AppResourceScope appResourceS searchContent ); watch.stop(); - - return bizHostService.pageListHostId( - basicConditions.getBizIds(), - basicConditions.getModuleIds(), - basicConditions.getCloudAreaIds(), - basicConditions.getSearchContents(), - agentAlive, - start, - pageSize - ); + watch.start("pageListHostId"); + HostQuery hostQuery = HostQuery.builder() + .bizIds(basicConditions.getBizIds()) + .moduleIds(basicConditions.getModuleIds()) + .cloudAreaIds(basicConditions.getCloudAreaIds()) + .searchContents(basicConditions.getSearchContents()) + .agentAlive(agentAlive) + .ipKeyList(ipKeyList) + .ipv6KeyList(ipv6KeyList) + .hostNameKeyList(hostNameKeyList) + .osNameKeyList(osNameKeyList) + .start(start) + .limit(pageSize) + .build(); + PageData result = bizHostService.pageListHostId(hostQuery); + watch.stop(); + if (watch.getTotalTimeMillis() > 5000) { + log.warn("listHostIdByBizTopologyNodes slow:" + watch.prettyPrint()); + } + return result; } @Override @@ -202,19 +217,20 @@ public PageData searchHost(AppResourceScope appResourceScope appTopoNodeList, searchContent ); - return bizHostService.pageListHost( - basicConditions.getBizIds(), - basicConditions.getModuleIds(), - basicConditions.getCloudAreaIds(), - basicConditions.getSearchContents(), - agentAlive, - ipKeyList, - ipv6KeyList, - hostNameKeyList, - osNameKeyList, - start, - pageSize - ); + HostQuery hostQuery = HostQuery.builder() + .bizIds(basicConditions.getBizIds()) + .moduleIds(basicConditions.getModuleIds()) + .cloudAreaIds(basicConditions.getCloudAreaIds()) + .searchContents(basicConditions.getSearchContents()) + .agentAlive(agentAlive) + .ipKeyList(ipKeyList) + .ipv6KeyList(ipv6KeyList) + .hostNameKeyList(hostNameKeyList) + .osNameKeyList(osNameKeyList) + .start(start) + .limit(pageSize) + .build(); + return bizHostService.pageListHost(hostQuery); } private BasicParsedSearchConditions buildSearchConditions(AppResourceScope appResourceScope,