improvements to caching for lists and relationship to accounts / follows

This commit is contained in:
kim 2024-09-11 22:38:35 +01:00
commit 002bd86a39
27 changed files with 1002 additions and 1333 deletions

View file

@ -62,7 +62,6 @@ func (c *Caches) Init() {
log.Infof(nil, "init: %p", c)
c.initAccount()
c.initAccountIDsFollowingTag()
c.initAccountNote()
c.initAccountSettings()
c.initAccountStats()
@ -84,11 +83,13 @@ func (c *Caches) Init() {
c.initFollowIDs()
c.initFollowRequest()
c.initFollowRequestIDs()
c.initFollowingTagIDs()
c.initInReplyToIDs()
c.initInstance()
c.initInteractionRequest()
c.initList()
c.initListEntry()
c.initListIDs()
c.initListedIDs()
c.initMarker()
c.initMedia()
c.initMention()
@ -105,7 +106,6 @@ func (c *Caches) Init() {
c.initStatusFave()
c.initStatusFaveIDs()
c.initTag()
c.initTagIDsFollowedByAccount()
c.initThreadMute()
c.initToken()
c.initTombstone()
@ -148,7 +148,6 @@ func (c *Caches) Stop() {
// significant overhead to all cache writes.
func (c *Caches) Sweep(threshold float64) {
c.DB.Account.Trim(threshold)
c.DB.AccountIDsFollowingTag.Trim(threshold)
c.DB.AccountNote.Trim(threshold)
c.DB.AccountSettings.Trim(threshold)
c.DB.AccountStats.Trim(threshold)
@ -168,11 +167,13 @@ func (c *Caches) Sweep(threshold float64) {
c.DB.FollowIDs.Trim(threshold)
c.DB.FollowRequest.Trim(threshold)
c.DB.FollowRequestIDs.Trim(threshold)
c.DB.FollowingTagIDs.Trim(threshold)
c.DB.InReplyToIDs.Trim(threshold)
c.DB.Instance.Trim(threshold)
c.DB.InteractionRequest.Trim(threshold)
c.DB.List.Trim(threshold)
c.DB.ListEntry.Trim(threshold)
c.DB.ListIDs.Trim(threshold)
c.DB.ListedIDs.Trim(threshold)
c.DB.Marker.Trim(threshold)
c.DB.Media.Trim(threshold)
c.DB.Mention.Trim(threshold)
@ -189,7 +190,6 @@ func (c *Caches) Sweep(threshold float64) {
c.DB.StatusFave.Trim(threshold)
c.DB.StatusFaveIDs.Trim(threshold)
c.DB.Tag.Trim(threshold)
c.DB.TagIDsFollowedByAccount.Trim(threshold)
c.DB.ThreadMute.Trim(threshold)
c.DB.Token.Trim(threshold)
c.DB.Tombstone.Trim(threshold)

98
internal/cache/db.go vendored
View file

@ -29,9 +29,6 @@ type DBCaches struct {
// Account provides access to the gtsmodel Account database cache.
Account StructCache[*gtsmodel.Account]
// AccountIDsFollowingTag caches account IDs following a given tag ID.
AccountIDsFollowingTag SliceCache[string]
// AccountNote provides access to the gtsmodel Note database cache.
AccountNote StructCache[*gtsmodel.AccountNote]
@ -103,6 +100,12 @@ type DBCaches struct {
// - '<' for follower IDs
FollowRequestIDs SliceCache[string]
// FollowingTagIDs provides access to account IDs following / tag IDs followed by
// account db cache. THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{id} WHERE:
// - '>{$accountID}' for tag IDs followed by account
// - '<{$tagIDs}' for account IDs following tag
FollowingTagIDs SliceCache[string]
// Instance provides access to the gtsmodel Instance database cache.
Instance StructCache[*gtsmodel.Instance]
@ -115,8 +118,17 @@ type DBCaches struct {
// List provides access to the gtsmodel List database cache.
List StructCache[*gtsmodel.List]
// ListEntry provides access to the gtsmodel ListEntry database cache.
ListEntry StructCache[*gtsmodel.ListEntry]
// ListIDs provides access to the list IDs owned by account / list IDs follow
// contained in db cache. THIS CACHE IS KEYED AS FOLLOWING {prefix}{id} WHERE:
// - 'a{$accountID}' for list IDs owned by account
// - 'f{$followID}' for list IDs follow contained in
ListIDs SliceCache[string]
// ListedIDs provides access to the account IDs in list / follow IDs in
// list db cache. THIS CACHE IS KEYED AS FOLLOWING {prefix}{id} WHERE:
// - 'a{listID}' for account IDs in list ID
// - 'f{listID}' for follow IDs in list ID
ListedIDs SliceCache[string]
// Marker provides access to the gtsmodel Marker database cache.
Marker StructCache[*gtsmodel.Marker]
@ -151,10 +163,10 @@ type DBCaches struct {
// Status provides access to the gtsmodel Status database cache.
Status StructCache[*gtsmodel.Status]
// StatusBookmark ...
// StatusBookmark provides access to the gtsmodel StatusBookmark database cache.
StatusBookmark StructCache[*gtsmodel.StatusBookmark]
// StatusBookmarkIDs ...
// StatusBookmarkIDs provides access to the status bookmark IDs list database cache.
StatusBookmarkIDs SliceCache[string]
// StatusFave provides access to the gtsmodel StatusFave database cache.
@ -166,9 +178,6 @@ type DBCaches struct {
// Tag provides access to the gtsmodel Tag database cache.
Tag StructCache[*gtsmodel.Tag]
// TagIDsFollowedByAccount caches tag IDs followed by a given account ID.
TagIDsFollowedByAccount SliceCache[string]
// ThreadMute provides access to the gtsmodel ThreadMute database cache.
ThreadMute StructCache[*gtsmodel.ThreadMute]
@ -243,17 +252,6 @@ func (c *Caches) initAccount() {
})
}
func (c *Caches) initAccountIDsFollowingTag() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
config.GetCacheAccountIDsFollowingTagMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
c.DB.AccountIDsFollowingTag.Init(0, cap)
}
func (c *Caches) initAccountNote() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
@ -761,6 +759,17 @@ func (c *Caches) initFollowRequestIDs() {
c.DB.FollowRequestIDs.Init(0, cap)
}
func (c *Caches) initFollowingTagIDs() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
config.GetCacheFollowingTagIDsMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
c.DB.FollowingTagIDs.Init(0, cap)
}
func (c *Caches) initInReplyToIDs() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
@ -860,7 +869,6 @@ func (c *Caches) initList() {
// will be populated separately.
// See internal/db/bundb/list.go.
l2.Account = nil
l2.ListEntries = nil
return l2
}
@ -876,37 +884,26 @@ func (c *Caches) initList() {
})
}
func (c *Caches) initListEntry() {
func (c *Caches) initListIDs() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
sizeofListEntry(), // model in-mem size.
config.GetCacheListEntryMemRatio(),
cap := calculateSliceCacheMax(
config.GetCacheListIDsMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
copyF := func(l1 *gtsmodel.ListEntry) *gtsmodel.ListEntry {
l2 := new(gtsmodel.ListEntry)
*l2 = *l1
c.DB.ListIDs.Init(0, cap)
}
// Don't include ptr fields that
// will be populated separately.
// See internal/db/bundb/list.go.
l2.Follow = nil
func (c *Caches) initListedIDs() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
config.GetCacheListedIDsMemRatio(),
)
return l2
}
log.Infof(nil, "cache size = %d", cap)
c.DB.ListEntry.Init(structr.CacheConfig[*gtsmodel.ListEntry]{
Indices: []structr.IndexConfig{
{Fields: "ID"},
{Fields: "ListID", Multiple: true},
{Fields: "FollowID", Multiple: true},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
})
c.DB.ListedIDs.Init(0, cap)
}
func (c *Caches) initMarker() {
@ -1368,17 +1365,6 @@ func (c *Caches) initTag() {
})
}
func (c *Caches) initTagIDsFollowedByAccount() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
config.GetCacheTagIDsFollowedByAccountMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
c.DB.TagIDsFollowedByAccount.Init(0, cap)
}
func (c *Caches) initThreadMute() {
cap := calculateResultCacheMax(
sizeofThreadMute(), // model in-mem size.

View file

@ -97,9 +97,6 @@ func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) {
// Invalidate follow request with this same ID.
c.DB.FollowRequest.Invalidate("ID", follow.ID)
// Invalidate any related list entries.
c.DB.ListEntry.Invalidate("FollowID", follow.ID)
// Invalidate follow origin account ID cached visibility.
c.Visibility.Invalidate("ItemID", follow.AccountID)
c.Visibility.Invalidate("RequesterID", follow.AccountID)
@ -121,6 +118,15 @@ func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) {
">"+follow.TargetAccountID,
"l>"+follow.TargetAccountID,
)
// Invalidate source account's lists
// and destination account's lists, and
// those specifically for this follow.
c.DB.ListIDs.Invalidate(
"a"+follow.AccountID,
"a"+follow.TargetAccountID,
"f"+follow.ID,
)
}
func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) {
@ -139,8 +145,11 @@ func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) {
}
func (c *Caches) OnInvalidateList(list *gtsmodel.List) {
// Invalidate all cached entries of this list.
c.DB.ListEntry.Invalidate("ListID", list.ID)
// Invalidate list ID entries.
c.DB.ListedIDs.Invalidate(
"a"+list.ID,
"f"+list.ID,
)
}
func (c *Caches) OnInvalidateMedia(media *gtsmodel.MediaAttachment) {

View file

@ -166,6 +166,7 @@ func calculateCacheMax(keySz, valSz uintptr, ratio float64) int {
// totalOfRatios returns the total of all cache ratios added together.
func totalOfRatios() float64 {
// NOTE: this is not performant calculating
// this every damn time (mainly the mutex unlocks
// required to access each config var). fortunately
@ -189,11 +190,13 @@ func totalOfRatios() float64 {
config.GetCacheFollowIDsMemRatio() +
config.GetCacheFollowRequestMemRatio() +
config.GetCacheFollowRequestIDsMemRatio() +
config.GetCacheFollowingTagIDsMemRatio() +
config.GetCacheInReplyToIDsMemRatio() +
config.GetCacheInstanceMemRatio() +
config.GetCacheInteractionRequestMemRatio() +
config.GetCacheInReplyToIDsMemRatio() +
config.GetCacheListMemRatio() +
config.GetCacheListEntryMemRatio() +
config.GetCacheListIDsMemRatio() +
config.GetCacheListedIDsMemRatio() +
config.GetCacheMarkerMemRatio() +
config.GetCacheMediaMemRatio() +
config.GetCacheMentionMemRatio() +
@ -201,7 +204,9 @@ func totalOfRatios() float64 {
config.GetCacheNotificationMemRatio() +
config.GetCachePollMemRatio() +
config.GetCachePollVoteMemRatio() +
config.GetCachePollVoteIDsMemRatio() +
config.GetCacheReportMemRatio() +
config.GetCacheSinBinStatusMemRatio() +
config.GetCacheStatusMemRatio() +
config.GetCacheStatusBookmarkMemRatio() +
config.GetCacheStatusBookmarkIDsMemRatio() +
@ -212,6 +217,8 @@ func totalOfRatios() float64 {
config.GetCacheTokenMemRatio() +
config.GetCacheTombstoneMemRatio() +
config.GetCacheUserMemRatio() +
config.GetCacheUserMuteMemRatio() +
config.GetCacheUserMuteIDsMemRatio() +
config.GetCacheWebfingerMemRatio() +
config.GetCacheVisibilityMemRatio()
}
@ -466,16 +473,6 @@ func sizeofList() uintptr {
}))
}
func sizeofListEntry() uintptr {
return uintptr(size.Of(&gtsmodel.ListEntry{
ID: exampleID,
CreatedAt: exampleTime,
UpdatedAt: exampleTime,
ListID: exampleID,
FollowID: exampleID,
}))
}
func sizeofMarker() uintptr {
return uintptr(size.Of(&gtsmodel.Marker{
AccountID: exampleID,