Browse Source

Remove deprecated passCheck() in Rule and polish interface

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao Jason Joo 4 years ago
parent
commit
a18ef7cbfa
8 changed files with 4 additions and 225 deletions
  1. +1
    -0
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/AbstractRule.java
  2. +3
    -10
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/Rule.java
  3. +0
    -7
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRule.java
  4. +0
    -8
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRule.java
  5. +0
    -7
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRule.java
  6. +0
    -7
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemRule.java
  7. +0
    -178
      sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleTest.java
  8. +0
    -8
      sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRule.java

+ 1
- 0
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/AbstractRule.java View File

@@ -39,6 +39,7 @@ public abstract class AbstractRule implements Rule {
*/
private String limitApp;

@Override
public String getResource() {
return resource;
}


+ 3
- 10
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/Rule.java View File

@@ -15,9 +15,6 @@
*/
package com.alibaba.csp.sentinel.slots.block;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.node.DefaultNode;

/**
* Base interface of all rules.
*
@@ -26,14 +23,10 @@ import com.alibaba.csp.sentinel.node.DefaultNode;
public interface Rule {

/**
* Check whether current statistical indicators meet this rule, which means not exceeding any threshold.
* Get target resource of this rule.
*
* @param context current {@link Context}
* @param node current {@link com.alibaba.csp.sentinel.node.Node}
* @param count tokens needed.
* @param args arguments of the original invocation.
* @return If current statistical indicators not exceeding any threshold return true, otherwise return false.
* @return target resource of this rule
*/
boolean passCheck(Context context, DefaultNode node, int count, Object... args);
String getResource();

}

+ 0
- 7
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRule.java View File

@@ -15,8 +15,6 @@
*/
package com.alibaba.csp.sentinel.slots.block.authority;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;

@@ -59,11 +57,6 @@ public class AuthorityRule extends AbstractRule {
return result;
}

@Override
public boolean passCheck(Context context, DefaultNode node, int count, Object... args) {
return true;
}

@Override
public String toString() {
return "AuthorityRule{" +


+ 0
- 8
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRule.java View File

@@ -15,8 +15,6 @@
*/
package com.alibaba.csp.sentinel.slots.block.degrade;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;

@@ -172,10 +170,4 @@ public class DegradeRule extends AbstractRule {
", statIntervalMs=" + statIntervalMs +
'}';
}

@Override
@Deprecated
public boolean passCheck(Context context, DefaultNode node, int count, Object... args) {
return false;
}
}

+ 0
- 7
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRule.java View File

@@ -15,8 +15,6 @@
*/
package com.alibaba.csp.sentinel.slots.block.flow;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;

@@ -186,11 +184,6 @@ public class FlowRule extends AbstractRule {
return this;
}

@Override
public boolean passCheck(Context context, DefaultNode node, int acquireCount, Object... args) {
return true;
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }


+ 0
- 7
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemRule.java View File

@@ -15,8 +15,6 @@
*/
package com.alibaba.csp.sentinel.slots.system;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;

/**
@@ -133,11 +131,6 @@ public class SystemRule extends AbstractRule {
this.highestCpuUsage = highestCpuUsage;
}

@Override
public boolean passCheck(Context context, DefaultNode node, int count, Object... args) {
return true;
}

@Override
public boolean equals(Object o) {
if (this == o) {


+ 0
- 178
sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleTest.java View File

@@ -1,178 +0,0 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.slots.block.flow;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Ignore;
import org.junit.Test;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.node.ClusterNode;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.controller.DefaultController;

/**
* @author jialiang.linjl
*/
@Ignore("Deprecated test for legacy FlowRule")
public class FlowRuleTest {

@Test
public void testFlowRule_grade() {

FlowRule flowRule = new FlowRule();
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setCount(1);
flowRule.setLimitApp("default");
flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT);

DefaultController defaultController = new DefaultController(1, flowRule.getGrade());
flowRule.setRater(defaultController);

Context context = mock(Context.class);
DefaultNode node = mock(DefaultNode.class);
ClusterNode cn = mock(ClusterNode.class);

when(context.getOrigin()).thenReturn("");
when(node.getClusterNode()).thenReturn(cn);
when(cn.passQps()).thenReturn(1d);

assertFalse(flowRule.passCheck(context, node, 1));

flowRule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
defaultController = new DefaultController(1, flowRule.getGrade());
flowRule.setRater(defaultController);
when(cn.curThreadNum()).thenReturn(1);
assertTrue(!flowRule.passCheck(context, node, 1));
}

