Skip to content

Commit

Permalink
Convert negative paramIdx of ParamFlowRule in ParamFlowSlot
Browse files Browse the repository at this point in the history
Signed-off-by: Carpenter Lee <[email protected]>
  • Loading branch information
CarpenterLee committed Mar 7, 2019
1 parent 9ec1985 commit b8e295a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static boolean passCheck(ResourceWrapper resourceWrapper, /*@Valid*/ ParamFlowRu
}

// Get parameter value. If value is null, then pass.
Object value = valueAt(args, paramIdx);
Object value = args[paramIdx];
if (value == null) {
return true;
}
Expand All @@ -64,18 +64,6 @@ static boolean passCheck(ResourceWrapper resourceWrapper, /*@Valid*/ ParamFlowRu
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -73,6 +72,16 @@ void checkFlow(ResourceWrapper resourceWrapper, int count, Object... args)
}

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());

Expand Down Expand Up @@ -105,7 +114,7 @@ private void addBlockCount(ResourceWrapper resourceWrapper, int count, Object...
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit b8e295a

Please sign in to comment.