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 +++++++++---- 5 files changed, 80 insertions(+), 19 deletions(-) (limited to 'store/sqlstore') 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() }) } -- cgit v1.2.3-1-g7c22