Skip to content

Commit

Permalink
[performance] cached oauth database types (superseriousbusiness#2838)
Browse files Browse the repository at this point in the history
* update token + client code to use struct caches

* add code comments

* slight tweak to default mem ratios

* fix envparsing

* add appropriate invalidate hooks

* update the tokenstore sweeping function to rely on caches

* update to use PutClient()

* add ClientID to list of token struct indices
  • Loading branch information
NyaaaWhatsUpDoc authored and nyarla committed Jun 19, 2024
1 parent da46f7f commit 1443fe5
Show file tree
Hide file tree
Showing 18 changed files with 428 additions and 67 deletions.
4 changes: 2 additions & 2 deletions cmd/gotosocial/action/testrig/testrig.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ var Start action.GTSAction = func(ctx context.Context) error {
testrig.StandardStorageSetup(state.Storage, "./testrig/media")

// Initialize workers.
state.Workers.Start()
defer state.Workers.Stop()
testrig.StartNoopWorkers(&state)
defer testrig.StopWorkers(&state)

// build backend handlers
transportController := testrig.NewTestTransportController(&state, testrig.NewMockHTTPClient(func(req *http.Request) (*http.Response, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/api/auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (m *Module) TokenPOSTHandler(c *gin.Context) {

form := &tokenRequestForm{}
if err := c.ShouldBind(form); err != nil {
apiutil.OAuthErrorHandler(c, gtserror.NewErrorBadRequest(oauth.InvalidRequest(), err.Error()))
apiutil.OAuthErrorHandler(c, gtserror.NewErrorBadRequest(oauth.ErrInvalidRequest, err.Error()))
return
}

Expand Down Expand Up @@ -98,7 +98,7 @@ func (m *Module) TokenPOSTHandler(c *gin.Context) {
}

if len(help) != 0 {
apiutil.OAuthErrorHandler(c, gtserror.NewErrorBadRequest(oauth.InvalidRequest(), help...))
apiutil.OAuthErrorHandler(c, gtserror.NewErrorBadRequest(oauth.ErrInvalidRequest, help...))
return
}

Expand Down
4 changes: 3 additions & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (c *Caches) Init() {
c.initBlock()
c.initBlockIDs()
c.initBoostOfIDs()
c.initClient()
c.initDomainAllow()
c.initDomainBlock()
c.initEmoji()
Expand All @@ -85,9 +86,10 @@ func (c *Caches) Init() {
c.initReport()
c.initStatus()
c.initStatusFave()
c.initStatusFaveIDs()
c.initTag()
c.initThreadMute()
c.initStatusFaveIDs()
c.initToken()
c.initTombstone()
c.initUser()
c.initWebfinger()
Expand Down
70 changes: 66 additions & 4 deletions internal/cache/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type GTSCaches struct {
// BoostOfIDs provides access to the boost of IDs list database cache.
BoostOfIDs SliceCache[string]

// Client provides access to the gtsmodel Client database cache.
Client StructCache[*gtsmodel.Client]

// DomainAllow provides access to the domain allow database cache.
DomainAllow *domain.Cache

Expand Down Expand Up @@ -150,6 +153,9 @@ type GTSCaches struct {
// Tag provides access to the gtsmodel Tag database cache.
Tag StructCache[*gtsmodel.Tag]

// Token provides access to the gtsmodel Token database cache.
Token StructCache[*gtsmodel.Token]

// Tombstone provides access to the gtsmodel Tombstone database cache.
Tombstone StructCache[*gtsmodel.Tombstone]

Expand Down Expand Up @@ -309,9 +315,10 @@ func (c *Caches) initApplication() {
{Fields: "ID"},
{Fields: "ClientID"},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
Invalidate: c.OnInvalidateApplication,
})
}

Expand Down Expand Up @@ -374,6 +381,32 @@ func (c *Caches) initBoostOfIDs() {
c.GTS.BoostOfIDs.Init(0, cap)
}

func (c *Caches) initClient() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
sizeofClient(), // model in-mem size.
config.GetCacheClientMemRatio(),
)

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

copyF := func(c1 *gtsmodel.Client) *gtsmodel.Client {
c2 := new(gtsmodel.Client)
*c2 = *c1
return c2
}

c.GTS.Client.Init(structr.CacheConfig[*gtsmodel.Client]{
Indices: []structr.IndexConfig{
{Fields: "ID"},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
Invalidate: c.OnInvalidateClient,
})
}

func (c *Caches) initDomainAllow() {
c.GTS.DomainAllow = new(domain.Cache)
}
Expand Down Expand Up @@ -1135,7 +1168,7 @@ func (c *Caches) initTag() {

func (c *Caches) initThreadMute() {
cap := calculateResultCacheMax(
sizeOfThreadMute(), // model in-mem size.
sizeofThreadMute(), // model in-mem size.
config.GetCacheThreadMuteMemRatio(),
)

Expand All @@ -1160,6 +1193,35 @@ func (c *Caches) initThreadMute() {
})
}

func (c *Caches) initToken() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
sizeofToken(), // model in-mem size.
config.GetCacheTokenMemRatio(),
)

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

copyF := func(t1 *gtsmodel.Token) *gtsmodel.Token {
t2 := new(gtsmodel.Token)
*t2 = *t1
return t2
}

c.GTS.Token.Init(structr.CacheConfig[*gtsmodel.Token]{
Indices: []structr.IndexConfig{
{Fields: "ID"},
{Fields: "Code"},
{Fields: "Access"},
{Fields: "Refresh"},
{Fields: "ClientID", Multiple: true},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
})
}

func (c *Caches) initTombstone() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
Expand Down
10 changes: 10 additions & 0 deletions internal/cache/invalidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func (c *Caches) OnInvalidateAccount(account *gtsmodel.Account) {
c.GTS.Move.Invalidate("TargetURI", account.URI)
}

func (c *Caches) OnInvalidateApplication(app *gtsmodel.Application) {
// Invalidate cached client of this application.
c.GTS.Client.Invalidate("ID", app.ClientID)
}

func (c *Caches) OnInvalidateBlock(block *gtsmodel.Block) {
// Invalidate block origin account ID cached visibility.
c.Visibility.Invalidate("ItemID", block.AccountID)
Expand All @@ -73,6 +78,11 @@ func (c *Caches) OnInvalidateBlock(block *gtsmodel.Block) {
c.GTS.BlockIDs.Invalidate(block.AccountID)
}

func (c *Caches) OnInvalidateClient(client *gtsmodel.Client) {
// Invalidate any tokens under this client.
c.GTS.Token.Invalidate("ClientID", client.ID)
}

func (c *Caches) OnInvalidateEmojiCategory(category *gtsmodel.EmojiCategory) {
// Invalidate any emoji in this category.
c.GTS.Emoji.Invalidate("CategoryID", category.ID)
Expand Down
38 changes: 37 additions & 1 deletion internal/cache/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func totalOfRatios() float64 {
config.GetCacheBlockMemRatio() +
config.GetCacheBlockIDsMemRatio() +
config.GetCacheBoostOfIDsMemRatio() +
config.GetCacheClientMemRatio() +
config.GetCacheEmojiMemRatio() +
config.GetCacheEmojiCategoryMemRatio() +
config.GetCacheFollowMemRatio() +
Expand All @@ -198,6 +199,7 @@ func totalOfRatios() float64 {
config.GetCacheStatusFaveIDsMemRatio() +
config.GetCacheTagMemRatio() +
config.GetCacheThreadMuteMemRatio() +
config.GetCacheTokenMemRatio() +
config.GetCacheTombstoneMemRatio() +
config.GetCacheUserMemRatio() +
config.GetCacheWebfingerMemRatio() +
Expand Down Expand Up @@ -287,6 +289,17 @@ func sizeofBlock() uintptr {
}))
}

func sizeofClient() uintptr {
return uintptr(size.Of(&gtsmodel.Client{
ID: exampleID,
CreatedAt: exampleTime,
UpdatedAt: exampleTime,
Secret: exampleID,
Domain: exampleURI,
UserID: exampleID,
}))
}

func sizeofEmoji() uintptr {
return uintptr(size.Of(&gtsmodel.Emoji{
ID: exampleID,
Expand Down Expand Up @@ -591,7 +604,7 @@ func sizeofTag() uintptr {
}))
}

func sizeOfThreadMute() uintptr {
func sizeofThreadMute() uintptr {
return uintptr(size.Of(&gtsmodel.ThreadMute{
ID: exampleID,
CreatedAt: exampleTime,
Expand All @@ -601,6 +614,29 @@ func sizeOfThreadMute() uintptr {
}))
}

func sizeofToken() uintptr {
return uintptr(size.Of(&gtsmodel.Token{
ID: exampleID,
CreatedAt: exampleTime,
UpdatedAt: exampleTime,
ClientID: exampleID,
UserID: exampleID,
RedirectURI: exampleURI,
Scope: "r:w",
Code: "", // TODO
CodeChallenge: "", // TODO
CodeChallengeMethod: "", // TODO
CodeCreateAt: exampleTime,
CodeExpiresAt: exampleTime,
Access: exampleID + exampleID,
AccessCreateAt: exampleTime,
AccessExpiresAt: exampleTime,
Refresh: "", // TODO: clients don't really support this very well yet
RefreshCreateAt: exampleTime,
RefreshExpiresAt: exampleTime,
}))
}

func sizeofTombstone() uintptr {
return uintptr(size.Of(&gtsmodel.Tombstone{
ID: exampleID,
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ type CacheConfiguration struct {
BlockMemRatio float64 `name:"block-mem-ratio"`
BlockIDsMemRatio float64 `name:"block-mem-ratio"`
BoostOfIDsMemRatio float64 `name:"boost-of-ids-mem-ratio"`
ClientMemRatio float64 `name:"client-mem-ratio"`
EmojiMemRatio float64 `name:"emoji-mem-ratio"`
EmojiCategoryMemRatio float64 `name:"emoji-category-mem-ratio"`
FilterMemRatio float64 `name:"filter-mem-ratio"`
Expand Down Expand Up @@ -226,6 +227,7 @@ type CacheConfiguration struct {
StatusFaveIDsMemRatio float64 `name:"status-fave-ids-mem-ratio"`
TagMemRatio float64 `name:"tag-mem-ratio"`
ThreadMuteMemRatio float64 `name:"thread-mute-mem-ratio"`
TokenMemRatio float64 `name:"token-mem-ratio"`
TombstoneMemRatio float64 `name:"tombstone-mem-ratio"`
UserMemRatio float64 `name:"user-mem-ratio"`
WebfingerMemRatio float64 `name:"webfinger-mem-ratio"`
Expand Down
2 changes: 2 additions & 0 deletions internal/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ var Defaults = Configuration{
BlockMemRatio: 2,
BlockIDsMemRatio: 3,
BoostOfIDsMemRatio: 3,
ClientMemRatio: 0.1,
EmojiMemRatio: 3,
EmojiCategoryMemRatio: 0.1,
FilterMemRatio: 0.5,
Expand Down Expand Up @@ -190,6 +191,7 @@ var Defaults = Configuration{
StatusFaveIDsMemRatio: 3,
TagMemRatio: 2,
ThreadMuteMemRatio: 0.2,
TokenMemRatio: 0.75,
TombstoneMemRatio: 0.5,
UserMemRatio: 0.25,
WebfingerMemRatio: 0.1,
Expand Down
50 changes: 50 additions & 0 deletions internal/config/helpers.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2925,6 +2925,31 @@ func GetCacheBoostOfIDsMemRatio() float64 { return global.GetCacheBoostOfIDsMemR
// SetCacheBoostOfIDsMemRatio safely sets the value for global configuration 'Cache.BoostOfIDsMemRatio' field
func SetCacheBoostOfIDsMemRatio(v float64) { global.SetCacheBoostOfIDsMemRatio(v) }

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

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

// CacheClientMemRatioFlag returns the flag name for the 'Cache.ClientMemRatio' field
func CacheClientMemRatioFlag() string { return "cache-client-mem-ratio" }

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

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

// GetCacheEmojiMemRatio safely fetches the Configuration value for state's 'Cache.EmojiMemRatio' field
func (st *ConfigState) GetCacheEmojiMemRatio() (v float64) {
st.mutex.RLock()
Expand Down Expand Up @@ -3600,6 +3625,31 @@ func GetCacheThreadMuteMemRatio() float64 { return global.GetCacheThreadMuteMemR
// SetCacheThreadMuteMemRatio safely sets the value for global configuration 'Cache.ThreadMuteMemRatio' field
func SetCacheThreadMuteMemRatio(v float64) { global.SetCacheThreadMuteMemRatio(v) }

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

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

// CacheTokenMemRatioFlag returns the flag name for the 'Cache.TokenMemRatio' field
func CacheTokenMemRatioFlag() string { return "cache-token-mem-ratio" }

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

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

// GetCacheTombstoneMemRatio safely fetches the Configuration value for state's 'Cache.TombstoneMemRatio' field
func (st *ConfigState) GetCacheTombstoneMemRatio() (v float64) {
st.mutex.RLock()
Expand Down
36 changes: 36 additions & 0 deletions internal/db/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,40 @@ type Application interface {

// DeleteApplicationByClientID deletes the application with corresponding client_id value from the database.
DeleteApplicationByClientID(ctx context.Context, clientID string) error

// GetClientByID ...
GetClientByID(ctx context.Context, id string) (*gtsmodel.Client, error)

// PutClient ...
PutClient(ctx context.Context, client *gtsmodel.Client) error

// DeleteClientByID ...
DeleteClientByID(ctx context.Context, id string) error

// GetAllTokens ...
GetAllTokens(ctx context.Context) ([]*gtsmodel.Token, error)

// GetTokenByCode ...
GetTokenByCode(ctx context.Context, code string) (*gtsmodel.Token, error)

// GetTokenByAccess ...
GetTokenByAccess(ctx context.Context, access string) (*gtsmodel.Token, error)

// GetTokenByRefresh ...
GetTokenByRefresh(ctx context.Context, refresh string) (*gtsmodel.Token, error)

// PutToken ...
PutToken(ctx context.Context, token *gtsmodel.Token) error

// DeleteTokenByID ...
DeleteTokenByID(ctx context.Context, id string) error

// DeleteTokenByCode ...
DeleteTokenByCode(ctx context.Context, code string) error

// DeleteTokenByAccess ...
DeleteTokenByAccess(ctx context.Context, access string) error

// DeleteTokenByRefresh ...
DeleteTokenByRefresh(ctx context.Context, refresh string) error
}
Loading

0 comments on commit 1443fe5

Please sign in to comment.