Skip to content

Commit 50c5672

Browse files
committed
Extract WaitForBalancerOff to topo package
1 parent efb1174 commit 50c5672

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

pbm/backup/backup.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ 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 := waitForBalancerOff(ctx, b.leadConn, time.Second*30, l)
296+
bs := topo.WaitForBalancerOff(ctx, b.leadConn, time.Second*30, l)
297297
l.Debug("balancer status: %s", bs)
298298
}
299299
}
@@ -378,40 +378,6 @@ func (b *Backup) Run(ctx context.Context, bcp *ctrl.BackupCmd, opid ctrl.OPID, l
378378
}
379379
}
380380

381-
func waitForBalancerOff(ctx context.Context, conn connect.Client, t time.Duration, l log.LogEvent) topo.BalancerMode {
382-
dn := time.NewTimer(t)
383-
defer dn.Stop()
384-
385-
tk := time.NewTicker(time.Millisecond * 500)
386-
defer tk.Stop()
387-
388-
var bs *topo.BalancerStatus
389-
var err error
390-
391-
Loop:
392-
for {
393-
select {
394-
case <-tk.C:
395-
bs, err = topo.GetBalancerStatus(ctx, conn)
396-
if err != nil {
397-
l.Error("get balancer status: %v", err)
398-
continue
399-
}
400-
if bs.Mode == topo.BalancerModeOff {
401-
return topo.BalancerModeOff
402-
}
403-
case <-dn.C:
404-
break Loop
405-
}
406-
}
407-
408-
if bs == nil {
409-
return topo.BalancerMode("")
410-
}
411-
412-
return bs.Mode
413-
}
414-
415381
func (b *Backup) toState(
416382
ctx context.Context,
417383
status defs.Status,

pbm/topo/topo.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"time"
78

89
"go.mongodb.org/mongo-driver/bson"
910
"go.mongodb.org/mongo-driver/bson/primitive"
@@ -12,6 +13,7 @@ import (
1213
"github.com/percona/percona-backup-mongodb/pbm/connect"
1314
"github.com/percona/percona-backup-mongodb/pbm/defs"
1415
"github.com/percona/percona-backup-mongodb/pbm/errors"
16+
"github.com/percona/percona-backup-mongodb/pbm/log"
1517
"github.com/percona/percona-backup-mongodb/pbm/version"
1618
)
1719

@@ -227,3 +229,37 @@ func ReplicationLag(ctx context.Context, m *mongo.Client, self string) (int, err
227229

228230
return primaryOptime - nodeOptime, nil
229231
}
232+
233+
func WaitForBalancerOff(ctx context.Context, conn connect.Client, t time.Duration, l log.LogEvent) BalancerMode {
234+
dn := time.NewTimer(t)
235+
defer dn.Stop()
236+
237+
tk := time.NewTicker(time.Millisecond * 500)
238+
defer tk.Stop()
239+
240+
var bs *BalancerStatus
241+
var err error
242+
243+
Loop:
244+
for {
245+
select {
246+
case <-tk.C:
247+
bs, err = GetBalancerStatus(ctx, conn)
248+
if err != nil {
249+
l.Error("get balancer status: %v", err)
250+
continue
251+
}
252+
if bs.Mode == BalancerModeOff {
253+
return BalancerModeOff
254+
}
255+
case <-dn.C:
256+
break Loop
257+
}
258+
}
259+
260+
if bs == nil {
261+
return BalancerMode("")
262+
}
263+
264+
return bs.Mode
265+
}

0 commit comments

Comments
 (0)