Java Code Examples for com.alibaba.csp.sentinel.context.Context#getCurEntry()

The following examples show how to use com.alibaba.csp.sentinel.context.Context#getCurEntry() . 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: AsyncEntry.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
/**
 * Remove current entry from local context, but does not exit.
 */
void cleanCurrentEntryInLocal() {
    if (context instanceof NullContext) {
        return;
    }
    Context originalContext = context;
    if (originalContext != null) {
        Entry curEntry = originalContext.getCurEntry();
        if (curEntry == this) {
            Entry parent = this.parent;
            originalContext.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry)parent).child = null;
            }
        } else {
            String curEntryName = curEntry == null ? "none"
                : curEntry.resourceWrapper.getName() + "@" + curEntry.hashCode();
            String msg = String.format("Bad async context state, expected entry: %s, but actual: %s",
                getResourceWrapper().getName() + "@" + hashCode(), curEntryName);
            throw new IllegalStateException(msg);
        }
    }
}
 
Example 2
Source File: AsyncEntry.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
/**
 * Remove current entry from local context, but does not exit.
 */
void cleanCurrentEntryInLocal() {
    if (context instanceof NullContext) {
        return;
    }
    Context originalContext = context;
    if (originalContext != null) {
        Entry curEntry = originalContext.getCurEntry();
        if (curEntry == this) {
            Entry parent = this.parent;
            originalContext.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry)parent).child = null;
            }
        } else {
            throw new IllegalStateException("Bad async context state");
        }
    }
}
 
Example 3
Source File: CtEntry.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
protected void exitForContext(Context context, int count, Object... args) throws ErrorEntryFreeException {
    if (context != null) {
        // Null context should exit without clean-up.
        if (context instanceof NullContext) {
            return;
        }
        if (context.getCurEntry() != this) {
            String curEntryNameInContext = context.getCurEntry() == null ? null : context.getCurEntry().getResourceWrapper().getName();
            // Clean previous call stack.
            CtEntry e = (CtEntry)context.getCurEntry();
            while (e != null) {
                e.exit(count, args);
                e = (CtEntry)e.parent;
            }
            String errorMessage = String.format("The order of entry exit can't be paired with the order of entry"
                + ", current entry in context: <%s>, but expected: <%s>", curEntryNameInContext, resourceWrapper.getName());
            throw new ErrorEntryFreeException(errorMessage);
        } else {
            if (chain != null) {
                chain.exit(context, resourceWrapper, count, args);
            }
            // Restore the call stack.
            context.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry)parent).child = null;
            }
            if (parent == null) {
                // Default context (auto entered) will be exited automatically.
                if (ContextUtil.isDefaultContext(context)) {
                    ContextUtil.exit();
                }
            }
            // Clean the reference of context in current entry to avoid duplicate exit.
            clearEntryContext();
        }
    }
}
 
Example 4
Source File: CtEntry.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void setUpEntryFor(Context context) {
    // The entry should not be associated to NullContext.
    if (context instanceof NullContext) {
        return;
    }
    this.parent = context.getCurEntry();
    if (parent != null) {
        ((CtEntry)parent).child = this;
    }
    context.setCurEntry(this);
}
 
Example 5
Source File: CtEntry.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void setUpEntryFor(Context context) {
    // The entry should not be associated to NullContext.
    if (context instanceof NullContext) {
        return;
    }
    this.parent = context.getCurEntry();
    if (parent != null) {
        ((CtEntry)parent).child = this;
    }
    context.setCurEntry(this);
}
 
Example 6
Source File: CtEntry.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
protected void exitForContext(Context context, int count, Object... args) throws ErrorEntryFreeException {
    if (context != null) {
        // Null context should exit without clean-up.
        if (context instanceof NullContext) {
            return;
        }
        if (context.getCurEntry() != this) {
            String curEntryNameInContext = context.getCurEntry() == null ? null : context.getCurEntry().getResourceWrapper().getName();
            // Clean previous call stack.
            CtEntry e = (CtEntry)context.getCurEntry();
            while (e != null) {
                e.exit(count, args);
                e = (CtEntry)e.parent;
            }
            String errorMessage = String.format("The order of entry exit can't be paired with the order of entry"
                + ", current entry in context: <%s>, but expected: <%s>", curEntryNameInContext, resourceWrapper.getName());
            throw new ErrorEntryFreeException(errorMessage);
        } else {
            if (chain != null) {
                chain.exit(context, resourceWrapper, count, args);
            }
            // Restore the call stack.
            context.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry)parent).child = null;
            }
            if (parent == null) {
                // Default context (auto entered) will be exited automatically.
                if (ContextUtil.isDefaultContext(context)) {
                    ContextUtil.exit();
                }
            }
            // Clean the reference of context in current entry to avoid duplicate exit.
            clearEntryContext();
        }
    }
}
 
Example 7
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 8
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());
}
 
Example 9
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 10
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 11
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 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: 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 14
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 15
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 16
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 17
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 18
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 19
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());
}