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