Search in sources :

Example 11 with DefaultNode

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

the class NodeSelectorTest method testMultipleLayer.

// @Test
public void testMultipleLayer() throws Exception {
    // TODO: fix this
    ContextUtil.enter("entry1", "appA");
    Entry nodeA = SphU.entry("nodeA");
    assertSame(ContextUtil.getContext().getCurEntry(), nodeA);
    DefaultNode dnA = (DefaultNode) nodeA.getCurNode();
    assertNotNull(dnA);
    assertSame("nodeA", dnA.getId().getName());
    Entry nodeB = SphU.entry("nodeB");
    assertSame(ContextUtil.getContext().getCurEntry(), nodeB);
    DefaultNode dnB = (DefaultNode) nodeB.getCurNode();
    assertNotNull(dnB);
    assertTrue(dnA.getChildList().contains(dnB));
    Entry nodeC = SphU.entry("nodeC");
    assertSame(ContextUtil.getContext().getCurEntry(), nodeC);
    DefaultNode dnC = (DefaultNode) nodeC.getCurNode();
    assertNotNull(dnC);
    assertTrue(dnB.getChildList().contains(dnC));
    if (nodeC != null) {
        nodeC.exit();
    }
    assertSame(ContextUtil.getContext().getCurEntry(), nodeB);
    if (nodeB != null) {
        nodeB.exit();
    }
    assertSame(ContextUtil.getContext().getCurEntry(), nodeA);
    if (nodeA != null) {
        nodeA.exit();
    }
    assertNull(ContextUtil.getContext().getCurEntry());
    ContextUtil.exit();
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode)

Example 12 with DefaultNode

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

the class ContextUtil method trueEnter.

protected static Context trueEnter(String name, String origin) {
    Context context = contextHolder.get();
    if (context == null) {
        Map<String, DefaultNode> localCacheNameMap = contextNameNodeMap;
        DefaultNode node = localCacheNameMap.get(name);
        if (node == null) {
            if (localCacheNameMap.size() > Constants.MAX_CONTEXT_NAME_SIZE) {
                setNullContext();
                return NULL_CONTEXT;
            } else {
                LOCK.lock();
                try {
                    node = contextNameNodeMap.get(name);
                    if (node == null) {
                        if (contextNameNodeMap.size() > Constants.MAX_CONTEXT_NAME_SIZE) {
                            setNullContext();
                            return NULL_CONTEXT;
                        } else {
                            node = new EntranceNode(new StringResourceWrapper(name, EntryType.IN), null);
                            // Add entrance node.
                            Constants.ROOT.addChild(node);
                            Map<String, DefaultNode> newMap = new HashMap<>(contextNameNodeMap.size() + 1);
                            newMap.putAll(contextNameNodeMap);
                            newMap.put(name, node);
                            contextNameNodeMap = newMap;
                        }
                    }
                } finally {
                    LOCK.unlock();
                }
            }
        }
        context = new Context(node, name);
        context.setOrigin(origin);
        contextHolder.set(context);
    }
    return context;
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) HashMap(java.util.HashMap) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode)

Example 13 with DefaultNode

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

the class FetchTreeCommandHandler method visitTree.

private void visitTree(int level, DefaultNode node, /*@NonNull*/
StringBuilder sb) {
    for (int i = 0; i < level; ++i) {
        sb.append("-");
    }
    if (!(node instanceof EntranceNode)) {
        sb.append(String.format("%s(t:%s pq:%s bq:%s tq:%s rt:%s prq:%s 1mp:%s 1mb:%s 1mt:%s)", node.getId().getShowName(), node.curThreadNum(), node.passQps(), node.blockQps(), node.totalQps(), node.avgRt(), node.successQps(), node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest())).append("\n");
    } else {
        sb.append(String.format("EntranceNode: %s(t:%s pq:%s bq:%s tq:%s rt:%s prq:%s 1mp:%s 1mb:%s 1mt:%s)", node.getId().getShowName(), node.curThreadNum(), node.passQps(), node.blockQps(), node.totalQps(), node.avgRt(), node.successQps(), node.totalRequest() - node.blockRequest(), node.blockRequest(), node.totalRequest())).append("\n");
    }
    for (Node n : node.getChildList()) {
        DefaultNode dn = (DefaultNode) n;
        visitTree(level + 1, dn, sb);
    }
}
Also used : EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode)

Example 14 with DefaultNode

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

the class FetchTreeCommandHandler method handle.

@Override
public CommandResponse<String> handle(CommandRequest request) {
    String id = request.getParam("id");
    StringBuilder sb = new StringBuilder();
    DefaultNode start = Constants.ROOT;
    if (id == null) {
        visitTree(0, start, sb);
    } else {
        boolean exactly = false;
        for (Node n : start.getChildList()) {
            DefaultNode dn = (DefaultNode) n;
            if (dn.getId().getName().equals(id)) {
                visitTree(0, dn, sb);
                exactly = true;
                break;
            }
        }
        if (!exactly) {
            for (Node n : start.getChildList()) {
                DefaultNode dn = (DefaultNode) n;
                if (dn.getId().getName().contains(id)) {
                    visitTree(0, dn, sb);
                }
            }
        }
    }
    sb.append("\r\n\r\n");
    sb.append("t:threadNum  pq:passQps  bq:blockQps  tq:totalQps  rt:averageRt  prq: passRequestQps 1mp:1m-pass " + "1mb:1m-block 1mt:1m-total").append("\r\n");
    return CommandResponse.ofSuccess(sb.toString());
}
Also used : DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode)

Example 15 with DefaultNode

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

the class ClusterBuilderSlot method entry.

@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable {
    if (clusterNode == null) {
        synchronized (lock) {
            if (clusterNode == null) {
                // Create the cluster node.
                clusterNode = new ClusterNode(resourceWrapper.getName(), resourceWrapper.getResourceType());
                HashMap<ResourceWrapper, ClusterNode> newMap = new HashMap<>(Math.max(clusterNodeMap.size(), 16));
                newMap.putAll(clusterNodeMap);
                newMap.put(node.getId(), clusterNode);
                clusterNodeMap = newMap;
            }
        }
    }
    node.setClusterNode(clusterNode);
    /*
         * if context origin is set, we should get or create a new {@link Node} of
         * the specific origin.
         */
    if (!"".equals(context.getOrigin())) {
        Node originNode = node.getClusterNode().getOrCreateOriginNode(context.getOrigin());
        context.getCurEntry().setOriginNode(originNode);
    }
    fireEntry(context, resourceWrapper, node, count, prioritized, args);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) HashMap(java.util.HashMap) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode)

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