Skip to content

Commit

Permalink
[feature/chore] Add Move database functions + cache (#2647)
Browse files Browse the repository at this point in the history
* [feature/chore] Add Move database functions + cache

* add move mem ratio to envparsing.sh

* update comment
  • Loading branch information
tsmethurst authored Mar 6, 2024
1 parent 61a2b91 commit b22e213
Show file tree
Hide file tree
Showing 17 changed files with 671 additions and 1 deletion.
2 changes: 2 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (c *Caches) Init() {
c.initMarker()
c.initMedia()
c.initMention()
c.initMove()
c.initNotification()
c.initPoll()
c.initPollVote()
Expand Down Expand Up @@ -135,6 +136,7 @@ func (c *Caches) Sweep(threshold float64) {
c.GTS.Marker.Trim(threshold)
c.GTS.Media.Trim(threshold)
c.GTS.Mention.Trim(threshold)
c.GTS.Move.Trim(threshold)
c.GTS.Notification.Trim(threshold)
c.GTS.Poll.Trim(threshold)
c.GTS.Report.Trim(threshold)
Expand Down
32 changes: 32 additions & 0 deletions internal/cache/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ type GTSCaches struct {
// Mention provides access to the gtsmodel Mention database cache.
Mention structr.Cache[*gtsmodel.Mention]

// Move provides access to the gtsmodel Move database cache.
Move structr.Cache[*gtsmodel.Move]

// Notification provides access to the gtsmodel Notification database cache.
Notification structr.Cache[*gtsmodel.Notification]

Expand Down Expand Up @@ -185,6 +188,8 @@ func (c *Caches) initAccount() {
a2.AvatarMediaAttachment = nil
a2.HeaderMediaAttachment = nil
a2.Emojis = nil
a2.AlsoKnownAs = nil
a2.Move = nil

return a2
}
Expand Down Expand Up @@ -816,6 +821,33 @@ func (c *Caches) initMention() {
})
}

func (c *Caches) initMove() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
sizeofMove(), // model in-mem size.
config.GetCacheMoveMemRatio(),
)

log.Infof(nil, "cache size = %d", cap)

c.GTS.Move.Init(structr.Config[*gtsmodel.Move]{
Indices: []structr.IndexConfig{
{Fields: "ID"},
{Fields: "URI"},
{Fields: "OriginURI,TargetURI"},
{Fields: "OriginURI", Multiple: true},
{Fields: "TargetURI", Multiple: true},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
CopyValue: func(m1 *gtsmodel.Move) *gtsmodel.Move {
m2 := new(gtsmodel.Move)
*m2 = *m1
return m2
},
})
}

func (c *Caches) initNotification() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
Expand Down
4 changes: 4 additions & 0 deletions internal/cache/invalidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (c *Caches) OnInvalidateAccount(account *gtsmodel.Account) {

// Invalidate this account's block lists.
c.GTS.BlockIDs.Invalidate(account.ID)

// Invalidate this account's Move(s).
c.GTS.Move.Invalidate("OriginURI", account.URI)
c.GTS.Move.Invalidate("TargetURI", account.URI)
}

func (c *Caches) OnInvalidateBlock(block *gtsmodel.Block) {
Expand Down
13 changes: 13 additions & 0 deletions internal/cache/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,19 @@ func sizeofMention() uintptr {
}))
}

func sizeofMove() uintptr {
return uintptr(size.Of(&gtsmodel.Move{
ID: exampleID,
CreatedAt: exampleTime,
UpdatedAt: exampleTime,
AttemptedAt: exampleTime,
SucceededAt: exampleTime,
OriginURI: exampleURI,
TargetURI: exampleURI,
URI: exampleURI,
}))
}