@Test
public void testFlowRule_strategy() {

FlowRule flowRule = new FlowRule();
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setCount(1);
flowRule.setLimitApp("default");
flowRule.setStrategy(RuleConstant.STRATEGY_CHAIN);
DefaultController defaultController = new DefaultController(1, flowRule.getGrade());
flowRule.setRater(defaultController);
flowRule.setRefResource("entry1");

Context context = mock(Context.class);
DefaultNode dn = mock(DefaultNode.class);

when(context.getName()).thenReturn("entry1");
when(dn.passQps()).thenReturn(1d);
assertFalse(flowRule.passCheck(context, dn, 1));

when(context.getName()).thenReturn("entry2");
assertTrue(flowRule.passCheck(context, dn, 1));

// Strategy == relate
flowRule.setStrategy(RuleConstant.STRATEGY_CHAIN);
ClusterNode cn = mock(ClusterNode.class);
assertTrue(flowRule.passCheck(context, dn, 1));
}

@Test
public void testOrigin() {
FlowRule flowRule = new FlowRule();
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setCount(1);
flowRule.setLimitApp("default");
flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT);
DefaultController defaultController = new DefaultController(1, flowRule.getGrade());
flowRule.setRater(defaultController);
flowRule.setRefResource("entry1");

Context context = mock(Context.class);
DefaultNode dn = mock(DefaultNode.class);
when(context.getOrigin()).thenReturn("origin1");
when(dn.passQps()).thenReturn(1d);
when(context.getOriginNode()).thenReturn(dn);

/*
* first scenario, limit app as default
*
*/
ClusterNode cn = mock(ClusterNode.class);
when(dn.getClusterNode()).thenReturn(cn);
when(cn.passQps()).thenReturn(1d);
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]) == false);
when(cn.passQps()).thenReturn(0d);
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]));

flowRule.setStrategy(RuleConstant.STRATEGY_CHAIN);
flowRule.setResource("entry1");
when(context.getName()).thenReturn("entry1");
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]) == false);
when(context.getName()).thenReturn("entry2");
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]));

// relate node
flowRule.setStrategy(RuleConstant.STRATEGY_RELATE);
flowRule.setResource("worong");
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]));

/*
* second scenario test a context with the same origin1
*
*/
flowRule.setLimitApp("origin1");
when(context.getName()).thenReturn("entry1");
// direct node
flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT);
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]) == false);

// chain node
flowRule.setResource("entry1");
flowRule.setStrategy(RuleConstant.STRATEGY_CHAIN);
when(context.getName()).thenReturn("entry1");
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]) == false);
when(context.getName()).thenReturn("entry2");
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]));

// relate node
flowRule.setStrategy(RuleConstant.STRATEGY_RELATE);
flowRule.setResource("not exits");
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]));

when(context.getOrigin()).thenReturn("origin2");
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]));

/*
* limit app= other
*/
flowRule.setLimitApp("other");
flowRule.setResource("hello world");

flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT);
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]) == false);

flowRule.setStrategy(RuleConstant.STRATEGY_CHAIN);
flowRule.setResource("entry1");
when(context.getName()).thenReturn("entry1");
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]) == false);

when(context.getName()).thenReturn("entry2");
assertTrue(flowRule.passCheck(context, dn, 1, new Object[0]));
}

}

+ 0
- 8
sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRule.java View File

@@ -21,8 +21,6 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;

@@ -190,12 +188,6 @@ public class ParamFlowRule extends AbstractRule {
return this;
}

@Override
@Deprecated
public boolean passCheck(Context context, DefaultNode node, int count, Object... args) {
return true;
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }


Loading…
Cancel
Save