Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PBM-1208: Add active lock check before running the backup #982

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions cmd/pbm-agent/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ func (a *Agent) Backup(ctx context.Context, cmd *ctrl.BackupCmd, opid ctrl.OPID,
}

isClusterLeader := nodeInfo.IsClusterLeader()

if isClusterLeader {
moveOn, err := a.startBcpLockCheck(ctx)
if err != nil {
l.Error("start backup lock check: %v", err)
return
}
if !moveOn {
l.Error("unable to proceed with the backup, active lock is present")
return
}
}

canRunBackup, err := topo.NodeSuitsExt(ctx, a.nodeConn, nodeInfo, cmd.Type)
if err != nil {
l.Error("node check: %v", err)
Expand Down Expand Up @@ -314,3 +327,32 @@ func (a *Agent) waitNomination(ctx context.Context, bcp string) (bool, error) {
}
}
}

// startBcpLockCheck checks if there is any active lock.
// It fetches all existing pbm locks, and if any exists, it is also
// checked for staleness.
// false is returned in case a single active lock exists or error happens.
// true means that there's no active locks.
func (a *Agent) startBcpLockCheck(ctx context.Context) (bool, error) {
locks, err := lock.GetLocks(ctx, a.leadConn, &lock.LockHeader{})
if err != nil {
return false, errors.Wrap(err, "get all locks for backup start")
}
if len(locks) == 0 {
return true, nil
}

// stale lock check
ts, err := topo.GetClusterTime(ctx, a.leadConn)
if err != nil {
return false, errors.Wrap(err, "read cluster time")
}

for _, l := range locks {
if l.Heartbeat.T+defs.StaleFrameSec >= ts.T {
return false, nil
}
}

return true, nil
}
Loading