Search in sources :

Example 41 with ResourceWrapper

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

the class SentinelSofaRpcConsumerFilterTest method verifyInvocationStructure.

/**
 * 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());
}
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 42 with ResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project skywalking-java by apache.

the class SentinelCtEntryConstructorInterceptor method onConstruct.

@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    if (!(objInst instanceof AsyncEntry)) {
        ResourceWrapper resourceWrapper = (ResourceWrapper) allArguments[0];
        AbstractSpan activeSpan = ContextManager.createLocalSpan("Sentinel/" + resourceWrapper.getName());
        activeSpan.setComponent(ComponentsDefine.SENTINEL);
        objInst.setSkyWalkingDynamicField(activeSpan);
        ContextManager.getRuntimeContext().put(SENTINEL_SPAN, activeSpan);
    }
}
Also used : ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) AsyncEntry(com.alibaba.csp.sentinel.AsyncEntry) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Aggregations

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