Skip to content

Commit

Permalink
perf: Job 切换 GSE2.0 支持灰度策略 TencentBlueKing#2461
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Oct 7, 2023
1 parent 14f8f99 commit a501315
Show file tree
Hide file tree
Showing 35 changed files with 161 additions and 117 deletions.
1 change: 1 addition & 0 deletions src/backend/commons/common-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ dependencies {
api 'io.prometheus:simpleclient_pushgateway'
api 'io.micrometer:micrometer-registry-prometheus'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mockito:mockito-inline'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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;

import com.tencent.bk.job.common.util.feature.FeatureStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
* 特性开关配置加载 ApplicationRunner
*/
@Component
@Slf4j
public class FeatureLoadApplicationRunner implements ApplicationRunner {
private final FeatureStore featureStore;

@Autowired
public FeatureLoadApplicationRunner(FeatureStore featureStore) {
this.featureStore = featureStore;
}

@Override
public void run(ApplicationArguments args) throws Exception {
// 初始化特性开关配置;如果初始化错误,那么抛出异常终止程序启动
log.info("FeatureLoadApplicationRunner start");
featureStore.load(false);
log.info("FeatureLoadApplicationRunner run success");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

package com.tencent.bk.job.common.service.feature;

import com.tencent.bk.job.common.service.feature.config.FeatureConfig;
import com.tencent.bk.job.common.service.feature.config.FeatureToggleConfig;
import com.tencent.bk.job.common.service.feature.config.ToggleStrategyConfig;
import com.tencent.bk.job.common.service.feature.strategy.AllMatchToggleStrategy;
import com.tencent.bk.job.common.service.feature.strategy.AnyMatchToggleStrategy;
import com.tencent.bk.job.common.service.feature.strategy.FeatureConfigParseException;
Expand All @@ -37,7 +40,7 @@
import com.tencent.bk.job.common.util.feature.ToggleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -47,6 +50,7 @@
* 特性开关配置存储实现
*/
@Slf4j
@Component
public class InMemoryFeatureStore implements FeatureStore {

/**
Expand All @@ -63,7 +67,7 @@ public Feature getFeature(String featureId) {
if (!isInitial) {
synchronized (this) {
if (!isInitial) {
load(false);
load(true);
}
}
}
Expand All @@ -72,6 +76,19 @@ public Feature getFeature(String featureId) {

@Override
public void load(boolean ignoreException) {
try {
loadInternal();
} catch (Throwable e) {
log.warn("Load feature config error", e);
if (ignoreException) {
log.warn("Ignore feature config load error");
} else {
throw e;
}
}
}

private void loadInternal() {
synchronized (this) {
log.info("Load feature toggle start ...");
FeatureToggleConfig featureToggleConfig = ApplicationContextRegister.getBean(FeatureToggleConfig.class);
Expand All @@ -83,19 +100,8 @@ public void load(boolean ignoreException) {

Map<String, Feature> tmpFeatures = new HashMap<>();
featureToggleConfig.getFeatures().forEach((featureId, featureConfig) -> {
try {
Feature feature = parseFeatureConfig(featureId, featureConfig);
tmpFeatures.put(featureId, feature);
} catch (Throwable e) {
String msg = MessageFormatter.format(
"Load feature toggle config fail, skip update feature toggle config! featureId: {}, " +
"featureConfig: {}", featureId, featureConfig).getMessage();
log.error(msg, e);
if (features.get(featureId) != null) {
// 如果加载失败,那么使用原有的特性配置
tmpFeatures.put(featureId, features.get(featureId));
}
}
Feature feature = parseFeatureConfig(featureId, featureConfig);
tmpFeatures.put(featureId, feature);
});

// 使用新的配置完全替换老的配置
Expand Down Expand Up @@ -132,24 +138,19 @@ private ToggleStrategy parseToggleStrategy(ToggleStrategyConfig strategyConfig)
ToggleStrategy toggleStrategy;
switch (strategyId) {
case ResourceScopeWhiteListToggleStrategy.STRATEGY_ID:
toggleStrategy = new ResourceScopeWhiteListToggleStrategy(strategyConfig.getDescription(),
strategyConfig.getParams());
toggleStrategy = new ResourceScopeWhiteListToggleStrategy(strategyConfig.getParams());
break;
case ResourceScopeBlackListToggleStrategy.STRATEGY_ID:
toggleStrategy = new ResourceScopeBlackListToggleStrategy(strategyConfig.getDescription(),
strategyConfig.getParams());
toggleStrategy = new ResourceScopeBlackListToggleStrategy(strategyConfig.getParams());
break;
case WeightToggleStrategy.STRATEGY_ID:
toggleStrategy = new WeightToggleStrategy(strategyConfig.getDescription(),
strategyConfig.getParams());
toggleStrategy = new WeightToggleStrategy(strategyConfig.getParams());
break;
case JobInstanceAttrToggleStrategy.STRATEGY_ID:
toggleStrategy = new JobInstanceAttrToggleStrategy(strategyConfig.getDescription(),
strategyConfig.getParams());
toggleStrategy = new JobInstanceAttrToggleStrategy(strategyConfig.getParams());
break;
case AllMatchToggleStrategy.STRATEGY_ID:
toggleStrategy = new AllMatchToggleStrategy(
strategyId,
strategyConfig.getStrategies()
.stream()
.map(this::parseToggleStrategy)
Expand All @@ -158,15 +159,14 @@ private ToggleStrategy parseToggleStrategy(ToggleStrategyConfig strategyConfig)
break;
case AnyMatchToggleStrategy.STRATEGY_ID:
toggleStrategy = new AnyMatchToggleStrategy(
strategyId,
strategyConfig.getStrategies()
.stream()
.map(this::parseToggleStrategy)
.collect(Collectors.toList()),
strategyConfig.getParams());
break;
default:
log.error("Unsupported toggle strategy: {} , ignore it!", strategyId);
log.error("Unsupported toggle strategy: {}", strategyId);
throw new FeatureConfigParseException("Unsupported toggle strategy " + strategyId);
}
return toggleStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.service.feature;
package com.tencent.bk.job.common.service.feature.config;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.service.feature;
package com.tencent.bk.job.common.service.feature.config;

import com.tencent.bk.job.common.util.json.JsonUtils;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.service.feature;
package com.tencent.bk.job.common.service.feature.config;

import lombok.Data;

Expand All @@ -39,11 +39,7 @@ public class ToggleStrategyConfig {
*/
private String id;
/**
* 策略说明
*/
private String description;
/**
* 组合策略
* 组合策略配置
*/
private List<ToggleStrategyConfig> strategies;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public abstract class AbstractResourceScopeToggleStrategy extends AbstractToggle

protected final Set<ResourceScope> resourceScopes = new HashSet<>();

public AbstractResourceScopeToggleStrategy(String strategyId, String description, Map<String, String> initParams) {
super(strategyId, description, null, initParams);
public AbstractResourceScopeToggleStrategy(String strategyId, Map<String, String> initParams) {
super(strategyId, null, initParams);
assertRequiredInitParam(INIT_PARAM_RESOURCE_SCOPE_LIST);

String resourceScopesValue = initParams.get(INIT_PARAM_RESOURCE_SCOPE_LIST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,30 @@
@Slf4j
public abstract class AbstractToggleStrategy implements ToggleStrategy {

/**
* 策略 ID
*/
protected final String id;
protected final String description;
/**
* 组合策略
*/
protected List<ToggleStrategy> compositeStrategies = null;
/**
* 策略初始化参数
*/
protected final Map<String, String> initParams;

/**
* 初始化特性开关
*
* @param strategyId 策略ID
* @param description 策略说明
* @param compositeStrategies 子策略
* @param compositeStrategies 组合策略
* @param initParams 初始化参数
*/
public AbstractToggleStrategy(String strategyId,
String description,
List<ToggleStrategy> compositeStrategies,
Map<String, String> initParams) {
this.id = strategyId;
this.description = description;
this.compositeStrategies = compositeStrategies;
if (initParams != null) {
this.initParams = initParams;
Expand All @@ -70,15 +75,12 @@ public AbstractToggleStrategy(String strategyId,
/**
* 初始化特性开关
*
* @param strategyId 策略ID
* @param description 策略说明
* @param initParams 初始化参数
* @param strategyId 策略ID
* @param initParams 初始化参数
*/
public AbstractToggleStrategy(String strategyId,
String description,
Map<String, String> initParams) {
this.id = strategyId;
this.description = description;
if (initParams != null) {
this.initParams = initParams;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public class AllMatchToggleStrategy extends AbstractToggleStrategy {
*/
public static final String STRATEGY_ID = "AllMatchToggleStrategy";

public AllMatchToggleStrategy(String description,
List<ToggleStrategy> strategies,
public AllMatchToggleStrategy(List<ToggleStrategy> strategies,
Map<String, String> initParams) {
super(STRATEGY_ID, description, strategies, initParams);
super(STRATEGY_ID, strategies, initParams);
assertRequiredAtLeastOneStrategy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ public class AnyMatchToggleStrategy extends AbstractToggleStrategy {
*/
public static final String STRATEGY_ID = "AnyMatchToggleStrategy";

public AnyMatchToggleStrategy(String description,
List<ToggleStrategy> strategies,
public AnyMatchToggleStrategy(List<ToggleStrategy> strategies,
Map<String, String> initParams) {
super(STRATEGY_ID, description, strategies, initParams);
super(STRATEGY_ID, strategies, initParams);
assertRequiredAtLeastOneStrategy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public class JobInstanceAttrToggleStrategy extends AbstractToggleStrategy {
private Set<String> operators;


public JobInstanceAttrToggleStrategy(String description, Map<String, String> initParams) {
super(STRATEGY_ID, description, initParams);
public JobInstanceAttrToggleStrategy(Map<String, String> initParams) {
super(STRATEGY_ID, initParams);
String requireAllGseV2AgentAvailableValue = initParams.get(INIT_PARAM_REQUIRE_ALL_GSE_V2_AGENT_AVAILABLE);
if (StringUtils.isNotBlank(requireAllGseV2AgentAvailableValue)) {
requireAllGseV2AgentAvailable = Boolean.valueOf(requireAllGseV2AgentAvailableValue);
Expand Down Expand Up @@ -121,7 +121,6 @@ public boolean evaluate(String featureId, FeatureExecutionContext ctx) {
public String toString() {
return new StringJoiner(", ", JobInstanceAttrToggleStrategy.class.getSimpleName() + "[", "]")
.add("id='" + id + "'")
.add("description='" + description + "'")
.add("initParams=" + initParams)
.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class ResourceScopeBlackListToggleStrategy extends AbstractResourceScopeT
*/
public static final String STRATEGY_ID = "ResourceScopeBlackListToggleStrategy";

public ResourceScopeBlackListToggleStrategy(String description, Map<String, String> initParams) {
super(STRATEGY_ID, description, initParams);
public ResourceScopeBlackListToggleStrategy(Map<String, String> initParams) {
super(STRATEGY_ID, initParams);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class ResourceScopeWhiteListToggleStrategy extends AbstractResourceScopeT
*/
public static final String STRATEGY_ID = "ResourceScopeWhiteListToggleStrategy";

public ResourceScopeWhiteListToggleStrategy(String description, Map<String, String> initParams) {
super(STRATEGY_ID, description, initParams);
public ResourceScopeWhiteListToggleStrategy(Map<String, String> initParams) {
super(STRATEGY_ID, initParams);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class WeightToggleStrategy extends AbstractToggleStrategy {

private final Random RANDOM = new SecureRandom();

public WeightToggleStrategy(String description, Map<String, String> initParams) {
super(STRATEGY_ID, description, initParams);
public WeightToggleStrategy(Map<String, String> initParams) {
super(STRATEGY_ID, initParams);
assertRequiredInitParam(INIT_PARAM_WEIGHT);
String weightStrValue = initParams.get(INIT_PARAM_WEIGHT);
this.weight = computeWeight(weightStrValue);
Expand Down
Loading

0 comments on commit a501315

Please sign in to comment.