Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Add a batch incremental Add rule for the Circuit Breaker manager #3365

Open
wants to merge 1 commit into
base: 1.8
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.alibaba.csp.sentinel.slots.block.degrade;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -141,6 +142,44 @@ public static boolean setRulesForResource(String resourceName, Set<DegradeRule>
return false;
}
}

/**
* Batch set degrade rules for provided resources. Former rules of the resources will be replaced.
*
* @param resourceToRulesMap new rules to add
* @return whether the rules has actually been updated
* @since 1.5.0
*/
public static boolean batchSetRulesForResources(Map<String, Set<DegradeRule>> resourceToRulesMap) {
AssertUtil.assertNotEmpty((Collection<?>) resourceToRulesMap, "resources cannot be empty");
try {
Map<String, List<DegradeRule>> newRuleMap = ruleMap.getOriginalRules();
for (Map.Entry<String, Set<DegradeRule>> entry : resourceToRulesMap.entrySet()) {
String resourceName = entry.getKey();
Set<DegradeRule> rules = entry.getValue();
if (rules == null) {
newRuleMap.remove(resourceName);
} else {
Set<DegradeRule> newSet = new HashSet<>();
for (DegradeRule rule : rules) {
if (isValidRule(rule) && resourceName.equals(rule.getResource())) {
newSet.add(rule);
}
}
newRuleMap.put(resourceName, new ArrayList<>(newSet));
}
}

List<DegradeRule> allRules = new ArrayList<>();
for (List<DegradeRule> set : newRuleMap.values()) {
allRules.addAll(set);
}
return currentProperty.updateValue(allRules);
} catch (Throwable e) {
RecordLog.error("[DegradeRuleManager] Unexpected error when batch setting circuit breaking rules" , e);
return false;
}
}

private static CircuitBreaker getExistingSameCbOrNew(/*@Valid*/ DegradeRule rule) {
List<CircuitBreaker> cbs = getCircuitBreakers(rule.getResource());
Expand Down
Loading