Skip to content

Commit

Permalink
Support Percona XtraDB Cluster by not using SERIALIZABLE transactions…
Browse files Browse the repository at this point in the history
… directly

The RDBMS rejects them by default. But it doesn't rejects their equivalent:
Append "LOCK IN SHARE MODE" to every SELECT in a REPEATABLE READ transaction.
Now we do the latter with MySQL.
  • Loading branch information
Al2Klimov committed Jun 22, 2023
1 parent 4ed4db3 commit 6dc4998
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pkg/icingadb/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/icinga/icingadb/internal"
"github.com/icinga/icingadb/pkg/backoff"
"github.com/icinga/icingadb/pkg/com"
"github.com/icinga/icingadb/pkg/driver"
v1 "github.com/icinga/icingadb/pkg/icingadb/v1"
"github.com/icinga/icingadb/pkg/icingaredis"
icingaredisv1 "github.com/icinga/icingadb/pkg/icingaredis/v1"
Expand Down Expand Up @@ -247,14 +248,23 @@ func (h *HA) realize(ctx context.Context, s *icingaredisv1.IcingaStatus, t *type
func(ctx context.Context) error {
takeover = false
otherResponsible = false
isoLvl := sql.LevelSerializable
selectLock := ""

if h.db.DriverName() == driver.MySQL {
// The RDBMS may actually be a Percona XtraDB Cluster which doesn't
// support serializable transactions, but only their following equivalent:
isoLvl = sql.LevelRepeatableRead
selectLock = " LOCK IN SHARE MODE"
}

tx, errBegin := h.db.BeginTxx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable})
tx, errBegin := h.db.BeginTxx(ctx, &sql.TxOptions{Isolation: isoLvl})
if errBegin != nil {
return errors.Wrap(errBegin, "can't start transaction")
}

query := h.db.Rebind("SELECT id, heartbeat FROM icingadb_instance " +
"WHERE environment_id = ? AND responsible = ? AND id <> ? AND heartbeat > ?")
query := h.db.Rebind("SELECT id, heartbeat FROM icingadb_instance "+
"WHERE environment_id = ? AND responsible = ? AND id <> ? AND heartbeat > ?") + selectLock

instance := &v1.IcingadbInstance{}

Expand Down

0 comments on commit 6dc4998

Please sign in to comment.