From 80797ae85ed36786bb1882408852e7b1a54b7ddc Mon Sep 17 00:00:00 2001 From: Carpenter Lee Date: Wed, 6 Mar 2019 19:06:58 +0800 Subject: [PATCH] Allow negative paramIndex as reversed-order in ParamFlowRule (#549) * Allow negative paramIndex in ParamFlowRule Signed-off-by: Carpenter Lee --- .../slots/block/flow/param/ParamFlowChecker.java | 16 ++++++++++++++-- .../block/flow/param/ParamFlowRuleUtil.java | 2 +- .../block/flow/param/ParamFlowRuleUtilTest.java | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowChecker.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowChecker.java index 5ea64dce..2206e9ef 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowChecker.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowChecker.java @@ -52,7 +52,7 @@ final class ParamFlowChecker { } // Get parameter value. If value is null, then pass. - Object value = args[paramIdx]; + Object value = valueAt(args, paramIdx); if (value == null) { return true; } @@ -64,6 +64,18 @@ final class ParamFlowChecker { return passLocalCheck(resourceWrapper, rule, count, value); } + private static Object valueAt(Object[] args, int paramIdx) { + Object value = null; + if (paramIdx < 0) { + if (-paramIdx <= args.length) { + return args[args.length + paramIdx]; + } + } else { + value = args[paramIdx]; + } + return value; + } + private static boolean passLocalCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int count, Object value) { try { @@ -112,7 +124,7 @@ final class ParamFlowChecker { int itemThreshold = rule.getParsedHotItems().get(value); return ++threadCount <= itemThreshold; } - long threshold = (long) rule.getCount(); + long threshold = (long)rule.getCount(); return ++threadCount <= threshold; } diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleUtil.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleUtil.java index 48e4a2c1..356ec050 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleUtil.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleUtil.java @@ -31,7 +31,7 @@ public final class ParamFlowRuleUtil { public static boolean isValidRule(ParamFlowRule rule) { return rule != null && !StringUtil.isBlank(rule.getResource()) && rule.getCount() >= 0 - && rule.getGrade() >= 0 && rule.getParamIdx() != null && rule.getParamIdx() >= 0 && checkCluster(rule); + && rule.getGrade() >= 0 && rule.getParamIdx() != null && checkCluster(rule); } private static boolean checkCluster(/*@PreChecked*/ ParamFlowRule rule) { diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleUtilTest.java b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleUtilTest.java index af49f476..e8eba95f 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleUtilTest.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleUtilTest.java @@ -35,7 +35,7 @@ public class ParamFlowRuleUtilTest { .setCount(1) .setParamIdx(-1); assertFalse(ParamFlowRuleUtil.isValidRule(rule4)); - assertFalse(ParamFlowRuleUtil.isValidRule(rule5)); + assertTrue(ParamFlowRuleUtil.isValidRule(rule5)); ParamFlowRule goodRule = new ParamFlowRule("abc") .setCount(10)