Search in sources :

Example 6 with Context

use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.

the class CtEntryTest method testExitNotMatchCurEntry.

@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");
}
Also used : NullContext(com.alibaba.csp.sentinel.context.NullContext) Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 7 with Context

use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.

the class CtEntryTest method testEntryAndExitWithNullContext.

@Test
public void testEntryAndExitWithNullContext() {
    Context context = new NullContext();
    CtEntry entry = new CtEntry(new StringResourceWrapper("testEntryAndExitWithNullContext", EntryType.IN), null, context);
    assertNull(context.getCurEntry());
    entry.exit();
    assertNull(context.getCurEntry());
    // Won't true exit, so the context won't be cleared.
    assertEquals(context, entry.context);
}
Also used : NullContext(com.alibaba.csp.sentinel.context.NullContext) Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) NullContext(com.alibaba.csp.sentinel.context.NullContext) Test(org.junit.Test)

Example 8 with Context

use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.

the class BaseTest method cleanUpAll.

/**
 * 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
    }
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) Method(java.lang.reflect.Method)

Example 9 with Context

use of com.alibaba.csp.sentinel.context.Context 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 10 with Context

use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.

the class ReactorSphU method entryWith.

public static <R> Mono<R> entryWith(String resourceName, EntryType entryType, Mono<R> actual) {
    final AtomicReference<AsyncEntry> entryWrapper = new AtomicReference<>(null);
    return Mono.defer(() -> {
        try {
            AsyncEntry entry = SphU.asyncEntry(resourceName, entryType);
            entryWrapper.set(entry);
            return actual.subscriberContext(context -> {
                if (entry == null) {
                    return context;
                }
                Context sentinelContext = entry.getAsyncContext();
                if (sentinelContext == null) {
                    return context;
                }
                // TODO: check GC friendly?
                return context.put(SentinelReactorConstants.SENTINEL_CONTEXT_KEY, sentinelContext);
            }).doOnSuccessOrError((o, t) -> {
                if (entry != null && entryWrapper.compareAndSet(entry, null)) {
                    if (t != null) {
                        Tracer.traceContext(t, 1, entry.getAsyncContext());
                    }
                    entry.exit();
                }
            });
        } catch (BlockException ex) {
            return Mono.error(ex);
        }
    });
}
Also used : SphU(com.alibaba.csp.sentinel.SphU) AsyncEntry(com.alibaba.csp.sentinel.AsyncEntry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) Context(com.alibaba.csp.sentinel.context.Context) EntryType(com.alibaba.csp.sentinel.EntryType) Mono(reactor.core.publisher.Mono) Tracer(com.alibaba.csp.sentinel.Tracer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Context(com.alibaba.csp.sentinel.context.Context) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) AsyncEntry(com.alibaba.csp.sentinel.AsyncEntry) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

Context (com.alibaba.csp.sentinel.context.Context)44 Test (org.junit.Test)29 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)17 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)15 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)10 Entry (com.alibaba.csp.sentinel.Entry)9 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)9 NullContext (com.alibaba.csp.sentinel.context.NullContext)8 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)7 BaseTest (com.alibaba.csp.sentinel.BaseTest)6 Node (com.alibaba.csp.sentinel.node.Node)6 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)5 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)4 Method (java.lang.reflect.Method)4 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 Invocation (com.alibaba.dubbo.rpc.Invocation)2 Invoker (com.alibaba.dubbo.rpc.Invoker)2 Result (com.alibaba.dubbo.rpc.Result)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2