Search in sources :

Example 1 with NullContext

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

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

the class AsyncEntry method cleanCurrentEntryInLocal.

/**
 * Remove current entry from local context, but does not exit.
 */
void cleanCurrentEntryInLocal() {
    if (context instanceof NullContext) {
        return;
    }
    Context originalContext = context;
    if (originalContext != null) {
        Entry curEntry = originalContext.getCurEntry();
        if (curEntry == this) {
            Entry parent = this.parent;
            originalContext.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry) parent).child = null;
            }
        } else {
            String curEntryName = curEntry == null ? "none" : curEntry.resourceWrapper.getName() + "@" + curEntry.hashCode();
            String msg = String.format("Bad async context state, expected entry: %s, but actual: %s", getResourceWrapper().getName() + "@" + hashCode(), curEntryName);
            throw new IllegalStateException(msg);
        }
    }
}
Also used : NullContext(com.alibaba.csp.sentinel.context.NullContext) Context(com.alibaba.csp.sentinel.context.Context) NullContext(com.alibaba.csp.sentinel.context.NullContext)

Example 3 with NullContext

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

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

use of com.alibaba.csp.sentinel.context.NullContext 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)5 NullContext (com.alibaba.csp.sentinel.context.NullContext)5 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)2 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)2 Test (org.junit.Test)2 Node (com.alibaba.csp.sentinel.node.Node)1