func sizeofNotification() uintptr {
return uintptr(size.Of(&gtsmodel.Notification{
ID: exampleID,
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ type CacheConfiguration struct {
MarkerMemRatio float64 `name:"marker-mem-ratio"`
MediaMemRatio float64 `name:"media-mem-ratio"`
MentionMemRatio float64 `name:"mention-mem-ratio"`
MoveMemRatio float64 `name:"move-mem-ratio"`
NotificationMemRatio float64 `name:"notification-mem-ratio"`
PollMemRatio float64 `name:"poll-mem-ratio"`
PollVoteMemRatio float64 `name:"poll-vote-mem-ratio"`
Expand Down
1 change: 1 addition & 0 deletions internal/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ var Defaults = Configuration{
MarkerMemRatio: 0.5,
MediaMemRatio: 4,
MentionMemRatio: 2,
MoveMemRatio: 0.1,
NotificationMemRatio: 2,
PollMemRatio: 1,
PollVoteMemRatio: 2,
Expand Down
25 changes: 25 additions & 0 deletions internal/config/helpers.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3325,6 +3325,31 @@ func GetCacheMentionMemRatio() float64 { return global.GetCacheMentionMemRatio()
// SetCacheMentionMemRatio safely sets the value for global configuration 'Cache.MentionMemRatio' field
func SetCacheMentionMemRatio(v float64) { global.SetCacheMentionMemRatio(v) }

// GetCacheMoveMemRatio safely fetches the Configuration value for state's 'Cache.MoveMemRatio' field
func (st *ConfigState) GetCacheMoveMemRatio() (v float64) {
st.mutex.RLock()
v = st.config.Cache.MoveMemRatio
st.mutex.RUnlock()
return
}

// SetCacheMoveMemRatio safely sets the Configuration value for state's 'Cache.MoveMemRatio' field
func (st *ConfigState) SetCacheMoveMemRatio(v float64) {
st.mutex.Lock()
defer st.mutex.Unlock()
st.config.Cache.MoveMemRatio = v
st.reloadToViper()
}

// CacheMoveMemRatioFlag returns the flag name for the 'Cache.MoveMemRatio' field
func CacheMoveMemRatioFlag() string { return "cache-move-mem-ratio" }

// GetCacheMoveMemRatio safely fetches the value for global configuration 'Cache.MoveMemRatio' field
func GetCacheMoveMemRatio() float64 { return global.GetCacheMoveMemRatio() }

// SetCacheMoveMemRatio safely sets the value for global configuration 'Cache.MoveMemRatio' field
func SetCacheMoveMemRatio(v float64) { global.SetCacheMoveMemRatio(v) }

// GetCacheNotificationMemRatio safely fetches the Configuration value for state's 'Cache.NotificationMemRatio' field
func (st *ConfigState) GetCacheNotificationMemRatio() (v float64) {
st.mutex.RLock()
Expand Down
11 changes: 11 additions & 0 deletions internal/db/bundb/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ func (a *accountDB) PopulateAccount(ctx context.Context, account *gtsmodel.Accou
account.AlsoKnownAs = alsoKnownAs
}

if account.Move == nil && account.MoveID != "" {
// Account move is not set, fetch from database.
account.Move, err = a.state.DB.GetMoveByID(
ctx,
account.MovedToURI,
)
if err != nil {
errs.Appendf("error populating move: %w", err)
}
}

if account.MovedTo == nil && account.MovedToURI != "" {
// Account movedTo is not set, fetch from database.
account.MovedTo, err = a.state.DB.GetAccountByURI(
Expand Down
5 changes: 5 additions & 0 deletions internal/db/bundb/bundb.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type DBService struct {
db.Marker
db.Media
db.Mention
db.Move
db.Notification
db.Poll
db.Relationship
Expand Down Expand Up @@ -221,6 +222,10 @@ func NewBunDBService(ctx context.Context, state *state.State) (db.DB, error) {
db: db,
state: state,
},
Move: &moveDB{
db: db,
state: state,
},
Notification: &notificationDB{
db: db,
state: state,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package migrations

import (
"context"
"strings"

"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/uptrace/bun"
)

func init() {
up := func(ctx context.Context, db *bun.DB) error {
_, err := db.ExecContext(ctx,
"ALTER TABLE ? ADD COLUMN ? CHAR(26)",
bun.Ident("accounts"), bun.Ident("move_id"),
)
if err != nil {
e := err.Error()
if !(strings.Contains(e, "already exists") ||
strings.Contains(e, "duplicate column name") ||
strings.Contains(e, "SQLSTATE 42701")) {
return err
}
}

// Create "moves" table.
if _, err := db.NewCreateTable().
IfNotExists().
Model(&gtsmodel.Move{}).
Exec(ctx); err != nil {
return err
}

return nil
}

down := func(ctx context.Context, db *bun.DB) error {
return nil
}

if err := Migrations.Register(up, down); err != nil {
panic(err)
}
}
Loading

0 comments on commit b22e213

Please sign in to comment.