Java Code Examples for com.alibaba.csp.sentinel.node.DefaultNode#getClusterNode()

The following examples show how to use com.alibaba.csp.sentinel.node.DefaultNode#getClusterNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlowRuleChecker.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
static Node selectNodeByRequesterAndStrategy(/*@NonNull*/ FlowRule rule, Context context, DefaultNode node) {
    // The limit app should not be empty.
    String limitApp = rule.getLimitApp();
    int strategy = rule.getStrategy();
    String origin = context.getOrigin();

    if (limitApp.equals(origin) && filterOrigin(origin)) {
        if (strategy == RuleConstant.STRATEGY_DIRECT) {
            // Matches limit origin, return origin statistic node.
            return context.getOriginNode();
        }

        return selectReferenceNode(rule, context, node);
    } else if (RuleConstant.LIMIT_APP_DEFAULT.equals(limitApp)) {
        if (strategy == RuleConstant.STRATEGY_DIRECT) {
            // Return the cluster node.
            return node.getClusterNode();
        }

        return selectReferenceNode(rule, context, node);
    } else if (RuleConstant.LIMIT_APP_OTHER.equals(limitApp)
        && FlowRuleManager.isOtherOrigin(origin, rule.getResource())) {
        if (strategy == RuleConstant.STRATEGY_DIRECT) {
            return context.getOriginNode();
        }

        return selectReferenceNode(rule, context, node);
    }

    return null;
}
 
Example 2
Source File: Tracer.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static void traceExceptionToNode(Throwable t, int count, Entry entry, DefaultNode curNode) {
    if (curNode == null) {
        return;
    }
    for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
        m.addException(entry.getResourceWrapper().getName(), count, t);
    }

    // clusterNode can be null when Constants.ON is false.
    ClusterNode clusterNode = curNode.getClusterNode();
    if (clusterNode == null) {
        return;
    }
    clusterNode.trace(t, count);
}
 
Example 3
Source File: FlowRuleChecker.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
static Node selectNodeByRequesterAndStrategy(/*@NonNull*/ FlowRule rule, Context context, DefaultNode node) {
    // The limit app should not be empty.
    String limitApp = rule.getLimitApp();
    int strategy = rule.getStrategy();
    String origin = context.getOrigin();

    if (limitApp.equals(origin) && filterOrigin(origin)) {
        if (strategy == RuleConstant.STRATEGY_DIRECT) {
            // Matches limit origin, return origin statistic node.
            return context.getOriginNode();
        }

        return selectReferenceNode(rule, context, node);
    } else if (RuleConstant.LIMIT_APP_DEFAULT.equals(limitApp)) {
        if (strategy == RuleConstant.STRATEGY_DIRECT) {
            // Return the cluster node.
            return node.getClusterNode();
        }

        return selectReferenceNode(rule, context, node);
    } else if (RuleConstant.LIMIT_APP_OTHER.equals(limitApp)
        && FlowRuleManager.isOtherOrigin(origin, rule.getResource())) {
        if (strategy == RuleConstant.STRATEGY_DIRECT) {
            return context.getOriginNode();
        }

        return selectReferenceNode(rule, context, node);
    }

    return null;
}
 
Example 4
Source File: SentinelDubboProviderFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
/**
 * 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 = DubboUtils.getResourceName(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.getType());

    // 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.getType());

    // 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.getType());

    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    assertNotNull(curEntry.getOriginNode());// As context origin is not "", originNode should be created

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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));
}
 
Example 5
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
/**
 * 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 = DubboUtils.getResourceName(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.getType());

    // 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.getType());

    // 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.getType());

    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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());
}
 
Example 6
Source File: SentinelDubboProviderFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
/**
 * 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.getResourceName(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.getType());

    // 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.getType());

    // 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.getType());

    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    assertNotNull(curEntry.getOriginNode());// As context origin is not "", originNode should be created

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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));
}
 
Example 7
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
/**
 * 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.getResourceName(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.getType());

    // 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.getType());

    // 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.getType());

    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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());
}
 
Example 8
Source File: SentinelDubboProviderFilterTest.java    From dubbo-sentinel-support with Apache License 2.0 4 votes vote down vote up
/**
 * 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 = DubboUtils.getResourceName(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.getType());

    // 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.getType());

    // 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.getType());

    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    assertNotNull(curEntry.getOriginNode());// As context origin is not "", originNode should be created

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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));
}
 
Example 9
Source File: SentinelDubboConsumerFilterTest.java    From dubbo-sentinel-support with Apache License 2.0 4 votes vote down vote up
/**
 * 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 = DubboUtils.getResourceName(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.getType());

    // 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.getType());

    // 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.getType());

    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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());
}
 
Example 10
Source File: SentinelDubboProviderFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
/**
 * Simply verify invocation structure in memory:
 * EntranceNode(methodResourceName)
 * --InterfaceNode(interfaceName)
 * ----MethodNode(methodResourceName)
 */
private void verifyInvocationStructure(String originApplication, Invoker invoker, Invocation invocation) {
    Context context = ContextUtil.getContext();
    assertNotNull(context);

    // As ContextUtil.enter(resourceName, application) in SentinelDubboProviderFilter
    String methodResourceName = filter.getMethodName(invoker, invocation);
    assertEquals(methodResourceName, context.getName());
    assertEquals(originApplication, context.getOrigin());

    DefaultNode entranceNode = context.getEntranceNode();
    ResourceWrapper entranceResource = entranceNode.getId();
    assertEquals(methodResourceName, 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(filter.getInterfaceName(invoker), 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(methodResourceName, methodResource.getName());
    assertSame(EntryType.IN, methodResource.getEntryType());

    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    assertNotNull(curEntry.getOriginNode());// As context origin is not "", originNode should be created

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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));
}
 
Example 11
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
/**
 * 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 = consumerFilter.getMethodName(invoker, invocation);
    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(1, 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(1, childList.size());
    DefaultNode methodNode = getNode(resourceName, entranceNode);
    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());
    assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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());
}
 
Example 12
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
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);
    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, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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());
}
 
Example 13
Source File: SentinelSofaRpcProviderFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
/**
 * 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));
}
 
Example 14
Source File: SentinelSofaRpcConsumerFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
/**
 * Verify Sentinel invocation structure in memory:
 * EntranceNode(defaultContextName)
 * --InterfaceNode(interfaceName)
 * ----MethodNode(resourceName)
 */
private void verifyInvocationStructure(String interfaceResourceName, String methodResourceName) {
    Context context = ContextUtil.getContext();
    assertNotNull(context);

    // As not call ContextUtil.enter(methodResourceName, applicationName) in SentinelSofaRpcConsumerFilter, use default context
    // In actual project, a consumer is usually also a provider, the context will be created by SentinelSofaRpcProviderFilter
    // If consumer is on the top of SOFARPC invocation chain, use default context
    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(interfaceResourceName, EntryType.OUT);
    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.OUT, interfaceResource.getEntryType());

    // As SphU.entry(methodResourceName, EntryType.OUT);
    childList = interfaceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode methodNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper methodResource = methodNode.getId();
    assertEquals(methodResourceName, 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());
}
 
Example 15
Source File: SentinelDubboProviderFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
/**
 * 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.getResourceName(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());
    assertNotNull(curEntry.getOriginNode());// As context origin is not "", originNode should be created

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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));
}
 
Example 16
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
/**
 * 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.getResourceName(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());
    assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry

    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode

    // 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());
}