Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 #3138: SystemRuleManager 保留原始 SystemRule 信息 #3143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public SystemBlockException(String resourceName, String message, Throwable cause
this.resourceName = resourceName;
}

public SystemBlockException(String resourceName, String limitType) {
super(limitType);
public SystemBlockException(String resourceName, String limitType, SystemRule systemRule) {
super(limitType, systemRule);
this.resourceName = resourceName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
*/
package com.alibaba.csp.sentinel.slots.system;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import com.alibaba.csp.sentinel.Constants;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
Expand All @@ -32,6 +25,15 @@
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* <p>
* Sentinel System Rule makes the inbound traffic and capacity meet. It takes
Expand Down Expand Up @@ -88,6 +90,8 @@ public final class SystemRuleManager {
private final static SystemPropertyListener listener = new SystemPropertyListener();
private static SentinelProperty<List<SystemRule>> currentProperty = new DynamicSentinelProperty<List<SystemRule>>();

private static final Map<String, SystemRule> ruleMap = new HashMap<>();

@SuppressWarnings("PMD.ThreadPoolCreationRule")
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1,
new NamedThreadFactory("sentinel-system-status-record-task", true));
Expand Down Expand Up @@ -247,6 +251,7 @@ public static void loadSystemConf(SystemRule rule) {
highestSystemLoad = Math.min(highestSystemLoad, rule.getHighestSystemLoad());
highestSystemLoadIsSet = true;
checkStatus = true;
ruleMap.put("load", rule);
}

if (rule.getHighestCpuUsage() >= 0) {
Expand All @@ -257,24 +262,28 @@ public static void loadSystemConf(SystemRule rule) {
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);
Expand Down Expand Up @@ -304,30 +313,30 @@ public static void checkSystem(ResourceWrapper resourceWrapper, int count) throw
// total qps
double currentQps = Constants.ENTRY_NODE.passQps();
if (currentQps + count > qps) {
throw new SystemBlockException(resourceWrapper.getName(), "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");
throw new SystemBlockException(resourceWrapper.getName(), "thread", ruleMap.get("thread"));
}

double rt = Constants.ENTRY_NODE.avgRt();
if (rt > maxRt) {
throw new SystemBlockException(resourceWrapper.getName(), "rt");
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");
throw new SystemBlockException(resourceWrapper.getName(), "load", ruleMap.get("load"));
}
}

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

Expand Down