Skip to content

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

Description

@juicewcode

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions