alibaba / alibaba/Sentinel

sentinel 并发限流结果与预期不一致的问题

Open
#1,861 1 comment 0 reactions 0 assignees View on GitHub
area/concurrency-limiting kind/question
Dominant language
Java
Stars
23.1k
Forks
8.1k
PR merge metrics
No merged PRs in 30d

Description

## Issue Description

Type: *bug report*

### Describe what happened (or what feature you want)

```java
public static void main(String[] args) throws InterruptedException {
FlowRule flowRule = (FlowRule) new FlowRule()
.setCount(10)
.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT)
.setGrade(RuleConstant.FLOW_GRADE_THREAD)
.setResource("test");
FlowRuleManager.loadRules(Collections.singletonList(flowRule));

AtomicLong block = new AtomicLong(0);
AtomicLong pass = new AtomicLong(0);

int loop = 100;

CountDownLatch countDownLatch = new CountDownLatch(loop);

for (int i = 0; i < loop; i++) {
new Thread(() -> {
Entry test = null;
try {
test = SphU.entry("test");
pass.incrementAndGet();
TimeUnit.SECONDS.sleep(1);
} catch (DegradeException e) {
Tracer.trace(e);
} catch (BlockException e) {
e.printStackTrace();
block.incrementAndGet();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (test != null) {
test.exit();
}
countDownLatch.countDown();
}
}).start();
}

countDownLatch.await();
System.out.println("pass: " + pass.get());
System.out.println("block: " + block.get());

}
```

### Describe what you expected to happen

我希望以上代码 pass 恒定输出 10,block 恒定输出 90;

但是经过测试,结果是变化的,我测试得到的结果如下

```
# 第1次运行
pass: 99
block: 1

# 第2次运行
pass: 63
block: 37

# 第3次运行
pass: 12
block: 88
```

我发现是因为 `StatisticSlot` 对于统计数据的逻辑与 `FlowSlot` 的逻辑是完全隔离的,导致在并发情况下会出现这样子的情况;
是否有办法解决这个问题亦或是我使用的方式有一些问题?

### How to reproduce it (as minimally and precisely as possible)

1.
2.
3.

### Tell us your environment

version: `1.8.1`

### Anything else we need to know?

Contributor guide

Open the contributing guide

Research direction

Reproduce the result with the Java example using FlowRuleManager, SphU.entry, StatisticSlot, and FlowSlot on Sentinel 1.8.1. Read how these slots handle concurrent thread-flow accounting and verify whether the expected pass/block totals are guaranteed. Done means establishing whether the behavior is a bug or an incorrect usage pattern, with a focused test or a documented conclusion.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.