Convert negative paramIdx of ParamFlowRule in ParamFlowSlot
Signed-off-by: Carpenter Lee <hooleeucas@163.com>
This commit is contained in:
parent
9ec1985755
commit
b8e295a138
|
|
@ -52,7 +52,7 @@ final class ParamFlowChecker {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get parameter value. If value is null, then pass.
|
// Get parameter value. If value is null, then pass.
|
||||||
Object value = valueAt(args, paramIdx);
|
Object value = args[paramIdx];
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -64,18 +64,6 @@ final class ParamFlowChecker {
|
||||||
return passLocalCheck(resourceWrapper, rule, count, value);
|
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,
|
private static boolean passLocalCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int count,
|
||||||
Object value) {
|
Object value) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -47,9 +47,8 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
|
||||||
private final Object LOCK = new Object();
|
private final Object LOCK = new Object();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args)
|
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
|
||||||
throws Throwable {
|
boolean prioritized, Object... args) throws Throwable {
|
||||||
|
|
||||||
if (!ParamFlowRuleManager.hasRules(resourceWrapper.getName())) {
|
if (!ParamFlowRuleManager.hasRules(resourceWrapper.getName())) {
|
||||||
fireEntry(context, resourceWrapper, node, count, prioritized, args);
|
fireEntry(context, resourceWrapper, node, count, prioritized, args);
|
||||||
return;
|
return;
|
||||||
|
|
@ -73,6 +72,16 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ParamFlowRule rule : rules) {
|
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.
|
// Initialize the parameter metrics.
|
||||||
initHotParamMetricsFor(resourceWrapper, rule.getParamIdx());
|
initHotParamMetricsFor(resourceWrapper, rule.getParamIdx());
|
||||||
|
|
||||||
|
|
@ -105,7 +114,7 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
|
||||||
* Package-private for test.
|
* Package-private for test.
|
||||||
*
|
*
|
||||||
* @param resourceWrapper resource to init
|
* @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) {
|
void initHotParamMetricsFor(ResourceWrapper resourceWrapper, /*@Valid*/ int index) {
|
||||||
ParameterMetric metric;
|
ParameterMetric metric;
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,28 @@ public class ParamFlowSlotTest {
|
||||||
|
|
||||||
private final ParamFlowSlot paramFlowSlot = new ParamFlowSlot();
|
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
|
@Test
|
||||||
public void testEntryWhenParamFlowRuleNotExists() throws Throwable {
|
public void testEntryWhenParamFlowRuleNotExists() throws Throwable {
|
||||||
String resourceName = "testEntryWhenParamFlowRuleNotExists";
|
String resourceName = "testEntryWhenParamFlowRuleNotExists";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue