* SQLStore: Prevent concurrent migrations (#44101) * SQLStore: Prevent concurrent migrations * Hide behind a feature toggle * Configurable locking attempt timeout * Update docs/sources/administration/configuration.md Co-authored-by: Igor Suleymanov <radiohead@users.noreply.github.com> Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> (cherry picked from commitd718ee1918) * Resolve dependency cycle (#45427) (cherry picked from commit6a38ce2307)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package migrator
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
@@ -8,6 +9,7 @@ import (
|
||||
|
||||
"github.com/VividCortex/mysqlerr"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/golang-migrate/migrate/v4/database"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
@@ -225,3 +227,66 @@ func (db *MySQLDialect) UpsertSQL(tableName string, keyCols, updateCols []string
|
||||
)
|
||||
return s
|
||||
}
|
||||
|
||||
func (db *MySQLDialect) Lock(cfg LockCfg) error {
|
||||
query := "SELECT GET_LOCK(?, ?)"
|
||||
var success sql.NullBool
|
||||
|
||||
lockName, err := db.getLockName()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to generate lock name: %w", err)
|
||||
}
|
||||
|
||||
// trying to obtain the lock with the specific name
|
||||
// the lock is exclusive per session and is released explicitly by executing RELEASE_LOCK() or implicitly when the session terminates
|
||||
// it returns 1 if the lock was obtained successfully,
|
||||
// 0 if the attempt timed out (for example, because another client has previously locked the name),
|
||||
// or NULL if an error occurred
|
||||
// starting from MySQL 5.7 it is even possible for a given session to acquire multiple locks for the same name
|
||||
// however other sessions cannot acquire a lock with that name until the acquiring session releases all its locks for the name.
|
||||
_, err = cfg.Session.SQL(query, lockName, cfg.Timeout).Get(&success)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !success.Valid || !success.Bool {
|
||||
return ErrLockDB
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *MySQLDialect) Unlock(cfg LockCfg) error {
|
||||
query := "SELECT RELEASE_LOCK(?)"
|
||||
var success sql.NullBool
|
||||
|
||||
lockName, err := db.getLockName()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to generate lock name: %w", err)
|
||||
}
|
||||
|
||||
// trying to release the lock with the specific name
|
||||
// it returns 1 if the lock was released,
|
||||
// 0 if the lock was not established by this thread (in which case the lock is not released),
|
||||
// and NULL if the named lock did not exist (it was never obtained by a call to GET_LOCK() or if it has previously been released)
|
||||
_, err = cfg.Session.SQL(query, lockName).Get(&success)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !success.Valid || !success.Bool {
|
||||
return ErrReleaseLockDB
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *MySQLDialect) getLockName() (string, error) {
|
||||
cfg, err := mysql.ParseDSN(db.engine.DataSourceName())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
s, err := database.GenerateAdvisoryLockId(cfg.DBName)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate advisory lock key: %w", err)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user