Skip to content

Commit

Permalink
MM-62077: Fix cluster broadcast for LRU caches (mattermost#29488)
Browse files Browse the repository at this point in the history
We were incorrectly not broadcasting status cache updates
inspite of that being an LRU cache.

We were also not doing it for profilesInChannel cache.
Now we fix it by properly checking the invalidationEvent
which is something local to the cache itself rather
than the cache provider.

https://mattermost.atlassian.net/browse/MM-62077
```release-note
NONE
```
  • Loading branch information
agnivade authored Dec 10, 2024
1 parent 69df5df commit 0f5d160
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/channels/app/platform/cluster_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (ps *PlatformService) InvalidateAllCachesSkipSend() {
func (ps *PlatformService) InvalidateAllCaches() *model.AppError {
ps.InvalidateAllCachesSkipSend()

if ps.clusterIFace != nil && *ps.Config().CacheSettings.CacheType == model.CacheTypeLRU {
if ps.clusterIFace != nil {
msg := &model.ClusterMessage{
Event: model.ClusterEventInvalidateAllCaches,
SendType: model.ClusterSendReliable,
Expand Down
2 changes: 1 addition & 1 deletion server/channels/app/platform/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (ps *PlatformService) AddStatusCacheSkipClusterSend(status *model.Status) {
func (ps *PlatformService) AddStatusCache(status *model.Status) {
ps.AddStatusCacheSkipClusterSend(status)

if ps.Cluster() != nil && *ps.Config().CacheSettings.CacheType == model.CacheTypeLRU {
if ps.Cluster() != nil {
statusJSON, err := json.Marshal(status)
if err != nil {
ps.logger.Warn("Failed to encode status to JSON", mlog.Err(err))
Expand Down
6 changes: 3 additions & 3 deletions server/channels/store/localcachelayer/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func (s *LocalCacheStore) doInvalidateCacheCluster(cache cache.Cache, key string
if err != nil {
s.logger.Warn("Error while removing cache entry", mlog.Err(err), mlog.String("cache_name", cache.Name()))
}
if s.cluster != nil && s.cacheType == model.CacheTypeLRU {
if s.cluster != nil && cache.GetInvalidateClusterEvent() != model.ClusterEventNone {
msg := &model.ClusterMessage{
Event: cache.GetInvalidateClusterEvent(),
SendType: model.ClusterSendBestEffort,
Expand All @@ -466,7 +466,7 @@ func (s *LocalCacheStore) doMultiInvalidateCacheCluster(cache cache.Cache, keys
if err != nil {
s.logger.Warn("Error while removing cache entry", mlog.Err(err), mlog.String("cache_name", cache.Name()))
}
if s.cluster != nil && s.cacheType == model.CacheTypeLRU {
if s.cluster != nil && cache.GetInvalidateClusterEvent() != model.ClusterEventNone {
for _, key := range keys {
msg := &model.ClusterMessage{
Event: cache.GetInvalidateClusterEvent(),
Expand Down Expand Up @@ -538,7 +538,7 @@ func (s *LocalCacheStore) doClearCacheCluster(cache cache.Cache) {
if err != nil {
s.logger.Warn("Error while purging cache", mlog.Err(err), mlog.String("cache_name", cache.Name()))
}
if s.cluster != nil && s.cacheType == model.CacheTypeLRU {
if s.cluster != nil && cache.GetInvalidateClusterEvent() != model.ClusterEventNone {
msg := &model.ClusterMessage{
Event: cache.GetInvalidateClusterEvent(),
SendType: model.ClusterSendBestEffort,
Expand Down
34 changes: 34 additions & 0 deletions server/channels/store/localcachelayer/layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
"github.com/mattermost/mattermost/server/v8/channels/store"
"github.com/mattermost/mattermost/server/v8/channels/store/sqlstore"
"github.com/mattermost/mattermost/server/v8/channels/store/storetest"
"github.com/mattermost/mattermost/server/v8/channels/testlib"
"github.com/mattermost/mattermost/server/v8/platform/services/cache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -148,3 +151,34 @@ func tearDownStores() {
wg.Wait()
})
}

func TestClearCacheCluster(t *testing.T) {
cluster := &testlib.FakeClusterInterface{}
lc := &LocalCacheStore{
cluster: cluster,
}

c := cache.NewLRU(&cache.CacheOptions{
Size: 10,
Name: "test",
InvalidateClusterEvent: model.ClusterEventInvalidateCacheForRoles,
})

lc.doClearCacheCluster(c)
assert.Len(t, cluster.GetMessages(), 1)
expectedMsg := &model.ClusterMessage{
Event: model.ClusterEventInvalidateCacheForRoles,
SendType: model.ClusterSendBestEffort,
Data: clearCacheMessageData,
}
require.Equal(t, expectedMsg, cluster.GetMessages()[0])

c = cache.NewLRU(&cache.CacheOptions{
Size: 10,
Name: "test",
InvalidateClusterEvent: model.ClusterEventNone,
})

lc.doClearCacheCluster(c)
assert.Len(t, cluster.GetMessages(), 1)
}

0 comments on commit 0f5d160

Please sign in to comment.