alibaba / alibaba/Sentinel

[BUG] 系统规则失效 SystemSlot 检测未通过抛出SystemBlockException,但是在LogSlot e.getRule().getId() 由于SystemSlot 抛出的异常没有rule导致 发生NullPointerException, 导致没有收到 BlockedException

Open
#3,138 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
23.1k
Forks
8.1k
PR merge metrics
No merged PRs in 30d

Description

## 问题描述
[BUG] 系统规则失效 SystemSlot 检测未通过抛出SystemBlockException,但是在LogSlot e.getRule().getId() 由于SystemSlot 抛出的异常没有rule导致 发生NullPointerException, 导致没有收到 BlockedException
Sentinel版本号: 1.8.6

```xml

com.alibaba.csp
sentinel-parent
1.8.6

```

Type: *bug report*

### 问题解决方案

```java
private static final Map ruleMap = new HashMap<>();

public static void loadSystemConf(SystemRule rule) {
boolean checkStatus = false;
if (rule.getHighestSystemLoad() >= 0) {
highestSystemLoad = Math.min(highestSystemLoad, rule.getHighestSystemLoad());
highestSystemLoadIsSet = true;
checkStatus = true;
ruleMap.put("load", rule);
}

if (rule.getHighestCpuUsage() >= 0) {
if (rule.getHighestCpuUsage() > 1) {
RecordLog.warn(String.format("[SystemRuleManager] Ignoring invalid SystemRule: "
+ "highestCpuUsage %.3f > 1", rule.getHighestCpuUsage()));
} else {
highestCpuUsage = Math.min(highestCpuUsage, rule.getHighestCpuUsage());
highestCpuUsageIsSet = true;
checkStatus = true;
ruleMap.put("cpu", rule);
}
}

if (rule.getAvgRt() >= 0) {
maxRt = Math.min(maxRt, rule.getAvgRt());
maxRtIsSet = true;
checkStatus = true;
ruleMap.put("rt", rule);
}
if (rule.getMaxThread() >= 0) {
maxThread = Math.min(maxThread, rule.getMaxThread());
maxThreadIsSet = true;
checkStatus = true;
ruleMap.put("thread", rule);
}

if (rule.getQps() >= 0) {
qps = Math.min(qps, rule.getQps());
qpsIsSet = true;
checkStatus = true;
ruleMap.put("qps", rule);
}

checkSystemStatus.set(checkStatus);

}

public static void checkSystem(ResourceWrapper resourceWrapper, int count) throws BlockException {
if (resourceWrapper == null) {
return;
}
// Ensure the checking switch is on.
if (!checkSystemStatus.get()) {
return;
}

// for inbound traffic only
if (resourceWrapper.getEntryType() != EntryType.IN) {
return;
}

// total qps
double currentQps = Constants.ENTRY_NODE.passQps();
if (currentQps + count > qps) {
throw new SystemBlockException(resourceWrapper.getName(), "qps", ruleMap.get("qps"));
}

// total thread
int currentThread = Constants.ENTRY_NODE.curThreadNum();
if (currentThread > maxThread) {
throw new SystemBlockException(resourceWrapper.getName(), "thread", ruleMap.get("thread"));
}

double rt = Constants.ENTRY_NODE.avgRt();
if (rt > maxRt) {
throw new SystemBlockException(resourceWrapper.getName(), "rt", ruleMap.get("rt"));
}

// load. BBR algorithm.
if (highestSystemLoadIsSet && getCurrentSystemAvgLoad() > highestSystemLoad) {
if (!checkBbr(currentThread)) {
throw new SystemBlockException(resourceWrapper.getName(), "load", ruleMap.get("load"));
}
}

// cpu usage
if (highestCpuUsageIsSet && getCurrentCpuUsage() > highestCpuUsage) {
throw new SystemBlockException(resourceWrapper.getName(), "cpu", ruleMap.get("cpu"));
}
}
```

通过private static final Map ruleMap = new HashMap<>(); 在加载时记录SystemRule 在抛出异常时 throw new SystemBlockException(resourceWrapper.getName(), "cpu", ruleMap.get("cpu")); ruleMap.get("cpu")的方式获取记录的值。

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.