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"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2017-05-15 05:19:19 -05:00
|
|
|
sqlite3 "github.com/mattn/go-sqlite3"
|
2015-02-04 08:37:26 -06: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 {
|
|
|
|
sess := startSession(ctx)
|
|
|
|
defer sess.Close()
|
2015-02-04 08:37:26 -06:00
|
|
|
|
2018-06-12 15:58:03 -05:00
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
withValue := context.WithValue(ctx, ContextSessionName, sess)
|
2017-05-23 03:56:23 -05:00
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
err := fn(withValue)
|
2017-05-15 05:19:19 -05:00
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
// special handling of database locked errors for sqlite, then we can retry 3 times
|
|
|
|
if sqlError, ok := err.(sqlite3.Error); ok && retry < 5 {
|
|
|
|
if sqlError.Code == sqlite3.ErrLocked {
|
|
|
|
sess.Rollback()
|
|
|
|
time.Sleep(time.Millisecond * time.Duration(10))
|
|
|
|
ss.log.Info("Database table locked, sleeping then retrying", "retry", retry)
|
|
|
|
return ss.inTransactionWithRetry(ctx, fn, retry+1)
|
|
|
|
}
|
|
|
|
}
|
2018-06-05 14:13:53 -05:00
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
if err != nil {
|
|
|
|
sess.Rollback()
|
|
|
|
return err
|
2018-06-05 14:13:53 -05:00
|
|
|
}
|
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
if err = sess.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-05 14:13:53 -05:00
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
if len(sess.events) > 0 {
|
|
|
|
for _, e := range sess.events {
|
|
|
|
if err = bus.Publish(e); err != nil {
|
|
|
|
ss.log.Error("Failed to publish event after commit", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-05 14:13:53 -05:00
|
|
|
|
2018-06-07 14:54:36 -05:00
|
|
|
return nil
|
2018-06-05 14:13:53 -05:00
|
|
|
}
|
|
|
|
|
2017-05-15 05:19:19 -05:00
|
|
|
func inTransactionWithRetry(callback dbTransactionFunc, retry int) error {
|
2018-06-05 14:13:53 -05:00
|
|
|
return inTransactionWithRetryCtx(context.Background(), callback, retry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func inTransactionWithRetryCtx(ctx context.Context, callback dbTransactionFunc, retry int) error {
|
2015-02-04 08:37:26 -06:00
|
|
|
var err error
|
|
|
|
|
2018-06-05 14:13:53 -05:00
|
|
|
sess := startSession(ctx)
|
2018-06-12 15:58:03 -05:00
|
|
|
|
2015-02-04 08:37:26 -06:00
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = callback(sess)
|
|
|
|
|
2017-05-15 05:19:19 -05:00
|
|
|
// special handling of database locked errors for sqlite, then we can retry 3 times
|
|
|
|
if sqlError, ok := err.(sqlite3.Error); ok && retry < 5 {
|
|
|
|
if sqlError.Code == sqlite3.ErrLocked {
|
|
|
|
sess.Rollback()
|
|
|
|
time.Sleep(time.Millisecond * time.Duration(10))
|
|
|
|
sqlog.Info("Database table locked, sleeping then retrying", "retry", retry)
|
|
|
|
return inTransactionWithRetry(callback, retry+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:37:26 -06:00
|
|
|
if err != nil {
|
|
|
|
sess.Rollback()
|
|
|
|
return err
|
|
|
|
} else if err = sess.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sess.events) > 0 {
|
|
|
|
for _, e := range sess.events {
|
|
|
|
if err = bus.Publish(e); err != nil {
|
|
|
|
log.Error(3, "Failed to publish event after commit", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|