Enhance log and null check for rule managers

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2018-09-10 15:44:42 +08:00
parent 02cf5e5639
commit d142a07a39
2 changed files with 19 additions and 9 deletions

View File

@ -56,6 +56,7 @@ public class DegradeRuleManager {
*/
public static void register2Property(SentinelProperty<List<DegradeRule>> property) {
synchronized (listener) {
RecordLog.info("[DegradeRuleManager] Registering new property to degrade rule manager");
currentProperty.removeListener(listener);
property.addListener(listener);
currentProperty = property;
@ -122,7 +123,7 @@ public class DegradeRuleManager {
degradeRules.clear();
degradeRules.putAll(rules);
}
RecordLog.info("receive degrade config: " + degradeRules);
RecordLog.info("[DegradeRuleManager] Degrade rules received: " + degradeRules);
}
@Override
@ -132,16 +133,22 @@ public class DegradeRuleManager {
degradeRules.clear();
degradeRules.putAll(rules);
}
RecordLog.info("init degrade config: " + degradeRules);
RecordLog.info("[DegradeRuleManager] Degrade rules loaded: " + degradeRules);
}
private Map<String, List<DegradeRule>> loadDegradeConf(List<DegradeRule> list) {
if (list == null) {
return null;
}
Map<String, List<DegradeRule>> newRuleMap = new ConcurrentHashMap<String, List<DegradeRule>>();
if (list == null || list.isEmpty()) {
return newRuleMap;
}
for (DegradeRule rule : list) {
if (!isValidRule(rule)) {
RecordLog.warn("[DegradeRuleManager] Ignoring invalid degrade rule when loading new rules: " + rule);
continue;
}
if (StringUtil.isBlank(rule.getLimitApp())) {
rule.setLimitApp(FlowRule.LIMIT_APP_DEFAULT);
}
@ -160,4 +167,7 @@ public class DegradeRuleManager {
}
private static boolean isValidRule(DegradeRule rule) {
return rule != null && !StringUtil.isBlank(rule.getResource()) && rule.getCount() >= 0;
}
}

View File

@ -51,7 +51,6 @@ import com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpController;
*
* @author jialiang.linjl
*/
public class FlowRuleManager {
private static final Map<String, List<FlowRule>> flowRules = new ConcurrentHashMap<String, List<FlowRule>>();
@ -105,12 +104,13 @@ public class FlowRuleManager {
private static Map<String, List<FlowRule>> loadFlowConf(List<FlowRule> list) {
Map<String, List<FlowRule>> newRuleMap = new ConcurrentHashMap<String, List<FlowRule>>();
if (list == null) {
if (list == null || list.isEmpty()) {
return newRuleMap;
}
for (FlowRule rule : list) {
if (!isValid(rule)) {
if (!isValidRule(rule)) {
RecordLog.warn("[FlowRuleManager] Ignoring invalid flow rule when loading new flow rules: " + rule);
continue;
}
if (StringUtil.isBlank(rule.getLimitApp())) {
@ -202,7 +202,7 @@ public class FlowRuleManager {
}
private static boolean isValid(FlowRule rule) {
private static boolean isValidRule(FlowRule rule) {
return rule != null && !StringUtil.isBlank(rule.getResource());
}
}