From dae7eb34189fe7cbcd73c955d04799a5d8ccbd3c Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 16 Nov 2023 11:24:58 +0100 Subject: [PATCH] MySQL driver: on connect try setting wsrep_sync_wait=4, swallow error 1193 In Galera clusters wsrep_sync_wait=4 ensures inserted rows to be synced over all nodes before reporting success to their inserter. That allows inserting child rows immediately after that on another node without running into foreign key errors. MySQL single nodes will reject this with error 1193 "Unknown system variable" which is OK. --- pkg/driver/driver.go | 14 +++++++++++--- pkg/driver/mysql.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 pkg/driver/mysql.go diff --git a/pkg/driver/driver.go b/pkg/driver/driver.go index f529db445..ec215598d 100644 --- a/pkg/driver/driver.go +++ b/pkg/driver/driver.go @@ -32,7 +32,13 @@ func (c RetryConnector) Connect(ctx context.Context) (driver.Conn, error) { err := errors.Wrap(retry.WithBackoff( ctx, func(ctx context.Context) (err error) { - conn, err = c.Connector.Connect(ctx) + if conn, err = c.Connector.Connect(ctx); err == nil && c.driver.initConn != nil { + if err = c.driver.initConn(ctx, conn); err != nil { + _ = conn.Close() + conn = nil + } + } + return }, shouldRetry, @@ -67,7 +73,9 @@ func (c RetryConnector) Driver() driver.Driver { // Driver wraps a driver.Driver that also must implement driver.DriverContext with logging capabilities and provides our RetryConnector. type Driver struct { ctxDriver - Logger *logging.Logger + + Logger *logging.Logger + initConn func(context.Context, driver.Conn) error } // OpenConnector implements the DriverContext interface. @@ -85,7 +93,7 @@ func (d Driver) OpenConnector(name string) (driver.Connector, error) { // Register makes our database Driver available under the name "icingadb-*sql". func Register(logger *logging.Logger) { - sql.Register(MySQL, &Driver{ctxDriver: &mysql.MySQLDriver{}, Logger: logger}) + sql.Register(MySQL, &Driver{ctxDriver: &mysql.MySQLDriver{}, Logger: logger, initConn: setGaleraOpts}) sql.Register(PostgreSQL, &Driver{ctxDriver: PgSQLDriver{}, Logger: logger}) _ = mysql.SetLogger(mysqlLogger(func(v ...interface{}) { logger.Debug(v...) })) sqlx.BindDriver(PostgreSQL, sqlx.DOLLAR) diff --git a/pkg/driver/mysql.go b/pkg/driver/mysql.go new file mode 100644 index 000000000..522191ca1 --- /dev/null +++ b/pkg/driver/mysql.go @@ -0,0 +1,33 @@ +package driver + +import ( + "context" + "database/sql/driver" + "github.com/go-sql-driver/mysql" + "github.com/pkg/errors" +) + +// setGaleraOpts tries SET SESSION wsrep_sync_wait=4. +// Error 1193 "Unknown system variable" is ignored to support MySQL single nodes. +func setGaleraOpts(ctx context.Context, conn driver.Conn) error { + const galeraOpts = "SET SESSION wsrep_sync_wait=4" + + stmt, err := conn.(driver.ConnPrepareContext).PrepareContext(ctx, galeraOpts) + if err != nil { + err = errors.Wrap(err, "can't prepare "+galeraOpts) + } else if _, err = stmt.(driver.StmtExecContext).ExecContext(ctx, nil); err != nil { + err = errors.Wrap(err, "can't execute "+galeraOpts) + _ = stmt.Close() + } else if err = stmt.Close(); err != nil { + err = errors.Wrap(err, "can't close statement "+galeraOpts) + } + + if err != nil { + var me *mysql.MySQLError + if errors.As(err, &me) && me.Number == 1193 { + err = nil + } + } + + return err +}