Java Code Examples for com.alibaba.csp.sentinel.context.ContextUtil#getContext()
The following examples show how to use
com.alibaba.csp.sentinel.context.ContextUtil#getContext() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SentinelDubboConsumerFilterTest.java From dubbo-sentinel-support with Apache License 2.0 | 6 votes |
@Test public void testInvoke() { final Invoker invoker = mock(Invoker.class); when(invoker.getInterface()).thenReturn(DemoService.class); final Invocation invocation = mock(Invocation.class); Method method = DemoService.class.getMethods()[0]; when(invocation.getMethodName()).thenReturn(method.getName()); when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes()); final Result result = mock(Result.class); when(result.hasException()).thenReturn(false); when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> { verifyInvocationStructure(invoker, invocation); return result; }); filter.invoke(invoker, invocation); verify(invoker).invoke(invocation); Context context = ContextUtil.getContext(); assertNull(context); }
Example 2
Source File: CtEntryTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
@Test public void testExitTwoLastEntriesWithCustomContext() { String contextName = "context-rpc"; ContextUtil.enter(contextName); Context context = ContextUtil.getContext(); try { CtEntry entry1 = new CtEntry(new StringResourceWrapper("resA", EntryType.IN), null, context); entry1.exit(); assertEquals(context, ContextUtil.getContext()); CtEntry entry2 = new CtEntry(new StringResourceWrapper("resB", EntryType.IN), null, context); entry2.exit(); assertEquals(context, ContextUtil.getContext()); } finally { ContextUtil.exit(); assertNull(ContextUtil.getContext()); } }
Example 3
Source File: BaseTest.java From Sentinel with Apache License 2.0 | 6 votes |
/** * Clean up resources. */ protected static void cleanUpAll() { Context context = ContextUtil.getContext(); if (context != null) { context.setCurEntry(null); ContextUtil.exit(); } Constants.ROOT.removeChildList(); ClusterBuilderSlot.getClusterNodeMap().clear(); // Clear chainMap in CtSph try { Method resetChainMapMethod = CtSph.class.getDeclaredMethod("resetChainMap"); resetChainMapMethod.setAccessible(true); resetChainMapMethod.invoke(null); } catch (Exception e) { // Empty } }
Example 4
Source File: AsyncEntryTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Test(expected = IllegalStateException.class) public void testCleanCurrentEntryInLocalError() { final String contextName = "abc"; try { ContextUtil.enter(contextName); Context curContext = ContextUtil.getContext(); AsyncEntry entry = new AsyncEntry(new StringResourceWrapper("testCleanCurrentEntryInLocal", EntryType.OUT), null, curContext); entry.cleanCurrentEntryInLocal(); entry.cleanCurrentEntryInLocal(); } finally { ContextTestUtil.cleanUpContext(); } }
Example 5
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Test public void testInvokeAsync() { Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne(); Invoker invoker = DubboTestUtil.getDefaultMockInvoker(); when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString()); final Result result = mock(Result.class); when(result.hasException()).thenReturn(false); when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> { verifyInvocationStructureForAsyncCall(invoker, invocation); return result; }); consumerFilter.invoke(invoker, invocation); verify(invoker).invoke(invocation); Context context = ContextUtil.getContext(); assertNotNull(context); }
Example 6
Source File: AsyncEntryTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
@Test public void testCleanCurrentEntryInLocal() { final String contextName = "abc"; try { ContextUtil.enter(contextName); Context curContext = ContextUtil.getContext(); Entry previousEntry = new CtEntry(new StringResourceWrapper("entry-sync", EntryType.IN), null, curContext); AsyncEntry entry = new AsyncEntry(new StringResourceWrapper("testCleanCurrentEntryInLocal", EntryType.OUT), null, curContext); assertSame(entry, curContext.getCurEntry()); entry.cleanCurrentEntryInLocal(); assertNotSame(entry, curContext.getCurEntry()); assertSame(previousEntry, curContext.getCurEntry()); } finally { ContextTestUtil.cleanUpContext(); } }
Example 7
Source File: SentinelDubboProviderFilterTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testInvoke() { final String originApplication = "consumerA"; final Invoker invoker = mock(Invoker.class); when(invoker.getInterface()).thenReturn(DemoService.class); final Invocation invocation = mock(Invocation.class); Method method = DemoService.class.getMethods()[0]; when(invocation.getMethodName()).thenReturn(method.getName()); when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes()); when(invocation.getAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY, "")) .thenReturn(originApplication); final Result result = mock(Result.class); when(result.hasException()).thenReturn(false); when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> { verifyInvocationStructure(originApplication, invoker, invocation); return result; }); filter.invoke(invoker, invocation); verify(invoker).invoke(invocation); Context context = ContextUtil.getContext(); assertNull(context); }
Example 8
Source File: SentinelDubboProviderFilterTest.java From Sentinel with Apache License 2.0 | 5 votes |
@Test public void testInvoke() { final String originApplication = "consumerA"; URL url = DubboTestUtil.getDefaultTestURL(); url = url.addParameter(CommonConstants.SIDE_KEY, CommonConstants.PROVIDER_SIDE); Invoker invoker = DubboTestUtil.getMockInvoker(url, DemoService.class); Invocation invocation = DubboTestUtil.getMockInvocation(DemoService.class.getMethods()[0]); when(invocation.getAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY, "")) .thenReturn(originApplication); final Result result = mock(Result.class); when(result.hasException()).thenReturn(false); when(result.getException()).thenReturn(new Exception()); when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> { verifyInvocationStructure(originApplication, invoker, invocation); return result; }); filter.invoke(invoker, invocation); verify(invoker).invoke(invocation); Context context = ContextUtil.getContext(); assertNull(context); }
Example 9
Source File: CtEntryTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testExitNotMatchCurEntry() { String contextName = "context-rpc"; ContextUtil.enter(contextName); Context context = ContextUtil.getContext(); CtEntry entry1 = null; CtEntry entry2 = null; try { entry1 = new CtEntry(new StringResourceWrapper("res1", EntryType.IN), null, ContextUtil.getContext()); assertSame(entry1, context.getCurEntry()); entry2 = new CtEntry(new StringResourceWrapper("res2", EntryType.IN), null, ContextUtil.getContext()); assertSame(entry2, context.getCurEntry()); // Forget to exit for entry 2... // Directly exit for entry 1, then boom... entry1.exit(); } catch (ErrorEntryFreeException ex) { assertNotNull(entry1); assertNotNull(entry2); assertNull(entry1.context); assertNull(entry2.context); assertNull(context.getCurEntry()); return; } finally { ContextUtil.exit(); } fail("Mismatch entry-exit should throw an ErrorEntryFreeException"); }
Example 10
Source File: SentinelDubboProviderFilterTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testInvoke() { final String originApplication = "consumerA"; final Invoker invoker = mock(Invoker.class); when(invoker.getInterface()).thenReturn(DemoService.class); final Invocation invocation = mock(Invocation.class); Method method = DemoService.class.getMethods()[0]; when(invocation.getMethodName()).thenReturn(method.getName()); when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes()); when(invocation.getAttachment(DubboUtils.DUBBO_APPLICATION_KEY, "")).thenReturn(originApplication); final Result result = mock(Result.class); when(result.hasException()).thenReturn(false); when(invoker.invoke(invocation)).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { verifyInvocationStructure(originApplication, invoker, invocation); return result; } }); filter.invoke(invoker, invocation); verify(invoker).invoke(invocation); Context context = ContextUtil.getContext(); assertNull(context); }
Example 11
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 4 votes |
/** * Simply verify invocation structure in memory: * EntranceNode(defaultContextName) * --InterfaceNode(interfaceName) * ----MethodNode(resourceName) */ private void verifyInvocationStructure(Invoker invoker, Invocation invocation) { Context context = ContextUtil.getContext(); assertNotNull(context); // As not call ContextUtil.enter(resourceName, application) in SentinelDubboConsumerFilter, use default context // In actual project, a consumer is usually also a provider, the context will be created by SentinelDubboProviderFilter // If consumer is on the top of Dubbo RPC invocation chain, use default context String resourceName = consumerFilter.getMethodName(invoker, invocation); assertEquals(com.alibaba.csp.sentinel.Constants.CONTEXT_DEFAULT_NAME, context.getName()); assertEquals("", context.getOrigin()); DefaultNode entranceNode = context.getEntranceNode(); ResourceWrapper entranceResource = entranceNode.getId(); assertEquals(com.alibaba.csp.sentinel.Constants.CONTEXT_DEFAULT_NAME, entranceResource.getName()); assertSame(EntryType.IN, entranceResource.getEntryType()); // As SphU.entry(interfaceName, EntryType.OUT); Set<Node> childList = entranceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode interfaceNode = getNode(DubboUtils.getInterfaceName(invoker), entranceNode); ResourceWrapper interfaceResource = interfaceNode.getId(); assertEquals(DubboUtils.getInterfaceName(invoker), interfaceResource.getName()); assertSame(EntryType.OUT, interfaceResource.getEntryType()); // As SphU.entry(resourceName, EntryType.OUT); childList = interfaceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode methodNode = getNode(resourceName, entranceNode); ResourceWrapper methodResource = methodNode.getId(); assertEquals(resourceName, methodResource.getName()); assertSame(EntryType.OUT, methodResource.getEntryType()); // Verify curEntry Entry curEntry = context.getCurEntry(); assertSame(methodNode, curEntry.getCurNode()); assertSame(interfaceNode, curEntry.getLastNode()); assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry // Verify clusterNode ClusterNode methodClusterNode = methodNode.getClusterNode(); ClusterNode interfaceClusterNode = interfaceNode.getClusterNode(); assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode // 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()); }
Example 12
Source File: SentinelDubboConsumerFilterTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
/** * Simply verify invocation structure in memory: * EntranceNode(defaultContextName) * --InterfaceNode(interfaceName) * ----MethodNode(resourceName) */ private void verifyInvocationStructure(Invoker invoker, Invocation invocation) { Context context = ContextUtil.getContext(); assertNotNull(context); // As not call ContextUtil.enter(resourceName, application) in SentinelDubboConsumerFilter, use default context // In actual project, a consumer is usually also a provider, the context will be created by SentinelDubboProviderFilter // If consumer is on the top of Dubbo RPC invocation chain, use default context String resourceName = filter.getResourceName(invoker, invocation); 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.getType()); // As SphU.entry(interfaceName, EntryType.OUT); Set<Node> childList = entranceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode interfaceNode = (DefaultNode) childList.iterator().next(); ResourceWrapper interfaceResource = interfaceNode.getId(); assertEquals(DemoService.class.getName(), interfaceResource.getName()); assertSame(EntryType.OUT, interfaceResource.getType()); // As SphU.entry(resourceName, EntryType.OUT); childList = interfaceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode methodNode = (DefaultNode) childList.iterator().next(); ResourceWrapper methodResource = methodNode.getId(); assertEquals(resourceName, methodResource.getName()); assertSame(EntryType.OUT, methodResource.getType()); // Verify curEntry Entry curEntry = context.getCurEntry(); assertSame(methodNode, curEntry.getCurNode()); assertSame(interfaceNode, curEntry.getLastNode()); assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry // Verify clusterNode ClusterNode methodClusterNode = methodNode.getClusterNode(); ClusterNode interfaceClusterNode = interfaceNode.getClusterNode(); assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode // 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()); }
Example 13
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 4 votes |
/** * Simply verify invocation structure in memory: * EntranceNode(defaultContextName) * --InterfaceNode(interfaceName) * ----MethodNode(resourceName) */ private void verifyInvocationStructure(Invoker invoker, Invocation invocation) { Context context = ContextUtil.getContext(); assertNotNull(context); // As not call ContextUtil.enter(resourceName, application) in SentinelDubboConsumerFilter, use default context // In actual project, a consumer is usually also a provider, the context will be created by SentinelDubboProviderFilter // If consumer is on the top of Dubbo RPC invocation chain, use default context String resourceName = filter.getResourceName(invoker, invocation); 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(interfaceName, EntryType.OUT); Set<Node> childList = entranceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode interfaceNode = (DefaultNode) childList.iterator().next(); ResourceWrapper interfaceResource = interfaceNode.getId(); assertEquals(DemoService.class.getName(), interfaceResource.getName()); assertSame(EntryType.OUT, interfaceResource.getEntryType()); // As SphU.entry(resourceName, EntryType.OUT); childList = interfaceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode methodNode = (DefaultNode) childList.iterator().next(); ResourceWrapper methodResource = methodNode.getId(); assertEquals(resourceName, methodResource.getName()); assertSame(EntryType.OUT, methodResource.getEntryType()); // Verify curEntry Entry curEntry = context.getCurEntry(); assertSame(methodNode, curEntry.getCurNode()); assertSame(interfaceNode, curEntry.getLastNode()); assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry // Verify clusterNode ClusterNode methodClusterNode = methodNode.getClusterNode(); ClusterNode interfaceClusterNode = interfaceNode.getClusterNode(); assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode // 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()); }
Example 14
Source File: SentinelDubboConsumerFilterTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
/** * Simply verify invocation structure in memory: * EntranceNode(defaultContextName) * --InterfaceNode(interfaceName) * ----MethodNode(resourceName) */ private void verifyInvocationStructure(Invoker invoker, Invocation invocation) { Context context = ContextUtil.getContext(); assertNotNull(context); // As not call ContextUtil.enter(resourceName, application) in SentinelDubboConsumerFilter, use default context // In actual project, a consumer is usually also a provider, the context will be created by SentinelDubboProviderFilter // If consumer is on the top of Dubbo RPC invocation chain, use default context String resourceName = DubboUtils.getResourceName(invoker, invocation); 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.getType()); // As SphU.entry(interfaceName, EntryType.OUT); Set<Node> childList = entranceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode interfaceNode = (DefaultNode) childList.iterator().next(); ResourceWrapper interfaceResource = interfaceNode.getId(); assertEquals(DemoService.class.getName(), interfaceResource.getName()); assertSame(EntryType.OUT, interfaceResource.getType()); // As SphU.entry(resourceName, EntryType.OUT); childList = interfaceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode methodNode = (DefaultNode) childList.iterator().next(); ResourceWrapper methodResource = methodNode.getId(); assertEquals(resourceName, methodResource.getName()); assertSame(EntryType.OUT, methodResource.getType()); // Verify curEntry Entry curEntry = context.getCurEntry(); assertSame(methodNode, curEntry.getCurNode()); assertSame(interfaceNode, curEntry.getLastNode()); assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry // Verify clusterNode ClusterNode methodClusterNode = methodNode.getClusterNode(); ClusterNode interfaceClusterNode = interfaceNode.getClusterNode(); assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode // 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()); }
Example 15
Source File: SentinelDubboProviderFilterTest.java From Sentinel with Apache License 2.0 | 4 votes |
/** * Simply verify invocation structure in memory: * EntranceNode(methodResourceName) * --InterfaceNode(interfaceName) * ----MethodNode(methodResourceName) */ private void verifyInvocationStructure(String originApplication, Invoker invoker, Invocation invocation) { Context context = ContextUtil.getContext(); assertNotNull(context); // As ContextUtil.enter(resourceName, application) in SentinelDubboProviderFilter String methodResourceName = filter.getMethodName(invoker, invocation); assertEquals(methodResourceName, context.getName()); assertEquals(originApplication, context.getOrigin()); DefaultNode entranceNode = context.getEntranceNode(); ResourceWrapper entranceResource = entranceNode.getId(); assertEquals(methodResourceName, entranceResource.getName()); assertSame(EntryType.IN, entranceResource.getEntryType()); // As SphU.entry(interfaceName, EntryType.IN); Set<Node> childList = entranceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode interfaceNode = (DefaultNode) childList.iterator().next(); ResourceWrapper interfaceResource = interfaceNode.getId(); assertEquals(filter.getInterfaceName(invoker), interfaceResource.getName()); assertSame(EntryType.IN, interfaceResource.getEntryType()); // As SphU.entry(resourceName, EntryType.IN, 1, invocation.getArguments()); 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()); assertNotNull(curEntry.getOriginNode());// As context origin is not "", originNode should be created // Verify clusterNode ClusterNode methodClusterNode = methodNode.getClusterNode(); ClusterNode interfaceClusterNode = interfaceNode.getClusterNode(); assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode // 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(originApplication)); Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap(); assertEquals(1, interfaceOriginCountMap.size()); assertTrue(interfaceOriginCountMap.containsKey(originApplication)); }
Example 16
Source File: SentinelDubboProviderFilterTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
/** * Simply verify invocation structure in memory: * EntranceNode(resourceName) * --InterfaceNode(interfaceName) * ----MethodNode(resourceName) */ private void verifyInvocationStructure(String originApplication, Invoker invoker, Invocation invocation) { Context context = ContextUtil.getContext(); assertNotNull(context); // As ContextUtil.enter(resourceName, application) in SentinelDubboProviderFilter String resourceName = DubboUtils.getResourceName(invoker, invocation); assertEquals(resourceName, context.getName()); assertEquals(originApplication, context.getOrigin()); DefaultNode entranceNode = context.getEntranceNode(); ResourceWrapper entranceResource = entranceNode.getId(); assertEquals(resourceName, entranceResource.getName()); assertSame(EntryType.IN, entranceResource.getType()); // As SphU.entry(interfaceName, EntryType.IN); Set<Node> childList = entranceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode interfaceNode = (DefaultNode) childList.iterator().next(); ResourceWrapper interfaceResource = interfaceNode.getId(); assertEquals(DemoService.class.getName(), interfaceResource.getName()); assertSame(EntryType.IN, interfaceResource.getType()); // As SphU.entry(resourceName, EntryType.IN, 1, invocation.getArguments()); childList = interfaceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode methodNode = (DefaultNode) childList.iterator().next(); ResourceWrapper methodResource = methodNode.getId(); assertEquals(resourceName, methodResource.getName()); assertSame(EntryType.IN, methodResource.getType()); // Verify curEntry Entry curEntry = context.getCurEntry(); assertSame(methodNode, curEntry.getCurNode()); assertSame(interfaceNode, curEntry.getLastNode()); assertNotNull(curEntry.getOriginNode());// As context origin is not "", originNode should be created // Verify clusterNode ClusterNode methodClusterNode = methodNode.getClusterNode(); ClusterNode interfaceClusterNode = interfaceNode.getClusterNode(); assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode // 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(originApplication)); Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap(); assertEquals(1, interfaceOriginCountMap.size()); assertTrue(interfaceOriginCountMap.containsKey(originApplication)); }
Example 17
Source File: SentinelSofaRpcProviderFilterTest.java From Sentinel with Apache License 2.0 | 4 votes |
/** * 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)); }
Example 18
Source File: SentinelZuulPreFilter.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
@Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); String origin = parseOrigin(ctx.getRequest()); String routeId = (String)ctx.get(ZuulConstant.PROXY_ID_KEY); Deque<AsyncEntry> asyncEntries = new ArrayDeque<>(); String fallBackRoute = routeId; try { if (StringUtil.isNotBlank(routeId)) { ContextUtil.enter(GATEWAY_CONTEXT_ROUTE_PREFIX + routeId, origin); doSentinelEntry(routeId, RESOURCE_MODE_ROUTE_ID, ctx, asyncEntries); } Set<String> matchingApis = pickMatchingApiDefinitions(ctx); if (!matchingApis.isEmpty() && ContextUtil.getContext() == null) { ContextUtil.enter(ZuulConstant.ZUUL_DEFAULT_CONTEXT, origin); } for (String apiName : matchingApis) { fallBackRoute = apiName; doSentinelEntry(apiName, RESOURCE_MODE_CUSTOM_API_NAME, ctx, asyncEntries); } } catch (BlockException ex) { ZuulBlockFallbackProvider zuulBlockFallbackProvider = ZuulBlockFallbackManager.getFallbackProvider( fallBackRoute); BlockResponse blockResponse = zuulBlockFallbackProvider.fallbackResponse(fallBackRoute, ex); // Prevent routing from running ctx.setRouteHost(null); ctx.set(ZuulConstant.SERVICE_ID_KEY, null); // Set fallback response. ctx.setResponseBody(blockResponse.toString()); ctx.setResponseStatusCode(blockResponse.getCode()); // Set Response ContentType ctx.getResponse().setContentType("application/json; charset=utf-8"); } finally { // We don't exit the entry here. We need to exit the entries in post filter to record Rt correctly. // So here the entries will be carried in the request context. if (!asyncEntries.isEmpty()) { ctx.put(ZuulConstant.ZUUL_CTX_SENTINEL_ENTRIES_KEY, asyncEntries); } } return null; }
Example 19
Source File: SentinelDubboConsumerFilterTest.java From dubbo-sentinel-support with Apache License 2.0 | 4 votes |
/** * Simply verify invocation structure in memory: * EntranceNode(defaultContextName) * --InterfaceNode(interfaceName) * ----MethodNode(resourceName) */ private void verifyInvocationStructure(Invoker invoker, Invocation invocation) { Context context = ContextUtil.getContext(); assertNotNull(context); // As not call ContextUtil.enter(resourceName, application) in SentinelDubboConsumerFilter, use default context // In actual project, a consumer is usually also a provider, the context will be created by SentinelDubboProviderFilter // If consumer is on the top of Dubbo RPC invocation chain, use default context String resourceName = DubboUtils.getResourceName(invoker, invocation); 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.getType()); // As SphU.entry(interfaceName, EntryType.OUT); Set<Node> childList = entranceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode interfaceNode = (DefaultNode) childList.iterator().next(); ResourceWrapper interfaceResource = interfaceNode.getId(); assertEquals(DemoService.class.getName(), interfaceResource.getName()); assertSame(EntryType.OUT, interfaceResource.getType()); // As SphU.entry(resourceName, EntryType.OUT); childList = interfaceNode.getChildList(); assertEquals(1, childList.size()); DefaultNode methodNode = (DefaultNode) childList.iterator().next(); ResourceWrapper methodResource = methodNode.getId(); assertEquals(resourceName, methodResource.getName()); assertSame(EntryType.OUT, methodResource.getType()); // Verify curEntry Entry curEntry = context.getCurEntry(); assertSame(methodNode, curEntry.getCurNode()); assertSame(interfaceNode, curEntry.getLastNode()); assertNull(curEntry.getOriginNode());// As context origin is not "", no originNode should be created in curEntry // Verify clusterNode ClusterNode methodClusterNode = methodNode.getClusterNode(); ClusterNode interfaceClusterNode = interfaceNode.getClusterNode(); assertNotSame(methodClusterNode, interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode // 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()); }
Example 20
Source File: SentinelZuulPreFilter.java From Sentinel with Apache License 2.0 | 4 votes |
@Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); String origin = parseOrigin(ctx.getRequest()); String routeId = (String)ctx.get(ZuulConstant.PROXY_ID_KEY); Deque<EntryHolder> holders = new ArrayDeque<>(); String fallBackRoute = routeId; try { if (StringUtil.isNotBlank(routeId)) { ContextUtil.enter(GATEWAY_CONTEXT_ROUTE_PREFIX + routeId, origin); doSentinelEntry(routeId, RESOURCE_MODE_ROUTE_ID, ctx, holders); } Set<String> matchingApis = pickMatchingApiDefinitions(ctx); if (!matchingApis.isEmpty() && ContextUtil.getContext() == null) { ContextUtil.enter(ZuulConstant.ZUUL_DEFAULT_CONTEXT, origin); } for (String apiName : matchingApis) { fallBackRoute = apiName; doSentinelEntry(apiName, RESOURCE_MODE_CUSTOM_API_NAME, ctx, holders); } } catch (BlockException ex) { ZuulBlockFallbackProvider zuulBlockFallbackProvider = ZuulBlockFallbackManager.getFallbackProvider( fallBackRoute); BlockResponse blockResponse = zuulBlockFallbackProvider.fallbackResponse(fallBackRoute, ex); // Prevent routing from running ctx.setRouteHost(null); ctx.set(ZuulConstant.SERVICE_ID_KEY, null); // Set fallback response. ctx.setResponseBody(blockResponse.toString()); ctx.setResponseStatusCode(blockResponse.getCode()); // Set Response ContentType ctx.getResponse().setContentType("application/json; charset=utf-8"); } finally { // We don't exit the entry here. We need to exit the entries in post filter to record Rt correctly. // So here the entries will be carried in the request context. if (!holders.isEmpty()) { ctx.put(ZuulConstant.ZUUL_CTX_SENTINEL_ENTRIES_KEY, holders); } } return null; }