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