From 44eb3e3f973a641b5edffdda1d688c75c9bb9b0a Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Thu, 10 Dec 2020 19:51:52 +0530 Subject: [PATCH] MM-31326: Fix Striped LRU to have minimum of 1 bucket (#16531) * MM-31326: Fix Striped LRU to have minimum of 1 bucket https://mattermost.atlassian.net/browse/MM-31236 ```release-note NONE ``` * Using a maxInt function --- app/server.go | 11 +++++++++-- store/localcachelayer/layer.go | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/server.go b/app/server.go index 9ddf68cc0d..92ab55d3cc 100644 --- a/app/server.go +++ b/app/server.go @@ -291,7 +291,7 @@ func NewServer(options ...Option) (*Server, error) { if s.sessionCache, err = s.CacheProvider.NewCache(&cache.CacheOptions{ Size: model.SESSION_CACHE_SIZE, Striped: true, - StripedBuckets: runtime.NumCPU() - 1, + StripedBuckets: maxInt(runtime.NumCPU()-1, 1), }); err != nil { return nil, errors.Wrap(err, "Unable to create session cache") } @@ -303,7 +303,7 @@ func NewServer(options ...Option) (*Server, error) { if s.statusCache, err = s.CacheProvider.NewCache(&cache.CacheOptions{ Size: model.STATUS_CACHE_SIZE, Striped: true, - StripedBuckets: runtime.NumCPU() - 1, + StripedBuckets: maxInt(runtime.NumCPU()-1, 1), }); err != nil { return nil, errors.Wrap(err, "Unable to create status cache") } @@ -564,6 +564,13 @@ func NewServer(options ...Option) (*Server, error) { return s, nil } +func maxInt(a, b int) int { + if a > b { + return a + } + return b +} + func (s *Server) RunJobs() { if s.runjobs { s.Go(func() { diff --git a/store/localcachelayer/layer.go b/store/localcachelayer/layer.go index 34b796cdd8..179000e79c 100644 --- a/store/localcachelayer/layer.go +++ b/store/localcachelayer/layer.go @@ -133,7 +133,7 @@ func NewLocalCacheLayer(baseStore store.Store, metrics einterfaces.MetricsInterf DefaultExpiry: ROLE_CACHE_SEC * time.Second, InvalidateClusterEvent: model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_ROLES, Striped: true, - StripedBuckets: runtime.NumCPU() - 1, + StripedBuckets: maxInt(runtime.NumCPU()-1, 1), }); err != nil { return } @@ -271,7 +271,7 @@ func NewLocalCacheLayer(baseStore store.Store, metrics einterfaces.MetricsInterf DefaultExpiry: USER_PROFILE_BY_ID_SEC * time.Second, InvalidateClusterEvent: model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_PROFILE_BY_IDS, Striped: true, - StripedBuckets: runtime.NumCPU() - 1, + StripedBuckets: maxInt(runtime.NumCPU()-1, 1), }); err != nil { return } @@ -319,6 +319,13 @@ func NewLocalCacheLayer(baseStore store.Store, metrics einterfaces.MetricsInterf return } +func maxInt(a, b int) int { + if a > b { + return a + } + return b +} + func (s LocalCacheStore) Reaction() store.ReactionStore { return s.reaction }