MM-25516: Changed to byte slice instead of string for cluster messages (#17998)

* MM-25516: Changed to byte slice instead of string for cluster messages

https://mattermost.atlassian.net/browse/MM-25116

Testing:
Manually tested.
Load-tested with Cluster Controller.

I looked into changing the serialization method to use msgpack,
but the ClusterMessage struct was mainly used for only 3 fields
which didn't lead to much of a CPU time improvement, whereas
actually led to more allocations using msgpack. Hence, I chose
to remain with JSON.

```
name              old time/op    new time/op    delta
ClusterMarshal-8    3.51µs ± 1%    3.10µs ± 2%  -11.59%  (p=0.000 n=9+10)

name              old alloc/op   new alloc/op   delta
ClusterMarshal-8      776B ± 0%     1000B ± 0%  +28.87%  (p=0.000 n=10+10)

name              old allocs/op  new allocs/op  delta
ClusterMarshal-8      12.0 ± 0%      13.0 ± 0%   +8.33%  (p=0.000 n=10+10)
```

```release-note
Changed the field type of Data in model.ClusterMessage to []byte from string.
```

* Trigger CI
```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2021-07-26 13:41:20 +05:30
коммит произвёл GitHub
родитель 23800326a0
Коммит 7be61af24f
28 изменённых файлов: 112 добавлений и 130 удалений

Просмотреть файл

@@ -4,6 +4,8 @@
package localcachelayer
import (
"bytes"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/store"
)
@@ -14,34 +16,34 @@ type LocalCacheChannelStore struct {
}
func (s *LocalCacheChannelStore) handleClusterInvalidateChannelMemberCounts(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.channelMemberCountsCache.Purge()
} else {
s.rootStore.channelMemberCountsCache.Remove(msg.Data)
s.rootStore.channelMemberCountsCache.Remove(string(msg.Data))
}
}
func (s *LocalCacheChannelStore) handleClusterInvalidateChannelPinnedPostCount(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.channelPinnedPostCountsCache.Purge()
} else {
s.rootStore.channelPinnedPostCountsCache.Remove(msg.Data)
s.rootStore.channelPinnedPostCountsCache.Remove(string(msg.Data))
}
}
func (s *LocalCacheChannelStore) handleClusterInvalidateChannelGuestCounts(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.channelGuestCountCache.Purge()
} else {
s.rootStore.channelGuestCountCache.Remove(msg.Data)
s.rootStore.channelGuestCountCache.Remove(string(msg.Data))
}
}
func (s *LocalCacheChannelStore) handleClusterInvalidateChannelById(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.channelByIdCache.Purge()
} else {
s.rootStore.channelByIdCache.Remove(msg.Data)
s.rootStore.channelByIdCache.Remove(string(msg.Data))
}
}

Просмотреть файл

@@ -4,6 +4,7 @@
package localcachelayer
import (
"bytes"
"context"
"sync"
@@ -22,24 +23,24 @@ type LocalCacheEmojiStore struct {
}
func (es *LocalCacheEmojiStore) handleClusterInvalidateEmojiById(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
es.rootStore.emojiCacheById.Purge()
} else {
es.emojiByIdMut.Lock()
es.emojiByIdInvalidations[msg.Data] = true
es.emojiByIdInvalidations[string(msg.Data)] = true
es.emojiByIdMut.Unlock()
es.rootStore.emojiCacheById.Remove(msg.Data)
es.rootStore.emojiCacheById.Remove(string(msg.Data))
}
}
func (es *LocalCacheEmojiStore) handleClusterInvalidateEmojiIdByName(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
es.rootStore.emojiIdCacheByName.Purge()
} else {
es.emojiByNameMut.Lock()
es.emojiByNameInvalidations[msg.Data] = true
es.emojiByNameInvalidations[string(msg.Data)] = true
es.emojiByNameMut.Unlock()
es.rootStore.emojiIdCacheByName.Remove(msg.Data)
es.rootStore.emojiIdCacheByName.Remove(string(msg.Data))
}
}

Просмотреть файл

@@ -4,6 +4,8 @@
package localcachelayer
import (
"bytes"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/store"
)
@@ -14,11 +16,11 @@ type LocalCacheFileInfoStore struct {
}
func (s *LocalCacheFileInfoStore) handleClusterInvalidateFileInfo(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.fileInfoCache.Purge()
return
}
s.rootStore.fileInfoCache.Remove(msg.Data)
s.rootStore.fileInfoCache.Remove(string(msg.Data))
}
func (s LocalCacheFileInfoStore) GetForPost(postId string, readFromMaster, includeDeleted, allowFromCache bool) ([]*model.FileInfo, error) {

Просмотреть файл

@@ -58,11 +58,11 @@ const (
TeamCacheSize = 20000
TeamCacheSec = 30 * 60
ClearCacheMessageData = ""
ChannelCacheSec = 15 * 60 // 15 mins
)
var clearCacheMessageData = []byte("")
type LocalCacheStore struct {
store.Store
metrics einterfaces.MetricsInterface
@@ -390,7 +390,7 @@ func (s *LocalCacheStore) doInvalidateCacheCluster(cache cache.Cache, key string
msg := &model.ClusterMessage{
Event: cache.GetInvalidateClusterEvent(),
SendType: model.ClusterSendBestEffort,
Data: key,
Data: []byte(key),
}
s.cluster.SendClusterMessage(msg)
}
@@ -420,7 +420,7 @@ func (s *LocalCacheStore) doClearCacheCluster(cache cache.Cache) {
msg := &model.ClusterMessage{
Event: cache.GetInvalidateClusterEvent(),
SendType: model.ClusterSendBestEffort,
Data: ClearCacheMessageData,
Data: clearCacheMessageData,
}
s.cluster.SendClusterMessage(msg)
}

Просмотреть файл

@@ -4,6 +4,7 @@
package localcachelayer
import (
"bytes"
"fmt"
"strconv"
"strings"
@@ -18,18 +19,18 @@ type LocalCachePostStore struct {
}
func (s *LocalCachePostStore) handleClusterInvalidateLastPostTime(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.lastPostTimeCache.Purge()
} else {
s.rootStore.lastPostTimeCache.Remove(msg.Data)
s.rootStore.lastPostTimeCache.Remove(string(msg.Data))
}
}
func (s *LocalCachePostStore) handleClusterInvalidateLastPosts(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.postLastPostsCache.Purge()
} else {
s.rootStore.postLastPostsCache.Remove(msg.Data)
s.rootStore.postLastPostsCache.Remove(string(msg.Data))
}
}

Просмотреть файл

@@ -4,6 +4,8 @@
package localcachelayer
import (
"bytes"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/store"
)
@@ -14,10 +16,10 @@ type LocalCacheReactionStore struct {
}
func (s *LocalCacheReactionStore) handleClusterInvalidateReaction(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.reactionCache.Purge()
} else {
s.rootStore.reactionCache.Remove(msg.Data)
s.rootStore.reactionCache.Remove(string(msg.Data))
}
}

Просмотреть файл

@@ -4,6 +4,7 @@
package localcachelayer
import (
"bytes"
"context"
"sort"
"strings"
@@ -18,18 +19,18 @@ type LocalCacheRoleStore struct {
}
func (s *LocalCacheRoleStore) handleClusterInvalidateRole(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.roleCache.Purge()
} else {
s.rootStore.roleCache.Remove(msg.Data)
s.rootStore.roleCache.Remove(string(msg.Data))
}
}
func (s *LocalCacheRoleStore) handleClusterInvalidateRolePermissions(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.rolePermissionsCache.Purge()
} else {
s.rootStore.rolePermissionsCache.Remove(msg.Data)
s.rootStore.rolePermissionsCache.Remove(string(msg.Data))
}
}

Просмотреть файл

@@ -4,6 +4,8 @@
package localcachelayer
import (
"bytes"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/store"
)
@@ -14,10 +16,10 @@ type LocalCacheSchemeStore struct {
}
func (s *LocalCacheSchemeStore) handleClusterInvalidateScheme(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.schemeCache.Purge()
} else {
s.rootStore.schemeCache.Remove(msg.Data)
s.rootStore.schemeCache.Remove(string(msg.Data))
}
}

Просмотреть файл

@@ -4,6 +4,8 @@
package localcachelayer
import (
"bytes"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/store"
)
@@ -14,10 +16,10 @@ type LocalCacheTeamStore struct {
}
func (s *LocalCacheTeamStore) handleClusterInvalidateTeam(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.teamAllTeamIdsForUserCache.Purge()
} else {
s.rootStore.teamAllTeamIdsForUserCache.Remove(msg.Data)
s.rootStore.teamAllTeamIdsForUserCache.Remove(string(msg.Data))
}
}

Просмотреть файл

@@ -4,6 +4,8 @@
package localcachelayer
import (
"bytes"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/store"
)
@@ -18,10 +20,10 @@ type LocalCacheTermsOfServiceStore struct {
}
func (s *LocalCacheTermsOfServiceStore) handleClusterInvalidateTermsOfService(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.termsOfServiceCache.Purge()
} else {
s.rootStore.termsOfServiceCache.Remove(msg.Data)
s.rootStore.termsOfServiceCache.Remove(string(msg.Data))
}
}

Просмотреть файл

@@ -4,6 +4,7 @@
package localcachelayer
import (
"bytes"
"context"
"sort"
"sync"
@@ -21,21 +22,21 @@ type LocalCacheUserStore struct {
}
func (s *LocalCacheUserStore) handleClusterInvalidateScheme(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.userProfileByIdsCache.Purge()
} else {
s.userProfileByIdsMut.Lock()
s.userProfileByIdsInvalidations[msg.Data] = true
s.userProfileByIdsInvalidations[string(msg.Data)] = true
s.userProfileByIdsMut.Unlock()
s.rootStore.userProfileByIdsCache.Remove(msg.Data)
s.rootStore.userProfileByIdsCache.Remove(string(msg.Data))
}
}
func (s *LocalCacheUserStore) handleClusterInvalidateProfilesInChannel(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.profilesInChannelCache.Purge()
} else {
s.rootStore.profilesInChannelCache.Remove(msg.Data)
s.rootStore.profilesInChannelCache.Remove(string(msg.Data))
}
}

Просмотреть файл

@@ -4,6 +4,8 @@
package localcachelayer
import (
"bytes"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/store"
)
@@ -14,10 +16,10 @@ type LocalCacheWebhookStore struct {
}
func (s *LocalCacheWebhookStore) handleClusterInvalidateWebhook(msg *model.ClusterMessage) {
if msg.Data == ClearCacheMessageData {
if bytes.Equal(msg.Data, clearCacheMessageData) {
s.rootStore.webhookCache.Purge()
} else {
s.rootStore.webhookCache.Remove(msg.Data)
s.rootStore.webhookCache.Remove(string(msg.Data))
}
}