[performance] retry db queries on busy errors (#2025)

* catch SQLITE_BUSY errors, wrap bun.DB to use our own busy retrier, remove unnecessary db.Error type

Signed-off-by: kim <grufwub@gmail.com>

* remove dead code

Signed-off-by: kim <grufwub@gmail.com>

* remove more dead code, add missing error arguments

Signed-off-by: kim <grufwub@gmail.com>

* update sqlite to use maxOpenConns()

Signed-off-by: kim <grufwub@gmail.com>

* add uncommitted changes

Signed-off-by: kim <grufwub@gmail.com>

* use direct calls-through for the ConnIface to make sure we don't double query hook

Signed-off-by: kim <grufwub@gmail.com>

* expose underlying bun.DB better

Signed-off-by: kim <grufwub@gmail.com>

* retry on the correct busy error

Signed-off-by: kim <grufwub@gmail.com>

* use longer possible maxRetries for db retry-backoff

Signed-off-by: kim <grufwub@gmail.com>

* remove the note regarding max-open-conns only applying to postgres

Signed-off-by: kim <grufwub@gmail.com>

* improved code commenting

Signed-off-by: kim <grufwub@gmail.com>

* remove unnecessary infof call (just use info)

Signed-off-by: kim <grufwub@gmail.com>

* rename DBConn to WrappedDB to better follow sql package name conventions

Signed-off-by: kim <grufwub@gmail.com>

* update test error string checks

Signed-off-by: kim <grufwub@gmail.com>

* shush linter

Signed-off-by: kim <grufwub@gmail.com>

* update backoff logic to be more transparent

Signed-off-by: kim <grufwub@gmail.com>

---------

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2023-07-25 09:34:05 +01:00 committed by GitHub
commit 5f3e095717
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
53 changed files with 1050 additions and 898 deletions

View file

@ -31,19 +31,19 @@ import (
)
type notificationDB struct {
conn *DBConn
db *WrappedDB
state *state.State
}
func (n *notificationDB) GetNotificationByID(ctx context.Context, id string) (*gtsmodel.Notification, db.Error) {
func (n *notificationDB) GetNotificationByID(ctx context.Context, id string) (*gtsmodel.Notification, error) {
return n.state.Caches.GTS.Notification().Load("ID", func() (*gtsmodel.Notification, error) {
var notif gtsmodel.Notification
q := n.conn.NewSelect().
q := n.db.NewSelect().
Model(&notif).
Where("? = ?", bun.Ident("notification.id"), id)
if err := q.Scan(ctx); err != nil {
return nil, n.conn.ProcessError(err)
return nil, n.db.ProcessError(err)
}
return &notif, nil
@ -56,11 +56,11 @@ func (n *notificationDB) GetNotification(
targetAccountID string,
originAccountID string,
statusID string,
) (*gtsmodel.Notification, db.Error) {
) (*gtsmodel.Notification, error) {
return n.state.Caches.GTS.Notification().Load("NotificationType.TargetAccountID.OriginAccountID.StatusID", func() (*gtsmodel.Notification, error) {
var notif gtsmodel.Notification
q := n.conn.NewSelect().
q := n.db.NewSelect().
Model(&notif).
Where("? = ?", bun.Ident("notification_type"), notificationType).
Where("? = ?", bun.Ident("target_account_id"), targetAccountID).
@ -68,7 +68,7 @@ func (n *notificationDB) GetNotification(
Where("? = ?", bun.Ident("status_id"), statusID)
if err := q.Scan(ctx); err != nil {
return nil, n.conn.ProcessError(err)
return nil, n.db.ProcessError(err)
}
return &notif, nil
@ -83,7 +83,7 @@ func (n *notificationDB) GetAccountNotifications(
minID string,
limit int,
excludeTypes []string,
) ([]*gtsmodel.Notification, db.Error) {
) ([]*gtsmodel.Notification, error) {
// Ensure reasonable
if limit < 0 {
limit = 0
@ -95,7 +95,7 @@ func (n *notificationDB) GetAccountNotifications(
frontToBack = true
)
q := n.conn.
q := n.db.
NewSelect().
TableExpr("? AS ?", bun.Ident("notifications"), bun.Ident("notification")).
Column("notification.id")
@ -140,7 +140,7 @@ func (n *notificationDB) GetAccountNotifications(
}
if err := q.Scan(ctx, &notifIDs); err != nil {
return nil, n.conn.ProcessError(err)
return nil, n.db.ProcessError(err)
}
if len(notifIDs) == 0 {
@ -174,12 +174,12 @@ func (n *notificationDB) GetAccountNotifications(
func (n *notificationDB) PutNotification(ctx context.Context, notif *gtsmodel.Notification) error {
return n.state.Caches.GTS.Notification().Store(notif, func() error {
_, err := n.conn.NewInsert().Model(notif).Exec(ctx)
return n.conn.ProcessError(err)
_, err := n.db.NewInsert().Model(notif).Exec(ctx)
return n.db.ProcessError(err)
})
}
func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string) db.Error {
func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string) error {
defer n.state.Caches.GTS.Notification().Invalidate("ID", id)
// Load notif into cache before attempting a delete,
@ -195,21 +195,21 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string)
}
// Finally delete notif from DB.
_, err = n.conn.NewDelete().
_, err = n.db.NewDelete().
TableExpr("? AS ?", bun.Ident("notifications"), bun.Ident("notification")).
Where("? = ?", bun.Ident("notification.id"), id).
Exec(ctx)
return n.conn.ProcessError(err)
return n.db.ProcessError(err)
}
func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) db.Error {
func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) error {
if targetAccountID == "" && originAccountID == "" {
return errors.New("DeleteNotifications: one of targetAccountID or originAccountID must be set")
}
var notifIDs []string
q := n.conn.
q := n.db.
NewSelect().
Column("id").
Table("notifications")
@ -227,7 +227,7 @@ func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string
}
if _, err := q.Exec(ctx, &notifIDs); err != nil {
return n.conn.ProcessError(err)
return n.db.ProcessError(err)
}
defer func() {
@ -248,24 +248,24 @@ func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string
}
// Finally delete all from DB.
_, err := n.conn.NewDelete().
_, err := n.db.NewDelete().
Table("notifications").
Where("? IN (?)", bun.Ident("id"), bun.In(notifIDs)).
Exec(ctx)
return n.conn.ProcessError(err)
return n.db.ProcessError(err)
}
func (n *notificationDB) DeleteNotificationsForStatus(ctx context.Context, statusID string) db.Error {
func (n *notificationDB) DeleteNotificationsForStatus(ctx context.Context, statusID string) error {
var notifIDs []string
q := n.conn.
q := n.db.
NewSelect().
Column("id").
Table("notifications").
Where("? = ?", bun.Ident("status_id"), statusID)
if _, err := q.Exec(ctx, &notifIDs); err != nil {
return n.conn.ProcessError(err)
return n.db.ProcessError(err)
}
defer func() {
@ -286,9 +286,9 @@ func (n *notificationDB) DeleteNotificationsForStatus(ctx context.Context, statu
}
// Finally delete all from DB.
_, err := n.conn.NewDelete().
_, err := n.db.NewDelete().
Table("notifications").
Where("? IN (?)", bun.Ident("id"), bun.In(notifIDs)).
Exec(ctx)
return n.conn.ProcessError(err)
return n.db.ProcessError(err)
}