alibaba / alibaba/Sentinel

ContextUtil为什么要实例化一个“sentinel_default_context”

Open
#2,955 2 comments 0 reactions 0 assignees View on GitHub
kind/question
Dominant language
Java
Stars
23.1k
Forks
8.1k
PR merge metrics
No merged PRs in 30d

Description

读源代码(版本:`1.8.2` )时, `com.alibaba.csp.sentinel.context.ContextUtil` 提供有 `initDefaultContext()` 方法,并在 `static` 块中初始化了一个`sentinel_default_context`,位置如图:
![image](https://user-images.githubusercontent.com/12644274/202660038-9a6c1f77-fe4f-491e-9bf3-978cd410c71f.png)
在测试过程中,有三个不同的场景,如图:
![image](https://user-images.githubusercontent.com/12644274/202672422-d4ec6740-3581-4216-a417-ea4dd8325bdd.png)
说明:
场景 **①** 是 `main` 方法直接跑的,生成了默认的 `sentinel_default_context`
场景 **②** 是 `web` 应用,使用了 `com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor`, 生成了默认的 `sentinel_default_context` 和 `sentinel_spring_web_context`
场景 **③** 是 `thrift` 应用,使用了自定义的 `CustomSentinelResourceAspect`(继承自 `com.alibaba.csp.sentinel.annotation.aspectj.AbstractSentinelAspectSupport`,并使用了自定义的`feature_thrift_context`),生成了默认的 `sentinel_default_context` 及自定义的 `feature_thrift_context`

问题:
1、我理解,默认的 `sentinel_default_context` 是用来兜底的,是否还有其它作用?
2、如果用户指定了自己的 `context` ,这个默认的 `sentinel_default_context` 是不是不应该创建?
3、如果第 `2、` 个问题是合理的,代码是不是有bug,不应该直接在 `static` 块中始化默认的`sentinel_default_context`,而在流程走到`com.alibaba.csp.sentinel.context.ContextUtil#enter(java.lang.String name, java.lang.String origin)`中再确定是否需要创建?

Contributor guide

Open the contributing guide

Research direction

Read com.alibaba.csp.sentinel.context.ContextUtil in version 1.8.2, starting with the static initialization, initDefaultContext(), and enter(String name, String origin). Trace the callers and the web and AspectJ integrations described in the issue to determine whether the default context is required beyond fallback. Done means documenting the intended lifecycle and, if eager creation is incorrect, defining the affected behavior clearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.