Skip to content

fix(plugin): bound match cache entries with configurable TTL (#7651) - #7664

Merged
Aias00 merged 1 commit into
apache:masterfrom
sunnysabor:fix/match-cache-ttl-7651
Oct 16, 2026
Merged

Aias00 merged 1 commit into
apache:masterfrom
sunnysabor:fix/match-cache-ttl-7651

Conversation

@sunnysabor

Copy link
Copy Markdown
Contributor

Fixes #7651.

MatchDataCache currently retains positive and negative matches until eviction or event-driven invalidation. Add expire-after-write to selector and rule caches with a 60-second default. Configure each independently through shenyu.selectorMatchCache.cache.ttl and shenyu.ruleMatchCache.cache.ttl (positive milliseconds). Existing cache methods remain available and use the default lifetime. Bootstrap YAML comments describe the reuse/staleness trade-off; event-driven invalidation remains unchanged.

TTL bounds reuse of derived match results. It cannot recover a missed update to BaseDataCache itself: expiration rematches against the gateway's current local configuration. Caffeine stops returning expired entries; physical cleanup follows its maintenance cycle. Configuration is applied when each plugin cache is created.

Validation:

  • New positive/negative selector/rule expiration regression fails with the previous non-expiring map constructor and passes with this fix.

  • ./mvnw -B -ntp -pl shenyu-plugin/shenyu-plugin-base -am test -Djacoco.skip=true passes (11-module reactor, including existing match and invalidation tests).

  • ./mvnw -B -ntp checkstyle:check passes across 213 modules.

  • Related reactor apache-rat:check passes in a clean source export with the patch. The original checkout's RAT check only flags three pre-existing untracked .omx files.

  • git diff --check passes. No performance benchmark or full-repository clean install was run.

  • I have read the contribution guidelines.

  • Tests back the changes.

  • Full ./mvnw clean install -Dmaven.javadoc.skip=true (targeted reactor tests and full Checkstyle used instead).

@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.

Summary

This is exactly the fix proposed in #7651 — thank you for picking it up.

I verified the critical detail that is easy to get wrong: WindowTinyLFUMap's four-arg constructor signature is (initialSize, expireAfterWrite, maximumSize, weakKey), and this PR calls new WindowTinyLFUMap<>(initialCapacity, ttl, maximumSize, Boolean.FALSE). So the TTL is correctly wired into expireAfterWrite(...) rather than accidentally mapping onto maximumSize.

  • ShenyuConfig.MatchCacheConfig.ttl (default 60000ms) with positive-value validation.
  • AbstractShenyuPlugin threads the TTL through cacheSelectorData / cacheRuleData.
  • MatchDataCache adds the TTL overload and uses the four-arg WindowTinyLFUMap; the old three-arg overload is kept (delegating to the default TTL) for backward compatibility.
  • testPositiveAndNegativeMatchesExpire verifies both positive and negative (empty-id) entries expire within the TTL via Awaitility.

This closes the staleness gap described in #7651 (time-based self-healing when a data-sync event is missed) while leaving the event-driven invalidation path untouched.

Non-blocking

  • The per-plugin WindowTinyLFUMap is created with the TTL at first-write time; a later change to the ttl config value does not retroactively apply to already-created maps. Acceptable (config changes effectively require a restart of the cache lifecycle), but worth a line in the docs/release notes.

CI

All build (shenyu-integrated-test-*) jobs pass; only k8s-examples-http (k8s infrastructure) fails, unrelated.

Approving.

@Aias00
Aias00 merged commit 297ac6f into apache:master Oct 16, 2026
49 of 50 checks passed
@Aias00 Aias00 added this to the 2.7.2 milestone Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactor] Add TTL / version-stamp to MatchDataCache to guard against stale cached match results

2 participants