juicewcode opened a new issue, #6972:
URL: https://github.com/apache/shenyu/issues/6972

   ### Is there an existing issue for this?
   
   - [x] I have searched the existing issues
   
   ### Current Behavior
   
     ## 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<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`
    ```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:
     ```java
     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);`
   
   ### Expected Behavior
   
   _No response_
   
   ### Steps To Reproduce
   
   _No response_
   
   ### Environment
   
   ```markdown
   ShenYu version(s):
   ```
   
   ### Debug logs
   
   _No response_
   
   ### Anything else?
   
   _No response_


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to