Param-level concurrency limiting don't work in some scene | 热点参数并发控制不准确
- Dominant language
- Java
- Stars
- 23.1k
- Forks
- 8.1k
- PR merge metrics
- No merged PRs in 30d
Description
## Issue Description
bug report
### Describe what happened (or what feature you want)
https://github.com/alibaba/Sentinel/blob/b1e88bdeb4dcb1c384c75ae1b2d0427934a7a788/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java#L171
as the code, the count reference was removed when decrease thread count and the reference value <= 0, but actually the reference value may not <= 0 when remove happen, because the calculate is not thread-safe, the worse is when you lost one, it more probability lost more
通过配置线程数限流在并发场景存在线程数计数错误(少计)的情况,这种错误可能自行恢复,但也可能会逐渐失控,从而无法达到通过线程数限流的目的。
具体来说Sentinel通过一个AtomicInteger对象来进行线程数计数,在线程entry和exit时分别进行加减计数,并发场景下,加减同步进行,因为计数对象为atomic的,计数是准确的;但做减计数时,如果计数值小于等于0时,对象会被remove掉,因为加减的方法并非线程安全的,被remove的对象实际值并非总是小于等于零,大于零时被remove就会导致漏记的问题,而一旦漏记后会更容易触发进一步漏记的问题,最终可能导致限流失败。
下面截图截取加减计数时的部分代码,以及代码的执行顺序导致漏记的产生
![Uploading lost.png…]()
### Describe what you expected to happen
you can reappear it very easy, just call a method with a fix thread pool
### How to reproduce it (as minimally and precisely as possible)
1. make it thread-safe
2. remove this line
https://github.com/alibaba/Sentinel/blob/b1e88bdeb4dcb1c384c75ae1b2d0427934a7a788/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java#L171
3. rewrite the logic
### Tell us your environment
the latest
### Anything else we need to know?
there is a solution we used,I can submit a pr latter
Contributor guide
Research direction
Inspect sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java around line 171, then reproduce the count loss with a fixed thread pool. Trace the concurrent entry and exit updates and verify that the parameter-level thread count remains accurate instead of losing references during removal.
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
- 38/100