Skip to content

Commit 1820660

Browse files
authored
PBM-1401 Backup fails with context deadline exceeded error (#1150)
* hide credentials from gcs output * add wait to file flag * add wait implementation to set flag
1 parent 414758c commit 1820660

File tree

4 files changed

+40
-42
lines changed

4 files changed

+40
-42
lines changed

cmd/pbm/common.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ func checkForAnotherOperation(ctx context.Context, pbm *sdk.Client) error {
4444
return nil
4545
}
4646

47+
func waitForResyncWithTimeout(ctx context.Context, pbm *sdk.Client, cid sdk.CommandID, timeout time.Duration) error {
48+
if timeout > time.Second {
49+
var cancel context.CancelFunc
50+
ctx, cancel = context.WithTimeout(ctx, timeout)
51+
defer cancel()
52+
}
53+
54+
if err := sdk.WaitForResync(ctx, pbm, cid); err != nil {
55+
if errors.Is(err, context.DeadlineExceeded) {
56+
err = errWaitTimeout
57+
}
58+
59+
return errors.Wrapf(err, "waiting for resync [opid %q]", cid)
60+
}
61+
return nil
62+
}
63+
4764
type concurrentOpError struct{ sdk.OpLock }
4865

4966
func (e *concurrentOpError) Error() string {

cmd/pbm/config.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,16 @@ func runConfig(
7777
}
7878
}
7979
if rsnc {
80-
if _, err := pbm.SyncFromStorage(ctx, false); err != nil {
80+
cid, err := pbm.SyncFromStorage(ctx, false)
81+
if err != nil {
8182
return nil, errors.Wrap(err, "resync")
8283
}
84+
85+
if c.wait {
86+
if err := waitForResyncWithTimeout(ctx, pbm, cid, c.waitTime); err != nil {
87+
return nil, err
88+
}
89+
}
8390
}
8491
return o, nil
8592
case len(c.key) > 0:
@@ -101,19 +108,8 @@ func runConfig(
101108
return outMsg{"Storage resync started"}, nil
102109
}
103110

104-
if c.waitTime > time.Second {
105-
var cancel context.CancelFunc
106-
ctx, cancel = context.WithTimeout(ctx, c.waitTime)
107-
defer cancel()
108-
}
109-
110-
err = sdk.WaitForResync(ctx, pbm, cid)
111-
if err != nil {
112-
if errors.Is(err, context.DeadlineExceeded) {
113-
err = errWaitTimeout
114-
}
115-
116-
return nil, errors.Wrapf(err, "waiting for resync [opid %q]", cid)
111+
if err := waitForResyncWithTimeout(ctx, pbm, cid, c.waitTime); err != nil {
112+
return nil, err
117113
}
118114

119115
return outMsg{"Storage resync finished"}, nil
@@ -144,9 +140,16 @@ func runConfig(
144140

145141
// resync storage only if Storage options have changed
146142
if !reflect.DeepEqual(newCfg.Storage, oldCfg.Storage) {
147-
if _, err := pbm.SyncFromStorage(ctx, false); err != nil {
143+
cid, err := pbm.SyncFromStorage(ctx, false)
144+
if err != nil {
148145
return nil, errors.Wrap(err, "resync")
149146
}
147+
148+
if c.wait {
149+
if err := waitForResyncWithTimeout(ctx, pbm, cid, c.waitTime); err != nil {
150+
return nil, err
151+
}
152+
}
150153
}
151154

152155
return newCfg, nil

cmd/pbm/profile.go

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,8 @@ func handleAddConfigProfile(
149149
}
150150

151151
if opts.wait {
152-
if opts.waitTime > time.Second {
153-
var cancel context.CancelFunc
154-
ctx, cancel = context.WithTimeout(ctx, opts.waitTime)
155-
defer cancel()
156-
}
157-
158-
err = sdk.WaitForResync(ctx, pbm, cid)
159-
if err != nil {
160-
if errors.Is(err, context.DeadlineExceeded) {
161-
err = errWaitTimeout
162-
}
163-
164-
return nil, errors.Wrap(err, "wait")
152+
if err := waitForResyncWithTimeout(ctx, pbm, cid, opts.waitTime); err != nil {
153+
return nil, err
165154
}
166155
}
167156
}
@@ -251,19 +240,8 @@ func handleSyncConfigProfile(
251240
}
252241

253242
if opts.wait {
254-
if opts.waitTime > time.Second {
255-
var cancel context.CancelFunc
256-
ctx, cancel = context.WithTimeout(ctx, opts.waitTime)
257-
defer cancel()
258-
}
259-
260-
err = sdk.WaitForResync(ctx, pbm, cid)
261-
if err != nil {
262-
if errors.Is(err, context.DeadlineExceeded) {
263-
err = errWaitTimeout
264-
}
265-
266-
return nil, errors.Wrap(err, "wait")
243+
if err := waitForResyncWithTimeout(ctx, pbm, cid, opts.waitTime); err != nil {
244+
return nil, err
267245
}
268246
}
269247

pbm/storage/gcs/gcs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
type Config struct {
1616
Bucket string `bson:"bucket" json:"bucket" yaml:"bucket"`
1717
Prefix string `bson:"prefix" json:"prefix" yaml:"prefix"`
18-
Credentials Credentials `bson:"credentials" json:"credentials" yaml:"credentials"`
18+
Credentials Credentials `bson:"credentials" json:"-" yaml:"credentials"`
1919

2020
// The maximum number of bytes that the Writer will attempt to send in a single request.
2121
// https://pkg.go.dev/cloud.google.com/go/storage#Writer

0 commit comments

Comments
 (0)