From 6392cb03361c46bae176a0aa74168ea20c5171a5 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Mon, 13 Aug 2018 14:42:33 +0800 Subject: [PATCH] Modify loading mechanism of authority rules: only pick first - Comment/javadoc refinement Signed-off-by: Eric Zhao --- .../slots/block/authority/AuthorityRule.java | 17 ++++++++------- .../block/authority/AuthorityRuleManager.java | 21 ++++++++++++------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRule.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRule.java index 852193f5..1a449139 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRule.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRule.java @@ -21,14 +21,15 @@ import com.alibaba.csp.sentinel.node.DefaultNode; import com.alibaba.csp.sentinel.slots.block.AbstractRule; import com.alibaba.csp.sentinel.slots.block.RuleConstant; -/*** - * +/** * @author youji.zj */ public class AuthorityRule extends AbstractRule { - /*** 0代表白名单;1代表黑名单 ***/ - private int strategy; + /** + * Mode: 0 for whitelist; 1 for blacklist. + */ + private int strategy = RuleConstant.AUTHORITY_WHITE; public int getStrategy() { return strategy; @@ -60,12 +61,12 @@ public class AuthorityRule extends AbstractRule { public boolean passCheck(Context context, DefaultNode node, int count, Object... args) { String requester = context.getOrigin(); - // 来源或者限流的应用为null直接通过 - if (StringUtil.isEmpty(requester) || this.getLimitApp() == null) { + // Empty origin or empty limitApp will pass. + if (StringUtil.isEmpty(requester) || StringUtil.isEmpty(this.getLimitApp())) { return true; } - // 白名单、黑名单列表为逗号分隔的应用列表, indexOf还不行,需要精确匹配 + // Do exact match with origin name. int pos = this.getLimitApp().indexOf(requester); boolean contain = pos > -1; @@ -99,6 +100,6 @@ public class AuthorityRule extends AbstractRule { "resource=" + getResource() + ", limitApp=" + getLimitApp() + ", strategy=" + strategy + - "} " + super.toString(); + "} "; } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRuleManager.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRuleManager.java index eb7e889d..96219f84 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRuleManager.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRuleManager.java @@ -31,9 +31,12 @@ import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; -/*** +/** + * Manager for authority rules. + * * @author youji.zj * @author jialiang.linjl + * @author Eric Zhao */ public class AuthorityRuleManager { @@ -60,7 +63,9 @@ public class AuthorityRuleManager { } /** - * @param rules + * Load the authority rules to memory. + * + * @param rules list of authority rules */ public static void loadRules(List rules) { currentProperty.updateValue(rules); @@ -114,7 +119,7 @@ public class AuthorityRuleManager { if (rules != null) { authorityRules.putAll(rules); } - RecordLog.info("receive authority config: " + authorityRules); + RecordLog.info("[AuthorityRuleManager] Authority rules received: " + authorityRules); } private Map> loadAuthorityConf(List list) { @@ -129,12 +134,15 @@ public class AuthorityRuleManager { String identity = rule.getResource(); List ruleM = newRuleMap.get(identity); + // putIfAbsent if (ruleM == null) { ruleM = new ArrayList(); + ruleM.add(rule); newRuleMap.put(identity, ruleM); + } else { + // One resource should only have at most one authority rule, so just ignore redundant rules. + RecordLog.warn("[AuthorityRuleManager] Ignoring redundant rule: " + rule.toString()); } - ruleM.add(rule); - } return newRuleMap; @@ -148,9 +156,8 @@ public class AuthorityRuleManager { if (rules != null) { authorityRules.putAll(rules); } - RecordLog.info("load authority config: " + authorityRules); + RecordLog.info("[AuthorityRuleManager] Load authority rules: " + authorityRules); } - } }