From bad2e97c5f615b6fade8d4700e69710ad3081537 Mon Sep 17 00:00:00 2001 From: wangyu096 Date: Mon, 9 Oct 2023 20:48:25 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20Job=20=E5=88=87=E6=8D=A2=20GSE2.0=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=81=B0=E5=BA=A6=E7=AD=96=E7=95=A5=20#2461?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feature/DefaultFeatureManager.java | 15 +++-- .../service/feature/InMemoryFeatureStore.java | 8 +++ .../AbstractCompositeToggleStrategy.java | 59 +++++++++++++++++++ .../AbstractResourceScopeToggleStrategy.java | 2 +- .../strategy/AbstractToggleStrategy.java | 36 +---------- .../strategy/AllMatchToggleStrategy.java | 10 +--- .../strategy/AnyMatchToggleStrategy.java | 10 +--- .../common/util/feature/ToggleStrategy.java | 9 --- 8 files changed, 86 insertions(+), 63 deletions(-) create mode 100644 src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractCompositeToggleStrategy.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 5d496d306c..8450b374a2 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 @@ -37,6 +37,15 @@ @Slf4j public class DefaultFeatureManager implements FeatureManager { + /** + * 度量指标 - 特性灰度检查总次数 + */ + private static final String METRIC_JOB_FEATURE_TOGGLE_TOTAL = "job.feature.toggle.total"; + /** + * 度量指标 - 特性灰度命中总次数 + */ + private static final String METRIC_JOB_FEATURE_TOGGLE_HIT_TOTAL = "job.feature.toggle.hit.total"; + private final FeatureStore featureStore; private final MeterRegistry meterRegistry; @@ -103,15 +112,13 @@ private String extractResourceScope(FeatureExecutionContext ctx) { } private void recordFeatureToggleTotal(String featureId, String resourceScope) { - String metricName = "job.feature.toggle.total"; - meterRegistry.counter(metricName, + meterRegistry.counter(METRIC_JOB_FEATURE_TOGGLE_TOTAL, Tags.of("resourceScope", resourceScope).and("feature", featureId)) .increment(); } private void recordFeatureToggleHitTotal(String featureId, String resourceScope) { - String metricName = "job.feature.toggle.hit.total"; - meterRegistry.counter(metricName, + meterRegistry.counter(METRIC_JOB_FEATURE_TOGGLE_HIT_TOTAL, Tags.of("resourceScope", resourceScope).and("feature", featureId)) .increment(); } diff --git a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/InMemoryFeatureStore.java b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/InMemoryFeatureStore.java index e1c27e2d56..38d4ea8c09 100644 --- a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/InMemoryFeatureStore.java +++ b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/InMemoryFeatureStore.java @@ -112,6 +112,14 @@ private void loadInternal() { } } + /** + * 解析特性配置 + * + * @param featureId 特性 ID + * @param featureConfig 特性配置信息 + * @return 解析之后的特性 + * @throws FeatureConfigParseException 如果解析报错,抛出异常 + */ private Feature parseFeatureConfig(String featureId, FeatureConfig featureConfig) throws FeatureConfigParseException { if (StringUtils.isBlank(featureId)) { diff --git a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractCompositeToggleStrategy.java b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractCompositeToggleStrategy.java new file mode 100644 index 0000000000..6591eee8f6 --- /dev/null +++ b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractCompositeToggleStrategy.java @@ -0,0 +1,59 @@ +/* + * 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; + +import com.tencent.bk.job.common.util.feature.ToggleStrategy; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; + +import java.util.List; +import java.util.Map; + +/** + * 组合策略抽象实现 + */ +@Slf4j +public abstract class AbstractCompositeToggleStrategy extends AbstractToggleStrategy { + /** + * 组合策略 + */ + protected final List compositeStrategies; + + public AbstractCompositeToggleStrategy(String strategyId, + List compositeStrategies, + Map initParams) { + super(strategyId, initParams); + this.compositeStrategies = compositeStrategies; + assertRequiredAtLeastOneStrategy(); + } + + protected void assertRequiredAtLeastOneStrategy() { + if (CollectionUtils.isEmpty(this.compositeStrategies)) { + String msg = "Required at least one strategy for this ToggleStrategy"; + log.error(msg); + throw new FeatureConfigParseException(msg); + } + } +} diff --git a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractResourceScopeToggleStrategy.java b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractResourceScopeToggleStrategy.java index 3be0beff7e..78dc954b3d 100644 --- a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractResourceScopeToggleStrategy.java +++ b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AbstractResourceScopeToggleStrategy.java @@ -47,7 +47,7 @@ public abstract class AbstractResourceScopeToggleStrategy extends AbstractToggle protected final Set resourceScopes = new HashSet<>(); public AbstractResourceScopeToggleStrategy(String strategyId, Map initParams) { - super(strategyId, null, initParams); + super(strategyId, initParams); assertRequiredInitParam(INIT_PARAM_RESOURCE_SCOPE_LIST); String resourceScopesValue = initParams.get(INIT_PARAM_RESOURCE_SCOPE_LIST); 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 b2e6627407..bba117c0ab 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 @@ -27,11 +27,9 @@ import com.tencent.bk.job.common.util.feature.FeatureExecutionContext; import com.tencent.bk.job.common.util.feature.ToggleStrategy; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.collections4.CollectionUtils; import org.slf4j.helpers.MessageFormatter; import java.util.HashMap; -import java.util.List; import java.util.Map; /** @@ -44,36 +42,14 @@ public abstract class AbstractToggleStrategy implements ToggleStrategy { * 策略 ID */ protected final String id; - /** - * 组合策略 - */ - protected List compositeStrategies = null; + /** * 策略初始化参数 */ protected final Map initParams; /** - * 初始化特性开关 - * - * @param strategyId 策略ID - * @param compositeStrategies 组合策略 - * @param initParams 初始化参数 - */ - public AbstractToggleStrategy(String strategyId, - List compositeStrategies, - Map initParams) { - this.id = strategyId; - this.compositeStrategies = compositeStrategies; - if (initParams != null) { - this.initParams = initParams; - } else { - this.initParams = new HashMap<>(); - } - } - - /** - * 初始化特性开关 + * 构造策略 * * @param strategyId 策略ID * @param initParams 初始化参数 @@ -107,13 +83,7 @@ public void assertRequiredInitParam(String paramName) { } } - public void assertRequiredAtLeastOneStrategy() { - if (CollectionUtils.isEmpty(this.compositeStrategies)) { - String msg = "Required at least one strategy for this ToggleStrategy"; - log.error(msg); - throw new FeatureConfigParseException(msg); - } - } + public void assertRequiredContextParam(FeatureExecutionContext context, String paramName) { if (context.getParam(paramName) == null) { diff --git a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AllMatchToggleStrategy.java b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AllMatchToggleStrategy.java index 0d808caad5..174ba71529 100644 --- a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AllMatchToggleStrategy.java +++ b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AllMatchToggleStrategy.java @@ -30,7 +30,7 @@ import java.util.List; import java.util.Map; -public class AllMatchToggleStrategy extends AbstractToggleStrategy { +public class AllMatchToggleStrategy extends AbstractCompositeToggleStrategy { /** * 特性开关开启策略ID */ @@ -39,12 +39,11 @@ public class AllMatchToggleStrategy extends AbstractToggleStrategy { public AllMatchToggleStrategy(List strategies, Map initParams) { super(STRATEGY_ID, strategies, initParams); - assertRequiredAtLeastOneStrategy(); } @Override public boolean evaluate(String featureId, FeatureExecutionContext ctx) { - for (ToggleStrategy strategy : getCompositeToggleStrategies()) { + for (ToggleStrategy strategy : compositeStrategies) { boolean isMatch = strategy.evaluate(featureId, ctx); if (!isMatch) { return false; @@ -52,9 +51,4 @@ public boolean evaluate(String featureId, FeatureExecutionContext ctx) { } return true; } - - @Override - public List getCompositeToggleStrategies() { - return this.compositeStrategies; - } } diff --git a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AnyMatchToggleStrategy.java b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AnyMatchToggleStrategy.java index d63a29d8ef..6390bc7f84 100644 --- a/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AnyMatchToggleStrategy.java +++ b/src/backend/commons/common-service/src/main/java/com/tencent/bk/job/common/service/feature/strategy/AnyMatchToggleStrategy.java @@ -30,7 +30,7 @@ import java.util.List; import java.util.Map; -public class AnyMatchToggleStrategy extends AbstractToggleStrategy { +public class AnyMatchToggleStrategy extends AbstractCompositeToggleStrategy { /** * 特性开关开启策略ID @@ -40,13 +40,12 @@ public class AnyMatchToggleStrategy extends AbstractToggleStrategy { public AnyMatchToggleStrategy(List strategies, Map initParams) { super(STRATEGY_ID, strategies, initParams); - assertRequiredAtLeastOneStrategy(); } @Override public boolean evaluate(String featureId, FeatureExecutionContext ctx) { assertRequiredAtLeastOneStrategy(); - for (ToggleStrategy strategy : getCompositeToggleStrategies()) { + for (ToggleStrategy strategy : compositeStrategies) { boolean isMatch = strategy.evaluate(featureId, ctx); if (isMatch) { return true; @@ -54,9 +53,4 @@ public boolean evaluate(String featureId, FeatureExecutionContext ctx) { } return false; } - - @Override - public List getCompositeToggleStrategies() { - return this.compositeStrategies; - } } diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/ToggleStrategy.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/ToggleStrategy.java index 7b4d9400c8..9a60a651ca 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/ToggleStrategy.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/feature/ToggleStrategy.java @@ -24,8 +24,6 @@ package com.tencent.bk.job.common.util.feature; -import java.util.Collections; -import java.util.List; import java.util.Map; /** @@ -42,13 +40,6 @@ public interface ToggleStrategy { */ Map getInitParams(); - /** - * 获取组合策略 - */ - default List getCompositeToggleStrategies() { - return Collections.emptyList(); - } - /** * 判断是否开启特性 *