/** * Create a new async context. * * @param entranceNode entrance node of the context * @param name context name * @return the new created context * @since 0.2.0 */ public static Context newAsyncContext(DefaultNode entranceNode, String name) { return new Context(name, entranceNode, true); }
private void setUpEntryFor(Context context) { // The entry should not be associated to NullContext. if (context instanceof NullContext) { return; } this.parent = context.getCurEntry(); if (parent != null) { ((CtEntry)parent).child = this; } context.setCurEntry(this); }
/** * 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; } }
/** * <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, ""); }
public static void exit(int count) { ContextUtil.getContext().getCurEntry().exit(count, OBJECTS0); }
/** * Exit context of current thread, that is removing {@link Context} in the * ThreadLocal. */ public static void exit() { Context context = contextHolder.get(); if (context != null && context.getCurEntry() == null) { contextHolder.set(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); }
public long getOriginTotalRequest() { return getOriginNode() == null ? 0 : getOriginNode().totalRequest(); }
/** * Check if provided context is a default auto-created context. * * @param context context to check * @return true if it is a default context, otherwise false * @since 0.2.0 */ public static boolean isDefaultContext(Context context) { if (context == null) { return false; } return Constants.CONTEXT_DEFAULT_NAME.equals(context.getName()); }
/** * Execute the code within provided context. * This is mainly designed for context switching (e.g. in asynchronous invocation). * * @param context the context * @param f lambda to run within the context * @since 0.2.0 */ public static void runOnContext(Context context, Runnable f) { Context curContext = replaceContext(context); try { f.run(); } finally { replaceContext(curContext); } } }
/** * Trace provided {@link Throwable} and add exception count to current entry in provided context. * * @param e exception to record * @param count exception count to add * @since 1.4.2 */ public static void traceContext(Throwable e, int count, Context context) { if (e == null || e instanceof BlockException) { return; } 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(); } }
public static void exit(int count, Object... args) { ContextUtil.getContext().getCurEntry().exit(count, args); }
public double getOriginAvgRt() { return getOriginNode() == null ? 0 : getOriginNode().avgRt(); }
public double getOriginTotalQps() { return getOriginNode() == null ? 0 : getOriginNode().totalQps(); }
public double getOriginBlockQps() { return getOriginNode() == null ? 0 : getOriginNode().blockQps(); }
public double getOriginPassReqQps() { return getOriginNode() == null ? 0 : getOriginNode().successQps(); }
public int getOriginCurThreadNum() { return getOriginNode() == null ? 0 : getOriginNode().curThreadNum(); }
public double getOriginPassQps() { return getOriginNode() == null ? 0 : getOriginNode().passQps(); }
public long getOriginBlockRequest() { return getOriginNode() == null ? 0 : getOriginNode().blockRequest(); }