Search in sources :

Example 21 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sentinel by alibaba.

the class DemoDegradeRuleApplication method initDegradeRule.

private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<>();
    DegradeRule rule = new DegradeRule(RESOURCE_KEY).setGrade(CircuitBreakerStrategy.SLOW_REQUEST_RATIO.getType()).setCount(20).setTimeWindow(10).setSlowRatioThreshold(0.2).setMinRequestAmount(10).setStatIntervalMs(20000);
    rules.add(rule);
    DegradeRuleManager.loadRules(rules);
    System.out.println("Degrade rule loaded: " + rules);
}
Also used : ArrayList(java.util.ArrayList) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 22 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sentinel by alibaba.

the class SentinelDubboConsumerFilterTest method initDegradeRule.

private void initDegradeRule(String resource) {
    DegradeRule degradeRule = new DegradeRule(resource).setCount(0.5).setGrade(DEGRADE_GRADE_EXCEPTION_RATIO);
    List<DegradeRule> degradeRules = new ArrayList<>();
    degradeRules.add(degradeRule);
    degradeRule.setTimeWindow(1);
    DegradeRuleManager.loadRules(degradeRules);
}
Also used : DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 23 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sentinel by alibaba.

the class ExceptionCircuitBreakerTest method testRecordErrorOrSuccess.

@Test
public void testRecordErrorOrSuccess() throws BlockException {
    String resource = "testRecordErrorOrSuccess";
    int retryTimeoutMillis = 10 * 1000;
    int retryTimeout = retryTimeoutMillis / 1000;
    DegradeRule rule = new DegradeRule("abc").setCount(0.2d).setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO).setStatIntervalMs(20 * 1000).setTimeWindow(retryTimeout).setMinRequestAmount(1);
    rule.setResource(resource);
    DegradeRuleManager.loadRules(Arrays.asList(rule));
    assertTrue(entryAndSleepFor(resource, 10));
    // -> open
    assertTrue(entryWithErrorIfPresent(resource, new IllegalArgumentException()));
    assertFalse(entryWithErrorIfPresent(resource, new IllegalArgumentException()));
    assertFalse(entryAndSleepFor(resource, 100));
    sleep(retryTimeoutMillis / 2);
    assertFalse(entryAndSleepFor(resource, 100));
    sleep(retryTimeoutMillis / 2);
    // -> half -> open
    assertTrue(entryWithErrorIfPresent(resource, new IllegalArgumentException()));
    assertFalse(entryAndSleepFor(resource, 100));
    assertFalse(entryAndSleepFor(resource, 100));
    sleep(retryTimeoutMillis);
    // -> half -> closed
    assertTrue(entryAndSleepFor(resource, 100));
    assertTrue(entryAndSleepFor(resource, 100));
    assertTrue(entryAndSleepFor(resource, 100));
    assertTrue(entryAndSleepFor(resource, 100));
    assertTrue(entryAndSleepFor(resource, 100));
    assertTrue(entryAndSleepFor(resource, 100));
    assertTrue(entryAndSleepFor(resource, 100));
    assertTrue(entryWithErrorIfPresent(resource, new IllegalArgumentException()));
    assertTrue(entryAndSleepFor(resource, 100));
}
Also used : DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 24 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project incubator-shenyu by apache.

the class SentinelRuleHandle method handlerRule.

