Search in sources :

Example 1 with DefaultNode

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

the class NodeSelectorSlot method entry.

@Override
public void entry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, Object... args) throws Throwable {
    /*
         * It's interesting that we use context name rather resource name as the map key.
         *
         * Remember that same resource({@link ResourceWrapper#equals(Object)}) will share
         * the same {@link ProcessorSlotChain} globally, no matter in which context. So if
         * code goes into {@link #entry(Context, ResourceWrapper, DefaultNode, int, Object...)},
         * the resource name must be same but context name may not.
         *
         * If we use {@link com.alibaba.csp.sentinel.SphU#entry(String resource)} to
         * enter same resource in different context, using context name as map key can
         * distinguish the same resource. In this case, multiple {@link DefaultNode}s will be created
         * of the same resource name, for every distinct context (different context name) each.
         *
         * Consider another question. One resource may have multiple {@link DefaultNode},
         * so what is the fastest way to get total statistics of the same resource?
         * The answer is all {@link DefaultNode}s with same resource name share one
         * {@link ClusterNode}. See {@link ClusterBuilderSlot} for detail.
         */
    DefaultNode node = map.get(context.getName());
    if (node == null) {
        synchronized (this) {
            node = map.get(context.getName());
            if (node == null) {
                node = new DefaultNode(resourceWrapper, null);
                HashMap<String, DefaultNode> cacheMap = new HashMap<String, DefaultNode>(map.size());
                cacheMap.putAll(map);
                cacheMap.put(context.getName(), node);
                map = cacheMap;
                // Build invocation tree
                ((DefaultNode) context.getLastNode()).addChild(node);
            }
        }
    }
    context.setCurNode(node);
    fireEntry(context, resourceWrapper, node, count, prioritized, args);
}
Also used : HashMap(java.util.HashMap) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode)

Example 2 with DefaultNode

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

the class AsyncEntryIntegrationTest method queryInvocationTree.

private DefaultNode queryInvocationTree(boolean check) {
    DefaultNode root = Constants.ROOT;
    DefaultNode entranceNode = shouldHasChildFor(root, contextName, check);
    DefaultNode testTopNode = shouldHasChildFor(entranceNode, "test-top", check);
    DefaultNode testAsyncNode = shouldHasChildFor(testTopNode, "test-async", check);
    shouldHasChildFor(testTopNode, "test-sync", check);
    shouldHasChildFor(testAsyncNode, "test-sync-in-async", check);
    DefaultNode anotherAsyncInAsyncNode = shouldHasChildFor(testAsyncNode, "test-another-async", check);
    return shouldHasChildFor(anotherAsyncInAsyncNode, "test-another-in-async", check);
}
Also used : DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode)

Example 3 with DefaultNode

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

the class SentinelSofaRpcProviderFilterTest method verifyInvocationStructure.

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

Example 4 with DefaultNode

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

the class FetchJsonTreeCommandHandler method visit.

/**
 * Preorder traversal.
 */
private void visit(DefaultNode node, List<NodeVo> results, String parentId) {
    NodeVo vo = NodeVo.fromDefaultNode(node, parentId);
    results.add(vo);
    String id = vo.getId();
    for (Node n : node.getChildList()) {
        visit((DefaultNode) n, results, id);
    }
}
Also used : DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) NodeVo(com.alibaba.csp.sentinel.command.vo.NodeVo)

Example 5 with DefaultNode

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

the class FlowRuleCheckerTest method testCustomOriginFlowSelectNode.

@Test
public void testCustomOriginFlowSelectNode() {
    String origin = "appA";
    String limitAppB = "appB";
    DefaultNode node = mock(DefaultNode.class);
    DefaultNode originNode = mock(DefaultNode.class);
    ClusterNode cn = mock(ClusterNode.class);
    when(node.getClusterNode()).thenReturn(cn);
    Context context = mock(Context.class);
    when(context.getOrigin()).thenReturn(origin);
    when(context.getOriginNode()).thenReturn(originNode);
    FlowRule rule = new FlowRule("testCustomOriginFlowSelectNode").setCount(1);
    rule.setLimitApp(origin);
    // Origin matches, return the origin node.
    assertEquals(originNode, FlowRuleChecker.selectNodeByRequesterAndStrategy(rule, context, node));
    rule.setLimitApp(limitAppB);
    // Origin mismatch, no node found.
    assertNull(FlowRuleChecker.selectNodeByRequesterAndStrategy(rule, context, node));
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Context(com.alibaba.csp.sentinel.context.Context) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Aggregations

DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)27 Context (com.alibaba.csp.sentinel.context.Context)15 Node (com.alibaba.csp.sentinel.node.Node)13 Test (org.junit.Test)13 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)12 Entry (com.alibaba.csp.sentinel.Entry)9 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)8 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)6 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)5 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)5 HashMap (java.util.HashMap)3 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 NodeVo (com.alibaba.csp.sentinel.command.vo.NodeVo)1 SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)1