|
|
@@ -16,12 +16,14 @@ |
|
|
|
package com.alibaba.csp.sentinel.slots.block.flow; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
|
|
|
|
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory; |
|
|
|
import com.alibaba.csp.sentinel.log.RecordLog; |
|
|
@@ -43,10 +45,11 @@ import com.alibaba.csp.sentinel.property.SentinelProperty; |
|
|
|
* |
|
|
|
* @author jialiang.linjl |
|
|
|
* @author Eric Zhao |
|
|
|
* @author Weihua |
|
|
|
*/ |
|
|
|
public class FlowRuleManager { |
|
|
|
|
|
|
|
private static final Map<String, List<FlowRule>> flowRules = new ConcurrentHashMap<String, List<FlowRule>>(); |
|
|
|
private static final AtomicReference<Map<String, List<FlowRule>>> flowRules = new AtomicReference<Map<String, List<FlowRule>>>(); |
|
|
|
|
|
|
|
private static final FlowPropertyListener LISTENER = new FlowPropertyListener(); |
|
|
|
private static SentinelProperty<List<FlowRule>> currentProperty = new DynamicSentinelProperty<List<FlowRule>>(); |
|
|
@@ -56,6 +59,7 @@ public class FlowRuleManager { |
|
|
|
new NamedThreadFactory("sentinel-metrics-record-task", true)); |
|
|
|
|
|
|
|
static { |
|
|
|
flowRules.set(Collections.<String, List<FlowRule>>emptyMap()); |
|
|
|
currentProperty.addListener(LISTENER); |
|
|
|
SCHEDULER.scheduleAtFixedRate(new MetricTimerListener(), 0, 1, TimeUnit.SECONDS); |
|
|
|
} |
|
|
@@ -83,7 +87,7 @@ public class FlowRuleManager { |
|
|
|
*/ |
|
|
|
public static List<FlowRule> getRules() { |
|
|
|
List<FlowRule> rules = new ArrayList<FlowRule>(); |
|
|
|
for (Map.Entry<String, List<FlowRule>> entry : flowRules.entrySet()) { |
|
|
|
for (Map.Entry<String, List<FlowRule>> entry : flowRules.get().entrySet()) { |
|
|
|
rules.addAll(entry.getValue()); |
|
|
|
} |
|
|
|
return rules; |
|
|
@@ -99,11 +103,11 @@ public class FlowRuleManager { |
|
|
|
} |
|
|
|
|
|
|
|
static Map<String, List<FlowRule>> getFlowRuleMap() { |
|
|
|
return flowRules; |
|
|
|
return flowRules.get(); |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean hasConfig(String resource) { |
|
|
|
return flowRules.containsKey(resource); |
|
|
|
return flowRules.get().containsKey(resource); |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean isOtherOrigin(String origin, String resourceName) { |
|
|
@@ -111,7 +115,7 @@ public class FlowRuleManager { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
List<FlowRule> rules = flowRules.get(resourceName); |
|
|
|
List<FlowRule> rules = flowRules.get().get(resourceName); |
|
|
|
|
|
|
|
if (rules != null) { |
|
|
|
for (FlowRule rule : rules) { |
|
|
@@ -129,21 +133,17 @@ public class FlowRuleManager { |
|
|
|
@Override |
|
|
|
public void configUpdate(List<FlowRule> value) { |
|
|
|
Map<String, List<FlowRule>> rules = FlowRuleUtil.buildFlowRuleMap(value); |
|
|
|
if (rules != null) { |
|
|
|
flowRules.clear(); |
|
|
|
flowRules.putAll(rules); |
|
|
|
} |
|
|
|
RecordLog.info("[FlowRuleManager] Flow rules received: {}", flowRules); |
|
|
|
//the rules was always not null, it's no need to check nullable |
|
|
|
//remove checking to avoid IDE warning |
|
|
|
flowRules.set(rules); |
|
|
|
RecordLog.info("[FlowRuleManager] Flow rules received: {}", rules); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void configLoad(List<FlowRule> conf) { |
|
|
|
Map<String, List<FlowRule>> rules = FlowRuleUtil.buildFlowRuleMap(conf); |
|
|
|
if (rules != null) { |
|
|
|
flowRules.clear(); |
|
|
|
flowRules.putAll(rules); |
|
|
|
} |
|
|
|
RecordLog.info("[FlowRuleManager] Flow rules loaded: {}", flowRules); |
|
|
|
flowRules.set(rules); |
|
|
|
RecordLog.info("[FlowRuleManager] Flow rules loaded: {}", rules); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|