Search in sources :

Example 1 with StatisticNode

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

the class SentinelSofaRpcProviderFilterTest method verifyInvocationStructure.

/**
 * Verify Sentinel invocation structure in memory:
 * EntranceNode(methodResourceName)
 * --InterfaceNode(interfaceResourceName)
 * ----MethodNode(methodResourceName)
 */
private void verifyInvocationStructure(String applicationName, String interfaceResourceName, String methodResourceName) {
    Context context = ContextUtil.getContext();
    assertNotNull(context);
    assertEquals(methodResourceName, context.getName());
    assertEquals(applicationName, context.getOrigin());
    DefaultNode entranceNode = context.getEntranceNode();
    ResourceWrapper entranceResource = entranceNode.getId();
    assertEquals(methodResourceName, entranceResource.getName());
    assertSame(EntryType.IN, entranceResource.getEntryType());
    // As SphU.entry(interfaceResourceName, EntryType.IN);
    Set<Node> childList = entranceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode interfaceNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper interfaceResource = interfaceNode.getId();
    assertEquals(interfaceResourceName, interfaceResource.getName());
    assertSame(EntryType.IN, interfaceResource.getEntryType());
    // As SphU.entry(methodResourceName, EntryType.IN, 1, methodArguments);
    childList = interfaceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode methodNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper methodResource = methodNode.getId();
    assertEquals(methodResourceName, methodResource.getName());
    assertSame(EntryType.IN, methodResource.getEntryType());
    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    // As context origin is not "", originNode should be created
    assertNotNull(curEntry.getOriginNode());
    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    // Different resource->Different ProcessorSlot->Different ClusterNode
    assertNotSame(methodClusterNode, interfaceClusterNode);
    // As context origin is not "", the StatisticNode should be created in originCountMap of ClusterNode
    Map<String, StatisticNode> methodOriginCountMap = methodClusterNode.getOriginCountMap();
    assertEquals(1, methodOriginCountMap.size());
    assertTrue(methodOriginCountMap.containsKey(applicationName));
    Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap();
    assertEquals(1, interfaceOriginCountMap.size());
    assertTrue(interfaceOriginCountMap.containsKey(applicationName));
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Entry(com.alibaba.csp.sentinel.Entry) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode)

Example 2 with StatisticNode

use of com.alibaba.csp.sentinel.node.StatisticNode 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 3 with StatisticNode

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

the class SentinelDubboConsumerFilterTest method verifyInvocationStructure.

/**
 * Simply verify invocation structure in memory:
 * EntranceNode(defaultContextName)
 * --InterfaceNode(interfaceName)
 * ----MethodNode(resourceName)
 */
private void verifyInvocationStructure(Invoker invoker, Invocation invocation) {
    Context context = ContextUtil.getContext();
    assertNotNull(context);
    // As not call ContextUtil.enter(resourceName, application) in SentinelDubboConsumerFilter, use default context
    // In actual project, a consumer is usually also a provider, the context will be created by SentinelDubboProviderFilter
    // If consumer is on the top of Dubbo RPC invocation chain, use default context
    String resourceName = filter.getMethodResourceName(invoker, invocation);
    assertEquals(Constants.CONTEXT_DEFAULT_NAME, context.getName());
    assertEquals("", context.getOrigin());
    DefaultNode entranceNode = context.getEntranceNode();
    ResourceWrapper entranceResource = entranceNode.getId();
    assertEquals(Constants.CONTEXT_DEFAULT_NAME, entranceResource.getName());
    assertSame(EntryType.IN, entranceResource.getEntryType());
    // As SphU.entry(interfaceName, EntryType.OUT);
    Set<Node> childList = entranceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode interfaceNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper interfaceResource = interfaceNode.getId();
    assertEquals(DemoService.class.getName(), interfaceResource.getName());
    assertSame(EntryType.OUT, interfaceResource.getEntryType());
    // As SphU.entry(resourceName, EntryType.OUT);
    childList = interfaceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode methodNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper methodResource = methodNode.getId();
    assertEquals(resourceName, methodResource.getName());
    assertSame(EntryType.OUT, methodResource.getEntryType());
    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    // As context origin is not "", no originNode should be created in curEntry
    assertNull(curEntry.getOriginNode());
    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    // Different resource->Different ProcessorSlot->Different ClusterNode
    assertNotSame(methodClusterNode, interfaceClusterNode);
    // As context origin is "", the StatisticNode should not be created in originCountMap of ClusterNode
    Map<String, StatisticNode> methodOriginCountMap = methodClusterNode.getOriginCountMap();
    assertEquals(0, methodOriginCountMap.size());
    Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap();
    assertEquals(0, interfaceOriginCountMap.size());
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Entry(com.alibaba.csp.sentinel.Entry) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Node(com.alibaba.csp.sentinel.node.Node) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) DemoService(com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode)

Example 4 with StatisticNode

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

the class SentinelDubboProviderFilterTest method verifyInvocationStructure.

/**
 * Simply verify invocation structure in memory:
 * EntranceNode(resourceName)
 * --InterfaceNode(interfaceName)
 * ----MethodNode(resourceName)
 */
