Search in sources :

Example 21 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class SentinelSpringMvcIntegrationTest method configureExceptionRulesFor.

private void configureExceptionRulesFor(String resource, int count, String limitApp) {
    FlowRule rule = new FlowRule().setCount(count).setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
    rule.setResource(resource);
    if (StringUtil.isNotBlank(limitApp)) {
        rule.setLimitApp(limitApp);
    }
    FlowRuleManager.loadRules(Collections.singletonList(rule));
}
Also used : FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 22 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class ClusterFlowRuleManager method getFlowRules.

/**
 * Get all cluster flow rules within a specific namespace.
 *
 * @param namespace valid namespace
 * @return cluster flow rules within the provided namespace
 */
public static List<FlowRule> getFlowRules(String namespace) {
    if (StringUtil.isEmpty(namespace)) {
        return new ArrayList<>();
    }
    List<FlowRule> rules = new ArrayList<>();
    Set<Long> flowIdSet = NAMESPACE_FLOW_ID_MAP.get(namespace);
    if (flowIdSet == null || flowIdSet.isEmpty()) {
        return rules;
    }
    for (Long flowId : flowIdSet) {
        FlowRule rule = FLOW_RULES.get(flowId);
        if (rule != null) {
            rules.add(rule);
        }
    }
    return rules;
}
Also used : FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 23 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class ClusterMetricNodeGenerator method flowToMetricNode.

public static ClusterMetricNode flowToMetricNode(long flowId) {
    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(flowId);
    if (rule == null) {
        return null;
    }
    ClusterMetric metric = ClusterMetricStatistics.getMetric(flowId);
    if (metric == null) {
        return new ClusterMetricNode().setFlowId(flowId).setResourceName(rule.getResource());
    }
    return new ClusterMetricNode().setFlowId(flowId).setResourceName(rule.getResource()).setBlockQps(metric.getAvg(ClusterFlowEvent.BLOCK)).setPassQps(metric.getAvg(ClusterFlowEvent.PASS)).setTimestamp(TimeUtil.currentTimeMillis());
}
Also used : ClusterMetric(com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterMetric) ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 24 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class SentinelEnvoyRlsServiceImplTest method testShouldRateLimitPass.

@Test
public void testShouldRateLimitPass() {
    SentinelEnvoyRlsServiceImpl rlsService = mock(SentinelEnvoyRlsServiceImpl.class);
    StreamObserver<RateLimitResponse> streamObserver = mock(StreamObserver.class);
    String domain = "testShouldRateLimitPass";
    int acquireCount = 1;
    RateLimitDescriptor descriptor1 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a1").setValue("b1").build()).build();
    RateLimitDescriptor descriptor2 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a2").setValue("b2").build()).addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a3").setValue("b3").build()).build();
    ArgumentCaptor<RateLimitResponse> responseCapture = ArgumentCaptor.forClass(RateLimitResponse.class);
    doNothing().when(streamObserver).onNext(responseCapture.capture());
    doCallRealMethod().when(rlsService).shouldRateLimit(any(), any());
    when(rlsService.checkToken(eq(domain), same(descriptor1), eq(acquireCount))).thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.OK)));
    when(rlsService.checkToken(eq(domain), same(descriptor2), eq(acquireCount))).thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.OK)));
    RateLimitRequest rateLimitRequest = RateLimitRequest.newBuilder().addDescriptors(descriptor1).addDescriptors(descriptor2).setDomain(domain).setHitsAddend(acquireCount).build();
    rlsService.shouldRateLimit(rateLimitRequest, streamObserver);
    RateLimitResponse response = responseCapture.getValue();
    assertEquals(Code.OK, response.getOverallCode());
    response.getStatusesList().forEach(e -> assertEquals(Code.OK, e.getCode()));
}
Also used : RateLimitDescriptor(io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse) Test(org.junit.Test)

Example 25 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class SentinelEnvoyRlsServiceImplTest method testShouldRatePartialBlock.

@Test
public void testShouldRatePartialBlock() {
    SentinelEnvoyRlsServiceImpl rlsService = mock(SentinelEnvoyRlsServiceImpl.class);
    StreamObserver<RateLimitResponse> streamObserver = mock(StreamObserver.class);
    String domain = "testShouldRatePartialBlock";
    int acquireCount = 1;
    RateLimitDescriptor descriptor1 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a1").setValue("b1").build()).build();
    RateLimitDescriptor descriptor2 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a2").setValue("b2").build()).addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a3").setValue("b3").build()).build();
    ArgumentCaptor<RateLimitResponse> responseCapture = ArgumentCaptor.forClass(RateLimitResponse.class);
    doNothing().when(streamObserver).onNext(responseCapture.capture());
    doCallRealMethod().when(rlsService).shouldRateLimit(any(), any());
    when(rlsService.checkToken(eq(domain), same(descriptor1), eq(acquireCount))).thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.BLOCKED)));
    when(rlsService.checkToken(eq(domain), same(descriptor2), eq(acquireCount))).thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.OK)));
    RateLimitRequest rateLimitRequest = RateLimitRequest.newBuilder().addDescriptors(descriptor1).addDescriptors(descriptor2).setDomain(domain).setHitsAddend(acquireCount).build();
    rlsService.shouldRateLimit(rateLimitRequest, streamObserver);
    RateLimitResponse response = responseCapture.getValue();
    assertEquals(Code.OVER_LIMIT, response.getOverallCode());
    assertEquals(2, response.getStatusesCount());
    assertTrue(response.getStatusesList().stream().anyMatch(e -> e.getCode().equals(Code.OVER_LIMIT)));
    assertFalse(response.getStatusesList().stream().allMatch(e -> e.getCode().equals(Code.OVER_LIMIT)));
}
Also used : RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest) Code(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse.Code) TokenResultStatus(com.alibaba.csp.sentinel.cluster.TokenResultStatus) Tuple2(com.alibaba.csp.sentinel.util.function.Tuple2) Test(org.junit.Test) RateLimitDescriptor(io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor) Mockito(org.mockito.Mockito) StreamObserver(io.grpc.stub.StreamObserver) ArgumentCaptor(org.mockito.ArgumentCaptor) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) Assert(org.junit.Assert) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) RateLimitDescriptor(io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse) Test(org.junit.Test)

Aggregations

FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)113 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)31 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)22 List (java.util.List)17 TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)11 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)9 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 Test (org.junit.jupiter.api.Test)6 ClusterFlowConfig (com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig)5 TypeReference (com.alibaba.fastjson.TypeReference)5 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)5 Before (org.junit.Before)5 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)4 ClusterMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterMetric)3 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)3 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)3 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)3 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)3 IOException (java.io.IOException)3