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