Skip to content

fix: Fix scale rule cache eviction by using rule ids(#6972) - #6975

Open
juicewcode wants to merge 4 commits into
apache:masterfrom
juicewcode:fix/6972-scale-rule-cache-eviction
Open

fix: Fix scale rule cache eviction by using rule ids(#6972)#6975
juicewcode wants to merge 4 commits into
apache:masterfrom
juicewcode:fix/6972-scale-rule-cache-eviction

Conversation

@juicewcode

@juicewcode juicewcode commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #6972
Fixes #6623
Fix scale rule cache consistency for create, update, and delete operations.

ScaleRuleCache stores rules in a map keyed by metricName, while database operations identify rules by their primary key id. This difference could cause cached rules to become inconsistent with the database:

  • A newly created rule could be cached with a different id from the database
    record.
  • Renaming a rule could leave the old metric-name entry in the cache.
  • Deleting a rule by id could fail to remove its cache entry because the id was
    incorrectly treated as a metric-name key.

##Changes

  • Cache the same ScaleRuleDO instance that is inserted into the database,
    preserving the database-generated id.
  • Remove the old metric-name cache entry when a rule is renamed.
  • Add removeRulesByIdsFromCache to find cached rules by their database ids and remove them using their metric-name map keys.
  • Update ScaleRuleServiceImpl#delete to use removeRulesByIdsFromCache.

These changes ensure that the in-memory scale-rule cache remains consistent with the database after rules are created, renamed, or deleted.

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true.

  - Add cache eviction by database primary key.
  - Keep metric names as cache keys.
  - Add scale rule deletion to remove entries by rule ID.
@juicewcode juicewcode changed the title fix: Fix scale rule cache eviction by using rule IDs. fix: Fix scale rule cache eviction by using rule ids(#6972) Aug 23, 2026

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This delete-by-ID eviction still depends on the cached rule ID matching the database row ID, but this PR is based on master, where ScaleRuleServiceImpl#create still inserts one ScaleRuleDO and then caches a second ScaleRuleDO built from the blank-id DTO. ScaleRuleDO.buildScaleRuleDO generates a fresh UUID when the DTO id is empty, so rules created through the service can still have a cached id that differs from the DB id being deleted. Could you include the create-side cache fix here, or base this after the fix from #6973, so delete-by-ID eviction can actually find newly created cached rules?

  - Reuse the persisted entity after create.
  - Reuse the updated entity after update.
@juicewcode

juicewcode commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

This delete-by-ID eviction still depends on the cached rule ID matching the database row ID, but this PR is based on master, where ScaleRuleServiceImpl#create still inserts one ScaleRuleDO and then caches a second ScaleRuleDO built from the blank-id DTO. ScaleRuleDO.buildScaleRuleDO generates a fresh UUID when the DTO id is empty, so rules created through the service can still have a cached id that differs from the DB id being deleted. Could you include the create-side cache fix here, or base this after the fix from #6973, so delete-by-ID eviction can actually find newly created cached rules?

This change is based on the cache consistency fix from PR #6973 .

The following cache consistency issues are addressed:

  • Newly created rules are cached using the same ScaleRuleDO instance that was inserted into the database, ensuring that
    the cached rule id matches the database id.
  • For deletion, cached rules are located by their database primary keys and removed using their metric-name cache keys.
  • When a rule's metricName is changed, the previous metric-name cache entry is removed before the updated rule is cached.

The PR body has also been updated to reflect these changes and the problems they resolve.

  - Base the change on the cache consistency fix from pr apache#6973
  - Remove stale cache entries by database rule id during deletion
  - Remove the old metric-name cache key when a rule is renamed
  - Keep the persisted entity id when caching newly created rules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants