From 91e00f85680f510035c150739203c6855ebc5130 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Mon, 23 Oct 2023 20:27:52 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20Job=E4=B8=AD=E7=9A=84=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=85=B6=E5=AF=B9=E5=BA=94=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E7=9A=84=E7=81=B0=E5=BA=A6=E6=83=85=E5=86=B5=E5=B1=95=E7=A4=BA?= =?UTF-8?q?Agent=E7=8A=B6=E6=80=81=20#2492?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 处理Review意见。 --- .../feature/DefaultFeatureManager.java | 13 ++- .../strategy/AbstractToggleStrategy.java | 3 +- .../FeatureParamMissingException.java | 35 ++++++++ .../JobInstanceAttrToggleStrategy.java | 28 ++----- .../common/util/feature/FeatureManager.java | 8 ++ .../common/util/feature/FeatureToggle.java | 18 +++- .../gse/config/GseAutoConfiguration.java | 3 +- .../gse/constants/DefaultBeanNames.java | 4 + .../AutoChoosingAgentStateClientImpl.java | 84 +++++++++++++++++++ .../bk/job/execute/config/GseConfig.java | 24 ++++-- .../service/impl/AgentServiceImpl.java | 5 +- .../impl/BizHostInfoQueryServiceImpl.java | 2 +- .../service/impl/TaskExecuteServiceImpl.java | 15 ++-- .../bk/job/manage/config/GseConfig.java | 25 ++++-- .../impl/BizHostInfoQueryServiceImpl.java | 2 +- .../impl/agent/AgentStatusService.java | 4 +- .../service/impl/sync/HostEventHandler.java | 5 +- .../service/impl/sync/HostEventWatcher.java | 5 +- 18 files changed, 234 insertions(+), 49 deletions(-) create mode 100644 src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/FeatureParamMissingException.java create mode 100644 src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/service/AutoChoosingAgentStateClientImpl.java diff --git a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/DefaultFeatureManager.java b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/DefaultFeatureManager.java index 278fd3a835..633deb3a8f 100644 --- a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/DefaultFeatureManager.java +++ b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/DefaultFeatureManager.java @@ -25,12 +25,12 @@ package com.tencent.bk.job.common.service.feature; import com.tencent.bk.job.common.model.dto.ResourceScope; -import com.tencent.bk.job.common.util.feature.ToggleStrategyContextParams; import com.tencent.bk.job.common.util.feature.Feature; import com.tencent.bk.job.common.util.feature.FeatureExecutionContext; import com.tencent.bk.job.common.util.feature.FeatureManager; import com.tencent.bk.job.common.util.feature.FeatureStore; import com.tencent.bk.job.common.util.feature.ToggleStrategy; +import com.tencent.bk.job.common.util.feature.ToggleStrategyContextParams; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Tags; import lombok.extern.slf4j.Slf4j; @@ -54,6 +54,17 @@ public DefaultFeatureManager(FeatureStore featureStore, MeterRegistry meterRegis this.meterRegistry = meterRegistry; } + public boolean isFeatureEnabled(String featureId) { + Feature feature = featureStore.getFeature(featureId); + if (feature == null) { + if (log.isDebugEnabled()) { + log.debug("Feature: {} is not exist!", featureId); + } + return false; + } + return feature.isEnabled(); + } + /** * 判断特性是否开启 * diff --git a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractToggleStrategy.java b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractToggleStrategy.java index bba117c0ab..02c551569d 100644 --- a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractToggleStrategy.java +++ b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractToggleStrategy.java @@ -84,13 +84,12 @@ public void assertRequiredInitParam(String paramName) { } - public void assertRequiredContextParam(FeatureExecutionContext context, String paramName) { if (context.getParam(paramName) == null) { String msg = MessageFormatter.format( "Context param {} is required for evaluate", paramName).getMessage(); log.error(msg); - throw new FeatureConfigParseException(msg); + throw new FeatureParamMissingException(msg); } } } diff --git a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/FeatureParamMissingException.java b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/FeatureParamMissingException.java new file mode 100644 index 0000000000..657636742c --- /dev/null +++ b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/FeatureParamMissingException.java @@ -0,0 +1,35 @@ +/* + * 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.common.service.feature.strategy; + +/** + * 特性开关配置参数缺失异常 + */ +public class FeatureParamMissingException extends RuntimeException { + + public FeatureParamMissingException(String message) { + super(message); + } +} diff --git a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/JobInstanceAttrToggleStrategy.java b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/JobInstanceAttrToggleStrategy.java index 80e2774b23..bb2bb4d50c 100644 --- a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/JobInstanceAttrToggleStrategy.java +++ b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/JobInstanceAttrToggleStrategy.java @@ -106,41 +106,29 @@ public JobInstanceAttrToggleStrategy(Map initParams) { @Override public boolean evaluate(String featureId, FeatureExecutionContext ctx) { if (requireAllGseV2AgentAvailable != null && requireAllGseV2AgentAvailable) { - Object isAllAgentV2AvailableObj = ctx.getParam(CTX_PARAM_IS_ALL_GSE_V2_AGENT_AVAILABLE); - if (isAllAgentV2AvailableObj == null) { - return false; - } - boolean isAllAgentV2Available = (boolean) isAllAgentV2AvailableObj; + assertRequiredContextParam(ctx, CTX_PARAM_IS_ALL_GSE_V2_AGENT_AVAILABLE); + boolean isAllAgentV2Available = (boolean) ctx.getParam(CTX_PARAM_IS_ALL_GSE_V2_AGENT_AVAILABLE); if (!isAllAgentV2Available) { return false; } } if (requireAnyGseV2AgentAvailable != null && requireAnyGseV2AgentAvailable) { - Object isAnyAgentV2AvailableObj = ctx.getParam(CTX_PARAM_IS_ANY_GSE_V2_AGENT_AVAILABLE); - if (isAnyAgentV2AvailableObj == null) { - return false; - } - boolean isAnyAgentV2Available = (boolean) isAnyAgentV2AvailableObj; + assertRequiredContextParam(ctx, CTX_PARAM_IS_ANY_GSE_V2_AGENT_AVAILABLE); + boolean isAnyAgentV2Available = (boolean) ctx.getParam(CTX_PARAM_IS_ANY_GSE_V2_AGENT_AVAILABLE); if (!isAnyAgentV2Available) { return false; } } if (CollectionUtils.isNotEmpty(startupModes)) { - Object startupModeObj = ctx.getParam(CTX_PARAM_STARTUP_MODE); - if (startupModeObj == null) { - return false; - } - String startupMode = (String) startupModeObj; + assertRequiredContextParam(ctx, CTX_PARAM_STARTUP_MODE); + String startupMode = (String) ctx.getParam(CTX_PARAM_STARTUP_MODE); if (!startupModes.contains(startupMode)) { return false; } } if (CollectionUtils.isNotEmpty(operators)) { - Object operatorObj = ctx.getParam(CTX_PARAM_OPERATOR); - if (operatorObj == null) { - return false; - } - String operator = (String) operatorObj; + assertRequiredContextParam(ctx, CTX_PARAM_OPERATOR); + String operator = (String) ctx.getParam(CTX_PARAM_OPERATOR); return operators.contains(operator); } return true; diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/FeatureManager.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/FeatureManager.java index 87188fa584..c67240d42b 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/FeatureManager.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/FeatureManager.java @@ -28,6 +28,14 @@ * 特性管理 */ public interface FeatureManager { + /** + * 判断特性是否开启(只考虑特性本身,忽略策略) + * + * @param featureId 特性ID + * @return 是否开启 + */ + boolean isFeatureEnabled(String featureId); + /** * 判断特性是否开启 * diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/FeatureToggle.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/FeatureToggle.java index e6495d172a..2e56e78fce 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/FeatureToggle.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/FeatureToggle.java @@ -43,6 +43,22 @@ public class FeatureToggle { * @return 是否开启 */ public static boolean checkFeature(String featureId, FeatureExecutionContext ctx) { + ensureFeatureManagerInited(); + return featureManager.checkFeature(featureId, ctx); + } + + /** + * 判断特性是否开启(只考虑特性本身,忽略策略) + * + * @param featureId 特性ID + * @return 是否开启 + */ + public static boolean isFeatureEnabled(String featureId) { + ensureFeatureManagerInited(); + return featureManager.isFeatureEnabled(featureId); + } + + private static void ensureFeatureManagerInited() { if (featureManager == null) { synchronized (FeatureToggle.class) { if (featureManager == null) { @@ -50,7 +66,5 @@ public static boolean checkFeature(String featureId, FeatureExecutionContext ctx } } } - - return featureManager.checkFeature(featureId, ctx); } } diff --git a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/config/GseAutoConfiguration.java b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/config/GseAutoConfiguration.java index fd8610d8f8..cf81972865 100644 --- a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/config/GseAutoConfiguration.java +++ b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/config/GseAutoConfiguration.java @@ -87,8 +87,7 @@ public ThreadPoolExecutor agentStatusQueryExecutor(MeterRegistry meterRegistry, ); } - @ConditionalOnMissingBean(value = AgentStateClient.class) - @Bean("PreferV2AgentStateClient") + @Bean(DefaultBeanNames.PREFER_V2_AGENT_STATE_CLIENT) public AgentStateClient preferV2AgentStateClientImpl(AgentStateQueryConfig agentStateQueryConfig, GseClient gseClient, @Qualifier("agentStatusQueryExecutor") diff --git a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/constants/DefaultBeanNames.java b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/constants/DefaultBeanNames.java index fbfdb6691f..c9794e941b 100644 --- a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/constants/DefaultBeanNames.java +++ b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/constants/DefaultBeanNames.java @@ -3,4 +3,8 @@ public class DefaultBeanNames { // Agent状态查询线程池Bean名称 public static final String AGENT_STATUS_QUERY_THREAD_POOL_EXECUTOR = "agentStatusQueryExecutor"; + // 优先使用GSE V2的Agent状态查询Bean名称 + public static final String PREFER_V2_AGENT_STATE_CLIENT = "PreferV2AgentStateClient"; + // 基于特性配置决定是否使用GSE V2的Agent状态查询Bean名称 + public static final String USE_V2_BY_FEATURE_AGENT_STATE_CLIENT = "UseV2ByFeatureAgentStateClient"; } diff --git a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/service/AutoChoosingAgentStateClientImpl.java b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/service/AutoChoosingAgentStateClientImpl.java new file mode 100644 index 0000000000..9844b038d8 --- /dev/null +++ b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/service/AutoChoosingAgentStateClientImpl.java @@ -0,0 +1,84 @@ +/* + * 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.common.gse.service; + +import com.tencent.bk.job.common.gse.service.model.HostAgentStateQuery; +import com.tencent.bk.job.common.gse.v2.model.resp.AgentState; +import com.tencent.bk.job.common.util.feature.FeatureIdConstants; +import com.tencent.bk.job.common.util.feature.FeatureToggle; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; +import java.util.Map; + +/** + * 根据动态变化的特性配置自动选择应当使用的AgentStateClient + */ +@Slf4j +public class AutoChoosingAgentStateClientImpl implements AgentStateClient { + + private final AgentStateClient preferV2AgentStateClient; + private final AgentStateClient useV2ByFeatureAgentStateClient; + + public AutoChoosingAgentStateClientImpl(AgentStateClient preferV2AgentStateClient, + AgentStateClient useV2ByFeatureAgentStateClient) { + this.preferV2AgentStateClient = preferV2AgentStateClient; + this.useV2ByFeatureAgentStateClient = useV2ByFeatureAgentStateClient; + } + + @Override + public String getEffectiveAgentId(HostAgentStateQuery hostAgentStateQuery) { + AgentStateClient agentStateClient = chooseAgentStateByFeatureConfig(); + return agentStateClient.getEffectiveAgentId(hostAgentStateQuery); + } + + @Override + public AgentState getAgentState(HostAgentStateQuery hostAgentStateQuery) { + AgentStateClient agentStateClient = chooseAgentStateByFeatureConfig(); + return agentStateClient.getAgentState(hostAgentStateQuery); + } + + @Override + public Map batchGetAgentState(List hostAgentStateQueryList) { + AgentStateClient agentStateClient = chooseAgentStateByFeatureConfig(); + return agentStateClient.batchGetAgentState(hostAgentStateQueryList); + } + + @Override + public Map batchGetAgentAliveStatus(List hostAgentStateQueryList) { + AgentStateClient agentStateClient = chooseAgentStateByFeatureConfig(); + return agentStateClient.batchGetAgentAliveStatus(hostAgentStateQueryList); + } + + private AgentStateClient chooseAgentStateByFeatureConfig() { + AgentStateClient agentStateClient; + if (FeatureToggle.isFeatureEnabled(FeatureIdConstants.FEATURE_AGENT_STATUS_GSE_V2)) { + agentStateClient = useV2ByFeatureAgentStateClient; + } else { + agentStateClient = preferV2AgentStateClient; + } + return agentStateClient; + } +} diff --git a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/config/GseConfig.java b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/config/GseConfig.java index 5d2129d4f1..08f0c4e07a 100644 --- a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/config/GseConfig.java +++ b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/config/GseConfig.java @@ -4,10 +4,10 @@ import com.tencent.bk.job.common.gse.config.AgentStateQueryConfig; import com.tencent.bk.job.common.gse.constants.DefaultBeanNames; import com.tencent.bk.job.common.gse.service.AgentStateClient; +import com.tencent.bk.job.common.gse.service.AutoChoosingAgentStateClientImpl; import com.tencent.bk.job.common.gse.service.BizHostInfoQueryService; import com.tencent.bk.job.common.gse.service.UseV2ByFeatureAgentStateClientImpl; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -17,12 +17,16 @@ @Configuration(value = "jobExecuteGseConfig") public class GseConfig { - @Primary - @ConditionalOnProperty(name = "job.features.agentStatusGseV2.enabled", havingValue = "true") - @Bean("UseV2ByFeatureAgentStateClient") + public static final String EXECUTE_BEAN_PREFIX = "jobExecute"; + public static final String EXECUTE_BEAN_AGENT_STATE_CLIENT = EXECUTE_BEAN_PREFIX + "AgentStateClient"; + public static final String EXECUTE_BEAN_USE_V2_BY_FEATURE_AGENT_STATE_CLIENT = + EXECUTE_BEAN_PREFIX + DefaultBeanNames.USE_V2_BY_FEATURE_AGENT_STATE_CLIENT; + + @Bean(EXECUTE_BEAN_USE_V2_BY_FEATURE_AGENT_STATE_CLIENT) public AgentStateClient useV2ByFeatureAgentStateClient(AgentStateQueryConfig agentStateQueryConfig, GseClient gseClient, - BizHostInfoQueryService bizHostInfoQueryService, + @Qualifier("jobExecuteBizHostInfoQueryService") + BizHostInfoQueryService bizHostInfoQueryService, @Qualifier(DefaultBeanNames.AGENT_STATUS_QUERY_THREAD_POOL_EXECUTOR) ThreadPoolExecutor threadPoolExecutor) { return new UseV2ByFeatureAgentStateClientImpl( @@ -32,4 +36,14 @@ public AgentStateClient useV2ByFeatureAgentStateClient(AgentStateQueryConfig age threadPoolExecutor ); } + + @Primary + @Bean(EXECUTE_BEAN_AGENT_STATE_CLIENT) + public AgentStateClient AutoChoosingAgentStateClientImpl( + @Qualifier(DefaultBeanNames.PREFER_V2_AGENT_STATE_CLIENT) AgentStateClient preferV2AgentStateClient, + @Qualifier(EXECUTE_BEAN_USE_V2_BY_FEATURE_AGENT_STATE_CLIENT) + AgentStateClient useV2ByFeatureAgentStateClient + ) { + return new AutoChoosingAgentStateClientImpl(preferV2AgentStateClient, useV2ByFeatureAgentStateClient); + } } diff --git a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/AgentServiceImpl.java b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/AgentServiceImpl.java index 057b9f9358..48e62a50fc 100644 --- a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/AgentServiceImpl.java +++ b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/AgentServiceImpl.java @@ -31,6 +31,7 @@ import com.tencent.bk.job.common.gse.service.model.HostAgentStateQuery; import com.tencent.bk.job.common.model.dto.HostDTO; import com.tencent.bk.job.common.util.ip.IpUtils; +import com.tencent.bk.job.execute.config.GseConfig; import com.tencent.bk.job.execute.engine.consts.Consts; import com.tencent.bk.job.execute.model.ServersDTO; import com.tencent.bk.job.execute.service.AgentService; @@ -39,6 +40,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -75,7 +77,8 @@ public HostDTO load(String key) { ); @Autowired - public AgentServiceImpl(AgentStateClient agentStateClient, + public AgentServiceImpl(@Qualifier(GseConfig.EXECUTE_BEAN_AGENT_STATE_CLIENT) + AgentStateClient agentStateClient, HostService hostService) { this.agentStateClient = agentStateClient; this.hostService = hostService; diff --git a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/BizHostInfoQueryServiceImpl.java b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/BizHostInfoQueryServiceImpl.java index 1b59d4f0c9..4032f85873 100644 --- a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/BizHostInfoQueryServiceImpl.java +++ b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/BizHostInfoQueryServiceImpl.java @@ -16,7 +16,7 @@ import java.util.stream.Collectors; @Slf4j -@Service +@Service("jobExecuteBizHostInfoQueryService") public class BizHostInfoQueryServiceImpl implements BizHostInfoQueryService { private final ServiceHostResource hostResource; diff --git a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskExecuteServiceImpl.java b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskExecuteServiceImpl.java index 941b2c7822..8880ef9b6a 100644 --- a/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskExecuteServiceImpl.java +++ b/src/backend/job-execute/service-job-execute/src/main/java/com/tencent/bk/job/execute/service/impl/TaskExecuteServiceImpl.java @@ -40,6 +40,7 @@ import com.tencent.bk.job.common.exception.ResourceExhaustedException; import com.tencent.bk.job.common.exception.ServiceException; import com.tencent.bk.job.common.gse.constants.AgentAliveStatusEnum; +import com.tencent.bk.job.common.gse.constants.DefaultBeanNames; import com.tencent.bk.job.common.gse.service.AgentStateClient; import com.tencent.bk.job.common.gse.service.model.HostAgentStateQuery; import com.tencent.bk.job.common.gse.util.AgentUtils; @@ -135,6 +136,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.util.StopWatch; @@ -146,7 +148,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -163,7 +164,7 @@ public class TaskExecuteServiceImpl implements TaskExecuteService { private final TaskExecuteMQEventDispatcher taskExecuteMQEventDispatcher; private final TaskPlanService taskPlanService; private final TaskInstanceVariableService taskInstanceVariableService; - private final AgentStateClient agentStateClient; + private final AgentStateClient preferV2AgentStateClient; private final TaskOperationLogService taskOperationLogService; private final TaskInstanceService taskInstanceService; private final StepInstanceService stepInstanceService; @@ -185,7 +186,8 @@ public TaskExecuteServiceImpl(AccountService accountService, TaskExecuteMQEventDispatcher taskExecuteMQEventDispatcher, TaskPlanService taskPlanService, TaskInstanceVariableService taskInstanceVariableService, - AgentStateClient agentStateClient, + @Qualifier(DefaultBeanNames.PREFER_V2_AGENT_STATE_CLIENT) + AgentStateClient preferV2AgentStateClient, TaskOperationLogService taskOperationLogService, ScriptService scriptService, StepInstanceService stepInstanceService, @@ -203,7 +205,7 @@ public TaskExecuteServiceImpl(AccountService accountService, this.taskExecuteMQEventDispatcher = taskExecuteMQEventDispatcher; this.taskPlanService = taskPlanService; this.taskInstanceVariableService = taskInstanceVariableService; - this.agentStateClient = agentStateClient; + this.preferV2AgentStateClient = preferV2AgentStateClient; this.taskOperationLogService = taskOperationLogService; this.scriptService = scriptService; this.stepInstanceService = stepInstanceService; @@ -2183,11 +2185,12 @@ private void setAgentStatus(List hosts) { hostAgentStateQueryMap.put(hostDTO, hostAgentStateQuery); }); - Map agentStateMap = agentStateClient.batchGetAgentState(hostAgentStateQueryList); + // 此处用于记录下发任务时的Agent状态快照数据,因此使用最终真实下发任务的agentId获取状态 + Map agentStateMap = preferV2AgentStateClient.batchGetAgentState(hostAgentStateQueryList); for (HostDTO host : hosts) { HostAgentStateQuery hostAgentStateQuery = hostAgentStateQueryMap.get(host); - String effectiveAgentId = agentStateClient.getEffectiveAgentId(hostAgentStateQuery); + String effectiveAgentId = preferV2AgentStateClient.getEffectiveAgentId(hostAgentStateQuery); if (StringUtils.isEmpty(effectiveAgentId)) { host.setAlive(AgentAliveStatusEnum.NOT_ALIVE.getStatusValue()); continue; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/config/GseConfig.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/config/GseConfig.java index 5b0bda8083..c3cd187e98 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/config/GseConfig.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/config/GseConfig.java @@ -4,10 +4,10 @@ import com.tencent.bk.job.common.gse.config.AgentStateQueryConfig; import com.tencent.bk.job.common.gse.constants.DefaultBeanNames; import com.tencent.bk.job.common.gse.service.AgentStateClient; +import com.tencent.bk.job.common.gse.service.AutoChoosingAgentStateClientImpl; import com.tencent.bk.job.common.gse.service.BizHostInfoQueryService; import com.tencent.bk.job.common.gse.service.UseV2ByFeatureAgentStateClientImpl; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -17,12 +17,16 @@ @Configuration(value = "jobManageGseConfig") public class GseConfig { - @Primary - @ConditionalOnProperty(name = "job.features.agentStatusGseV2.enabled", havingValue = "true") - @Bean("UseV2ByFeatureAgentStateClient") + public static final String MANAGE_BEAN_PREFIX = "jobManage"; + public static final String MANAGE_BEAN_AGENT_STATE_CLIENT = MANAGE_BEAN_PREFIX + "AgentStateClient"; + public static final String MANAGE_BEAN_USE_V2_BY_FEATURE_AGENT_STATE_CLIENT = + MANAGE_BEAN_PREFIX + DefaultBeanNames.USE_V2_BY_FEATURE_AGENT_STATE_CLIENT; + + @Bean(MANAGE_BEAN_USE_V2_BY_FEATURE_AGENT_STATE_CLIENT) public AgentStateClient useV2ByFeatureAgentStateClient(AgentStateQueryConfig agentStateQueryConfig, GseClient gseClient, - BizHostInfoQueryService bizHostInfoQueryService, + @Qualifier("jobManageBizHostInfoQueryService") + BizHostInfoQueryService bizHostInfoQueryService, @Qualifier(DefaultBeanNames.AGENT_STATUS_QUERY_THREAD_POOL_EXECUTOR) ThreadPoolExecutor threadPoolExecutor) { return new UseV2ByFeatureAgentStateClientImpl( @@ -32,4 +36,15 @@ public AgentStateClient useV2ByFeatureAgentStateClient(AgentStateQueryConfig age threadPoolExecutor ); } + + @Primary + @Bean(MANAGE_BEAN_AGENT_STATE_CLIENT) + public AgentStateClient AutoChoosingAgentStateClientImpl( + @Qualifier(DefaultBeanNames.PREFER_V2_AGENT_STATE_CLIENT) + AgentStateClient preferV2AgentStateClient, + @Qualifier(MANAGE_BEAN_USE_V2_BY_FEATURE_AGENT_STATE_CLIENT) + AgentStateClient useV2ByFeatureAgentStateClient + ) { + return new AutoChoosingAgentStateClientImpl(preferV2AgentStateClient, useV2ByFeatureAgentStateClient); + } } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/BizHostInfoQueryServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/BizHostInfoQueryServiceImpl.java index 63db3cbd75..72085c5629 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/BizHostInfoQueryServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/BizHostInfoQueryServiceImpl.java @@ -16,7 +16,7 @@ import java.util.stream.Collectors; @Slf4j -@Service +@Service("jobManageBizHostInfoQueryService") public class BizHostInfoQueryServiceImpl implements BizHostInfoQueryService { private final HostService hostService; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/agent/AgentStatusService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/agent/AgentStatusService.java index 8a6388a9fd..74ee216a74 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/agent/AgentStatusService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/agent/AgentStatusService.java @@ -31,12 +31,14 @@ import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; import com.tencent.bk.job.common.model.dto.HostSimpleDTO; import com.tencent.bk.job.common.util.LogUtil; +import com.tencent.bk.job.manage.config.GseConfig; import com.tencent.bk.job.manage.model.web.vo.common.AgentStatistics; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.slf4j.helpers.FormattingTuple; import org.slf4j.helpers.MessageFormatter; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -54,7 +56,7 @@ public class AgentStatusService { private final AgentStateClient agentStateClient; @Autowired - public AgentStatusService(AgentStateClient agentStateClient) { + public AgentStatusService(@Qualifier(GseConfig.MANAGE_BEAN_AGENT_STATE_CLIENT) AgentStateClient agentStateClient) { this.agentStateClient = agentStateClient; } 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 80ae298a49..4f352d05c9 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 @@ -32,6 +32,7 @@ import com.tencent.bk.job.common.gse.v2.model.resp.AgentState; import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; import com.tencent.bk.job.common.util.json.JsonUtils; +import com.tencent.bk.job.manage.config.GseConfig; import com.tencent.bk.job.manage.metrics.CmdbEventSampler; import com.tencent.bk.job.manage.metrics.MetricsConstants; import com.tencent.bk.job.manage.service.host.HostService; @@ -39,6 +40,7 @@ import lombok.extern.slf4j.Slf4j; import org.slf4j.helpers.FormattingTuple; import org.slf4j.helpers.MessageFormatter; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.cloud.sleuth.Tracer; import java.util.concurrent.BlockingQueue; @@ -53,7 +55,8 @@ public class HostEventHandler extends EventsHandler { CmdbEventSampler cmdbEventSampler, BlockingQueue> queue, HostService hostService, - AgentStateClient agentStateClient) { + @Qualifier(GseConfig.MANAGE_BEAN_AGENT_STATE_CLIENT) + AgentStateClient agentStateClient) { super(queue, tracer, cmdbEventSampler); this.hostService = hostService; this.agentStateClient = agentStateClient; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventWatcher.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventWatcher.java index b28aa0277e..a5efb51b59 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventWatcher.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/sync/HostEventWatcher.java @@ -29,6 +29,7 @@ import com.tencent.bk.job.common.cc.model.result.ResourceWatchResult; import com.tencent.bk.job.common.cc.sdk.BizCmdbClient; import com.tencent.bk.job.common.gse.service.AgentStateClient; +import com.tencent.bk.job.manage.config.GseConfig; import com.tencent.bk.job.manage.config.JobManageConfig; import com.tencent.bk.job.manage.metrics.CmdbEventSampler; import com.tencent.bk.job.manage.metrics.MetricsConstants; @@ -37,6 +38,7 @@ import io.micrometer.core.instrument.Tags; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.cloud.sleuth.Tracer; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @@ -71,7 +73,8 @@ public HostEventWatcher(RedisTemplate redisTemplate, CmdbEventSampler cmdbEventSampler, BizCmdbClient bizCmdbClient, HostService hostService, - AgentStateClient agentStateClient, + @Qualifier(GseConfig.MANAGE_BEAN_AGENT_STATE_CLIENT) + AgentStateClient agentStateClient, JobManageConfig jobManageConfig) { super("host", redisTemplate, tracer, cmdbEventSampler); this.tracer = tracer;