16
16
package com .alibaba .csp .sentinel .slots .block .flow ;
17
17
18
18
import java .util .ArrayList ;
19
- import java .util .Collections ;
19
+ import java .util .HashMap ;
20
20
import java .util .List ;
21
21
import java .util .Map ;
22
- import java .util .concurrent .ConcurrentHashMap ;
23
22
import java .util .concurrent .Executors ;
24
23
import java .util .concurrent .ScheduledExecutorService ;
25
24
import java .util .concurrent .TimeUnit ;
26
- import java .util .concurrent .atomic .AtomicReference ;
27
25
28
26
import com .alibaba .csp .sentinel .concurrent .NamedThreadFactory ;
29
27
import com .alibaba .csp .sentinel .config .SentinelConfig ;
50
48
*/
51
49
public class FlowRuleManager {
52
50
53
- private static final AtomicReference < Map <String , List <FlowRule >>> flowRules = new AtomicReference < Map < String , List < FlowRule >> >();
51
+ private static volatile Map <String , List <FlowRule >> flowRules = new HashMap < >();
54
52
55
53
private static final FlowPropertyListener LISTENER = new FlowPropertyListener ();
56
54
private static SentinelProperty <List <FlowRule >> currentProperty = new DynamicSentinelProperty <List <FlowRule >>();
@@ -60,11 +58,10 @@ public class FlowRuleManager {
60
58
new NamedThreadFactory ("sentinel-metrics-record-task" , true ));
61
59
62
60
static {
63
- flowRules .set (Collections .<String , List <FlowRule >>emptyMap ());
64
61
currentProperty .addListener (LISTENER );
65
62
startMetricTimerListener ();
66
63
}
67
-
64
+
68
65
/**
69
66
* <p> Start the MetricTimerListener
70
67
* <ol>
@@ -79,12 +76,12 @@ private static void startMetricTimerListener() {
79
76
if (flushInterval <= 0 ) {
80
77
RecordLog .info ("[FlowRuleManager] The MetricTimerListener isn't started. If you want to start it, "
81
78
+ "please change the value(current: {}) of config({}) more than 0 to start it." , flushInterval ,
82
- SentinelConfig .METRIC_FLUSH_INTERVAL );
79
+ SentinelConfig .METRIC_FLUSH_INTERVAL );
83
80
return ;
84
81
}
85
82
SCHEDULER .scheduleAtFixedRate (new MetricTimerListener (), 0 , flushInterval , TimeUnit .SECONDS );
86
83
}
87
-
84
+
88
85
/**
89
86
* Listen to the {@link SentinelProperty} for {@link FlowRule}s. The property is the source of {@link FlowRule}s.
90
87
* Flow rules can also be set by {@link #loadRules(List)} directly.
@@ -108,7 +105,7 @@ public static void register2Property(SentinelProperty<List<FlowRule>> property)
108
105
*/
109
106
public static List <FlowRule > getRules () {
110
107
List <FlowRule > rules = new ArrayList <FlowRule >();
111
- for (Map .Entry <String , List <FlowRule >> entry : flowRules .get (). entrySet ()) {
108
+ for (Map .Entry <String , List <FlowRule >> entry : flowRules .entrySet ()) {
112
109
rules .addAll (entry .getValue ());
113
110
}
114
111
return rules ;
@@ -124,19 +121,19 @@ public static void loadRules(List<FlowRule> rules) {
124
121
}
125
122
126
123
static Map <String , List <FlowRule >> getFlowRuleMap () {
127
- return flowRules . get () ;
124
+ return flowRules ;
128
125
}
129
126
130
127
public static boolean hasConfig (String resource ) {
131
- return flowRules .get (). containsKey (resource );
128
+ return flowRules .containsKey (resource );
132
129
}
133
130
134
131
public static boolean isOtherOrigin (String origin , String resourceName ) {
135
132
if (StringUtil .isEmpty (origin )) {
136
133
return false ;
137
134
}
138
135
139
- List <FlowRule > rules = flowRules .get (). get ( resourceName );
136
+ List <FlowRule > rules = flowRules .get (resourceName );
140
137
141
138
if (rules != null ) {
142
139
for (FlowRule rule : rules ) {
@@ -152,18 +149,20 @@ public static boolean isOtherOrigin(String origin, String resourceName) {
152
149
private static final class FlowPropertyListener implements PropertyListener <List <FlowRule >> {
153
150
154
151
@ Override
155
- public void configUpdate (List <FlowRule > value ) {
152
+ public synchronized void configUpdate (List <FlowRule > value ) {
156
153
Map <String , List <FlowRule >> rules = FlowRuleUtil .buildFlowRuleMap (value );
157
- //the rules was always not null, it's no need to check nullable
158
- //remove checking to avoid IDE warning
159
- flowRules . set ( rules );
154
+ if ( rules != null ) {
155
+ flowRules = rules ;
156
+ }
160
157
RecordLog .info ("[FlowRuleManager] Flow rules received: {}" , rules );
161
158
}
162
159
163
160
@ Override
164
- public void configLoad (List <FlowRule > conf ) {
161
+ public synchronized void configLoad (List <FlowRule > conf ) {
165
162
Map <String , List <FlowRule >> rules = FlowRuleUtil .buildFlowRuleMap (conf );
166
- flowRules .set (rules );
163
+ if (rules != null ) {
164
+ flowRules = rules ;
165
+ }
167
166
RecordLog .info ("[FlowRuleManager] Flow rules loaded: {}" , rules );
168
167
}
169
168
}
0 commit comments