Skip to content

Commit 1c18a24

Browse files
committed
[PBM-1187] add --wait option to pbm config --force-resync (#883)
1 parent f076283 commit 1c18a24

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

cmd/pbm/config.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"reflect"
99
"strings"
10+
"time"
1011

1112
"go.mongodb.org/mongo-driver/mongo"
1213
"gopkg.in/yaml.v2"
@@ -17,8 +18,11 @@ import (
1718
"github.com/percona/percona-backup-mongodb/sdk"
1819
)
1920

21+
const resyncWaitDuration = 30 * time.Second
22+
2023
type configOpts struct {
2124
rsync bool
25+
wait bool
2226
list bool
2327
file string
2428
set map[string]string
@@ -75,11 +79,27 @@ func runConfig(ctx context.Context, conn connect.Client, pbmSDK sdk.Client, c *c
7579
}
7680
return confKV{c.key, fmt.Sprint(k)}, nil
7781
case c.rsync:
78-
79-
if _, err := pbmSDK.SyncFromStorage(ctx); err != nil {
82+
cid, err := pbmSDK.SyncFromStorage(ctx)
83+
if err != nil {
8084
return nil, errors.Wrap(err, "resync")
8185
}
82-
return outMsg{"Storage resync started"}, nil
86+
87+
if !c.wait {
88+
return outMsg{"Storage resync started"}, nil
89+
}
90+
91+
ctx, cancel := context.WithTimeout(ctx, resyncWaitDuration)
92+
defer cancel()
93+
94+
err = sdk.WaitForResync(ctx, pbmSDK, cid)
95+
if err != nil {
96+
if errors.Is(err, context.DeadlineExceeded) {
97+
err = errors.New("timeout")
98+
}
99+
return nil, errors.Wrapf(err, "waiting for resync [opid %q]", cid)
100+
}
101+
102+
return outMsg{"Storage resync finished"}, nil
83103
case len(c.file) > 0:
84104
var buf []byte
85105
var err error

cmd/pbm/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func main() {
9696
StringMapVar(&cfg.set)
9797
configCmd.Arg("key", "Show the value of a specified key").
9898
StringVar(&cfg.key)
99+
configCmd.Flag("wait", "Wait for finish").
100+
Short('w').
101+
BoolVar(&cfg.wait)
99102

100103
backupCmd := pbmCmd.Command("backup", "Make backup")
101104
backup := backupOpts{}

internal/log/history.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ func Follow(
214214
e.ObjID, _ = cur.Current.Lookup("_id").ObjectIDOK()
215215
outC <- e
216216
}
217+
218+
if err := cur.Err(); err != nil {
219+
errC <- err
220+
}
217221
}()
218222

219223
return outC, errC

sdk/util.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package sdk
22

33
import (
44
"context"
5-
"errors"
65

76
"go.mongodb.org/mongo-driver/bson/primitive"
87
"go.mongodb.org/mongo-driver/mongo"
98

9+
"github.com/percona/percona-backup-mongodb/internal/ctrl"
1010
"github.com/percona/percona-backup-mongodb/internal/defs"
11+
"github.com/percona/percona-backup-mongodb/internal/errors"
12+
"github.com/percona/percona-backup-mongodb/internal/log"
1113
"github.com/percona/percona-backup-mongodb/internal/topo"
1214
)
1315

@@ -28,3 +30,29 @@ func GetClusterTime(ctx context.Context, m *mongo.Client) (Timestamp, error) {
2830

2931
return info.ClusterTime.ClusterTime, nil
3032
}
33+
34+
func WaitForResync(ctx context.Context, c Client, cid CommandID) error {
35+
ctx, cancel := context.WithCancel(ctx)
36+
defer cancel()
37+
38+
r := &log.LogRequest{
39+
LogKeys: log.LogKeys{
40+
Event: string(ctrl.CmdResync),
41+
OPID: string(cid),
42+
Severity: log.Info,
43+
},
44+
}
45+
46+
outC, errC := log.Follow(ctx, c.(*clientImpl).conn.LogCollection(), r, false)
47+
48+
for {
49+
select {
50+
case entry := <-outC:
51+
if entry != nil && entry.Msg == "succeed" {
52+
return nil
53+
}
54+
case err := <-errC:
55+
return err
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)