X Tutup
Skip to content

Commit bb02302

Browse files
committed
improve rollback for overlay.prepare
improve err message Signed-off-by: yason <yan.xuean@zte.com.cn>
1 parent 16a2177 commit bb02302

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

snapshots/overlay/overlay.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -287,41 +287,37 @@ func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
287287
if err != nil {
288288
return nil, err
289289
}
290+
isRollback := true
291+
defer func() {
292+
if isRollback {
293+
if rerr := t.Rollback(); rerr != nil {
294+
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
295+
}
296+
}
297+
}()
290298

291299
s, err := storage.CreateSnapshot(ctx, kind, key, parent, opts...)
292300
if err != nil {
293-
if rerr := t.Rollback(); rerr != nil {
294-
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
295-
}
296-
return nil, errors.Wrap(err, "failed to create active")
301+
return nil, errors.Wrap(err, "failed to create snapshot")
297302
}
298303

299304
if len(s.ParentIDs) > 0 {
300-
st, err := os.Stat(filepath.Join(o.upperPath(s.ParentIDs[0])))
305+
st, err := os.Stat(o.upperPath(s.ParentIDs[0]))
301306
if err != nil {
302-
if rerr := t.Rollback(); rerr != nil {
303-
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
304-
}
305307
return nil, errors.Wrap(err, "failed to stat parent")
306308
}
307309

308310
stat := st.Sys().(*syscall.Stat_t)
309-
310311
if err := os.Lchown(fs, int(stat.Uid), int(stat.Gid)); err != nil {
311-
if rerr := t.Rollback(); rerr != nil {
312-
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
313-
}
314312
return nil, errors.Wrap(err, "failed to chown")
315313
}
316314
}
317315

318316
path = filepath.Join(snapshotDir, s.ID)
319317
if err = os.Rename(td, path); err != nil {
320-
if rerr := t.Rollback(); rerr != nil {
321-
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
322-
}
323318
return nil, errors.Wrap(err, "failed to rename")
324319
}
320+
isRollback = false
325321
td = ""
326322

327323
if err = t.Commit(); err != nil {

snapshots/storage/bolt.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ func CreateSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string
207207
if parent != "" {
208208
spbkt = bkt.Bucket([]byte(parent))
209209
if spbkt == nil {
210-
return errors.Wrap(errdefs.ErrNotFound, "missing parent bucket")
210+
return errors.Wrapf(errdefs.ErrNotFound, "missing parent %q bucket", parent)
211211
}
212212

213213
if readKind(spbkt) != snapshots.KindCommitted {
214-
return errors.Wrap(errdefs.ErrInvalidArgument, "parent is not committed snapshot")
214+
return errors.Wrapf(errdefs.ErrInvalidArgument, "parent %q is not committed snapshot", parent)
215215
}
216216
}
217217
sbkt, err := bkt.CreateBucket([]byte(key))
@@ -224,7 +224,7 @@ func CreateSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string
224224

225225
id, err := bkt.NextSequence()
226226
if err != nil {
227-
return errors.Wrap(err, "unable to get identifier")
227+
return errors.Wrapf(err, "unable to get identifier for snapshot %q", key)
228228
}
229229

230230
t := time.Now().UTC()
@@ -245,12 +245,12 @@ func CreateSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string
245245
// Store a backlink from the key to the parent. Store the snapshot name
246246
// as the value to allow following the backlink to the snapshot value.
247247
if err := pbkt.Put(parentKey(pid, id), []byte(key)); err != nil {
248-
return errors.Wrap(err, "failed to write parent link")
248+
return errors.Wrapf(err, "failed to write parent link for snapshot %q", key)
249249
}
250250

251251
s.ParentIDs, err = parents(bkt, spbkt, pid)
252252
if err != nil {
253-
return errors.Wrap(err, "failed to get parent chain")
253+
return errors.Wrapf(err, "failed to get parent chain for snapshot %q", key)
254254
}
255255
}
256256

@@ -438,7 +438,7 @@ func createBucketIfNotExists(ctx context.Context, fn func(context.Context, *bolt
438438
}
439439
pbkt, err := bkt.CreateBucketIfNotExists(bucketKeyParents)
440440
if err != nil {
441-
return errors.Wrap(err, "failed to create snapshots bucket")
441+
return errors.Wrap(err, "failed to create parents bucket")
442442
}
443443
return fn(ctx, sbkt, pbkt)
444444
}

0 commit comments

Comments
 (0)
X Tutup