Skip to content

Commit 06640a7

Browse files
authored
PBM-1452: Expand procedure for disabling balancer (#1094)
* Expand procedure for disabling balancer * Fix line length
1 parent 6dc6bd6 commit 06640a7

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

cmd/pbm-agent/restore.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,13 @@ func (a *Agent) Restore(ctx context.Context, r *ctrl.RestoreCmd, opid ctrl.OPID,
102102
}
103103

104104
l.Debug("waiting for balancer off")
105-
bs := topo.WaitForBalancerOff(ctx, a.leadConn, time.Second*30, l)
106-
l.Debug("balancer status: %s", bs)
105+
bs := topo.WaitForBalancerDisabled(ctx, a.leadConn, time.Second*30, l)
106+
if bs.IsDisabled() {
107+
l.Debug("balancer is disabled")
108+
} else {
109+
l.Warning("balancer is not disabled: balancer mode: %s, in balancer round: %t",
110+
bs.Mode, bs.InBalancerRound)
111+
}
107112
}
108113
}
109114

pbm/backup/backup.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,14 @@ func (b *Backup) Run(ctx context.Context, bcp *ctrl.BackupCmd, opid ctrl.OPID, l
293293
}
294294

295295
l.Debug("waiting for balancer off")
296-
bs := topo.WaitForBalancerOff(ctx, b.leadConn, time.Second*30, l)
297-
l.Debug("balancer status: %s", bs)
296+
bs := topo.WaitForBalancerDisabled(ctx, b.leadConn, time.Second*30, l)
297+
if bs.IsDisabled() {
298+
l.Debug("balancer is disabled")
299+
} else {
300+
l.Warning("balancer is not disabled: balancer mode: %s, in balancer round: %t",
301+
bs.Mode, bs.InBalancerRound)
302+
}
303+
298304
}
299305
}
300306

pbm/topo/cluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ func (b *BalancerStatus) IsOn() bool {
215215
return b.Mode == BalancerModeOn
216216
}
217217

218+
func (b *BalancerStatus) IsDisabled() bool {
219+
return b.Mode == BalancerModeOff && !b.InBalancerRound
220+
}
221+
218222
// SetBalancerStatus sets balancer status
219223
func SetBalancerStatus(ctx context.Context, m connect.Client, mode BalancerMode) error {
220224
var cmd string

pbm/topo/topo.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,16 @@ func ReplicationLag(ctx context.Context, m *mongo.Client, self string) (int, err
230230
return primaryOptime - nodeOptime, nil
231231
}
232232

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

@@ -249,17 +258,17 @@ Loop:
249258
l.Error("get balancer status: %v", err)
250259
continue
251260
}
252-
if bs.Mode == BalancerModeOff {
253-
return BalancerModeOff
261+
if bs.IsDisabled() {
262+
return bs
254263
}
255264
case <-dn.C:
256265
break Loop
257266
}
258267
}
259268

260269
if bs == nil {
261-
return BalancerMode("")
270+
return &BalancerStatus{}
262271
}
263272

264-
return bs.Mode
273+
return bs
265274
}

0 commit comments

Comments
 (0)