@Override
public void handlerRule(final RuleData ruleData) {
    SentinelHandle sentinelHandle = GsonUtils.getInstance().fromJson(ruleData.getHandle(), SentinelHandle.class);
    sentinelHandle.checkData(sentinelHandle);
    String key = CacheKeyUtils.INST.getKey(ruleData);
    List<FlowRule> flowRules = FlowRuleManager.getRules().stream().filter(r -> !r.getResource().equals(key)).collect(Collectors.toList());
    if (sentinelHandle.getFlowRuleEnable() == Constants.SENTINEL_ENABLE_FLOW_RULE) {
        FlowRule rule = new FlowRule(key);
        rule.setCount(sentinelHandle.getFlowRuleCount());
        rule.setGrade(sentinelHandle.getFlowRuleGrade());
        rule.setControlBehavior(sentinelHandle.getFlowRuleControlBehavior());
        flowRules.add(rule);
    }
    FlowRuleManager.loadRules(flowRules);
    List<DegradeRule> degradeRules = DegradeRuleManager.getRules().stream().filter(r -> !r.getResource().equals(key)).collect(Collectors.toList());
    if (sentinelHandle.getDegradeRuleEnable() == Constants.SENTINEL_ENABLE_DEGRADE_RULE) {
        DegradeRule rule = new DegradeRule(key);
        rule.setCount(sentinelHandle.getDegradeRuleCount());
        rule.setGrade(sentinelHandle.getDegradeRuleGrade());
        rule.setTimeWindow(sentinelHandle.getDegradeRuleTimeWindow());
        rule.setStatIntervalMs(sentinelHandle.getDegradeRuleStatIntervals() * 1000);
        rule.setMinRequestAmount(sentinelHandle.getDegradeRuleMinRequestAmount());
        rule.setSlowRatioThreshold(sentinelHandle.getDegradeRuleSlowRatioThreshold());
        degradeRules.add(rule);
    }
    DegradeRuleManager.loadRules(degradeRules);
}
Also used : Constants(org.apache.shenyu.common.constant.Constants) PluginEnum(org.apache.shenyu.common.enums.PluginEnum) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule) SentinelHandle(org.apache.shenyu.common.dto.convert.rule.SentinelHandle) RuleData(org.apache.shenyu.common.dto.RuleData) FlowRuleManager(com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager) Collectors(java.util.stream.Collectors) GsonUtils(org.apache.shenyu.common.utils.GsonUtils) List(java.util.List) PluginDataHandler(org.apache.shenyu.plugin.base.handler.PluginDataHandler) DegradeRuleManager(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) CacheKeyUtils(org.apache.shenyu.plugin.base.utils.CacheKeyUtils) SentinelHandle(org.apache.shenyu.common.dto.convert.rule.SentinelHandle) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 25 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project incubator-shenyu by apache.

the class SentinelRuleHandleTest method removeRule.

@Test
public void removeRule() {
    RuleData data = new RuleData();
    data.setSelectorId("sentinel");
    data.setName("removeRule");
    SentinelHandle sentinelHandle = new SentinelHandle();
    sentinelHandle.setFlowRuleCount(10);
    sentinelHandle.setFlowRuleGrade(0);
    sentinelHandle.setFlowRuleControlBehavior(0);
    sentinelHandle.setDegradeRuleCount(1);
    sentinelHandle.setDegradeRuleGrade(0);
    sentinelHandle.setDegradeRuleTimeWindow(5);
    sentinelHandle.setDegradeRuleMinRequestAmount(5);
    sentinelHandle.setDegradeRuleStatIntervals(10);
    sentinelHandle.setDegradeRuleSlowRatioThreshold(0.5d);
    data.setHandle(GsonUtils.getGson().toJson(sentinelHandle));
    sentinelRuleHandle.handlerRule(data);
    FlowRule flowRule = FlowRuleManager.getRules().get(0);
    assertThat(flowRule.getCount(), is(10.0));
    assertThat(flowRule.getResource(), is("sentinel_removeRule"));
    DegradeRule degradeRule = DegradeRuleManager.getRules().get(0);
    assertThat(degradeRule.getCount(), is(1.0));
    assertThat(degradeRule.getResource(), is("sentinel_removeRule"));
    sentinelRuleHandle.removeRule(data);
    assertTrue(FlowRuleManager.getRules().isEmpty());
    assertTrue(DegradeRuleManager.getRules().isEmpty());
}
Also used : RuleData(org.apache.shenyu.common.dto.RuleData) SentinelHandle(org.apache.shenyu.common.dto.convert.rule.SentinelHandle) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule) Test(org.junit.jupiter.api.Test)

Aggregations

DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)34 ArrayList (java.util.ArrayList)12 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)9 Test (org.junit.Test)4 List (java.util.List)3 ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)2 GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)2 AuthorityRule (com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)2 ParamFlowItem (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem)2 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)2 SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)2 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)2 SentinelDegradeRule (com.github.howinfun.sentinel.pojo.SentinelDegradeRule)2 SentinelFlowRule (com.github.howinfun.sentinel.pojo.SentinelFlowRule)2 HashSet (java.util.HashSet)2 RuleData (org.apache.shenyu.common.dto.RuleData)2 SentinelHandle (org.apache.shenyu.common.dto.convert.rule.SentinelHandle)2 EntryType (com.alibaba.csp.sentinel.EntryType)1 DegradeRuleManager (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager)1 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)1