private void verifyInvocationStructure(String originApplication, Invoker invoker, Invocation invocation) {
    Context context = ContextUtil.getContext();
    assertNotNull(context);
    // As ContextUtil.enter(resourceName, application) in SentinelDubboProviderFilter
    String resourceName = filter.getMethodResourceName(invoker, invocation);
    assertEquals(resourceName, context.getName());
    assertEquals(originApplication, context.getOrigin());
    DefaultNode entranceNode = context.getEntranceNode();
    ResourceWrapper entranceResource = entranceNode.getId();
    assertEquals(resourceName, entranceResource.getName());
    assertSame(EntryType.IN, entranceResource.getEntryType());
    // As SphU.entry(interfaceName, EntryType.IN);
    Set<Node> childList = entranceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode interfaceNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper interfaceResource = interfaceNode.getId();
    assertEquals(DemoService.class.getName(), interfaceResource.getName());
    assertSame(EntryType.IN, interfaceResource.getEntryType());
    // As SphU.entry(resourceName, EntryType.IN, 1, invocation.getArguments());
    childList = interfaceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode methodNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper methodResource = methodNode.getId();
    assertEquals(resourceName, methodResource.getName());
    assertSame(EntryType.IN, methodResource.getEntryType());
    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    // As context origin is not "", originNode should be created
    assertNotNull(curEntry.getOriginNode());
    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    // Different resource->Different ProcessorSlot->Different ClusterNode
    assertNotSame(methodClusterNode, interfaceClusterNode);
    // As context origin is not "", the StatisticNode should be created in originCountMap of ClusterNode
    Map<String, StatisticNode> methodOriginCountMap = methodClusterNode.getOriginCountMap();
    assertEquals(1, methodOriginCountMap.size());
    assertTrue(methodOriginCountMap.containsKey(originApplication));
    Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap();
    assertEquals(1, interfaceOriginCountMap.size());
    assertTrue(interfaceOriginCountMap.containsKey(originApplication));
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Entry(com.alibaba.csp.sentinel.Entry) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) DemoService(com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode)

Example 5 with StatisticNode

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

the class SentinelDubboConsumerFilterTest method verifyInvocationStructureForAsyncCall.

private void verifyInvocationStructureForAsyncCall(Invoker invoker, Invocation invocation) {
    Context context = ContextUtil.getContext();
    assertNotNull(context);
    // As not call ContextUtil.enter(resourceName, application) in SentinelDubboConsumerFilter, use default context
    // In actual project, a consumer is usually also a provider, the context will be created by
    // SentinelDubboProviderFilter
    // If consumer is on the top of Dubbo RPC invocation chain, use default context
    String resourceName = consumerFilter.getMethodName(invoker, invocation, null);
    assertEquals(com.alibaba.csp.sentinel.Constants.CONTEXT_DEFAULT_NAME, context.getName());
    assertEquals("", context.getOrigin());
    DefaultNode entranceNode = context.getEntranceNode();
    ResourceWrapper entranceResource = entranceNode.getId();
    assertEquals(com.alibaba.csp.sentinel.Constants.CONTEXT_DEFAULT_NAME, entranceResource.getName());
    assertSame(EntryType.IN, entranceResource.getEntryType());
    // As SphU.entry(interfaceName, EntryType.OUT);
    Set<Node> childList = entranceNode.getChildList();
    assertEquals(2, childList.size());
    DefaultNode interfaceNode = getNode(DubboUtils.getInterfaceName(invoker), entranceNode);
    ResourceWrapper interfaceResource = interfaceNode.getId();
    assertEquals(DubboUtils.getInterfaceName(invoker), interfaceResource.getName());
    assertSame(EntryType.OUT, interfaceResource.getEntryType());
    // As SphU.entry(resourceName, EntryType.OUT);
    childList = interfaceNode.getChildList();
    assertEquals(0, childList.size());
    DefaultNode methodNode = getNode(resourceName, entranceNode);
    ResourceWrapper methodResource = methodNode.getId();
    assertEquals(resourceName, methodResource.getName());
    assertSame(EntryType.OUT, methodResource.getEntryType());
    // Verify curEntry
    // nothing will bind to local context when use the AsyncEntry
    Entry curEntry = context.getCurEntry();
    assertNull(curEntry);
    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, // Different resource->Different ProcessorSlot->Different ClusterNode
    interfaceClusterNode);
    // As context origin is "", the StatisticNode should not be created in originCountMap of ClusterNode
    Map<String, StatisticNode> methodOriginCountMap = methodClusterNode.getOriginCountMap();
    assertEquals(0, methodOriginCountMap.size());
    Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap();
    assertEquals(0, interfaceOriginCountMap.size());
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Entry(com.alibaba.csp.sentinel.Entry) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Node(com.alibaba.csp.sentinel.node.Node) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode)

Aggregations

ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)6 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)6 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)6 Entry (com.alibaba.csp.sentinel.Entry)5 Context (com.alibaba.csp.sentinel.context.Context)5 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)5 Node (com.alibaba.csp.sentinel.node.Node)5 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)2