Skip to content

Commit

Permalink
issue-708: Set lock_wait_timeout to SetReadOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
shunki-fujita committed Jul 8, 2024
1 parent 5f8ebc1 commit 9b64073
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
29 changes: 24 additions & 5 deletions clustering/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,31 @@ func (p *managerProcess) switchover(ctx context.Context, ss *StatusSet) error {
log.Info("begin switchover the primary", "current", ss.Primary, "next", ss.Candidate)

pdb := ss.DBOps[ss.Primary]
if err := pdb.SetReadOnly(ctx, true); err != nil {
return fmt.Errorf("failed to make instance %d read-only: %w", ss.Primary, err)

// SetReadOnly waits for a running DML.
// Therefore, if it waits for a long time, deleteGracePeriodSeconds may be reached.
// To avoid this, set lock_wait_timeout to a short time temporarily.
// If SetReadOnly fails, kill all processes and retry.
succeeded := false
if err := pdb.SetSessionLockWaitTimeout(ctx, 15); err != nil {
return fmt.Errorf("failed to set lock_wait_timeout: %w", err)
}
for i := 0; i < 2; i++ {
if err := pdb.SetReadOnly(ctx, true); err != nil {
log.Error(err, "failed to set read-only mode", "instance", ss.Primary)
} else {
succeeded = true
}
time.Sleep(100 * time.Millisecond)
if err := pdb.KillConnections(ctx); err != nil {
return fmt.Errorf("failed to kill connections in instance %d: %w", ss.Primary, err)
}
if succeeded {
break
}
}
time.Sleep(100 * time.Millisecond)
if err := pdb.KillConnections(ctx); err != nil {
return fmt.Errorf("failed to kill connections in instance %d: %w", ss.Primary, err)
if !succeeded {
return fmt.Errorf("failed to set read-only mode in instance %d", ss.Primary)
}
pst, err := pdb.GetStatus(ctx)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/dbop/nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (o NopOperator) WaitForGTID(ctx context.Context, gtidSet string, timeoutSec
return ErrNop
}

func (o NopOperator) SetSessionLockWaitTimeout(ctx context.Context, timeoutSeconds int) error {
return ErrNop
}

func (o NopOperator) SetReadOnly(context.Context, bool) error {
return ErrNop
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/dbop/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type Operator interface {
// If `timeoutSeconds` is zero, this will not timeout.
WaitForGTID(ctx context.Context, gtidSet string, timeoutSeconds int) error

// SetSessionLockWaitTimeout set @@session.lock_wait_timeout to `timeoutSeconds`.
SetSessionLockWaitTimeout(ctx context.Context, timeoutSeconds int) error

// SetReadOnly makes the instance super_read_only if `true` is passed.
// Otherwise, this stops the replication and makes the instance writable.
SetReadOnly(context.Context, bool) error
Expand Down
7 changes: 7 additions & 0 deletions pkg/dbop/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func (o *operator) WaitForGTID(ctx context.Context, gtid string, timeoutSeconds
return nil
}

func (o *operator) SetSessionLockWaitTimeout(ctx context.Context, timeoutSeconds int) error {
if _, err := o.db.ExecContext(ctx, "SET SESSION lock_wait_timeout=?", timeoutSeconds); err != nil {
return fmt.Errorf("failed to set lock_wait_timeout: %w", err)
}
return nil
}

func (o *operator) SetReadOnly(ctx context.Context, readOnly bool) error {
if readOnly {
if _, err := o.db.ExecContext(ctx, "SET GLOBAL super_read_only=1"); err != nil {
Expand Down

0 comments on commit 9b64073

Please sign in to comment.