Description
ScaleRuleServiceImpl.delete deletes scale rules from the database by their primary keys, but passes those primary keys
directly to ScaleRuleCache for cache eviction.
However, ScaleRuleCache stores rules with metricName as the cache key. As a result, the cache attempts to remove
entries by rule ID while its keys are metric names. Unless a rule ID happens to equal its metric name, the deleted rule
remains in the cache.
Location
shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ScaleRuleServiceImpl.java
/**
* delete rules.
*
* @param ids primary key
* @return rows int
*/
public int delete(final List<String> ids) {
int rows = scaleRuleMapper.delete(ids);
if (rows > 0) {
scaleRuleCache.removeRulesFromCache(ids);
}
return rows;
}
shenyu-admin/src/main/java/org/apache/shenyu/admin/scale/monitor/subject/cache/ScaleRuleCache.java
public void addOrUpdateRuleToCache(final ScaleRuleDO rule) {
ruleCache.put(rule.getMetricName(), rule);
}
public void removeRulesFromCache(final List<String> metricNames) {
metricNames.forEach(ruleCache::remove);
}
Suggested fix
Keep the cache keyed by metricName, but evict rules by comparing the supplied primary keys with ScaleRuleDO.getId().
For example, introduce a method with explicit semantics:
public void removeRulesByIdsFromCache(final List<String> ids) {
final Set<String> idSet = new HashSet<>(ids);
ruleCache.forEach((metricName, rule) -> {
if (idSet.contains(rule.getId())) {
ruleCache.remove(metricName, rule);
}
});
}
Then update ScaleRuleServiceImpl.delete to call:
scaleRuleCache.removeRulesByIdsFromCache(ids);
Description
ScaleRuleServiceImpl.deletedeletes scale rules from the database by their primary keys, but passes those primary keysdirectly to
ScaleRuleCachefor cache eviction.However,
ScaleRuleCachestores rules withmetricNameas the cache key. As a result, the cache attempts to removeentries by rule ID while its keys are metric names. Unless a rule ID happens to equal its metric name, the deleted rule
remains in the cache.
Location
shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ScaleRuleServiceImpl.javashenyu-admin/src/main/java/org/apache/shenyu/admin/scale/monitor/subject/cache/ScaleRuleCache.javaSuggested fix
Keep the cache keyed by metricName, but evict rules by comparing the supplied primary keys with
ScaleRuleDO.getId().For example, introduce a method with explicit semantics:
Then update
ScaleRuleServiceImpl.deleteto call:scaleRuleCache.removeRulesByIdsFromCache(ids);