diff --git a/common/cache/lru.go b/common/cache/lru.go index 27ef6e4c6d5..d2c804e8f60 100644 --- a/common/cache/lru.go +++ b/common/cache/lru.go @@ -63,6 +63,7 @@ type ( pin bool timeSource clock.TimeSource metricsHandler metrics.Handler + enabled bool } iteratorImpl struct { @@ -78,6 +79,10 @@ type ( refCount int size int } + // + //hasUpdate interface { + // UpdateRegistry(ctx context.Context) update.Registry + //} ) // Close closes the iterator @@ -154,12 +159,12 @@ func (entry *entryImpl) CreateTime() time.Time { // New creates a new cache with the given options func New(maxSize int, opts *Options) Cache { - return NewWithMetrics(maxSize, opts, metrics.NoopMetricsHandler) + return NewWithMetrics(maxSize, opts, metrics.NoopMetricsHandler, false) } // NewWithMetrics creates a new cache that will emit capacity and ttl metrics. // handler should be tagged with metrics.CacheTypeTag. -func NewWithMetrics(maxSize int, opts *Options, handler metrics.Handler) Cache { +func NewWithMetrics(maxSize int, opts *Options, handler metrics.Handler, enabled bool) Cache { if opts == nil { opts = &Options{} } @@ -181,6 +186,7 @@ func NewWithMetrics(maxSize int, opts *Options, handler metrics.Handler) Cache { onEvict: opts.OnEvict, timeSource: timeSource, metricsHandler: handler, + enabled: enabled, } } @@ -273,6 +279,9 @@ func (c *lru) Release(key interface{}) { if entry.refCount == 0 { c.pinnedSize -= entry.Size() metrics.CachePinnedUsage.With(c.metricsHandler).Record(float64(c.pinnedSize)) + if !c.enabled { + c.tryEvictAndGetPreviousElement(entry, c.byKey[key]) + } } // Entry size might have changed. Recalculate size and evict entries if necessary. newEntrySize := getSize(entry.value) diff --git a/common/cache/lru_test.go b/common/cache/lru_test.go index 7cf3635674f..6147093b401 100644 --- a/common/cache/lru_test.go +++ b/common/cache/lru_test.go @@ -57,7 +57,7 @@ func TestLRU(t *testing.T) { metricsHandler := metricstest.NewCaptureHandler() capture := metricsHandler.StartCapture() - cache := NewWithMetrics(4, nil, metricsHandler) + cache := NewWithMetrics(4, nil, metricsHandler, true) cache.Put("A", "Foo") assert.Equal(t, "Foo", cache.Get("A")) @@ -148,6 +148,7 @@ func TestLRUWithTTL(t *testing.T) { TimeSource: timeSource, }, metricsHandler, + false, ) cache.Put("A", "foo") assert.Equal(t, "foo", cache.Get("A")) @@ -249,6 +250,7 @@ func TestTTLWithPin(t *testing.T) { TimeSource: timeSource, }, metricsHandler, + false, ) capture := metricsHandler.StartCapture() diff --git a/common/dynamicconfig/constants.go b/common/dynamicconfig/constants.go index f6d2b280ec0..32d1751b10c 100644 --- a/common/dynamicconfig/constants.go +++ b/common/dynamicconfig/constants.go @@ -2626,4 +2626,9 @@ WorkerActivitiesPerSecond, MaxConcurrentActivityTaskPollers. false, `ActivityAPIsEnabled is a "feature enable" flag. `, ) + WorkflowCacheEnabled = NewGlobalBoolSetting( + "history.workflowCacheEnabled", + true, + ``, + ) ) diff --git a/common/nexus/endpoint_registry.go b/common/nexus/endpoint_registry.go index 312e7fb8fc6..1700015eb0b 100644 --- a/common/nexus/endpoint_registry.go +++ b/common/nexus/endpoint_registry.go @@ -126,7 +126,7 @@ func NewEndpointRegistry( logger: logger, readThroughCacheByID: cache.NewWithMetrics(config.readThroughCacheSize(), &cache.Options{ TTL: config.readThroughCacheTTL(), - }, metricsHandler.WithTags(metrics.CacheTypeTag(metrics.NexusEndpointRegistryReadThroughCacheTypeTagValue))), + }, metricsHandler.WithTags(metrics.CacheTypeTag(metrics.NexusEndpointRegistryReadThroughCacheTypeTagValue)), true), } } diff --git a/service/history/configs/config.go b/service/history/configs/config.go index de8d33d8191..fd1d64a813e 100644 --- a/service/history/configs/config.go +++ b/service/history/configs/config.go @@ -376,7 +376,8 @@ type Config struct { BreakdownMetricsByTaskQueue dynamicconfig.BoolPropertyFnWithTaskQueueFilter - LogAllReqErrors dynamicconfig.BoolPropertyFnWithNamespaceFilter + LogAllReqErrors dynamicconfig.BoolPropertyFnWithNamespaceFilter + WorkflowCacheEnabled dynamicconfig.BoolPropertyFn } // NewConfig returns new service config with default values @@ -686,7 +687,8 @@ func NewConfig( BreakdownMetricsByTaskQueue: dynamicconfig.MetricsBreakdownByTaskQueue.Get(dc), - LogAllReqErrors: dynamicconfig.LogAllReqErrors.Get(dc), + LogAllReqErrors: dynamicconfig.LogAllReqErrors.Get(dc), + WorkflowCacheEnabled: dynamicconfig.WorkflowCacheEnabled.Get(dc), } return cfg diff --git a/service/history/events/cache.go b/service/history/events/cache.go index 0e0b799fb2a..84ac4aac437 100644 --- a/service/history/events/cache.go +++ b/service/history/events/cache.go @@ -110,7 +110,7 @@ func newEventsCache( taggedMetricHandler := metricsHandler.WithTags(metrics.CacheTypeTag(metrics.EventsCacheTypeTagValue)) return &CacheImpl{ - Cache: cache.NewWithMetrics(maxSize, opts, taggedMetricHandler), + Cache: cache.NewWithMetrics(maxSize, opts, taggedMetricHandler, true), executionManager: executionManager, metricsHandler: taggedMetricHandler, logger: logger, diff --git a/service/history/replication/progress_cache.go b/service/history/replication/progress_cache.go index cbde60eb945..630f60be0c8 100644 --- a/service/history/replication/progress_cache.go +++ b/service/history/replication/progress_cache.go @@ -82,7 +82,7 @@ func NewProgressCache( TTL: config.ReplicationProgressCacheTTL(), } return &progressCacheImpl{ - cache: cache.NewWithMetrics(maxSize, opts, handler.WithTags(metrics.CacheTypeTag(metrics.MutableStateCacheTypeTagValue))), + cache: cache.NewWithMetrics(maxSize, opts, handler.WithTags(metrics.CacheTypeTag(metrics.MutableStateCacheTypeTagValue)), true), } } diff --git a/service/history/workflow/cache/cache.go b/service/history/workflow/cache/cache.go index 697d7240764..35c03ba9aa6 100644 --- a/service/history/workflow/cache/cache.go +++ b/service/history/workflow/cache/cache.go @@ -125,6 +125,7 @@ func NewHostLevelCache( config.HistoryCacheNonUserContextLockTimeout(), logger, handler, + config.WorkflowCacheEnabled(), ) } @@ -143,6 +144,7 @@ func NewShardLevelCache( config.HistoryCacheNonUserContextLockTimeout(), logger, handler, + config.WorkflowCacheEnabled(), ) } @@ -152,6 +154,7 @@ func newCache( nonUserContextLockTimeout time.Duration, logger log.Logger, handler metrics.Handler, + cacheEnabled bool, ) Cache { opts := &cache.Options{ TTL: ttl, @@ -193,7 +196,7 @@ func newCache( }, } - withMetrics := cache.NewWithMetrics(size, opts, handler.WithTags(metrics.CacheTypeTag(metrics.MutableStateCacheTypeTagValue))) + withMetrics := cache.NewWithMetrics(size, opts, handler.WithTags(metrics.CacheTypeTag(metrics.MutableStateCacheTypeTagValue)), cacheEnabled) return &cacheImpl{ Cache: withMetrics,