Search in sources :

Example 16 with ResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper 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 17 with ResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper 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)

Example 18 with ResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.

the class ParamFlowCheckerTest method testHotParamCheckerPassCheckExceedArgs.

@Test
public void testHotParamCheckerPassCheckExceedArgs() {
    final String resourceName = "testHotParamCheckerPassCheckExceedArgs";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 1;
    ParamFlowRule rule = new ParamFlowRule();
    rule.setResource(resourceName);
    rule.setCount(10);
    rule.setParamIdx(paramIdx);
    assertTrue("The rule will pass if the paramIdx exceeds provided args", ParamFlowChecker.passCheck(resourceWrapper, rule, 1, "abc"));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 19 with ResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.

the class ParamFlowCheckerTest method testSingleValueCheckThreadCountWithExceptionItems.

@Test
public void testSingleValueCheckThreadCountWithExceptionItems() {
    final String resourceName = "testSingleValueCheckThreadCountWithExceptionItems";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 0;
    long globalThreshold = 5L;
    int thresholdB = 3;
    int thresholdD = 7;
    ParamFlowRule rule = new ParamFlowRule(resourceName).setCount(globalThreshold).setParamIdx(paramIdx).setGrade(RuleConstant.FLOW_GRADE_THREAD);
    String valueA = "valueA";
    String valueB = "valueB";
    String valueC = "valueC";
    String valueD = "valueD";
    // Directly set parsed map for test.
    Map<Object, Integer> map = new HashMap<Object, Integer>();
    map.put(valueB, thresholdB);
    map.put(valueD, thresholdD);
    rule.setParsedHotItems(map);
    ParameterMetric metric = mock(ParameterMetric.class);
    when(metric.getThreadCount(paramIdx, valueA)).thenReturn(globalThreshold - 1);
    when(metric.getThreadCount(paramIdx, valueB)).thenReturn(globalThreshold - 1);
    when(metric.getThreadCount(paramIdx, valueC)).thenReturn(globalThreshold - 1);
    when(metric.getThreadCount(paramIdx, valueD)).thenReturn(globalThreshold + 1);
    ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueB));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueC));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueD));
    when(metric.getThreadCount(paramIdx, valueA)).thenReturn(globalThreshold);
    when(metric.getThreadCount(paramIdx, valueB)).thenReturn(thresholdB - 1L);
    when(metric.getThreadCount(paramIdx, valueC)).thenReturn(globalThreshold + 1);
    when(metric.getThreadCount(paramIdx, valueD)).thenReturn(globalThreshold - 1).thenReturn((long) thresholdD);
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueB));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueC));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueD));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueD));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 20 with ResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.

the class ParamFlowCheckerTest method testPassLocalCheckForCollection.

@Test
public void testPassLocalCheckForCollection() throws InterruptedException {
    final String resourceName = "testPassLocalCheckForCollection";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 0;
    double globalThreshold = 1;
    ParamFlowRule rule = new ParamFlowRule(resourceName).setParamIdx(paramIdx).setCount(globalThreshold);
    String v1 = "a", v2 = "B", v3 = "Cc";
    List<String> list = Arrays.asList(v1, v2, v3);
    ParameterMetric metric = new ParameterMetric();
    ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
    metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
    metric.getRuleTokenCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
    assertTrue(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, list));
    assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, list));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Aggregations

ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)42 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)31 Test (org.junit.Test)26 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)10 Context (com.alibaba.csp.sentinel.context.Context)7 Node (com.alibaba.csp.sentinel.node.Node)7 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)6 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)6 Entry (com.alibaba.csp.sentinel.Entry)5 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)5 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)5 HashMap (java.util.HashMap)5 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)2 AsyncEntry (com.alibaba.csp.sentinel.AsyncEntry)1 MetricNode (com.alibaba.csp.sentinel.node.metric.MetricNode)1 MethodResourceWrapper (com.alibaba.csp.sentinel.slotchain.MethodResourceWrapper)1