Skip to content

Commit

Permalink
Merge pull request #51 from Icinga/wsrep_sync_wait-prepare-execute
Browse files Browse the repository at this point in the history
setGaleraOpts(): execute SET SESSION wsrep_sync_wait directly
  • Loading branch information
julianbrost authored Aug 2, 2024
2 parents 87fa4f2 + 87f219b commit 420fbff
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/icinga/icinga-go-library/strcase"
"github.com/icinga/icinga-go-library/types"
"github.com/pkg/errors"
"strconv"
)

// CantPerformQuery wraps the given error with the specified query that cannot be executed.
Expand Down Expand Up @@ -50,29 +51,17 @@ func SplitOnDupId[T IDer]() com.BulkChunkSplitPolicy[T] {
//
// https://mariadb.com/kb/en/galera-cluster-system-variables/#wsrep_sync_wait
func setGaleraOpts(ctx context.Context, conn driver.Conn, wsrepSyncWait int64) error {
const galeraOpts = "SET SESSION wsrep_sync_wait=?"
galeraOpts := "SET SESSION wsrep_sync_wait=" + strconv.FormatInt(wsrepSyncWait, 10)

stmt, err := conn.(driver.ConnPrepareContext).PrepareContext(ctx, galeraOpts)
_, err := conn.(driver.ExecerContext).ExecContext(ctx, galeraOpts, nil)
if err != nil {
if errors.Is(err, &mysql.MySQLError{Number: 1193}) { // Unknown system variable
return nil
}

return errors.Wrap(err, "cannot prepare "+galeraOpts)
}
// This is just for an unexpected exit and any returned error can safely be ignored and in case
// of the normal function exit, the stmt is closed manually, and its error is handled gracefully.
defer func() { _ = stmt.Close() }()

_, err = stmt.(driver.StmtExecContext).ExecContext(ctx, []driver.NamedValue{{Value: wsrepSyncWait}})
if err != nil {
return errors.Wrap(err, "cannot execute "+galeraOpts)
}

if err = stmt.Close(); err != nil {
return errors.Wrap(err, "cannot close prepared statement "+galeraOpts)
}

return nil
}

Expand Down

0 comments on commit 420fbff

Please sign in to comment.