private static void testFor(/*@NonNull*/ String resource, /*@NonNull*/ String origin) { ContextUtil.enter(resource, origin); Entry entry = null; try { entry = SphU.entry(resource); System.out.println(String.format("Passed for resource %s, origin is %s", resource, origin)); } catch (BlockException ex) { System.err.println(String.format("Blocked for resource %s, origin is %s", resource, origin)); } finally { if (entry != null) { entry.exit(); } ContextUtil.exit(); } }
/** * While loop will exit all entries, * Case serviceId and URL entry twice in {@link SentinelPreFilter}. * The ContextUtil.getContext().getCurEntry() will exit from inner to outer. */ @Override public Object run() throws ZuulException { while (ContextUtil.getContext() != null && ContextUtil.getContext().getCurEntry() != null) { ContextUtil.getContext().getCurEntry().exit(); } ContextUtil.exit(); return null; } }
/** * Not thread-safe, only for test. */ static void resetContextMap() { if (contextNameNodeMap != null) { RecordLog.warn("Context map cleared and reset to initial state"); contextNameNodeMap.clear(); initDefaultContext(); } }
/** * <p> * Enter the invocation context. The context is ThreadLocal, meaning that * each thread has it's own {@link Context}. New context will be created if * current thread doesn't have one. * </p> * <p> * A context will related to A {@link EntranceNode}, which is the entrance * of the invocation tree. New {@link EntranceNode} will be created if * current context does't have one. Note that same resource name will share * same {@link EntranceNode} globally. * </p> * <p> * Same resource in different context will count separately, see {@link NodeSelectorSlot}. * </p> * * @param name the context name. * @return The invocation context of the current thread. */ public static Context enter(String name) { return enter(name, ""); }
if (ContextUtil.isDefaultContext(context)) { ContextUtil.exit();
public static void exit(int count) { ContextUtil.getContext().getCurEntry().exit(count, OBJECTS0); }
if (StringUtil.isNotEmpty(serviceTarget)) { RecordLog.info(String.format("[Sentinel Pre Filter] Origin: %s enter ServiceId: %s", origin, serviceTarget)); ContextUtil.enter(serviceTarget, origin); SphU.entry(serviceTarget, EntryType.IN); } else { RecordLog.info("[Sentinel Pre Filter] ServiceId is empty"); ContextUtil.enter(ZUUL_DEFAULT_CONTEXT, origin);
if (ContextUtil.isDefaultContext(context)) { ContextUtil.exit();
public static void exit(int count, Object... args) { ContextUtil.getContext().getCurEntry().exit(count, args); }
private static void doSomething(MessageExt message) { pool.submit(() -> { Entry entry = null; try { ContextUtil.enter(KEY); entry = SphU.entry(KEY, EntryType.OUT); // Your business logic here. System.out.printf("[%d][%s][Success: %d] Receive New Messages: %s %n", System.currentTimeMillis(), Thread.currentThread().getName(), SUCCESS_COUNT.addAndGet(1), new String(message.getBody())); } catch (BlockException ex) { // Blocked. System.out.println("Blocked: " + FAIL_COUNT.addAndGet(1)); } finally { if (entry != null) { entry.exit(); } ContextUtil.exit(); } }); }
/** * <p> * Enter the invocation context. The context is ThreadLocal, meaning that * each thread has it's own {@link Context}. New context will be created if * current thread doesn't have one. * </p> * <p> * A context will related to A {@link EntranceNode}, which is the entrance * of the invocation tree. New {@link EntranceNode} will be created if * current context does't have one. Note that same resource name will share * same {@link EntranceNode} globally. * </p> * <p> * Same resource in different context will count separately, see {@link NodeSelectorSlot}. * </p> * * @param name the context name. * @return The invocation context of the current thread. */ public static Context enter(String name) { return enter(name, ""); }
/** * Trace not {@link BlockException} ex. * While loop will exit all entries, * Case serviceId and URL entry twice in {@link SentinelPreFilter}. * The ContextUtil.getContext().getCurEntry() will exit from inner to outer. */ @Override public Object run() throws ZuulException { try { RequestContext ctx = RequestContext.getCurrentContext(); Throwable throwable = ctx.getThrowable(); if (throwable != null) { if (!BlockException.isBlockException(throwable)) { Tracer.trace(throwable.getCause()); RecordLog.info("[Sentinel Error Filter] Trace cause", throwable.getCause()); } } } finally { while (ContextUtil.getContext() != null && ContextUtil.getContext().getCurEntry() != null) { ContextUtil.getContext().getCurEntry().exit(); } ContextUtil.exit(); } return null; } }
/** * Trace provided {@link Throwable} and add exception count to entry in current context. * * @param e exception to record * @param count exception count to add */ public static void trace(Throwable e, int count) { if (e == null || e instanceof BlockException) { return; } Context context = ContextUtil.getContext(); if (context == null) { return; } DefaultNode curNode = (DefaultNode)context.getCurNode(); traceExceptionToNode(e, count, curNode); }
/** * Not thread-safe, only for test. */ static void resetContextMap() { if (contextNameNodeMap != null) { RecordLog.warn("Context map cleared and reset to initial state"); contextNameNodeMap.clear(); initDefaultContext(); } }
private Entry entryWithPriority(ResourceWrapper resourceWrapper, int count, boolean prioritized, Object... args) throws BlockException { Context context = ContextUtil.getContext(); if (context instanceof NullContext) {
private static void doSomething(MessageExt message) { pool.submit(() -> { Entry entry = null; try { ContextUtil.enter(KEY); entry = SphU.entry(KEY, EntryType.OUT); // Your business logic here. System.out.printf("[%d][%s][Success: %d] Receive New Messages: %s %n", System.currentTimeMillis(), Thread.currentThread().getName(), SUCCESS_COUNT.addAndGet(1), new String(message.getBody())); } catch (BlockException ex) { // Blocked. System.out.println("Blocked: " + FAIL_COUNT.addAndGet(1)); } finally { if (entry != null) { entry.exit(); } ContextUtil.exit(); } }); }
private AsyncEntry asyncEntryWithPriorityInternal(ResourceWrapper resourceWrapper, int count, boolean prioritized, Object... args) throws BlockException { Context context = ContextUtil.getContext(); if (context instanceof NullContext) {
public static void main(String[] args) throws Exception { initFlowRule(); AsyncEntryDemo service = new AsyncEntryDemo(); // Expected invocation chain: // // EntranceNode: machine-root // -EntranceNode: async-context // --test-top // ---test-sync // ---test-async // ----test-another-async // -----test-another-sync-in-async // ----test-sync-in-async ContextUtil.enter("async-context", "originA"); Entry entry = null; try { entry = SphU.entry("test-top"); System.out.println("Do something..."); service.doAsyncThenSync(); } catch (BlockException ex) { // Request blocked, handle the exception. ex.printStackTrace(); } finally { if (entry != null) { entry.exit(); } ContextUtil.exit(); } TimeUnit.SECONDS.sleep(20); }
public static void exit(int count, Object... args) { ContextUtil.getContext().getCurEntry().exit(count, args); }