apache / apache/shenyu

[BUG] Scale rule deletion does not evict entries from ScaleRuleCache

Open
#6,972 1 comment 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
Java
Stars
8.8k
Forks
3.1k
Avg merge
7d 1h
Merged PRs (30d)
85

Description

## 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`

```java
/**
* delete rules.
*
* @param ids primary key
* @return rows int
*/
public int delete(final List 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`
```java
public void addOrUpdateRuleToCache(final ScaleRuleDO rule) {
ruleCache.put(rule.getMetricName(), rule);
}

public void removeRulesFromCache(final List 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:
```java
public void removeRulesByIdsFromCache(final List ids) {
final Set 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);`

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with ScaleRuleServiceImpl.java and ScaleRuleCache.java to trace the delete flow and confirm that the cache is keyed by metricName while deletion receives rule IDs. Update the eviction path so IDs are matched against ScaleRuleDO.getId(), then verify that deleting a rule removes its cached metricName entry without affecting other rules.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.