Parcourir la source

Improve deprecated ParameterMetric purge mechanism (#1372)

* Clear useless data in ParameterMetric for all removed rules
master
wavesZh GitHub il y a 4 ans
Parent
révision
096a9ebf7d
Aucune clé connue n'a été trouvée dans la base pour cette signature ID de la clé GPG: 4AEE18F83AFDEB23
4 fichiers modifiés avec 66 ajouts et 15 suppressions
  1. +9
    -2
      sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/rule/GatewayRuleManager.java
  2. +19
    -13
      sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleManager.java
  3. +8
    -0
      sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java
  4. +30
    -0
      sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleManagerTest.java

+ 9
- 2
sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/rule/GatewayRuleManager.java Voir le fichier

@@ -217,10 +217,17 @@ public final class GatewayRuleManager {
}

// Clear unused parameter metrics.
Set<String> previousResources = CONVERTED_PARAM_RULE_MAP.keySet();
for (String resource : previousResources) {
for (Map.Entry<String, List<ParamFlowRule>> entry : CONVERTED_PARAM_RULE_MAP.entrySet()) {
String resource = entry.getKey();
if (!newRuleMap.containsKey(resource)) {
ParameterMetricStorage.clearParamMetricForResource(resource);
continue;
}
List<ParamFlowRule> newRuleList = newRuleMap.get(resource);
List<ParamFlowRule> oldRuleList = new ArrayList<>(entry.getValue());
oldRuleList.removeAll(newRuleList);
for (ParamFlowRule rule : oldRuleList) {
ParameterMetricStorage.getParamMetricForResource(resource).clearForRule(rule);
}
}



+ 19
- 13
sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleManager.java Voir le fichier

@@ -18,7 +18,6 @@ package com.alibaba.csp.sentinel.slots.block.flow.param;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.csp.sentinel.log.RecordLog;
@@ -36,7 +35,7 @@ import com.alibaba.csp.sentinel.util.AssertUtil;
*/
public final class ParamFlowRuleManager {

private static final Map<String, List<ParamFlowRule>> paramFlowRules = new ConcurrentHashMap<>();
private static final Map<String, List<ParamFlowRule>> PARAM_FLOW_RULES = new ConcurrentHashMap<>();

private final static RulePropertyListener PROPERTY_LISTENER = new RulePropertyListener();
private static SentinelProperty<List<ParamFlowRule>> currentProperty = new DynamicSentinelProperty<>();
@@ -76,11 +75,11 @@ public final class ParamFlowRuleManager {
}

public static List<ParamFlowRule> getRulesOfResource(String resourceName) {
return new ArrayList<>(paramFlowRules.get(resourceName));
return new ArrayList<>(PARAM_FLOW_RULES.get(resourceName));
}

public static boolean hasRules(String resourceName) {
List<ParamFlowRule> rules = paramFlowRules.get(resourceName);
List<ParamFlowRule> rules = PARAM_FLOW_RULES.get(resourceName);
return rules != null && !rules.isEmpty();
}

@@ -91,7 +90,7 @@ public final class ParamFlowRuleManager {
*/
public static List<ParamFlowRule> getRules() {
List<ParamFlowRule> rules = new ArrayList<>();
for (Map.Entry<String, List<ParamFlowRule>> entry : paramFlowRules.entrySet()) {
for (Map.Entry<String, List<ParamFlowRule>> entry : PARAM_FLOW_RULES.entrySet()) {
rules.addAll(entry.getValue());
}
return rules;
@@ -103,20 +102,20 @@ public final class ParamFlowRuleManager {
public void configUpdate(List<ParamFlowRule> list) {
Map<String, List<ParamFlowRule>> rules = aggregateAndPrepareParamRules(list);
if (rules != null) {
paramFlowRules.clear();
paramFlowRules.putAll(rules);
PARAM_FLOW_RULES.clear();
PARAM_FLOW_RULES.putAll(rules);
}
RecordLog.info("[ParamFlowRuleManager] Parameter flow rules received: " + paramFlowRules);
RecordLog.info("[ParamFlowRuleManager] Parameter flow rules received: " + PARAM_FLOW_RULES);
}

@Override
public void configLoad(List<ParamFlowRule> list) {
Map<String, List<ParamFlowRule>> rules = aggregateAndPrepareParamRules(list);
if (rules != null) {
paramFlowRules.clear();
paramFlowRules.putAll(rules);
PARAM_FLOW_RULES.clear();
PARAM_FLOW_RULES.putAll(rules);
}
RecordLog.info("[ParamFlowRuleManager] Parameter flow rules received: " + paramFlowRules);
RecordLog.info("[ParamFlowRuleManager] Parameter flow rules received: " + PARAM_FLOW_RULES);
}

private Map<String, List<ParamFlowRule>> aggregateAndPrepareParamRules(List<ParamFlowRule> list) {
@@ -129,10 +128,17 @@ public final class ParamFlowRuleManager {
}

// Clear unused parameter metrics.
Set<String> previousResources = paramFlowRules.keySet();
for (String resource : previousResources) {
for (Map.Entry<String, List<ParamFlowRule>> entry : PARAM_FLOW_RULES.entrySet()) {
String resource = entry.getKey();
if (!newRuleMap.containsKey(resource)) {
ParameterMetricStorage.clearParamMetricForResource(resource);
continue;
}
List<ParamFlowRule> newRuleList = newRuleMap.get(resource);
List<ParamFlowRule> oldRuleList = new ArrayList<>(entry.getValue());
oldRuleList.removeAll(newRuleList);
for (ParamFlowRule rule : oldRuleList) {
ParameterMetricStorage.getParamMetricForResource(resource).clearForRule(rule);
}
}



+ 8
- 0
sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java Voir le fichier

@@ -84,6 +84,14 @@ public class ParameterMetric {
}
}

public void clearForRule(ParamFlowRule rule) {
synchronized (lock) {
ruleTimeCounters.remove(rule);
ruleTokenCounter.remove(rule);
threadCountMap.remove(rule.getParamIdx());
}
}

public void initialize(ParamFlowRule rule) {
if (!ruleTimeCounters.containsKey(rule)) {
synchronized (lock) {


+ 30
- 0
sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowRuleManagerTest.java Voir le fichier

@@ -66,6 +66,36 @@ public class ParamFlowRuleManagerTest {
ParameterMetricStorage.getParamMetricForResource(resA));
}

@Test
public void testLoadParamRulesClearingUnusedMetricsForRule() {
final String resA = "resA";
ParamFlowRule ruleA1 = new ParamFlowRule(resA)
.setCount(1)
.setParamIdx(0);
ParamFlowRule ruleA2 = new ParamFlowRule(resA)
.setCount(2)
.setParamIdx(1);

ParamFlowRuleManager.loadRules(Arrays.asList(ruleA1, ruleA2));
ParameterMetric metric = new ParameterMetric();
metric.initialize(ruleA1);
metric.initialize(ruleA2);
ParameterMetricStorage.getMetricsMap().put(resA, metric);

ParameterMetric metric1 = ParameterMetricStorage.getParamMetricForResource(resA);
assertNotNull(metric1);
assertNotNull(metric1.getRuleTimeCounter(ruleA1));
assertNotNull(metric1.getRuleTimeCounter(ruleA2));

ParamFlowRuleManager.loadRules(Arrays.asList(ruleA1));

ParameterMetric metric2 = ParameterMetricStorage.getParamMetricForResource(resA);
assertNotNull(metric2);
assertNotNull(metric2.getRuleTimeCounter(ruleA1));
assertNull(metric2.getRuleTimeCounter(ruleA2));
}


@Test
public void testLoadParamRulesAndGet() {
final String resA = "abc";


Chargement…
Annuler
Enregistrer