Skip to content

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

@hialuoy

Description

@hialuoy

问题描述

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

<parent>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-parent</artifactId>
    <version>1.8.6</version>
</parent>

Type: bug report

问题解决方案

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions