From 4a1802c039a0db2d97e8351c462963a99da857bf Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Mon, 5 Mar 2018 10:35:26 -0500 Subject: MM-9664 Add invalidation metrics for store caches (#8340) * Add invalidation metrics for store caches * Increment session invalidation metric * Fix tests --- store/sqlstore/channel_store.go | 47 +++++++++++++++++++++++++--------- store/sqlstore/file_info_store.go | 8 +++++- store/sqlstore/post_store.go | 12 ++++++++- store/sqlstore/user_store.go | 17 +++++++++++- store/sqlstore/webhook_store.go | 15 ++++++++--- store/store.go | 5 ++++ store/storetest/mocks/ChannelStore.go | 5 ++++ store/storetest/mocks/FileInfoStore.go | 5 ++++ store/storetest/mocks/PostStore.go | 5 ++++ store/storetest/mocks/UserStore.go | 5 ++++ store/storetest/mocks/WebhookStore.go | 5 ++++ 11 files changed, 110 insertions(+), 19 deletions(-) (limited to 'store') diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index 131e5649b..c6d353959 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -43,12 +43,20 @@ var allChannelMembersNotifyPropsForChannelCache = utils.NewLru(ALL_CHANNEL_MEMBE var channelCache = utils.NewLru(model.CHANNEL_CACHE_SIZE) var channelByNameCache = utils.NewLru(model.CHANNEL_CACHE_SIZE) -func ClearChannelCaches() { +func (s SqlChannelStore) ClearCaches() { channelMemberCountsCache.Purge() allChannelMembersForUserCache.Purge() allChannelMembersNotifyPropsForChannelCache.Purge() channelCache.Purge() channelByNameCache.Purge() + + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("Channel Member Counts - Purge") + s.metrics.IncrementMemCacheInvalidationCounter("All Channel Members for User - Purge") + s.metrics.IncrementMemCacheInvalidationCounter("All Channel Members Notify Props for Channel - Purge") + s.metrics.IncrementMemCacheInvalidationCounter("Channel - Purge") + s.metrics.IncrementMemCacheInvalidationCounter("Channel By Name - Purge") + } } func NewSqlChannelStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.ChannelStore { @@ -308,12 +316,18 @@ func (s SqlChannelStore) GetChannelUnread(channelId, userId string) store.StoreC }) } -func (us SqlChannelStore) InvalidateChannel(id string) { +func (s SqlChannelStore) InvalidateChannel(id string) { channelCache.Remove(id) + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("Channel - Remove by ChannelId") + } } -func (us SqlChannelStore) InvalidateChannelByName(teamId, name string) { +func (s SqlChannelStore) InvalidateChannelByName(teamId, name string) { channelByNameCache.Remove(teamId + name) + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("Channel by Name - Remove by TeamId and Name") + } } func (s SqlChannelStore) Get(id string, allowFromCache bool) store.StoreChannel { @@ -814,14 +828,17 @@ func (s SqlChannelStore) GetMember(channelId string, userId string) store.StoreC }) } -func (us SqlChannelStore) InvalidateAllChannelMembersForUser(userId string) { +func (s SqlChannelStore) InvalidateAllChannelMembersForUser(userId string) { allChannelMembersForUserCache.Remove(userId) + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("All Channel Members for User - Remove by UserId") + } } -func (us SqlChannelStore) IsUserInChannelUseCache(userId string, channelId string) bool { +func (s SqlChannelStore) IsUserInChannelUseCache(userId string, channelId string) bool { if cacheItem, ok := allChannelMembersForUserCache.Get(userId); ok { - if us.metrics != nil { - us.metrics.IncrementMemCacheHitCounter("All Channel Members for User") + if s.metrics != nil { + s.metrics.IncrementMemCacheHitCounter("All Channel Members for User") } ids := cacheItem.(map[string]string) if _, ok := ids[channelId]; ok { @@ -830,12 +847,12 @@ func (us SqlChannelStore) IsUserInChannelUseCache(userId string, channelId strin return false } } else { - if us.metrics != nil { - us.metrics.IncrementMemCacheMissCounter("All Channel Members for User") + if s.metrics != nil { + s.metrics.IncrementMemCacheMissCounter("All Channel Members for User") } } - if result := <-us.GetAllChannelMembersForUser(userId, true); result.Err != nil { + if result := <-s.GetAllChannelMembersForUser(userId, true); result.Err != nil { l4g.Error("SqlChannelStore.IsUserInChannelUseCache: " + result.Err.Error()) return false } else { @@ -915,8 +932,11 @@ func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCac }) } -func (us SqlChannelStore) InvalidateCacheForChannelMembersNotifyProps(channelId string) { +func (s SqlChannelStore) InvalidateCacheForChannelMembersNotifyProps(channelId string) { allChannelMembersNotifyPropsForChannelCache.Remove(channelId) + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("All Channel Members Notify Props for Channel - Remove by ChannelId") + } } type allChannelMemberNotifyProps struct { @@ -966,8 +986,11 @@ func (s SqlChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId str }) } -func (us SqlChannelStore) InvalidateMemberCount(channelId string) { +func (s SqlChannelStore) InvalidateMemberCount(channelId string) { channelMemberCountsCache.Remove(channelId) + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("Channel Member Counts - Remove by ChannelId") + } } func (s SqlChannelStore) GetMemberCountFromCache(channelId string) int64 { diff --git a/store/sqlstore/file_info_store.go b/store/sqlstore/file_info_store.go index 1d0767d1e..7559640c8 100644 --- a/store/sqlstore/file_info_store.go +++ b/store/sqlstore/file_info_store.go @@ -25,8 +25,11 @@ const ( var fileInfoCache *utils.Cache = utils.NewLru(FILE_INFO_CACHE_SIZE) -func ClearFileCaches() { +func (fs SqlFileInfoStore) ClearCaches() { fileInfoCache.Purge() + if fs.metrics != nil { + fs.metrics.IncrementMemCacheInvalidationCounter("File Info Cache - Purge") + } } func NewSqlFileInfoStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.FileInfoStore { @@ -118,6 +121,9 @@ func (fs SqlFileInfoStore) GetByPath(path string) store.StoreChannel { func (fs SqlFileInfoStore) InvalidateFileInfosForPostCache(postId string) { fileInfoCache.Remove(postId) + if fs.metrics != nil { + fs.metrics.IncrementMemCacheInvalidationCounter("File Info Cache - Remove by PostId") + } } func (fs SqlFileInfoStore) GetForPost(postId string, readFromMaster bool, allowFromCache bool) store.StoreChannel { diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 25c3c4913..92ee28ffa 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -35,9 +35,14 @@ const ( var lastPostTimeCache = utils.NewLru(LAST_POST_TIME_CACHE_SIZE) var lastPostsCache = utils.NewLru(LAST_POSTS_CACHE_SIZE) -func ClearPostCaches() { +func (s SqlPostStore) ClearCaches() { lastPostTimeCache.Purge() lastPostsCache.Purge() + + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("Last Post Time - Purge") + s.metrics.IncrementMemCacheInvalidationCounter("Last Posts Cache - Purge") + } } func NewSqlPostStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.PostStore { @@ -326,6 +331,11 @@ func (s SqlPostStore) InvalidateLastPostTimeCache(channelId string) { // Keys are "{channelid}{limit}" and caching only occurs on limits of 30 and 60 lastPostsCache.Remove(channelId + "30") lastPostsCache.Remove(channelId + "60") + + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("Last Post Time - Remove by Channel Id") + s.metrics.IncrementMemCacheInvalidationCounter("Last Posts Cache - Remove by Channel Id") + } } func (s SqlPostStore) GetEtag(channelId string, allowFromCache bool) store.StoreChannel { diff --git a/store/sqlstore/user_store.go b/store/sqlstore/user_store.go index d67a45704..04e0c994c 100644 --- a/store/sqlstore/user_store.go +++ b/store/sqlstore/user_store.go @@ -38,13 +38,22 @@ type SqlUserStore struct { var profilesInChannelCache *utils.Cache = utils.NewLru(PROFILES_IN_CHANNEL_CACHE_SIZE) var profileByIdsCache *utils.Cache = utils.NewLru(PROFILE_BY_IDS_CACHE_SIZE) -func ClearUserCaches() { +func (us SqlUserStore) ClearCaches() { profilesInChannelCache.Purge() profileByIdsCache.Purge() + + if us.metrics != nil { + us.metrics.IncrementMemCacheInvalidationCounter("Profiles in Channel - Purge") + us.metrics.IncrementMemCacheInvalidationCounter("Profile By Ids - Purge") + } } func (us SqlUserStore) InvalidatProfileCacheForUser(userId string) { profileByIdsCache.Remove(userId) + + if us.metrics != nil { + us.metrics.IncrementMemCacheInvalidationCounter("Profile By Ids - Remove") + } } func NewSqlUserStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.UserStore { @@ -384,6 +393,9 @@ func (us SqlUserStore) InvalidateProfilesInChannelCacheByUser(userId string) { userMap := cacheItem.(map[string]*model.User) if _, userInCache := userMap[userId]; userInCache { profilesInChannelCache.Remove(key) + if us.metrics != nil { + us.metrics.IncrementMemCacheInvalidationCounter("Profiles in Channel - Remove by User") + } } } } @@ -391,6 +403,9 @@ func (us SqlUserStore) InvalidateProfilesInChannelCacheByUser(userId string) { func (us SqlUserStore) InvalidateProfilesInChannelCache(channelId string) { profilesInChannelCache.Remove(channelId) + if us.metrics != nil { + us.metrics.IncrementMemCacheInvalidationCounter("Profiles in Channel - Remove by Channel") + } } func (us SqlUserStore) GetProfilesInChannel(channelId string, offset int, limit int) store.StoreChannel { diff --git a/store/sqlstore/webhook_store.go b/store/sqlstore/webhook_store.go index 8a3720fa0..45ad90e52 100644 --- a/store/sqlstore/webhook_store.go +++ b/store/sqlstore/webhook_store.go @@ -26,8 +26,12 @@ const ( var webhookCache = utils.NewLru(WEBHOOK_CACHE_SIZE) -func ClearWebhookCaches() { +func (s SqlWebhookStore) ClearCaches() { webhookCache.Purge() + + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("Webhook - Purge") + } } func NewSqlWebhookStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.WebhookStore { @@ -78,6 +82,9 @@ func (s SqlWebhookStore) CreateIndexesIfNotExists() { func (s SqlWebhookStore) InvalidateWebhookCache(webhookId string) { webhookCache.Remove(webhookId) + if s.metrics != nil { + s.metrics.IncrementMemCacheInvalidationCounter("Webhook - Remove by WebhookId") + } } func (s SqlWebhookStore) SaveIncoming(webhook *model.IncomingWebhook) store.StoreChannel { @@ -164,7 +171,7 @@ func (s SqlWebhookStore) PermanentDeleteIncomingByUser(userId string) store.Stor result.Err = model.NewAppError("SqlWebhookStore.DeleteIncomingByUser", "store.sql_webhooks.permanent_delete_incoming_by_user.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError) } - ClearWebhookCaches() + s.ClearCaches() }) } @@ -175,7 +182,7 @@ func (s SqlWebhookStore) PermanentDeleteIncomingByChannel(channelId string) stor result.Err = model.NewAppError("SqlWebhookStore.DeleteIncomingByChannel", "store.sql_webhooks.permanent_delete_incoming_by_channel.app_error", nil, "id="+channelId+", err="+err.Error(), http.StatusInternalServerError) } - ClearWebhookCaches() + s.ClearCaches() }) } @@ -322,7 +329,7 @@ func (s SqlWebhookStore) PermanentDeleteOutgoingByChannel(channelId string) stor result.Err = model.NewAppError("SqlWebhookStore.DeleteOutgoingByChannel", "store.sql_webhooks.permanent_delete_outgoing_by_channel.app_error", nil, "id="+channelId+", err="+err.Error(), http.StatusInternalServerError) } - ClearWebhookCaches() + s.ClearCaches() }) } diff --git a/store/store.go b/store/store.go index 9435a6f61..0394277b7 100644 --- a/store/store.go +++ b/store/store.go @@ -161,6 +161,7 @@ type ChannelStore interface { GetMembersByIds(channelId string, userIds []string) StoreChannel AnalyticsDeletedTypeCount(teamId string, channelType string) StoreChannel GetChannelUnread(channelId, userId string) StoreChannel + ClearCaches() } type ChannelMemberHistoryStore interface { @@ -190,6 +191,7 @@ type PostStore interface { AnalyticsUserCountsWithPostsByDay(teamId string) StoreChannel AnalyticsPostCountsByDay(teamId string) StoreChannel AnalyticsPostCount(teamId string, mustHaveFile bool, mustHaveHashtag bool) StoreChannel + ClearCaches() InvalidateLastPostTimeCache(channelId string) GetPostsCreatedAt(channelId string, time int64) StoreChannel Overwrite(post *model.Post) StoreChannel @@ -210,6 +212,7 @@ type UserStore interface { UpdateMfaActive(userId string, active bool) StoreChannel Get(id string) StoreChannel GetAll() StoreChannel + ClearCaches() InvalidateProfilesInChannelCacheByUser(userId string) InvalidateProfilesInChannelCache(channelId string) GetProfilesInChannel(channelId string, offset int, limit int) StoreChannel @@ -343,6 +346,7 @@ type WebhookStore interface { AnalyticsIncomingCount(teamId string) StoreChannel AnalyticsOutgoingCount(teamId string) StoreChannel InvalidateWebhookCache(webhook string) + ClearCaches() } type CommandStore interface { @@ -420,6 +424,7 @@ type FileInfoStore interface { DeleteForPost(postId string) StoreChannel PermanentDelete(fileId string) StoreChannel PermanentDeleteBatch(endTime int64, limit int64) StoreChannel + ClearCaches() } type ReactionStore interface { diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index 5379c2fb4..4ed3e39fb 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -61,6 +61,11 @@ func (_m *ChannelStore) AutocompleteInTeam(teamId string, term string) store.Sto return r0 } +// ClearCaches provides a mock function with given fields: +func (_m *ChannelStore) ClearCaches() { + _m.Called() +} + // CreateDirectChannel provides a mock function with given fields: userId, otherUserId func (_m *ChannelStore) CreateDirectChannel(userId string, otherUserId string) store.StoreChannel { ret := _m.Called(userId, otherUserId) diff --git a/store/storetest/mocks/FileInfoStore.go b/store/storetest/mocks/FileInfoStore.go index 9b479ff3a..4dddf0bd7 100644 --- a/store/storetest/mocks/FileInfoStore.go +++ b/store/storetest/mocks/FileInfoStore.go @@ -29,6 +29,11 @@ func (_m *FileInfoStore) AttachToPost(fileId string, postId string) store.StoreC return r0 } +// ClearCaches provides a mock function with given fields: +func (_m *FileInfoStore) ClearCaches() { + _m.Called() +} + // DeleteForPost provides a mock function with given fields: postId func (_m *FileInfoStore) DeleteForPost(postId string) store.StoreChannel { ret := _m.Called(postId) diff --git a/store/storetest/mocks/PostStore.go b/store/storetest/mocks/PostStore.go index 05e3bde34..c405d5030 100644 --- a/store/storetest/mocks/PostStore.go +++ b/store/storetest/mocks/PostStore.go @@ -61,6 +61,11 @@ func (_m *PostStore) AnalyticsUserCountsWithPostsByDay(teamId string) store.Stor return r0 } +// ClearCaches provides a mock function with given fields: +func (_m *PostStore) ClearCaches() { + _m.Called() +} + // Delete provides a mock function with given fields: postId, time func (_m *PostStore) Delete(postId string, time int64) store.StoreChannel { ret := _m.Called(postId, time) diff --git a/store/storetest/mocks/UserStore.go b/store/storetest/mocks/UserStore.go index 7d1fd8c38..2f921ae6e 100644 --- a/store/storetest/mocks/UserStore.go +++ b/store/storetest/mocks/UserStore.go @@ -77,6 +77,11 @@ func (_m *UserStore) AnalyticsUniqueUserCount(teamId string) store.StoreChannel return r0 } +// ClearCaches provides a mock function with given fields: +func (_m *UserStore) ClearCaches() { + _m.Called() +} + // Get provides a mock function with given fields: id func (_m *UserStore) Get(id string) store.StoreChannel { ret := _m.Called(id) diff --git a/store/storetest/mocks/WebhookStore.go b/store/storetest/mocks/WebhookStore.go index aa66e0600..bf5b636eb 100644 --- a/store/storetest/mocks/WebhookStore.go +++ b/store/storetest/mocks/WebhookStore.go @@ -45,6 +45,11 @@ func (_m *WebhookStore) AnalyticsOutgoingCount(teamId string) store.StoreChannel return r0 } +// ClearCaches provides a mock function with given fields: +func (_m *WebhookStore) ClearCaches() { + _m.Called() +} + // DeleteIncoming provides a mock function with given fields: webhookId, time func (_m *WebhookStore) DeleteIncoming(webhookId string, time int64) store.StoreChannel { ret := _m.Called(webhookId, time) -- cgit v1.2.3-1-g7c22