瀏覽代碼

Convert negative paramIdx of ParamFlowRule in ParamFlowSlot

Signed-off-by: Carpenter Lee <hooleeucas@163.com>
master
Carpenter Lee 5 年之前
父節點
當前提交
b8e295a138
共有 3 個文件被更改,包括 36 次插入17 次删除
  1. +1
    -13
      sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowChecker.java
  2. +13
    -4
      sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java
  3. +22
    -0
      sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java

+ 1
- 13
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 = valueAt(args, paramIdx);
Object value = args[paramIdx];
if (value == null) {
return true;
}
@@ -64,18 +64,6 @@ 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 {


+ 13
- 4
sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlot.java 查看文件

@@ -47,9 +47,8 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
private final Object LOCK = new Object();

@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args)
throws Throwable {

public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
boolean prioritized, Object... args) throws Throwable {
if (!ParamFlowRuleManager.hasRules(resourceWrapper.getName())) {
fireEntry(context, resourceWrapper, node, count, prioritized, args);
return;
@@ -73,6 +72,16 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
}

for (ParamFlowRule rule : rules) {
int paramIdx = rule.getParamIdx();
if (paramIdx < 0) {
if (-paramIdx <= args.length) {
rule.setParamIdx(args.length + paramIdx);
} else {
// illegal index, give it a illegal positive value, latter rule check will pass
rule.setParamIdx(-paramIdx);
}
}

// Initialize the parameter metrics.
initHotParamMetricsFor(resourceWrapper, rule.getParamIdx());

@@ -105,7 +114,7 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
* Package-private for test.
*
* @param resourceWrapper resource to init
* @param index index to initialize, which must be valid
* @param index index to initialize, which must be valid
*/
void initHotParamMetricsFor(ResourceWrapper resourceWrapper, /*@Valid*/ int index) {
ParameterMetric metric;


+ 22
- 0
sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java 查看文件

@@ -39,6 +39,28 @@ public class ParamFlowSlotTest {

private final ParamFlowSlot paramFlowSlot = new ParamFlowSlot();

@Test
public void testNegativeParamIdx() throws Throwable {
String resourceName = "testNegativeParamIdx";
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
ParamFlowRule rule = new ParamFlowRule(resourceName)
.setCount(1)
.setParamIdx(-1);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi");
assertEquals(2, rule.getParamIdx().longValue());

rule.setParamIdx(-100);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi");
assertEquals(100, rule.getParamIdx().longValue());

rule.setParamIdx(0);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi");
assertEquals(0, rule.getParamIdx().longValue());
}

@Test
public void testEntryWhenParamFlowRuleNotExists() throws Throwable {
String resourceName = "testEntryWhenParamFlowRuleNotExists";


Loading…
取消
儲存