2015-02-04 15:37:26 +01:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
|
|
import (
|
2018-06-05 21:13:53 +02:00
|
|
|
"context"
|
2020-11-19 14:47:17 +01:00
|
|
|
"errors"
|
2022-02-08 09:02:23 -05:00
|
|
|
"fmt"
|
2017-05-15 12:19:19 +02:00
|
|
|
"time"
|
|
|
|
|
|
2022-02-08 09:02:23 -05:00
|
|
|
"github.com/mattn/go-sqlite3"
|
|
|
|
|
"xorm.io/xorm"
|
|
|
|
|
|
2015-02-05 10:37:13 +01:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-05-13 14:45:54 +08:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2015-02-04 15:37:26 +01:00
|
|
|
)
|
|
|
|
|
|
2021-11-08 17:56:56 +01:00
|
|
|
var tsclogger = log.New("sqlstore.transactions")
|
|
|
|
|
|
2020-08-18 14:58:08 +02:00
|
|
|
// WithTransactionalDbSession calls the callback with a session within a transaction.
|
2022-02-01 14:51:22 +01:00
|
|
|
func (ss *SQLStore) WithTransactionalDbSession(ctx context.Context, callback DBTransactionFunc) error {
|
2022-10-17 21:23:44 +03:00
|
|
|
return ss.inTransactionWithRetryCtx(ctx, ss.engine, ss.bus, callback, 0)
|
2019-05-16 12:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-29 15:55:47 +03:00
|
|
|
// InTransaction starts a transaction and calls the fn
|
|
|
|
|
// It stores the session in the context
|
2020-11-11 06:21:08 +01:00
|
|
|
func (ss *SQLStore) InTransaction(ctx context.Context, fn func(ctx context.Context) error) error {
|
2018-06-07 12:54:36 -07:00
|
|
|
return ss.inTransactionWithRetry(ctx, fn, 0)
|
2015-02-04 15:37:26 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-11 06:21:08 +01:00
|
|
|
func (ss *SQLStore) inTransactionWithRetry(ctx context.Context, fn func(ctx context.Context) error, retry int) error {
|
2022-10-17 21:23:44 +03:00
|
|
|
return ss.inTransactionWithRetryCtx(ctx, ss.engine, ss.bus, func(sess *DBSession) error {
|
2020-03-23 13:37:53 +01:00
|
|
|
withValue := context.WithValue(ctx, ContextSessionKey{}, sess)
|
2019-05-16 12:39:59 +02:00
|
|
|
return fn(withValue)
|
|
|
|
|
}, retry)
|
2018-06-05 21:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-17 21:23:44 +03:00
|
|
|
func (ss *SQLStore) inTransactionWithRetryCtx(ctx context.Context, engine *xorm.Engine, bus bus.Bus, callback DBTransactionFunc, retry int) error {
|
2023-01-09 09:41:15 -05:00
|
|
|
sess, isNew, span, err := startSessionOrUseExisting(ctx, engine, true, ss.tracer)
|
2018-06-15 20:49:14 +02:00
|
|
|
if err != nil {
|
2015-02-04 15:37:26 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 09:02:23 -05:00
|
|
|
if !sess.transactionOpen && !isNew {
|
|
|
|
|
// this should not happen because the only place that creates reusable session begins a new transaction.
|
|
|
|
|
return fmt.Errorf("cannot reuse existing session that did not start transaction")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isNew { // if this call initiated the session, it should be responsible for closing it.
|
2023-01-09 09:41:15 -05:00
|
|
|
defer func() {
|
|
|
|
|
if span != nil {
|
|
|
|
|
span.End()
|
|
|
|
|
}
|
|
|
|
|
sess.Close()
|
|
|
|
|
}()
|
2022-02-08 09:02:23 -05:00
|
|
|
}
|
2018-06-15 20:49:14 +02:00
|
|
|
|
2015-02-04 15:37:26 +01:00
|
|
|
err = callback(sess)
|
|
|
|
|
|
2022-09-20 18:32:06 +02:00
|
|
|
ctxLogger := tsclogger.FromContext(ctx)
|
|
|
|
|
|
2022-02-08 09:02:23 -05:00
|
|
|
if !isNew {
|
2022-09-20 18:32:06 +02:00
|
|
|
ctxLogger.Debug("skip committing the transaction because it belongs to a session created in the outer scope")
|
2022-02-08 09:02:23 -05:00
|
|
|
// Do not commit the transaction if the session was reused.
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 09:24:15 +02:00
|
|
|
// special handling of database locked errors for sqlite, then we can retry 5 times
|
2020-11-19 14:47:17 +01:00
|
|
|
var sqlError sqlite3.Error
|
2022-10-17 21:23:44 +03:00
|
|
|
if errors.As(err, &sqlError) && retry < ss.dbCfg.TransactionRetries && (sqlError.Code == sqlite3.ErrLocked || sqlError.Code == sqlite3.ErrBusy) {
|
2019-10-22 14:08:18 +02:00
|
|
|
if rollErr := sess.Rollback(); rollErr != nil {
|
2022-06-06 16:30:31 -04:00
|
|
|
return fmt.Errorf("rolling back transaction due to error failed: %s: %w", rollErr, err)
|
2017-05-15 12:19:19 +02:00
|
|
|
}
|
2019-10-22 14:08:18 +02:00
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * time.Duration(10))
|
2022-10-17 21:23:44 +03:00
|
|
|
ctxLogger.Info("Database locked, sleeping then retrying", "error", err, "retry", retry, "code", sqlError.Code)
|
|
|
|
|
return ss.inTransactionWithRetryCtx(ctx, engine, bus, callback, retry+1)
|
2017-05-15 12:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-04 15:37:26 +01:00
|
|
|
if err != nil {
|
2019-10-22 14:08:18 +02:00
|
|
|
if rollErr := sess.Rollback(); rollErr != nil {
|
2022-06-06 16:30:31 -04:00
|
|
|
return fmt.Errorf("rolling back transaction due to error failed: %s: %w", rollErr, err)
|
2019-10-22 14:08:18 +02:00
|
|
|
}
|
2015-02-04 15:37:26 +01:00
|
|
|
return err
|
2019-10-22 14:08:18 +02:00
|
|
|
}
|
|
|
|
|
if err := sess.Commit(); err != nil {
|
2015-02-04 15:37:26 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 14:17:56 +01:00
|
|
|
for _, e := range sess.events {
|
|
|
|
|
if err = bus.Publish(ctx, e); err != nil {
|
|
|
|
|
ctxLogger.Error("Failed to publish event after commit.", "error", err)
|
2015-02-04 15:37:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|