2015-02-04 08:37:26 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2018-06-05 14:13:53 -05:00
|
|
|
"context"
|
2017-05-15 05:19:19 -05:00
|
|
|
"time"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-10-22 07:08:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2020-02-05 02:00:40 -06:00
|
|
|
"github.com/mattn/go-sqlite3"
|
2020-04-01 08:57:21 -05:00
|
|
|
"xorm.io/xorm"
|
2015-02-04 08:37:26 -06:00
|
|
|
)
|
|
|
|
|
2020-08-18 07:58:08 -05:00
|
|
|
// WithTransactionalDbSession calls the callback with a session within a transaction.
|
2019-05-16 05:39:59 -05:00
|
|
|
func (ss *SqlStore) WithTransactionalDbSession(ctx context.Context, callback dbTransactionFunc) error {
|
2019-05-17 00:35:37 -05:00
|
|
|
return inTransactionWithRetryCtx(ctx, ss.engine, callback, 0)
|
2019-05-16 05:39:59 -05:00
|
|
|
}
|
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
func (ss *SqlStore) InTransaction(ctx context.Context, fn func(ctx context.Context) error) error {
|
|
|
|
return ss.inTransactionWithRetry(ctx, fn, 0)
|
2015-02-04 08:37:26 -06:00
|
|
|
}
|
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
func (ss *SqlStore) inTransactionWithRetry(ctx context.Context, fn func(ctx context.Context) error, retry int) error {
|
2019-05-17 00:35:37 -05:00
|
|
|
return inTransactionWithRetryCtx(ctx, ss.engine, func(sess *DBSession) error {
|
2020-03-23 07:37:53 -05:00
|
|
|
withValue := context.WithValue(ctx, ContextSessionKey{}, sess)
|
2019-05-16 05:39:59 -05:00
|
|
|
return fn(withValue)
|
|
|
|
}, retry)
|
2018-06-05 14:13:53 -05:00
|
|
|
}
|
|
|
|
|
2017-05-15 05:19:19 -05:00
|
|
|
func inTransactionWithRetry(callback dbTransactionFunc, retry int) error {
|
2019-05-17 00:35:37 -05:00
|
|
|
return inTransactionWithRetryCtx(context.Background(), x, callback, retry)
|
2018-06-05 14:13:53 -05:00
|
|
|
}
|
|
|
|
|
2019-05-17 00:35:37 -05:00
|
|
|
func inTransactionWithRetryCtx(ctx context.Context, engine *xorm.Engine, callback dbTransactionFunc, retry int) error {
|
2019-05-16 05:39:59 -05:00
|
|
|
sess, err := startSession(ctx, engine, true)
|
2018-06-15 13:49:14 -05:00
|
|
|
if err != nil {
|
2015-02-04 08:37:26 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-15 13:49:14 -05:00
|
|
|
defer sess.Close()
|
|
|
|
|
2015-02-04 08:37:26 -06:00
|
|
|
err = callback(sess)
|
|
|
|
|
2019-05-27 02:24:15 -05:00
|
|
|
// special handling of database locked errors for sqlite, then we can retry 5 times
|
2019-10-22 07:08:18 -05:00
|
|
|
if sqlError, ok := err.(sqlite3.Error); ok && retry < 5 && sqlError.Code ==
|
|
|
|
sqlite3.ErrLocked || sqlError.Code == sqlite3.ErrBusy {
|
|
|
|
if rollErr := sess.Rollback(); rollErr != nil {
|
|
|
|
return errutil.Wrapf(err, "Rolling back transaction due to error failed: %s", rollErr)
|
2017-05-15 05:19:19 -05:00
|
|
|
}
|
2019-10-22 07:08:18 -05:00
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * time.Duration(10))
|
|
|
|
sqlog.Info("Database locked, sleeping then retrying", "error", err, "retry", retry)
|
|
|
|
return inTransactionWithRetry(callback, retry+1)
|
2017-05-15 05:19:19 -05:00
|
|
|
}
|
|
|
|
|
2015-02-04 08:37:26 -06:00
|
|
|
if err != nil {
|
2019-10-22 07:08:18 -05:00
|
|
|
if rollErr := sess.Rollback(); rollErr != nil {
|
|
|
|
return errutil.Wrapf(err, "Rolling back transaction due to error failed: %s", rollErr)
|
|
|
|
}
|
2015-02-04 08:37:26 -06:00
|
|
|
return err
|
2019-10-22 07:08:18 -05:00
|
|
|
}
|
|
|
|
if err := sess.Commit(); err != nil {
|
2015-02-04 08:37:26 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sess.events) > 0 {
|
|
|
|
for _, e := range sess.events {
|
|
|
|
if err = bus.Publish(e); err != nil {
|
2020-07-23 01:14:39 -05:00
|
|
|
log.Errorf(3, "Failed to publish event after commit. error: %v", err)
|
2015-02-04 08:37:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-10 09:54:21 -05:00
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
func inTransaction(callback dbTransactionFunc) error {
|
|
|
|
return inTransactionWithRetry(callback, 0)
|
2018-05-10 09:54:21 -05:00
|
|
|
}
|
2018-06-15 14:57:13 -05:00
|
|
|
|
|
|
|
func inTransactionCtx(ctx context.Context, callback dbTransactionFunc) error {
|
2019-05-17 00:35:37 -05:00
|
|
|
return inTransactionWithRetryCtx(ctx, x, callback, 0)
|
2018-06-15 14:57:13 -05:00
|
|
|
}
|