Skip to content

Commit d142a07

Browse files
committed
Enhance log and null check for rule managers
Signed-off-by: Eric Zhao <[email protected]>
1 parent 02cf5e5 commit d142a07

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class DegradeRuleManager {
5656
*/
5757
public static void register2Property(SentinelProperty<List<DegradeRule>> property) {
5858
synchronized (listener) {
59+
RecordLog.info("[DegradeRuleManager] Registering new property to degrade rule manager");
5960
currentProperty.removeListener(listener);
6061
property.addListener(listener);
6162
currentProperty = property;
@@ -122,7 +123,7 @@ public void configUpdate(List<DegradeRule> conf) {
122123
degradeRules.clear();
123124
degradeRules.putAll(rules);
124125
}
125-
RecordLog.info("receive degrade config: " + degradeRules);
126+
RecordLog.info("[DegradeRuleManager] Degrade rules received: " + degradeRules);
126127
}
127128

128129
@Override
@@ -132,16 +133,22 @@ public void configLoad(List<DegradeRule> conf) {
132133
degradeRules.clear();
133134
degradeRules.putAll(rules);
134135
}
135-
RecordLog.info("init degrade config: " + degradeRules);
136+
RecordLog.info("[DegradeRuleManager] Degrade rules loaded: " + degradeRules);
136137
}
137138

138139
private Map<String, List<DegradeRule>> loadDegradeConf(List<DegradeRule> list) {
139-
if (list == null) {
140-
return null;
141-
}
142140
Map<String, List<DegradeRule>> newRuleMap = new ConcurrentHashMap<String, List<DegradeRule>>();
143141

142+
if (list == null || list.isEmpty()) {
143+
return newRuleMap;
144+
}
145+
144146
for (DegradeRule rule : list) {
147+
if (!isValidRule(rule)) {
148+
RecordLog.warn("[DegradeRuleManager] Ignoring invalid degrade rule when loading new rules: " + rule);
149+
continue;
150+
}
151+
145152
if (StringUtil.isBlank(rule.getLimitApp())) {
146153
rule.setLimitApp(FlowRule.LIMIT_APP_DEFAULT);
147154
}
@@ -160,4 +167,7 @@ private Map<String, List<DegradeRule>> loadDegradeConf(List<DegradeRule> list) {
160167

161168
}
162169

170+
private static boolean isValidRule(DegradeRule rule) {
171+
return rule != null && !StringUtil.isBlank(rule.getResource()) && rule.getCount() >= 0;
172+
}
163173
}

sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
*
5252
* @author jialiang.linjl
5353
*/
54-
5554
public class FlowRuleManager {
5655

5756
private static final Map<String, List<FlowRule>> flowRules = new ConcurrentHashMap<String, List<FlowRule>>();
@@ -105,12 +104,13 @@ public static void loadRules(List<FlowRule> rules) {
105104
private static Map<String, List<FlowRule>> loadFlowConf(List<FlowRule> list) {
106105
Map<String, List<FlowRule>> newRuleMap = new ConcurrentHashMap<String, List<FlowRule>>();
107106

108-
if (list == null) {
107+
if (list == null || list.isEmpty()) {
109108
return newRuleMap;
110109
}
111110

112111
for (FlowRule rule : list) {
113-
if (!isValid(rule)) {
112+
if (!isValidRule(rule)) {
113+
RecordLog.warn("[FlowRuleManager] Ignoring invalid flow rule when loading new flow rules: " + rule);
114114
continue;
115115
}
116116
if (StringUtil.isBlank(rule.getLimitApp())) {
@@ -202,7 +202,7 @@ public void configLoad(List<FlowRule> conf) {
202202

203203
}
204204

205-
private static boolean isValid(FlowRule rule) {
205+
private static boolean isValidRule(FlowRule rule) {
206206
return rule != null && !StringUtil.isBlank(rule.getResource());
207207
}
208208
}

0 commit comments

Comments
 (0)