Search in sources :

Example 21 with Context

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

the class CtSph method entryWithPriority.

private Entry entryWithPriority(ResourceWrapper resourceWrapper, int count, boolean prioritized, Object... args) throws BlockException {
    Context context = ContextUtil.getContext();
    if (context instanceof NullContext) {
        // so here init the entry only. No rule checking will be done.
        return new CtEntry(resourceWrapper, null, context);
    }
    if (context == null) {
        // Using default context.
        context = InternalContextUtil.internalEnter(Constants.CONTEXT_DEFAULT_NAME);
    }
    // Global switch is close, no rule checking will do.
    if (!Constants.ON) {
        return new CtEntry(resourceWrapper, null, context);
    }
    ProcessorSlot<Object> chain = lookProcessChain(resourceWrapper);
    /*
         * Means amount of resources (slot chain) exceeds {@link Constants.MAX_SLOT_CHAIN_SIZE},
         * so no rule checking will be done.
         */
    if (chain == null) {
        return new CtEntry(resourceWrapper, null, context);
    }
    Entry e = new CtEntry(resourceWrapper, chain, context);
    try {
        chain.entry(context, resourceWrapper, null, count, prioritized, args);
    } catch (BlockException e1) {
        e.exit(count, args);
        throw e1;
    } catch (Throwable e1) {
        // This should not happen, unless there are errors existing in Sentinel internal.
        RecordLog.info("Sentinel unexpected exception", e1);
    }
    return e;
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) NullContext(com.alibaba.csp.sentinel.context.NullContext) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) NullContext(com.alibaba.csp.sentinel.context.NullContext)

Example 22 with Context

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

the class CtSph method asyncEntryWithPriorityInternal.

private AsyncEntry asyncEntryWithPriorityInternal(ResourceWrapper resourceWrapper, int count, boolean prioritized, Object... args) throws BlockException {
    Context context = ContextUtil.getContext();
    if (context instanceof NullContext) {
        // so here init the entry only. No rule checking will be done.
        return asyncEntryWithNoChain(resourceWrapper, context);
    }
    if (context == null) {
        // Using default context.
        context = InternalContextUtil.internalEnter(Constants.CONTEXT_DEFAULT_NAME);
    }
    // Global switch is turned off, so no rule checking will be done.
    if (!Constants.ON) {
        return asyncEntryWithNoChain(resourceWrapper, context);
    }
    ProcessorSlot<Object> chain = lookProcessChain(resourceWrapper);
    // Means processor cache size exceeds {@link Constants.MAX_SLOT_CHAIN_SIZE}, so no rule checking will be done.
    if (chain == null) {
        return asyncEntryWithNoChain(resourceWrapper, context);
    }
    AsyncEntry asyncEntry = new AsyncEntry(resourceWrapper, chain, context);
    try {
        chain.entry(context, resourceWrapper, null, count, prioritized, args);
        // Initiate the async context only when the entry successfully passed the slot chain.
        asyncEntry.initAsyncContext();
        // The asynchronous call may take time in background, and current context should not be hanged on it.
        // So we need to remove current async entry from current context.
        asyncEntry.cleanCurrentEntryInLocal();
    } catch (BlockException e1) {
        // When blocked, the async entry will be exited on current context.
        // The async context will not be initialized.
        asyncEntry.exitForContext(context, count, args);
        throw e1;
    } catch (Throwable e1) {
        // This should not happen, unless there are errors existing in Sentinel internal.
        // When this happens, async context is not initialized.
        RecordLog.warn("Sentinel unexpected exception in asyncEntryInternal", e1);
        asyncEntry.cleanCurrentEntryInLocal();
    }
    return asyncEntry;
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) NullContext(com.alibaba.csp.sentinel.context.NullContext) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) NullContext(com.alibaba.csp.sentinel.context.NullContext)

Example 23 with Context

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

the class AsyncEntryTest method testCleanCurrentEntryInLocalError.

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

Example 24 with Context

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

the class AsyncEntryTest method testDuplicateInitAsyncContext.

@Test
public void testDuplicateInitAsyncContext() {
    Context context = new Context(null, "abc");
    AsyncEntry entry = new AsyncEntry(new StringResourceWrapper("testDuplicateInitAsyncContext", EntryType.OUT), null, context);
    entry.initAsyncContext();
    Context asyncContext = entry.getAsyncContext();
    // Duplicate init.
    entry.initAsyncContext();
    assertSame(asyncContext, entry.getAsyncContext());
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 25 with Context

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

the class CtEntryTest method testGetLastNode.

@Test
public void testGetLastNode() {
    Context context = new NullContext();
    CtEntry entry = new CtEntry(new StringResourceWrapper("testGetLastNode", EntryType.IN), null, context);
    assertNull(entry.parent);
    assertNull(entry.getLastNode());
    Entry parentEntry = mock(Entry.class);
    Node node = mock(Node.class);
    when(parentEntry.getCurNode()).thenReturn(node);
    entry.parent = parentEntry;
    assertSame(node, entry.getLastNode());
}
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) Node(com.alibaba.csp.sentinel.node.Node) Test(org.junit.Test)

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