forked from TencentBlueKing/bk-job
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Job 切换 GSE2.0 支持灰度策略 TencentBlueKing#2461
- Loading branch information
Showing
22 changed files
with
621 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
.../src/main/java/com/tencent/bk/job/common/util/feature/FeatureExecutionContextBuilder.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ain/java/com/tencent/bk/job/common/util/feature/strategy/AllGseV2AgentToggleStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.tencent.bk.job.common.util.feature.strategy; | ||
|
||
import com.tencent.bk.job.common.util.feature.FeatureExecutionContext; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.StringJoiner; | ||
|
||
@Slf4j | ||
public class AllGseV2AgentToggleStrategy extends AbstractToggleStrategy { | ||
/** | ||
* 策略参数-权重 | ||
*/ | ||
public static final String INIT_PARAM_WEIGHT = "weight"; | ||
/** | ||
* 特性开关开启策略ID | ||
*/ | ||
public static final String STRATEGY_ID = "AllGseV2AgentToggleStrategy"; | ||
|
||
private final int weight; | ||
|
||
private final Random RANDOM = new SecureRandom(); | ||
|
||
public AllGseV2AgentToggleStrategy(String description, Map<String, String> initParams) { | ||
super(STRATEGY_ID, description, initParams); | ||
assertRequiredParameter(INIT_PARAM_WEIGHT); | ||
String weightStrValue = initParams.get(INIT_PARAM_WEIGHT); | ||
this.weight = computeWeight(weightStrValue); | ||
} | ||
|
||
private int computeWeight(String weightStrValue) { | ||
String weightValue = weightStrValue.trim(); | ||
if (StringUtils.isBlank(weightStrValue)) { | ||
log.error("Weight is empty!"); | ||
throw new FeatureConfigParseException("Weight is empty!"); | ||
} | ||
try { | ||
int weight = Integer.parseInt(weightValue); | ||
if (weight < 0 || weight > 100) { | ||
log.error("Weight should be set between 0 and 100, value: {}", weight); | ||
throw new FeatureConfigParseException("Weight should be set between 0 and 100"); | ||
} | ||
return weight; | ||
} catch (NumberFormatException e) { | ||
log.error("Invalid weight value: {}, not a valid number", weightValue); | ||
throw new FeatureConfigParseException("Weight should be a number"); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean evaluate(String featureId, FeatureExecutionContext ctx) { | ||
if (weight == 0) { | ||
return false; | ||
} else if (weight == 100) { | ||
return true; | ||
} else { | ||
int random = RANDOM.nextInt(100) + 1; | ||
return random <= weight; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringJoiner(", ", AllGseV2AgentToggleStrategy.class.getSimpleName() + "[", "]") | ||
.add("id='" + id + "'") | ||
.add("initParams=" + initParams) | ||
.add("weight=" + weight) | ||
.toString(); | ||
} | ||
} |
Oops, something went wrong.