Skip to content

Commit

Permalink
[performance] add account block DB cache and remove block query joins (
Browse files Browse the repository at this point in the history
…#1085)

* add account block DB cache and remove reliance on relational joins

* actually include cache key arguments...

* add a PutBlock() method which also updates the block cache, update tests accordingly

* use `PutBlock` instead of `Put(ctx, block)`

* add + use functions for deleting + invalidating blocks

Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
  • Loading branch information
NyaaaWhatsUpDoc and tsmethurst committed Nov 20, 2022
1 parent 9be1685 commit 5d55e8d
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 76 deletions.
2 changes: 1 addition & 1 deletion internal/api/s2s/user/inboxpost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (suite *InboxPostTestSuite) TestPostUnblock() {
TargetAccountID: blockedAccount.ID,
}

err = suite.db.Put(context.Background(), dbBlock)
err = suite.db.PutBlock(context.Background(), dbBlock)
suite.NoError(err)

asBlock, err := suite.tc.BlockToAS(context.Background(), dbBlock)
Expand Down
7 changes: 4 additions & 3 deletions internal/db/bundb/bundb.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func NewBunDBService(ctx context.Context) (db.DB, error) {
notif := &notificationDB{conn: conn}
status := &statusDB{conn: conn}
emoji := &emojiDB{conn: conn}
relationship := &relationshipDB{conn: conn}
timeline := &timelineDB{conn: conn}
tombstone := &tombstoneDB{conn: conn}
user := &userDB{conn: conn}
Expand All @@ -174,6 +175,7 @@ func NewBunDBService(ctx context.Context) (db.DB, error) {
account.emojis = emoji
account.status = status
admin.users = user
relationship.accounts = account
status.accounts = account
status.emojis = emoji
status.mentions = mention
Expand All @@ -185,6 +187,7 @@ func NewBunDBService(ctx context.Context) (db.DB, error) {
emoji.init()
mention.init()
notif.init()
relationship.init()
status.init()
tombstone.init()
user.init()
Expand All @@ -209,9 +212,7 @@ func NewBunDBService(ctx context.Context) (db.DB, error) {
},
Mention: mention,
Notification: notif,
Relationship: &relationshipDB{
conn: conn,
},
Relationship: relationship,
Session: &sessionDB{
conn: conn,
},
Expand Down
204 changes: 153 additions & 51 deletions internal/db/bundb/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,37 @@ package bundb
import (
"context"
"database/sql"
"errors"
"fmt"
"time"

"codeberg.org/gruf/go-cache/v3/result"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/uptrace/bun"
)

type relationshipDB struct {
conn *DBConn
conn *DBConn
accounts *accountDB
blockCache *result.Cache[*gtsmodel.Block]
}

func (r *relationshipDB) newBlockQ(block *gtsmodel.Block) *bun.SelectQuery {
return r.conn.
NewSelect().
Model(block).
Relation("Account").
Relation("TargetAccount")
func (r *relationshipDB) init() {
// Initialize block result cache
r.blockCache = result.NewSized([]result.Lookup{
{Name: "ID"},
{Name: "AccountID.TargetAccountID"},
{Name: "URI"},
}, func(b1 *gtsmodel.Block) *gtsmodel.Block {
b2 := new(gtsmodel.Block)
*b2 = *b1
return b2
}, 1000)

// Set cache TTL and start sweep routine
r.blockCache.SetTTL(time.Minute*5, false)
r.blockCache.Start(time.Second * 10)
}

func (r *relationshipDB) newFollowQ(follow interface{}) *bun.SelectQuery {
Expand All @@ -49,43 +63,143 @@ func (r *relationshipDB) newFollowQ(follow interface{}) *bun.SelectQuery {
}

func (r *relationshipDB) IsBlocked(ctx context.Context, account1 string, account2 string, eitherDirection bool) (bool, db.Error) {
// Look for a block in direction of account1->account2
block1, err := r.getBlock(ctx, account1, account2)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return false, err
}

if block1 != nil {
// account1 blocks account2
return true, nil
} else if !eitherDirection {
// Don't check for mutli-directional
return false, nil
}

// Look for a block in direction of account2->account1
block2, err := r.getBlock(ctx, account2, account1)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return false, err
}

return (block2 != nil), nil
}

func (r *relationshipDB) GetBlock(ctx context.Context, account1 string, account2 string) (*gtsmodel.Block, db.Error) {
// Fetch block from database
block, err := r.getBlock(ctx, account1, account2)
if err != nil {
return nil, err
}

// Set the block originating account
block.Account, err = r.accounts.GetAccountByID(ctx, block.AccountID)
if err != nil {
return nil, err
}

// Set the block target account
block.TargetAccount, err = r.accounts.GetAccountByID(ctx, block.TargetAccountID)
if err != nil {
return nil, err
}

return block, nil
}

func (r *relationshipDB) getBlock(ctx context.Context, account1 string, account2 string) (*gtsmodel.Block, db.Error) {
return r.blockCache.Load("AccountID.TargetAccountID", func() (*gtsmodel.Block, error) {
var block gtsmodel.Block

q := r.conn.NewSelect().Model(&block).
Where("? = ?", bun.Ident("block.account_id"), account1).
Where("? = ?", bun.Ident("block.target_account_id"), account2)
if err := q.Scan(ctx); err != nil {
return nil, r.conn.ProcessError(err)
}

return &block, nil
}, account1, account2)
}

func (r *relationshipDB) PutBlock(ctx context.Context, block *gtsmodel.Block) db.Error {
return r.blockCache.Store(block, func() error {
_, err := r.conn.NewInsert().Model(block).Exec(ctx)
return r.conn.ProcessError(err)
})
}

func (r *relationshipDB) DeleteBlockByID(ctx context.Context, id string) db.Error {
if _, err := r.conn.
NewDelete().
TableExpr("? AS ?", bun.Ident("blocks"), bun.Ident("block")).
Where("? = ?", bun.Ident("block.id"), id).
Exec(ctx); err != nil {
return r.conn.ProcessError(err)
}

// Drop any old value from cache by this ID
r.blockCache.Invalidate("ID", id)
return nil
}

func (r *relationshipDB) DeleteBlockByURI(ctx context.Context, uri string) db.Error {
if _, err := r.conn.
NewDelete().
TableExpr("? AS ?", bun.Ident("blocks"), bun.Ident("block")).
Where("? = ?", bun.Ident("block.uri"), uri).
Exec(ctx); err != nil {
return r.conn.ProcessError(err)
}

// Drop any old value from cache by this URI
r.blockCache.Invalidate("URI", uri)
return nil
}

func (r *relationshipDB) DeleteBlocksByOriginAccountID(ctx context.Context, originAccountID string) db.Error {
blockIDs := []string{}

q := r.conn.
NewSelect().
TableExpr("? AS ?", bun.Ident("blocks"), bun.Ident("block")).
Column("block.id")
Column("block.id").
Where("? = ?", bun.Ident("block.account_id"), originAccountID)

if eitherDirection {
q = q.
WhereGroup(" OR ", func(inner *bun.SelectQuery) *bun.SelectQuery {
return inner.
Where("? = ?", bun.Ident("block.account_id"), account1).
Where("? = ?", bun.Ident("block.target_account_id"), account2)
}).
WhereGroup(" OR ", func(inner *bun.SelectQuery) *bun.SelectQuery {
return inner.
Where("? = ?", bun.Ident("block.account_id"), account2).
Where("? = ?", bun.Ident("block.target_account_id"), account1)
})
} else {
q = q.
Where("? = ?", bun.Ident("block.account_id"), account1).
Where("? = ?", bun.Ident("block.target_account_id"), account2)
if err := q.Scan(ctx, &blockIDs); err != nil {
return r.conn.ProcessError(err)
}

return r.conn.Exists(ctx, q)
for _, blockID := range blockIDs {
if err := r.DeleteBlockByID(ctx, blockID); err != nil {
return err
}
}

return nil
}

func (r *relationshipDB) GetBlock(ctx context.Context, account1 string, account2 string) (*gtsmodel.Block, db.Error) {
block := &gtsmodel.Block{}
func (r *relationshipDB) DeleteBlocksByTargetAccountID(ctx context.Context, targetAccountID string) db.Error {
blockIDs := []string{}

q := r.newBlockQ(block).
Where("? = ?", bun.Ident("block.account_id"), account1).
Where("? = ?", bun.Ident("block.target_account_id"), account2)
q := r.conn.
NewSelect().
TableExpr("? AS ?", bun.Ident("blocks"), bun.Ident("block")).
Column("block.id").
Where("? = ?", bun.Ident("block.target_account_id"), targetAccountID)

if err := q.Scan(ctx); err != nil {
return nil, r.conn.ProcessError(err)
if err := q.Scan(ctx, &blockIDs); err != nil {
return r.conn.ProcessError(err)
}
return block, nil

for _, blockID := range blockIDs {
if err := r.DeleteBlockByID(ctx, blockID); err != nil {
return err
}
}

return nil
}

func (r *relationshipDB) GetRelationship(ctx context.Context, requestingAccount string, targetAccount string) (*gtsmodel.Relationship, db.Error) {
Expand Down Expand Up @@ -144,30 +258,18 @@ func (r *relationshipDB) GetRelationship(ctx context.Context, requestingAccount
rel.Requested = requested

// check if the requesting account is blocking the target account
blockingQ := r.conn.
NewSelect().
TableExpr("? AS ?", bun.Ident("blocks"), bun.Ident("block")).
Column("block.id").
Where("? = ?", bun.Ident("block.account_id"), requestingAccount).
Where("? = ?", bun.Ident("block.target_account_id"), targetAccount)
blocking, err := r.conn.Exists(ctx, blockingQ)
if err != nil {
blockA2T, err := r.getBlock(ctx, requestingAccount, targetAccount)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, fmt.Errorf("GetRelationship: error checking blocking: %s", err)
}
rel.Blocking = blocking
rel.Blocking = (blockA2T != nil)

// check if the requesting account is blocked by the target account
blockedByQ := r.conn.
NewSelect().
TableExpr("? AS ?", bun.Ident("blocks"), bun.Ident("block")).
Column("block.id").
Where("? = ?", bun.Ident("block.account_id"), targetAccount).
Where("? = ?", bun.Ident("block.target_account_id"), requestingAccount)
blockedBy, err := r.conn.Exists(ctx, blockedByQ)
if err != nil {
blockT2A, err := r.getBlock(ctx, targetAccount, requestingAccount)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, fmt.Errorf("GetRelationship: error checking blockedBy: %s", err)
}
rel.BlockedBy = blockedBy
rel.BlockedBy = (blockT2A != nil)

return rel, nil
}
Expand Down

0 comments on commit 5d55e8d

Please sign in to comment.