类 ContextUtil

java.lang.Object
com.alibaba.csp.sentinel.context.ContextUtil

public class ContextUtil extends Object
Utility class to get or create Context in current thread.

Each SphU#entry() or SphO#entry() should be in a Context. If we don't invoke ContextUtil#enter() explicitly, DEFAULT context will be used.

作者:
jialiang.linjl, leyou(lihao), Eric Zhao
  • 构造器详细资料

    • ContextUtil

      public ContextUtil()
  • 方法详细资料

    • enter

      public static Context enter(String name, String origin)

      Enter the invocation context, which marks as the entrance of an invocation chain. The context is wrapped with ThreadLocal, meaning that each thread has it's own Context. New context will be created if current thread doesn't have one.

      A context will be bound with an EntranceNode, which represents the entrance statistic node of the invocation chain. New EntranceNode will be created if current context does't have one. Note that same context name will share same EntranceNode globally.

      The origin node will be created in ClusterBuilderSlot. Note that each distinct origin of different resources will lead to creating different new Node, meaning that total amount of created origin statistic nodes will be:
      distinct resource name amount * distinct origin count.
      So when there are too many origins, memory footprint should be carefully considered.

      Same resource in different context will count separately, see NodeSelectorSlot.

      参数:
      name - the context name
      origin - the origin of this invocation, usually the origin could be the Service Consumer's app name. The origin is useful when we want to control different invoker/consumer separately.
      返回:
      The invocation context of the current thread
    • trueEnter

      protected static Context trueEnter(String name, String origin)
    • enter

      public static Context enter(String name)

      Enter the invocation context, which marks as the entrance of an invocation chain. The context is wrapped with ThreadLocal, meaning that each thread has it's own Context. New context will be created if current thread doesn't have one.

      A context will be bound with an EntranceNode, which represents the entrance statistic node of the invocation chain. New EntranceNode will be created if current context does't have one. Note that same context name will share same EntranceNode globally.

      Same resource in different context will count separately, see NodeSelectorSlot.

      参数:
      name - the context name
      返回:
      The invocation context of the current thread
    • exit

      public static void exit()
      Exit context of current thread, that is removing Context in the ThreadLocal.
    • contextSize

      public static int contextSize()
      Get current size of context entrance node map.
      返回:
      current size of context entrance node map
      从以下版本开始:
      0.2.0
    • isDefaultContext

      public static boolean isDefaultContext(Context context)
      Check if provided context is a default auto-created context.
      参数:
      context - context to check
      返回:
      true if it is a default context, otherwise false
      从以下版本开始:
      0.2.0
    • getContext

      public static Context getContext()
      Get Context of current thread.
      返回:
      context of current thread. Null value will be return if current thread does't have context.
    • runOnContext

      public static void runOnContext(Context context, Runnable f)
      Execute the code within provided context. This is mainly designed for context switching (e.g. in asynchronous invocation).
      参数:
      context - the context
      f - lambda to run within the context
      从以下版本开始:
      0.2.0