diff --git a/api4/plugin_test.go b/api4/plugin_test.go index 4c1e5fde73..cb97f30324 100644 --- a/api4/plugin_test.go +++ b/api4/plugin_test.go @@ -1758,7 +1758,7 @@ func TestInstallMarketplacePlugin(t *testing.T) { }, "missing prepackaged and remote plugin signatures") } -func findClusterMessages(event string, msgs []*model.ClusterMessage) []*model.ClusterMessage { +func findClusterMessages(event model.ClusterEvent, msgs []*model.ClusterMessage) []*model.ClusterMessage { var result []*model.ClusterMessage for _, msg := range msgs { if msg.Event == event { diff --git a/app/busy_test.go b/app/busy_test.go index 947cba1f90..a2c3237cf3 100644 --- a/app/busy_test.go +++ b/app/busy_test.go @@ -126,7 +126,7 @@ func (c *ClusterMock) SendClusterMessageToNode(nodeID string, msg *model.Cluster func (c *ClusterMock) StartInterNodeCommunication() {} func (c *ClusterMock) StopInterNodeCommunication() {} -func (c *ClusterMock) RegisterClusterMessageHandler(event string, crm einterfaces.ClusterMessageHandler) { +func (c *ClusterMock) RegisterClusterMessageHandler(event model.ClusterEvent, crm einterfaces.ClusterMessageHandler) { } func (c *ClusterMock) GetClusterId() string { return "cluster_mock" } func (c *ClusterMock) IsLeader() bool { return false } diff --git a/app/plugin_event.go b/app/plugin_event.go index 47bbe406cc..d0b6e50e15 100644 --- a/app/plugin_event.go +++ b/app/plugin_event.go @@ -7,7 +7,7 @@ import ( "github.com/mattermost/mattermost-server/v5/model" ) -func (s *Server) notifyClusterPluginEvent(event string, data model.PluginEventData) { +func (s *Server) notifyClusterPluginEvent(event model.ClusterEvent, data model.PluginEventData) { if s.Cluster != nil { s.Cluster.SendClusterMessage(&model.ClusterMessage{ Event: event, diff --git a/einterfaces/cluster.go b/einterfaces/cluster.go index bf70c0b85c..8fe0c550cd 100644 --- a/einterfaces/cluster.go +++ b/einterfaces/cluster.go @@ -12,7 +12,7 @@ type ClusterMessageHandler func(msg *model.ClusterMessage) type ClusterInterface interface { StartInterNodeCommunication() StopInterNodeCommunication() - RegisterClusterMessageHandler(event string, crm ClusterMessageHandler) + RegisterClusterMessageHandler(event model.ClusterEvent, crm ClusterMessageHandler) GetClusterId() string IsLeader() bool // HealthScore returns a number which is indicative of how well an instance is meeting diff --git a/einterfaces/metrics.go b/einterfaces/metrics.go index babea3b349..d2c3df5036 100644 --- a/einterfaces/metrics.go +++ b/einterfaces/metrics.go @@ -5,6 +5,7 @@ package einterfaces import ( "github.com/mattermost/logr" + "github.com/mattermost/mattermost-server/v5/model" ) type MetricsInterface interface { @@ -22,7 +23,7 @@ type MetricsInterface interface { IncrementClusterRequest() ObserveClusterRequestDuration(elapsed float64) - IncrementClusterEventType(eventType string) + IncrementClusterEventType(eventType model.ClusterEvent) IncrementLogin() IncrementLoginFail() diff --git a/einterfaces/mocks/ClusterInterface.go b/einterfaces/mocks/ClusterInterface.go index 626aa64f75..4aab8a6024 100644 --- a/einterfaces/mocks/ClusterInterface.go +++ b/einterfaces/mocks/ClusterInterface.go @@ -187,7 +187,7 @@ func (_m *ClusterInterface) NotifyMsg(buf []byte) { } // RegisterClusterMessageHandler provides a mock function with given fields: event, crm -func (_m *ClusterInterface) RegisterClusterMessageHandler(event string, crm einterfaces.ClusterMessageHandler) { +func (_m *ClusterInterface) RegisterClusterMessageHandler(event model.ClusterEvent, crm einterfaces.ClusterMessageHandler) { _m.Called(event, crm) } diff --git a/einterfaces/mocks/MetricsInterface.go b/einterfaces/mocks/MetricsInterface.go index ba65347577..95c5d4ee2e 100644 --- a/einterfaces/mocks/MetricsInterface.go +++ b/einterfaces/mocks/MetricsInterface.go @@ -7,6 +7,8 @@ package mocks import ( logr "github.com/mattermost/logr" mock "github.com/stretchr/testify/mock" + + model "github.com/mattermost/mattermost-server/v5/model" ) // MetricsInterface is an autogenerated mock type for the MetricsInterface type @@ -61,7 +63,7 @@ func (_m *MetricsInterface) IncrementChannelIndexCounter() { } // IncrementClusterEventType provides a mock function with given fields: eventType -func (_m *MetricsInterface) IncrementClusterEventType(eventType string) { +func (_m *MetricsInterface) IncrementClusterEventType(eventType model.ClusterEvent) { _m.Called(eventType) } diff --git a/model/cluster_message.go b/model/cluster_message.go index a017c4d0f6..6998e3009f 100644 --- a/model/cluster_message.go +++ b/model/cluster_message.go @@ -8,38 +8,40 @@ import ( "io" ) +type ClusterEvent string + const ( - ClusterEventPublish = "publish" - ClusterEventUpdateStatus = "update_status" - ClusterEventInvalidateAllCaches = "inv_all_caches" - ClusterEventInvalidateCacheForReactions = "inv_reactions" - ClusterEventInvalidateCacheForChannelMembersNotifyProps = "inv_channel_members_notify_props" - ClusterEventInvalidateCacheForChannelByName = "inv_channel_name" - ClusterEventInvalidateCacheForChannel = "inv_channel" - ClusterEventInvalidateCacheForChannelGuestCount = "inv_channel_guest_count" - ClusterEventInvalidateCacheForUser = "inv_user" - ClusterEventInvalidateCacheForUserTeams = "inv_user_teams" - ClusterEventClearSessionCacheForUser = "clear_session_user" - ClusterEventInvalidateCacheForRoles = "inv_roles" - ClusterEventInvalidateCacheForRolePermissions = "inv_role_permissions" - ClusterEventInvalidateCacheForProfileByIds = "inv_profile_ids" - ClusterEventInvalidateCacheForProfileInChannel = "inv_profile_in_channel" - ClusterEventInvalidateCacheForSchemes = "inv_schemes" - ClusterEventInvalidateCacheForFileInfos = "inv_file_infos" - ClusterEventInvalidateCacheForWebhooks = "inv_webhooks" - ClusterEventInvalidateCacheForEmojisById = "inv_emojis_by_id" - ClusterEventInvalidateCacheForEmojisIdByName = "inv_emojis_id_by_name" - ClusterEventInvalidateCacheForChannelPinnedpostsCounts = "inv_channel_pinnedposts_counts" - ClusterEventInvalidateCacheForChannelMemberCounts = "inv_channel_member_counts" - ClusterEventInvalidateCacheForLastPosts = "inv_last_posts" - ClusterEventInvalidateCacheForLastPostTime = "inv_last_post_time" - ClusterEventInvalidateCacheForTeams = "inv_teams" - ClusterEventClearSessionCacheForAllUsers = "inv_all_user_sessions" - ClusterEventInstallPlugin = "install_plugin" - ClusterEventRemovePlugin = "remove_plugin" - ClusterEventPluginEvent = "plugin_event" - ClusterEventInvalidateCacheForTermsOfService = "inv_terms_of_service" - ClusterEventBusyStateChanged = "busy_state_change" + ClusterEventPublish ClusterEvent = "publish" + ClusterEventUpdateStatus ClusterEvent = "update_status" + ClusterEventInvalidateAllCaches ClusterEvent = "inv_all_caches" + ClusterEventInvalidateCacheForReactions ClusterEvent = "inv_reactions" + ClusterEventInvalidateCacheForChannelMembersNotifyProps ClusterEvent = "inv_channel_members_notify_props" + ClusterEventInvalidateCacheForChannelByName ClusterEvent = "inv_channel_name" + ClusterEventInvalidateCacheForChannel ClusterEvent = "inv_channel" + ClusterEventInvalidateCacheForChannelGuestCount ClusterEvent = "inv_channel_guest_count" + ClusterEventInvalidateCacheForUser ClusterEvent = "inv_user" + ClusterEventInvalidateCacheForUserTeams ClusterEvent = "inv_user_teams" + ClusterEventClearSessionCacheForUser ClusterEvent = "clear_session_user" + ClusterEventInvalidateCacheForRoles ClusterEvent = "inv_roles" + ClusterEventInvalidateCacheForRolePermissions ClusterEvent = "inv_role_permissions" + ClusterEventInvalidateCacheForProfileByIds ClusterEvent = "inv_profile_ids" + ClusterEventInvalidateCacheForProfileInChannel ClusterEvent = "inv_profile_in_channel" + ClusterEventInvalidateCacheForSchemes ClusterEvent = "inv_schemes" + ClusterEventInvalidateCacheForFileInfos ClusterEvent = "inv_file_infos" + ClusterEventInvalidateCacheForWebhooks ClusterEvent = "inv_webhooks" + ClusterEventInvalidateCacheForEmojisById ClusterEvent = "inv_emojis_by_id" + ClusterEventInvalidateCacheForEmojisIdByName ClusterEvent = "inv_emojis_id_by_name" + ClusterEventInvalidateCacheForChannelPinnedpostsCounts ClusterEvent = "inv_channel_pinnedposts_counts" + ClusterEventInvalidateCacheForChannelMemberCounts ClusterEvent = "inv_channel_member_counts" + ClusterEventInvalidateCacheForLastPosts ClusterEvent = "inv_last_posts" + ClusterEventInvalidateCacheForLastPostTime ClusterEvent = "inv_last_post_time" + ClusterEventInvalidateCacheForTeams ClusterEvent = "inv_teams" + ClusterEventClearSessionCacheForAllUsers ClusterEvent = "inv_all_user_sessions" + ClusterEventInstallPlugin ClusterEvent = "install_plugin" + ClusterEventRemovePlugin ClusterEvent = "remove_plugin" + ClusterEventPluginEvent ClusterEvent = "plugin_event" + ClusterEventInvalidateCacheForTermsOfService ClusterEvent = "inv_terms_of_service" + ClusterEventBusyStateChanged ClusterEvent = "busy_state_change" // Gossip communication ClusterGossipEventRequestGetLogs = "gossip_request_get_logs" @@ -57,7 +59,7 @@ const ( ) type ClusterMessage struct { - Event string `json:"event"` + Event ClusterEvent `json:"event"` SendType string `json:"-"` WaitForAllToSend bool `json:"-"` Data string `json:"data,omitempty"` diff --git a/services/cache/cache.go b/services/cache/cache.go index 59da07e525..6e3d1dc03d 100644 --- a/services/cache/cache.go +++ b/services/cache/cache.go @@ -6,6 +6,8 @@ package cache import ( "errors" "time" + + "github.com/mattermost/mattermost-server/v5/model" ) // ErrKeyNotFound is the error when the given key is not found @@ -42,7 +44,7 @@ type Cache interface { Len() (int, error) // GetInvalidateClusterEvent returns the cluster event configured when this cache was created. - GetInvalidateClusterEvent() string + GetInvalidateClusterEvent() model.ClusterEvent // Name returns the name of the cache Name() string diff --git a/services/cache/lru.go b/services/cache/lru.go index ac13dddfdd..fdfe37cc81 100644 --- a/services/cache/lru.go +++ b/services/cache/lru.go @@ -24,7 +24,7 @@ type LRU struct { items map[string]*list.Element defaultExpiry time.Duration name string - invalidateClusterEvent string + invalidateClusterEvent model.ClusterEvent } // LRUOptions contains options for initializing LRU cache @@ -32,7 +32,7 @@ type LRUOptions struct { Name string Size int DefaultExpiry time.Duration - InvalidateClusterEvent string + InvalidateClusterEvent model.ClusterEvent // StripedBuckets is used only by LRUStriped and shouldn't be greater than the number // of CPUs available on the machine running this cache. StripedBuckets int @@ -128,7 +128,7 @@ func (l *LRU) Len() (int, error) { } // GetInvalidateClusterEvent returns the cluster event configured when this cache was created. -func (l *LRU) GetInvalidateClusterEvent() string { +func (l *LRU) GetInvalidateClusterEvent() model.ClusterEvent { return l.invalidateClusterEvent } diff --git a/services/cache/lru_striped.go b/services/cache/lru_striped.go index 710cdbeb5a..3b1ace9433 100644 --- a/services/cache/lru_striped.go +++ b/services/cache/lru_striped.go @@ -9,6 +9,8 @@ import ( "time" "github.com/cespare/xxhash/v2" + + "github.com/mattermost/mattermost-server/v5/model" ) // LRUStriped keeps LRU caches in buckets in order to lower mutex contention. @@ -39,7 +41,7 @@ import ( type LRUStriped struct { buckets []*LRU name string - invalidateClusterEvent string + invalidateClusterEvent model.ClusterEvent } func (L LRUStriped) hashkeyMapHash(key string) uint64 { @@ -108,7 +110,7 @@ func (L LRUStriped) Len() (int, error) { } // GetInvalidateClusterEvent does the same as LRU.GetInvalidateClusterEvent -func (L LRUStriped) GetInvalidateClusterEvent() string { +func (L LRUStriped) GetInvalidateClusterEvent() model.ClusterEvent { return L.invalidateClusterEvent } diff --git a/services/cache/provider.go b/services/cache/provider.go index 406174e75b..54b2d19aeb 100644 --- a/services/cache/provider.go +++ b/services/cache/provider.go @@ -5,6 +5,8 @@ package cache import ( "time" + + "github.com/mattermost/mattermost-server/v5/model" ) // CacheOptions contains options for initializaing a cache @@ -12,7 +14,7 @@ type CacheOptions struct { Size int DefaultExpiry time.Duration Name string - InvalidateClusterEvent string + InvalidateClusterEvent model.ClusterEvent Striped bool StripedBuckets int } diff --git a/services/cache/provider_test.go b/services/cache/provider_test.go index 8a4dc8235a..f96eb01598 100644 --- a/services/cache/provider_test.go +++ b/services/cache/provider_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "github.com/mattermost/mattermost-server/v5/model" "github.com/stretchr/testify/require" ) @@ -56,7 +57,7 @@ func TestNewCache(t *testing.T) { size := 1 expiry := 1 * time.Second - event := "clusterEvent" + event := model.ClusterEvent("clusterEvent") c, err := p.NewCache(&CacheOptions{ Size: size, Name: "name", @@ -139,7 +140,7 @@ func TestNewCache_Striped(t *testing.T) { size := 1 expiry := 1 * time.Second - event := "clusterEvent" + event := model.ClusterEvent("clusterEvent") c, err := p.NewCache(&CacheOptions{ Size: size, Name: "name", diff --git a/testlib/cluster.go b/testlib/cluster.go index 3158e50af7..750f945b1b 100644 --- a/testlib/cluster.go +++ b/testlib/cluster.go @@ -20,7 +20,7 @@ func (c *FakeClusterInterface) StartInterNodeCommunication() {} func (c *FakeClusterInterface) StopInterNodeCommunication() {} -func (c *FakeClusterInterface) RegisterClusterMessageHandler(event string, crm einterfaces.ClusterMessageHandler) { +func (c *FakeClusterInterface) RegisterClusterMessageHandler(event model.ClusterEvent, crm einterfaces.ClusterMessageHandler) { c.clusterMessageHandler = crm }