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