From e2b0c12e193adf3e8cc52aac0aae3103879b88d0 Mon Sep 17 00:00:00 2001 From: Garrett Bates Date: Wed, 3 Jun 2026 15:30:21 -0400 Subject: [PATCH] add support for events cache background eviction --- common/dynamicconfig/constants.go | 7 +++++++ common/dynamicconfig/shared_constants.go | 6 ++++++ service/history/configs/config.go | 2 ++ service/history/events/cache.go | 7 +++++-- service/history/events/cache_test.go | 4 ++++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/common/dynamicconfig/constants.go b/common/dynamicconfig/constants.go index d052cfc87fa..4d6931af6e6 100644 --- a/common/dynamicconfig/constants.go +++ b/common/dynamicconfig/constants.go @@ -1755,6 +1755,13 @@ This can help reduce effects of shard movement.`, false, `EnableHostLevelEventsCache controls if the events cache is host level. Requires service restart to take effect.`, ) + EventsCacheBackgroundEvict = NewGlobalTypedSetting( + "history.eventsCacheBackgroundEvict", + DefaultEventsCacheBackgroundEvictSettings, + `EventsCacheBackgroundEvict configures background processing to purge expired entries from the events cache. +Requires service restart to take effect.`, + ) + AcquireShardInterval = NewGlobalDurationSetting( "history.acquireShardInterval", time.Minute, diff --git a/common/dynamicconfig/shared_constants.go b/common/dynamicconfig/shared_constants.go index 9fba872dba5..b92e78e1cba 100644 --- a/common/dynamicconfig/shared_constants.go +++ b/common/dynamicconfig/shared_constants.go @@ -118,6 +118,12 @@ var DefaultHistoryCacheBackgroundEvictSettings = CacheBackgroundEvictSettings{ MaxEntryPerCall: 1024, } +var DefaultEventsCacheBackgroundEvictSettings = CacheBackgroundEvictSettings{ + Enabled: false, + LoopInterval: 1 * time.Minute, + MaxEntryPerCall: 1024, +} + type PartitionScaleAllowedDrift struct { // Delta and Ratio controls how far off client counts can be before we reject an RPC. // If the client count is within the delta, it's allowed. Also, if the ratio of diff --git a/service/history/configs/config.go b/service/history/configs/config.go index 58fbbe6ed6c..590fba8700c 100644 --- a/service/history/configs/config.go +++ b/service/history/configs/config.go @@ -84,6 +84,7 @@ type Config struct { // Change of these configs require service restart EnableHostLevelEventsCache dynamicconfig.BoolPropertyFn EventsHostLevelCacheMaxSizeBytes dynamicconfig.IntPropertyFn + EventsCacheBackgroundEvict dynamicconfig.TypedPropertyFn[dynamicconfig.CacheBackgroundEvictSettings] // ShardController settings RangeSizeBits uint @@ -507,6 +508,7 @@ func NewConfig( EventsHostLevelCacheMaxSizeBytes: dynamicconfig.EventsHostLevelCacheMaxSizeBytes.Get(dc), // 256MB EventsCacheTTL: dynamicconfig.EventsCacheTTL.Get(dc), EnableHostLevelEventsCache: dynamicconfig.EnableHostLevelEventsCache.Get(dc), + EventsCacheBackgroundEvict: dynamicconfig.EventsCacheBackgroundEvict.Get(dc), RangeSizeBits: 20, // 20 bits for sequencer, 2^20 sequence number for any range diff --git a/service/history/events/cache.go b/service/history/events/cache.go index a4362f21009..5a484067cbc 100644 --- a/service/history/events/cache.go +++ b/service/history/events/cache.go @@ -10,6 +10,7 @@ import ( "go.temporal.io/api/serviceerror" "go.temporal.io/server/common" "go.temporal.io/server/common/cache" + "go.temporal.io/server/common/dynamicconfig" "go.temporal.io/server/common/log" "go.temporal.io/server/common/log/tag" "go.temporal.io/server/common/metrics" @@ -60,7 +61,7 @@ func NewHostLevelEventsCache( logger log.Logger, disabled bool, ) Cache { - return newEventsCache(executionManager, handler, logger, config.EventsHostLevelCacheMaxSizeBytes(), config.EventsCacheTTL(), disabled) + return newEventsCache(executionManager, handler, logger, config.EventsHostLevelCacheMaxSizeBytes(), config.EventsCacheTTL(), config.EventsCacheBackgroundEvict, disabled) } func NewShardLevelEventsCache( @@ -70,7 +71,7 @@ func NewShardLevelEventsCache( logger log.Logger, disabled bool, ) Cache { - return newEventsCache(executionManager, handler, logger, config.EventsShardLevelCacheMaxSizeBytes(), config.EventsCacheTTL(), disabled) + return newEventsCache(executionManager, handler, logger, config.EventsShardLevelCacheMaxSizeBytes(), config.EventsCacheTTL(), config.EventsCacheBackgroundEvict, disabled) } func newEventsCache( @@ -79,10 +80,12 @@ func newEventsCache( logger log.Logger, maxSize int, ttl time.Duration, + backgroundEvict dynamicconfig.TypedPropertyFn[dynamicconfig.CacheBackgroundEvictSettings], disabled bool, ) *CacheImpl { opts := &cache.Options{} opts.TTL = ttl + opts.BackgroundEvict = backgroundEvict taggedMetricHandler := metricsHandler.WithTags(metrics.CacheTypeTag(metrics.EventsCacheTypeTagValue)) return &CacheImpl{ diff --git a/service/history/events/cache_test.go b/service/history/events/cache_test.go index 95b0dbc58d7..7bc81c9966d 100644 --- a/service/history/events/cache_test.go +++ b/service/history/events/cache_test.go @@ -11,6 +11,7 @@ import ( enumspb "go.temporal.io/api/enums/v1" historypb "go.temporal.io/api/history/v1" "go.temporal.io/server/common" + "go.temporal.io/server/common/dynamicconfig" "go.temporal.io/server/common/log" "go.temporal.io/server/common/metrics" "go.temporal.io/server/common/namespace" @@ -66,6 +67,9 @@ func (s *eventsCacheSuite) newTestEventsCache() *CacheImpl { s.logger, 32, time.Minute, + func() dynamicconfig.CacheBackgroundEvictSettings { + return dynamicconfig.DefaultEventsCacheBackgroundEvictSettings + }, false) }