Skip to content

Commit ec8d7c1

Browse files
committed
PBM-1649 Profile support for delete and cleanup in PBM agent
1 parent f585684 commit ec8d7c1

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

cmd/pbm-agent/delete.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (a *Agent) Delete(ctx context.Context, d *ctrl.DeleteBackupCmd, opid ctrl.O
9696
}
9797

9898
l.Info("deleting backups older than %v", t)
99-
err = backup.DeleteBackupBefore(ctx, a.leadConn, t, bcpType, nodeInfo.Me)
99+
err = backup.DeleteBackupBefore(ctx, a.leadConn, t, bcpType, d.Profile, nodeInfo.Me)
100100
if err != nil {
101101
l.Error("deleting: %v", err)
102102
return
@@ -254,7 +254,7 @@ func (a *Agent) Cleanup(ctx context.Context, d *ctrl.CleanupCmd, opid ctrl.OPID,
254254
return
255255
}
256256

257-
cfg, err := config.GetConfig(ctx, a.leadConn)
257+
cfg, err := config.GetProfiledConfig(ctx, a.leadConn, d.Profile)
258258
if err != nil {
259259
l.Error("get config: %v", err)
260260
}
@@ -267,7 +267,7 @@ func (a *Agent) Cleanup(ctx context.Context, d *ctrl.CleanupCmd, opid ctrl.OPID,
267267
eg := errgroup.Group{}
268268
eg.SetLimit(runtime.NumCPU())
269269

270-
cr, err := backup.MakeCleanupInfo(ctx, a.leadConn, d.OlderThan)
270+
cr, err := backup.MakeCleanupInfo(ctx, a.leadConn, d.OlderThan, d.Profile)
271271
if err != nil {
272272
l.Error("make cleanup report: " + err.Error())
273273
return
@@ -289,15 +289,20 @@ func (a *Agent) Cleanup(ctx context.Context, d *ctrl.CleanupCmd, opid ctrl.OPID,
289289
bcp := &cr.Backups[i]
290290

291291
eg.Go(func() error {
292-
err := backup.DeleteBackupFiles(stg, bcp.Name)
293-
return errors.Wrapf(err, "delete backup files %q", bcp.Name)
292+
err := backup.DeleteBackup(ctx, a.leadConn, bcp.Name, a.brief.Me)
293+
return errors.Wrapf(err, "delete backup %q", bcp.Name)
294294
})
295295
}
296296
if err := eg.Wait(); err != nil {
297297
l.Error(err.Error())
298298
}
299299

300-
err = resync.Resync(ctx, a.leadConn, &cfg.Storage, a.brief.Me, false)
300+
if d.Profile == "" {
301+
err = resync.Resync(ctx, a.leadConn, &cfg.Storage, a.brief.Me, false)
302+
} else {
303+
err = resync.SyncBackupList(ctx, a.leadConn, &cfg.Storage, d.Profile, a.brief.Me)
304+
}
305+
301306
if err != nil {
302307
l.Error("storage resync: " + err.Error())
303308
}
@@ -306,7 +311,7 @@ func (a *Agent) Cleanup(ctx context.Context, d *ctrl.CleanupCmd, opid ctrl.OPID,
306311
func (a *Agent) deletePITRImpl(ctx context.Context, ts primitive.Timestamp) error {
307312
l := log.LogEventFromContext(ctx)
308313

309-
r, err := backup.MakeCleanupInfo(ctx, a.leadConn, ts)
314+
r, err := backup.MakeCleanupInfo(ctx, a.leadConn, ts, "")
310315
if err != nil {
311316
return errors.Wrap(err, "get pitr chunks")
312317
}

pbm/backup/delete.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/percona/percona-backup-mongodb/pbm/errors"
1616
"github.com/percona/percona-backup-mongodb/pbm/log"
1717
"github.com/percona/percona-backup-mongodb/pbm/oplog"
18+
"github.com/percona/percona-backup-mongodb/pbm/storage"
1819
"github.com/percona/percona-backup-mongodb/pbm/util"
1920
)
2021

@@ -321,17 +322,23 @@ func DeleteBackupBefore(
321322
conn connect.Client,
322323
t time.Time,
323324
bcpType defs.BackupType,
325+
profile string,
324326
node string,
325327
) error {
326-
backups, err := ListDeleteBackupBefore(ctx, conn, primitive.Timestamp{T: uint32(t.Unix())}, bcpType)
328+
backups, err := ListDeleteBackupBefore(ctx, conn, primitive.Timestamp{T: uint32(t.Unix())}, bcpType, profile)
327329
if err != nil {
328330
return err
329331
}
330332
if len(backups) == 0 {
331333
return nil
332334
}
333335

334-
stg, err := util.GetStorage(ctx, conn, node, log.LogEventFromContext(ctx))
336+
var stg storage.Storage
337+
if profile == "" {
338+
stg, err = util.GetStorage(ctx, conn, node, log.LogEventFromContext(ctx))
339+
} else {
340+
stg, err = util.GetProfiledStorage(ctx, conn, profile, node, log.LogEventFromContext(ctx))
341+
}
335342
if err != nil {
336343
return errors.Wrap(err, "get storage")
337344
}
@@ -360,7 +367,7 @@ func ListDeleteBackupBefore(
360367
bcpType defs.BackupType,
361368
profile string,
362369
) ([]BackupMeta, error) {
363-
info, err := MakeCleanupInfo(ctx, conn, ts)
370+
info, err := MakeCleanupInfo(ctx, conn, ts, profile)
364371
if err != nil {
365372
return nil, err
366373
}
@@ -391,8 +398,13 @@ func ListDeleteBackupBefore(
391398
return rv, nil
392399
}
393400

394-
func MakeCleanupInfo(ctx context.Context, conn connect.Client, ts primitive.Timestamp) (CleanupInfo, error) {
395-
backups, err := listBackupsBefore(ctx, conn, primitive.Timestamp{T: ts.T + 1})
401+
func MakeCleanupInfo(
402+
ctx context.Context,
403+
conn connect.Client,
404+
ts primitive.Timestamp,
405+
profile string,
406+
) (CleanupInfo, error) {
407+
backups, err := listBackupsBefore(ctx, conn, primitive.Timestamp{T: ts.T + 1}, profile)
396408
if err != nil {
397409
return CleanupInfo{}, errors.Wrap(err, "list backups before")
398410
}
@@ -500,9 +512,13 @@ func MakeCleanupInfo(ctx context.Context, conn connect.Client, ts primitive.Time
500512
// listBackupsBefore returns backups with restore cluster time less than or equals to ts.
501513
//
502514
// It does not include backups stored on an external storages.
503-
func listBackupsBefore(ctx context.Context, conn connect.Client, ts primitive.Timestamp) ([]BackupMeta, error) {
515+
func listBackupsBefore(
516+
ctx context.Context,
517+
conn connect.Client,
518+
ts primitive.Timestamp,
519+
profile string,
520+
) ([]BackupMeta, error) {
504521
f := bson.D{
505-
{"store.profile", nil},
506522
{"last_write_ts", bson.M{"$lt": ts}},
507523
{"status", bson.M{"$in": bson.A{
508524
defs.StatusDone,

sdk/impl.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,8 @@ func (c *Client) DeleteOplogRange(ctx context.Context, until Timestamp) (Command
332332
return CommandID(opid.String()), err
333333
}
334334

335-
func (c *Client) CleanupReport(ctx context.Context, beforeTS Timestamp) (CleanupReport, error) {
336-
return backup.MakeCleanupInfo(ctx, c.conn, beforeTS)
337335
func (c *Client) CleanupReport(ctx context.Context, beforeTS Timestamp, profile string) (CleanupReport, error) {
336+
return backup.MakeCleanupInfo(ctx, c.conn, beforeTS, profile)
338337
}
339338

340339
func (c *Client) RunCleanup(ctx context.Context, beforeTS Timestamp, profile string) (CommandID, error) {

sdk/sdk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func ListDeleteChunksBefore(
225225
client *Client,
226226
ts primitive.Timestamp,
227227
) ([]OplogChunk, error) {
228-
r, err := backup.MakeCleanupInfo(ctx, client.conn, ts)
228+
r, err := backup.MakeCleanupInfo(ctx, client.conn, ts, "")
229229
return r.Chunks, err
230230
}
231231

0 commit comments

Comments
 (0)