Search in sources :

Example 41 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class SentinelAnnotationIntegrationTest method testAnnotationExceptionsToIgnore.

@Test
public void testAnnotationExceptionsToIgnore() {
    assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
    String resourceName = "apiBaz";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    try {
        fooService.baz("fail");
        fail("should not reach here");
    } catch (IllegalMonitorStateException ex) {
        assertThat(cn.exceptionQps()).isZero();
    }
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test)

Example 42 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class SentinelAnnotationIntegrationTest method testClassLevelDefaultFallbackWithSingleParam.

@Test
public void testClassLevelDefaultFallbackWithSingleParam() {
    assertThat(barService.anotherBar(1)).isEqualTo("Hello for 1");
    String resourceName = "apiAnotherBarWithDefaultFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    assertThat(barService.doSomething(1)).isEqualTo("do something");
    String resourceName1 = "com.alibaba.csp.sentinel.annotation.aspectj.integration.service.BarService:doSomething(int)";
    ClusterNode cn1 = ClusterBuilderSlot.getClusterNode(resourceName1);
    assertThat(cn1).isNotNull();
    assertThat(cn1.passQps()).isPositive();
    assertThat(barService.anotherBar(5758)).isEqualTo("eee...");
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();
    assertThat(barService.doSomething(5758)).isEqualTo("GlobalFallback:doFallback");
    assertThat(cn1.exceptionQps()).isPositive();
    assertThat(cn1.blockQps()).isZero();
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test)

Example 43 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class FetchClusterNodeByIdCommandHandler method handle.

@Override
public CommandResponse<String> handle(CommandRequest request) {
    String id = request.getParam("id");
    if (StringUtil.isEmpty(id)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("Invalid parameter: empty clusterNode name"));
    }
    ClusterNode node = ClusterBuilderSlot.getClusterNode(id);
    if (node != null) {
        return CommandResponse.ofSuccess(JSON.toJSONString(NodeVo.fromClusterNode(id, node)));
    } else {
        return CommandResponse.ofSuccess("{}");
    }
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode)

Example 44 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class FetchClusterNodeHumanCommandHandler method handle.

@Override
public CommandResponse<String> handle(CommandRequest request) {
    String name = request.getParam("id");
    if (StringUtil.isEmpty(name)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("Invalid parameter: empty clusterNode name"));
    }
    StringBuilder sb = new StringBuilder();
    int i = 0;
    int nameLength = 0;
    for (Entry<ResourceWrapper, ClusterNode> e : ClusterBuilderSlot.getClusterNodeMap().entrySet()) {
        if (e.getKey().getName().contains(name)) {
            int l = e.getKey().getShowName().length();
            if (l > nameLength) {
                nameLength = l;
            }
            if (++i == 30) {
                break;
            }
        }
    }
    nameLength = nameLength > MAX_LEN ? MAX_LEN : nameLength;
    String format = FORMAT.replaceAll("80", String.valueOf(nameLength + 1));
    sb.append(String.format(format, "idx", "id", "thread", "pass", "blocked", "success", "total", "aRt", "1m-pass", "1m-block", "1m-all", "exception")).append("\n");
    for (Entry<ResourceWrapper, ClusterNode> e : ClusterBuilderSlot.getClusterNodeMap().entrySet()) {
        if (e.getKey().getName().contains(name)) {
            ClusterNode node = e.getValue();
            String id = e.getKey().getShowName();
            int lenNum = (int) Math.ceil((double) id.length() / nameLength) - 1;
            sb.append(String.format(format, i + 1, lenNum == 0 ? id : id.substring(0, nameLength), node.curThreadNum(), node.passQps(), node.blockQps(), node.successQps(), node.totalQps(), node.avgRt(), node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest(), node.exceptionQps())).append("\n");
            for (int j = 1; j <= lenNum; ++j) {
                int start = nameLength * j;
                int end = j == lenNum ? id.length() : nameLength * (j + 1);
                sb.append(String.format(format, "", id.substring(start, end), "", "", "", "", "", "", "", "", "", "", "", "")).append("\n");
            }
            if (++i == 30) {
                break;
            }
        }
    }
    return CommandResponse.ofSuccess(sb.toString());
}
Also used : ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode)

Example 45 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class FetchOriginCommandHandler method handle.

@Override
public CommandResponse<String> handle(CommandRequest request) {
    StringBuilder sb = new StringBuilder();
    String name = request.getParam("id");
    ClusterNode cNode = null;
    boolean exactly = false;
    for (Entry<ResourceWrapper, ClusterNode> e : ClusterBuilderSlot.getClusterNodeMap().entrySet()) {
        if (e.getKey().getName().equals(name)) {
            cNode = e.getValue();
            sb.append("id: ").append(e.getKey().getShowName()).append("\n");
            sb.append("\n");
            exactly = true;
            break;
        }
    }
    if (!exactly) {
        for (Entry<ResourceWrapper, ClusterNode> e : ClusterBuilderSlot.getClusterNodeMap().entrySet()) {
            if (e.getKey().getName().indexOf(name) > 0) {
                cNode = e.getValue();
                sb.append("id: ").append(e.getKey().getShowName()).append("\n");
                sb.append("\n");
                break;
            }
        }
    }
    if (cNode == null) {
        return CommandResponse.ofSuccess("Not find cNode with id " + name);
    }
    int i = 0;
    int nameLength = 0;
    for (Entry<String, StatisticNode> e : cNode.getOriginCountMap().entrySet()) {
        int l = e.getKey().length();
        if (l > nameLength) {
            nameLength = l;
        }
        if (++i == 120) {
            break;
        }
    }
    nameLength = nameLength > MAX_LEN ? MAX_LEN : nameLength;
    String format = FORMAT.replaceAll("80", String.valueOf(nameLength + 1));
    i = 0;
    sb.append(String.format(format, "idx", "origin", "threadNum", "passQps", "blockQps", "totalQps", "aRt", "1m-pass", "1m-block", "1m-total")).append("\n");
    for (Entry<String, StatisticNode> e : cNode.getOriginCountMap().entrySet()) {
        StatisticNode node = e.getValue();
        String id = e.getKey();
        int lenNum = (int) Math.ceil((double) id.length() / nameLength) - 1;
        sb.append(String.format(format, i + 1, lenNum == 0 ? id : id.substring(0, nameLength), node.curThreadNum(), node.passQps(), node.blockQps(), node.totalQps(), node.avgRt(), node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest())).append("\n");
        for (int j = 1; j <= lenNum; ++j) {
            int start = nameLength * j;
            int end = j == lenNum ? id.length() : nameLength * (j + 1);
            sb.append(String.format(format, "", id.substring(start, end), "", "", "", "", "", "", "", "", "", "", "", "")).append("\n");
        }
        if (++i == 30) {
            break;
        }
    }
    return CommandResponse.ofSuccess(sb.toString());
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode)

Aggregations

ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)92 Test (org.junit.Test)61 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)26 Response (io.restassured.response.Response)14 Node (com.alibaba.csp.sentinel.node.Node)13 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)13 Test (org.junit.jupiter.api.Test)13 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)13 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)11 Response (javax.ws.rs.core.Response)11 Context (com.alibaba.csp.sentinel.context.Context)10 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)10 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)9 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)9 ArrayList (java.util.ArrayList)7 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)6 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)6 ClusterBuilderSlot (com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot)6 Collections (java.util.Collections)6 Assert (org.junit.Assert)6