Search in sources :

Example 1 with Tuple2

use of com.alibaba.csp.sentinel.util.function.Tuple2 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)

Example 2 with Tuple2

use of com.alibaba.csp.sentinel.util.function.Tuple2 in project mogu_blog_v2 by moxi624.

the class SentinelHealthIndicator method doHealthCheck.

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
    Map<String, Object> detailMap = new HashMap<>();
    // detail
    if (!sentinelProperties.isEnabled()) {
        detailMap.put("enabled", false);
        builder.up().withDetails(detailMap);
        return;
    }
    detailMap.put("enabled", true);
    // Check health of Dashboard
    boolean dashboardUp = true;
    List<Tuple2<String, Integer>> consoleServer = TransportConfig.getConsoleServerList();
    if (consoleServer == null) {
        // If Dashboard isn't configured, it's OK and mark the status of Dashboard
        // with UNKNOWN.
        detailMap.put("dashboard", new Status(Status.UNKNOWN.getCode(), "dashboard isn't configured"));
    } else {
        // If Dashboard is configured, send a heartbeat message to it and check the
        // result
        HeartbeatSender heartbeatSender = HeartbeatSenderProvider.getHeartbeatSender();
        boolean result = heartbeatSender.sendHeartbeat();
        if (result) {
            detailMap.put("dashboard", Status.UP);
        } else {
            // If failed to send heartbeat message, means that the Dashboard is DOWN
            dashboardUp = false;
            detailMap.put("dashboard", new Status(Status.DOWN.getCode(), consoleServer + " can't be connected"));
        }
    }
    // Check health of DataSource
    boolean dataSourceUp = true;
    Map<String, Object> dataSourceDetailMap = new HashMap<>();
    detailMap.put("dataSource", dataSourceDetailMap);
    // Get all DataSources and each call loadConfig to check if it's OK
    // If no Exception thrown, it's OK
    // Note:
    // Even if the dynamic config center is down, the loadConfig() might return
    // successfully
    // e.g. for Nacos client, it might retrieve from the local cache)
    // But in most circumstances it's okay
    Map<String, AbstractDataSource> dataSourceMap = beanFactory.getBeansOfType(AbstractDataSource.class);
    for (Map.Entry<String, AbstractDataSource> dataSourceMapEntry : dataSourceMap.entrySet()) {
        String dataSourceBeanName = dataSourceMapEntry.getKey();
        AbstractDataSource dataSource = dataSourceMapEntry.getValue();
        try {
            dataSource.loadConfig();
            dataSourceDetailMap.put(dataSourceBeanName, Status.UP);
        } catch (Exception e) {
            // If one DataSource failed to loadConfig, means that the DataSource is
            // DOWN
            dataSourceUp = false;
            dataSourceDetailMap.put(dataSourceBeanName, new Status(Status.DOWN.getCode(), e.getMessage()));
        }
    }
    // If Dashboard and DataSource are both OK, the health status is UP
    if (dashboardUp && dataSourceUp) {
        builder.up().withDetails(detailMap);
    } else {
        builder.down().withDetails(detailMap);
    }
}
Also used : Status(org.springframework.boot.actuate.health.Status) HashMap(java.util.HashMap) AbstractDataSource(com.alibaba.csp.sentinel.datasource.AbstractDataSource) HeartbeatSender(com.alibaba.csp.sentinel.transport.HeartbeatSender) Tuple2(com.alibaba.csp.sentinel.util.function.Tuple2) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Tuple2

use of com.alibaba.csp.sentinel.util.function.Tuple2 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("rk1").setValue("rv1").build()).build();
    RateLimitDescriptor descriptor2 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("rk2").setValue("rv2").build()).addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("rk3").setValue("rv3").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(RateLimitResponse.Code.OVER_LIMIT, response.getOverallCode());
    assertEquals(2, response.getStatusesCount());
    assertTrue(response.getStatusesList().stream().anyMatch(e -> e.getCode().equals(RateLimitResponse.Code.OVER_LIMIT)));
    assertFalse(response.getStatusesList().stream().allMatch(e -> e.getCode().equals(RateLimitResponse.Code.OVER_LIMIT)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TokenResultStatus(com.alibaba.csp.sentinel.cluster.TokenResultStatus) RateLimitDescriptor(io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Tuple2(com.alibaba.csp.sentinel.util.function.Tuple2) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito(org.mockito.Mockito) StreamObserver(io.grpc.stub.StreamObserver) ArgumentCaptor(org.mockito.ArgumentCaptor) RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) Assert(org.junit.Assert) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) RateLimitDescriptor(io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse) Test(org.junit.Test)

Aggregations

Tuple2 (com.alibaba.csp.sentinel.util.function.Tuple2)3 TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)2 TokenResultStatus (com.alibaba.csp.sentinel.cluster.TokenResultStatus)2 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)2 StreamObserver (io.grpc.stub.StreamObserver)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 Mockito (org.mockito.Mockito)2 AbstractDataSource (com.alibaba.csp.sentinel.datasource.AbstractDataSource)1 HeartbeatSender (com.alibaba.csp.sentinel.transport.HeartbeatSender)1 RateLimitDescriptor (io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor)1 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)1 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest)1 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse)1 Code (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse.Code)1 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest)1 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1