Skip to content

Commit

Permalink
PBM-1452: Expand procedure for disabling balancer (#1094)
Browse files Browse the repository at this point in the history
* Expand procedure for disabling balancer

* Fix line length
  • Loading branch information
boris-ilijic authored Feb 14, 2025
1 parent 6dc6bd6 commit 06640a7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
9 changes: 7 additions & 2 deletions cmd/pbm-agent/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ func (a *Agent) Restore(ctx context.Context, r *ctrl.RestoreCmd, opid ctrl.OPID,
}

l.Debug("waiting for balancer off")
bs := topo.WaitForBalancerOff(ctx, a.leadConn, time.Second*30, l)
l.Debug("balancer status: %s", bs)
bs := topo.WaitForBalancerDisabled(ctx, a.leadConn, time.Second*30, l)
if bs.IsDisabled() {
l.Debug("balancer is disabled")
} else {
l.Warning("balancer is not disabled: balancer mode: %s, in balancer round: %t",
bs.Mode, bs.InBalancerRound)
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions pbm/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,14 @@ func (b *Backup) Run(ctx context.Context, bcp *ctrl.BackupCmd, opid ctrl.OPID, l
}

l.Debug("waiting for balancer off")
bs := topo.WaitForBalancerOff(ctx, b.leadConn, time.Second*30, l)
l.Debug("balancer status: %s", bs)
bs := topo.WaitForBalancerDisabled(ctx, b.leadConn, time.Second*30, l)
if bs.IsDisabled() {
l.Debug("balancer is disabled")
} else {
l.Warning("balancer is not disabled: balancer mode: %s, in balancer round: %t",
bs.Mode, bs.InBalancerRound)
}

}
}

Expand Down
4 changes: 4 additions & 0 deletions pbm/topo/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ func (b *BalancerStatus) IsOn() bool {
return b.Mode == BalancerModeOn
}

func (b *BalancerStatus) IsDisabled() bool {
return b.Mode == BalancerModeOff && !b.InBalancerRound
}

// SetBalancerStatus sets balancer status
func SetBalancerStatus(ctx context.Context, m connect.Client, mode BalancerMode) error {
var cmd string
Expand Down
19 changes: 14 additions & 5 deletions pbm/topo/topo.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,16 @@ func ReplicationLag(ctx context.Context, m *mongo.Client, self string) (int, err
return primaryOptime - nodeOptime, nil
}

func WaitForBalancerOff(ctx context.Context, conn connect.Client, t time.Duration, l log.LogEvent) BalancerMode {
// WaitForBalancerDisabled waits balancer to be disabled for specified time duration t.
// Disabling balancer means that balancer is in OFF mode and that it doesn't have active balancing round.
// If balancer is not desibled during the specified duration t, function will stop waiting
// and it'll return current statuses.
func WaitForBalancerDisabled(
ctx context.Context,
conn connect.Client,
t time.Duration,
l log.LogEvent,
) *BalancerStatus {
dn := time.NewTimer(t)
defer dn.Stop()

Expand All @@ -249,17 +258,17 @@ Loop:
l.Error("get balancer status: %v", err)
continue
}
if bs.Mode == BalancerModeOff {
return BalancerModeOff
if bs.IsDisabled() {
return bs
}
case <-dn.C:
break Loop
}
}

if bs == nil {
return BalancerMode("")
return &BalancerStatus{}
}

return bs.Mode
return bs
}

0 comments on commit 06640a7

Please sign in to comment.