Skip to content

Commit 51f3c7d

Browse files
authored
Merge pull request #1130 from percona/PBM-1514-Improve-resync-performance
2 parents b8a1286 + 9852524 commit 51f3c7d

File tree

9 files changed

+69
-55
lines changed

9 files changed

+69
-55
lines changed

cmd/pbm-agent/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func (a *Agent) Cleanup(ctx context.Context, d *ctrl.CleanupCmd, opid ctrl.OPID,
297297
l.Error(err.Error())
298298
}
299299

300-
err = resync.Resync(ctx, a.leadConn, &cfg.Storage, a.brief.Me)
300+
err = resync.Resync(ctx, a.leadConn, &cfg.Storage, a.brief.Me, false)
301301
if err != nil {
302302
l.Error("storage resync: " + err.Error())
303303
}

cmd/pbm-agent/resync.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (a *Agent) Resync(ctx context.Context, cmd *ctrl.ResyncCmd, opid ctrl.OPID,
7171
} else if cmd.Name != "" {
7272
err = a.handleSyncProfile(ctx, cmd.Name, cmd.Clear)
7373
} else {
74-
err = a.handleSyncMainStorage(ctx)
74+
err = a.handleSyncMainStorage(ctx, cmd.SkipRestores)
7575
}
7676
if err != nil {
7777
l.Error(err.Error())
@@ -134,13 +134,13 @@ func (a *Agent) helpSyncProfileBackups(ctx context.Context, profile *config.Conf
134134
return errors.Wrapf(err, "sync backup list for %q", profile.Name)
135135
}
136136

137-
func (a *Agent) handleSyncMainStorage(ctx context.Context) error {
137+
func (a *Agent) handleSyncMainStorage(ctx context.Context, skipRestores bool) error {
138138
cfg, err := config.GetConfig(ctx, a.leadConn)
139139
if err != nil {
140140
return errors.Wrap(err, "get config")
141141
}
142142

143-
err = resync.Resync(ctx, a.leadConn, &cfg.Storage, a.brief.Me)
143+
err = resync.Resync(ctx, a.leadConn, &cfg.Storage, a.brief.Me, skipRestores)
144144
if err != nil {
145145
return errors.Wrap(err, "resync")
146146
}

cmd/pbm/config.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ import (
1818
)
1919

2020
type configOpts struct {
21-
rsync bool
22-
wait bool
23-
waitTime time.Duration
24-
list bool
25-
file string
26-
set map[string]string
27-
key string
21+
rsync bool
22+
skipRestores bool
23+
wait bool
24+
waitTime time.Duration
25+
list bool
26+
file string
27+
set map[string]string
28+
key string
2829
}
2930

3031
type confKV struct {
@@ -76,7 +77,7 @@ func runConfig(
7677
}
7778
}
7879
if rsnc {
79-
if _, err := pbm.SyncFromStorage(ctx); err != nil {
80+
if _, err := pbm.SyncFromStorage(ctx, false); err != nil {
8081
return nil, errors.Wrap(err, "resync")
8182
}
8283
}
@@ -91,7 +92,7 @@ func runConfig(
9192
}
9293
return confKV{c.key, fmt.Sprint(k)}, nil
9394
case c.rsync:
94-
cid, err := pbm.SyncFromStorage(ctx)
95+
cid, err := pbm.SyncFromStorage(ctx, c.skipRestores)
9596
if err != nil {
9697
return nil, errors.Wrap(err, "resync")
9798
}
@@ -143,7 +144,7 @@ func runConfig(
143144

144145
// resync storage only if Storage options have changed
145146
if !reflect.DeepEqual(newCfg.Storage, oldCfg.Storage) {
146-
if _, err := pbm.SyncFromStorage(ctx); err != nil {
147+
if _, err := pbm.SyncFromStorage(ctx, false); err != nil {
147148
return nil, errors.Wrap(err, "resync")
148149
}
149150
}

cmd/pbm/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ func (app *pbmApp) buildConfigCmd() *cobra.Command {
368368
Use: "config [key]",
369369
Short: "Set, change or list the config",
370370
RunE: app.wrapRunE(func(cmd *cobra.Command, args []string) (fmt.Stringer, error) {
371+
if cfg.skipRestores && !cfg.rsync {
372+
return nil, errors.New("--skip-restores cannot be used without --force-resync")
373+
}
374+
371375
if len(args) == 1 {
372376
cfg.key = args[0]
373377
}
@@ -376,6 +380,9 @@ func (app *pbmApp) buildConfigCmd() *cobra.Command {
376380
}
377381

378382
configCmd.Flags().BoolVar(&cfg.rsync, "force-resync", false, "Resync backup list with the current store")
383+
configCmd.Flags().BoolVar(
384+
&cfg.skipRestores, "skip-restores", false, "Skip physical restore metadata during force-resync",
385+
)
379386
configCmd.Flags().BoolVar(&cfg.list, "list", false, "List current settings")
380387
configCmd.Flags().StringVar(&cfg.file, "file", "", "Upload config from YAML file")
381388
configCmd.Flags().StringToStringVar(&cfg.set, "set", nil, "Set the option value <key.name=value>")

e2e-tests/pkg/pbm/mongo_pbm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (m *MongoPBM) StoreResync(ctx context.Context) error {
7171
return errors.Wrap(err, "get config")
7272
}
7373

74-
return resync.Resync(ctx, m.conn, &cfg.Storage, "")
74+
return resync.Resync(ctx, m.conn, &cfg.Storage, "", false)
7575
}
7676

7777
func (m *MongoPBM) Conn() connect.Client {

pbm/ctrl/cmd.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ type ProfileCmd struct {
121121
}
122122

123123
type ResyncCmd struct {
124-
Name string `bson:"name,omitempty"`
125-
All bool `bson:"all,omitempty"`
126-
Clear bool `bson:"clear,omitempty"`
124+
Name string `bson:"name,omitempty"`
125+
All bool `bson:"all,omitempty"`
126+
Clear bool `bson:"clear,omitempty"`
127+
SkipRestores bool `bson:"skipRestores,omitempty"`
127128
}
128129

129130
type BackupCmd struct {

pbm/ctrl/send.go

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,37 +93,15 @@ func SendRemoveConfigProfile(ctx context.Context, m connect.Client, name string)
9393
return sendCommand(ctx, m, cmd)
9494
}
9595

96-
func SendResync(ctx context.Context, m connect.Client) (OPID, error) {
97-
return sendCommand(ctx, m, Cmd{Cmd: CmdResync})
98-
}
99-
100-
func SendSyncMetaFrom(ctx context.Context, m connect.Client, name string) (OPID, error) {
101-
opts := &ResyncCmd{}
102-
if name != "" {
103-
opts.Name = name
104-
} else {
105-
opts.All = true
106-
}
107-
96+
func SendResync(ctx context.Context, m connect.Client, opts *ResyncCmd) (OPID, error) {
10897
cmd := Cmd{
109-
Cmd: CmdResync,
110-
Resync: opts,
98+
Cmd: CmdResync,
11199
}
112-
return sendCommand(ctx, m, cmd)
113-
}
114100

115-
func SendClearMetaFrom(ctx context.Context, m connect.Client, name string) (OPID, error) {
116-
opts := &ResyncCmd{Clear: true}
117-
if name != "" {
118-
opts.Name = name
119-
} else {
120-
opts.All = true
101+
if opts != nil {
102+
cmd.Resync = opts
121103
}
122104

123-
cmd := Cmd{
124-
Cmd: CmdResync,
125-
Resync: opts,
126-
}
127105
return sendCommand(ctx, m, cmd)
128106
}
129107

pbm/resync/rsync.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ import (
2626
//
2727
// It checks for read and write permissions, drops all meta from the database
2828
// and populate it again by reading meta from the storage.
29-
func Resync(ctx context.Context, conn connect.Client, cfg *config.StorageConf, node string) error {
29+
func Resync(
30+
ctx context.Context,
31+
conn connect.Client,
32+
cfg *config.StorageConf,
33+
node string,
34+
skipRestores bool,
35+
) error {
3036
l := log.LogEventFromContext(ctx)
3137

3238
stg, err := util.StorageFromConfig(cfg, node, l)
@@ -62,7 +68,7 @@ func Resync(ctx context.Context, conn connect.Client, cfg *config.StorageConf, n
6268
l.Error("failed sync oplog range: %v", err)
6369
}
6470

65-
err = resyncPhysicalRestores(ctx, conn, stg)
71+
err = resyncPhysicalRestores(ctx, conn, stg, skipRestores)
6672
if err != nil {
6773
l.Error("failed sync physical restore metadata: %v", err)
6874
}
@@ -244,6 +250,7 @@ func resyncPhysicalRestores(
244250
ctx context.Context,
245251
conn connect.Client,
246252
stg storage.Storage,
253+
skipRestores bool,
247254
) error {
248255
_, err := conn.RestoresCollection().DeleteMany(ctx, bson.D{})
249256
if err != nil {
@@ -262,7 +269,7 @@ func resyncPhysicalRestores(
262269
return nil
263270
}
264271

265-
restoreMeta, err := getAllRestoreMetaFromStorage(ctx, stg)
272+
restoreMeta, err := getAllRestoreMetaFromStorage(ctx, stg, skipRestores)
266273
if err != nil {
267274
return errors.Wrap(err, "get all restore meta from storage")
268275
}
@@ -315,6 +322,7 @@ func getAllBackupMetaFromStorage(
315322
func getAllRestoreMetaFromStorage(
316323
ctx context.Context,
317324
stg storage.Storage,
325+
skipRestores bool,
318326
) ([]*restore.RestoreMeta, error) {
319327
l := log.LogEventFromContext(ctx)
320328

@@ -323,8 +331,23 @@ func getAllRestoreMetaFromStorage(
323331
return nil, errors.Wrap(err, "get physical restores list from the storage")
324332
}
325333

326-
rv := make([]*restore.RestoreMeta, 0, len(restoreMeta))
327-
for _, file := range restoreMeta {
334+
var targets []storage.FileInfo
335+
336+
if skipRestores && len(restoreMeta) > 0 {
337+
l.Debug("only processing last restore")
338+
latest := restoreMeta[0]
339+
for _, f := range restoreMeta[1:] {
340+
if f.Name > latest.Name {
341+
latest = f
342+
}
343+
}
344+
targets = []storage.FileInfo{latest}
345+
} else {
346+
targets = restoreMeta
347+
}
348+
349+
rv := make([]*restore.RestoreMeta, 0, len(targets))
350+
for _, file := range targets {
328351
filename := strings.TrimSuffix(file.Name, ".json")
329352
meta, err := restore.GetPhysRestoreMeta(filename, stg, l)
330353
if err != nil {

sdk/impl.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,12 @@ func (c *Client) GetRestoreByOpID(ctx context.Context, opid string) (*RestoreMet
262262
return restore.GetRestoreMetaByOPID(ctx, c.conn, opid)
263263
}
264264

265-
func (c *Client) SyncFromStorage(ctx context.Context) (CommandID, error) {
266-
opid, err := ctrl.SendResync(ctx, c.conn)
265+
func (c *Client) SyncFromStorage(ctx context.Context, skipRestores bool) (CommandID, error) {
266+
var opts *ctrl.ResyncCmd
267+
if skipRestores {
268+
opts = &ctrl.ResyncCmd{SkipRestores: true}
269+
}
270+
opid, err := ctrl.SendResync(ctx, c.conn, opts)
267271
return CommandID(opid.String()), err
268272
}
269273

@@ -272,12 +276,12 @@ func (c *Client) SyncFromExternalStorage(ctx context.Context, name string) (Comm
272276
return NoOpID, errors.New("name is not provided")
273277
}
274278

275-
opid, err := ctrl.SendSyncMetaFrom(ctx, c.conn, name)
279+
opid, err := ctrl.SendResync(ctx, c.conn, &ctrl.ResyncCmd{Name: name})
276280
return CommandID(opid.String()), err
277281
}
278282

279283
func (c *Client) SyncFromAllExternalStorages(ctx context.Context) (CommandID, error) {
280-
opid, err := ctrl.SendSyncMetaFrom(ctx, c.conn, "")
284+
opid, err := ctrl.SendResync(ctx, c.conn, &ctrl.ResyncCmd{All: true})
281285
return CommandID(opid.String()), err
282286
}
283287

@@ -286,12 +290,12 @@ func (c *Client) ClearSyncFromExternalStorage(ctx context.Context, name string)
286290
return NoOpID, errors.New("name is not provided")
287291
}
288292

289-
opid, err := ctrl.SendClearMetaFrom(ctx, c.conn, name)
293+
opid, err := ctrl.SendResync(ctx, c.conn, &ctrl.ResyncCmd{Name: name, Clear: true})
290294
return CommandID(opid.String()), err
291295
}
292296

293297
func (c *Client) ClearSyncFromAllExternalStorages(ctx context.Context) (CommandID, error) {
294-
opid, err := ctrl.SendClearMetaFrom(ctx, c.conn, "")
298+
opid, err := ctrl.SendResync(ctx, c.conn, &ctrl.ResyncCmd{All: true, Clear: true})
295299
return CommandID(opid.String()), err
296300
}
297301

0 commit comments

Comments
 (0)