overlay snapshotter: Make use of WithTransaction

Move the overlay snapshotter over to using the WithTransaction convenience
method. This simplifies needing to check if we need to rollback a transaction
and saves us from needing to manually Commit ourselves.

Signed-off-by: Danny Canter <danny@dcantah.dev>
This commit is contained in:
Danny Canter 2022-12-14 02:49:56 -08:00
parent 3b71cfd407
commit aa8a389c51

View File

@ -127,15 +127,13 @@ func NewSnapshotter(root string, opts ...Opt) (snapshots.Snapshotter, error) {
// //
// Should be used for parent resolution, existence checks and to discern // Should be used for parent resolution, existence checks and to discern
// the kind of snapshot. // the kind of snapshot.
func (o *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, error) { func (o *snapshotter) Stat(ctx context.Context, key string) (info snapshots.Info, err error) {
ctx, t, err := o.ms.TransactionContext(ctx, false) var id string
if err != nil { if err := o.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
return snapshots.Info{}, err id, info, _, err = storage.GetInfo(ctx, key)
} return err
defer t.Rollback() }); err != nil {
id, info, _, err := storage.GetInfo(ctx, key) return info, err
if err != nil {
return snapshots.Info{}, err
} }
if o.upperdirLabel { if o.upperdirLabel {
@ -144,47 +142,29 @@ func (o *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, err
} }
info.Labels[upperdirKey] = o.upperPath(id) info.Labels[upperdirKey] = o.upperPath(id)
} }
return info, nil return info, nil
} }
func (o *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error) { func (o *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (newInfo snapshots.Info, err error) {
ctx, t, err := o.ms.TransactionContext(ctx, true) err = o.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
if err != nil { newInfo, err = storage.UpdateInfo(ctx, info, fieldpaths...)
return snapshots.Info{}, err
}
rollback := true
defer func() {
if rollback {
if rerr := t.Rollback(); rerr != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
}
}()
info, err = storage.UpdateInfo(ctx, info, fieldpaths...)
if err != nil {
return snapshots.Info{}, err
}
if o.upperdirLabel {
id, _, _, err := storage.GetInfo(ctx, info.Name)
if err != nil { if err != nil {
return snapshots.Info{}, err return err
} }
if info.Labels == nil {
info.Labels = make(map[string]string) if o.upperdirLabel {
id, _, _, err := storage.GetInfo(ctx, newInfo.Name)
if err != nil {
return err
}
if newInfo.Labels == nil {
newInfo.Labels = make(map[string]string)
}
newInfo.Labels[upperdirKey] = o.upperPath(id)
} }
info.Labels[upperdirKey] = o.upperPath(id) return nil
} })
return newInfo, err
rollback = false
if err := t.Commit(); err != nil {
return snapshots.Info{}, err
}
return info, nil
} }
// Usage returns the resources taken by the snapshot identified by key. // Usage returns the resources taken by the snapshot identified by key.
@ -193,16 +173,17 @@ func (o *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpath
// "upper") directory and may take some time. // "upper") directory and may take some time.
// //
// For committed snapshots, the value is returned from the metadata database. // For committed snapshots, the value is returned from the metadata database.
func (o *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, error) { func (o *snapshotter) Usage(ctx context.Context, key string) (_ snapshots.Usage, err error) {
ctx, t, err := o.ms.TransactionContext(ctx, false) var (
if err != nil { usage snapshots.Usage
return snapshots.Usage{}, err info snapshots.Info
} id string
id, info, usage, err := storage.GetInfo(ctx, key) )
t.Rollback() // transaction no longer needed at this point. if err := o.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
id, info, usage, err = storage.GetInfo(ctx, key)
if err != nil { return err
return snapshots.Usage{}, err }); err != nil {
return usage, err
} }
if info.Kind == snapshots.KindActive { if info.Kind == snapshots.KindActive {
@ -212,10 +193,8 @@ func (o *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, e
// TODO(stevvooe): Consider not reporting an error in this case. // TODO(stevvooe): Consider not reporting an error in this case.
return snapshots.Usage{}, err return snapshots.Usage{}, err
} }
usage = snapshots.Usage(du) usage = snapshots.Usage(du)
} }
return usage, nil return usage, nil
} }
@ -231,117 +210,91 @@ func (o *snapshotter) View(ctx context.Context, key, parent string, opts ...snap
// called on an read-write or readonly transaction. // called on an read-write or readonly transaction.
// //
// This can be used to recover mounts after calling View or Prepare. // This can be used to recover mounts after calling View or Prepare.
func (o *snapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) { func (o *snapshotter) Mounts(ctx context.Context, key string) (_ []mount.Mount, err error) {
ctx, t, err := o.ms.TransactionContext(ctx, false) var s storage.Snapshot
if err != nil { if err := o.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
s, err = storage.GetSnapshot(ctx, key)
if err != nil {
return fmt.Errorf("failed to get active mount: %w", err)
}
return nil
}); err != nil {
return nil, err return nil, err
} }
s, err := storage.GetSnapshot(ctx, key)
t.Rollback()
if err != nil {
return nil, fmt.Errorf("failed to get active mount: %w", err)
}
return o.mounts(s), nil return o.mounts(s), nil
} }
func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error { func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
ctx, t, err := o.ms.TransactionContext(ctx, true) return o.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
if err != nil { // grab the existing id
return err id, _, _, err := storage.GetInfo(ctx, key)
}
defer func() {
if err != nil { if err != nil {
if rerr := t.Rollback(); rerr != nil { return err
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
} }
}()
// grab the existing id usage, err := fs.DiskUsage(ctx, o.upperPath(id))
id, _, _, err := storage.GetInfo(ctx, key) if err != nil {
if err != nil { return err
return err }
}
usage, err := fs.DiskUsage(ctx, o.upperPath(id)) if _, err = storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil {
if err != nil { return fmt.Errorf("failed to commit snapshot %s: %w", key, err)
return err }
} return nil
})
if _, err = storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil {
return fmt.Errorf("failed to commit snapshot: %w", err)
}
return t.Commit()
} }
// Remove abandons the snapshot identified by key. The snapshot will // Remove abandons the snapshot identified by key. The snapshot will
// immediately become unavailable and unrecoverable. Disk space will // immediately become unavailable and unrecoverable. Disk space will
// be freed up on the next call to `Cleanup`. // be freed up on the next call to `Cleanup`.
func (o *snapshotter) Remove(ctx context.Context, key string) (err error) { func (o *snapshotter) Remove(ctx context.Context, key string) (err error) {
ctx, t, err := o.ms.TransactionContext(ctx, true) var removals []string
if err != nil { // Remove directories after the transaction is closed, failures must not
return err // return error since the transaction is committed with the removal
} // key no longer available.
defer func() { defer func() {
if err != nil { if err == nil {
if rerr := t.Rollback(); rerr != nil { for _, dir := range removals {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction") if err := os.RemoveAll(dir); err != nil {
log.G(ctx).WithError(err).WithField("path", dir).Warn("failed to remove directory")
}
} }
} }
}() }()
return o.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
_, _, err = storage.Remove(ctx, key) _, _, err = storage.Remove(ctx, key)
if err != nil {
return fmt.Errorf("failed to remove: %w", err)
}
if !o.asyncRemove {
var removals []string
removals, err = o.getCleanupDirectories(ctx)
if err != nil { if err != nil {
return fmt.Errorf("unable to get directories for removal: %w", err) return fmt.Errorf("failed to remove snapshot %s: %w", key, err)
} }
// Remove directories after the transaction is closed, failures must not if !o.asyncRemove {
// return error since the transaction is committed with the removal removals, err = o.getCleanupDirectories(ctx)
// key no longer available. if err != nil {
defer func() { return fmt.Errorf("unable to get directories for removal: %w", err)
if err == nil {
for _, dir := range removals {
if err := os.RemoveAll(dir); err != nil {
log.G(ctx).WithError(err).WithField("path", dir).Warn("failed to remove directory")
}
}
} }
}() }
return nil
} })
return t.Commit()
} }
// Walk the snapshots. // Walk the snapshots.
func (o *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...string) error { func (o *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...string) error {
ctx, t, err := o.ms.TransactionContext(ctx, false) return o.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
if err != nil { if o.upperdirLabel {
return err return storage.WalkInfo(ctx, func(ctx context.Context, info snapshots.Info) error {
} id, _, _, err := storage.GetInfo(ctx, info.Name)
defer t.Rollback() if err != nil {
if o.upperdirLabel { return err
return storage.WalkInfo(ctx, func(ctx context.Context, info snapshots.Info) error { }
id, _, _, err := storage.GetInfo(ctx, info.Name) if info.Labels == nil {
if err != nil { info.Labels = make(map[string]string)
return err }
} info.Labels[upperdirKey] = o.upperPath(id)
if info.Labels == nil { return fn(ctx, info)
info.Labels = make(map[string]string) }, fs...)
} }
info.Labels[upperdirKey] = o.upperPath(id) return storage.WalkInfo(ctx, fn, fs...)
return fn(ctx, info) })
}, fs...)
}
return storage.WalkInfo(ctx, fn, fs...)
} }
// Cleanup cleans up disk resources from removed or abandoned snapshots // Cleanup cleans up disk resources from removed or abandoned snapshots
@ -360,16 +313,17 @@ func (o *snapshotter) Cleanup(ctx context.Context) error {
return nil return nil
} }
func (o *snapshotter) cleanupDirectories(ctx context.Context) ([]string, error) { func (o *snapshotter) cleanupDirectories(ctx context.Context) (_ []string, err error) {
var cleanupDirs []string
// Get a write transaction to ensure no other write transaction can be entered // Get a write transaction to ensure no other write transaction can be entered
// while the cleanup is scanning. // while the cleanup is scanning.
ctx, t, err := o.ms.TransactionContext(ctx, true) if err := o.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
if err != nil { cleanupDirs, err = o.getCleanupDirectories(ctx)
return err
}); err != nil {
return nil, err return nil, err
} }
return cleanupDirs, nil
defer t.Rollback()
return o.getCleanupDirectories(ctx)
} }
func (o *snapshotter) getCleanupDirectories(ctx context.Context) ([]string, error) { func (o *snapshotter) getCleanupDirectories(ctx context.Context) ([]string, error) {
@ -402,12 +356,11 @@ func (o *snapshotter) getCleanupDirectories(ctx context.Context) ([]string, erro
} }
func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) (_ []mount.Mount, err error) { func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) (_ []mount.Mount, err error) {
ctx, t, err := o.ms.TransactionContext(ctx, true) var (
if err != nil { s storage.Snapshot
return nil, err td, path string
} )
var td, path string
defer func() { defer func() {
if err != nil { if err != nil {
if td != "" { if td != "" {
@ -424,50 +377,39 @@ func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
} }
}() }()
snapshotDir := filepath.Join(o.root, "snapshots") if err := o.ms.WithTransaction(ctx, true, func(ctx context.Context) (err error) {
td, err = o.prepareDirectory(ctx, snapshotDir, kind) snapshotDir := filepath.Join(o.root, "snapshots")
if err != nil { td, err = o.prepareDirectory(ctx, snapshotDir, kind)
if rerr := t.Rollback(); rerr != nil { if err != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction") return fmt.Errorf("failed to create prepare snapshot dir: %w", err)
} }
return nil, fmt.Errorf("failed to create prepare snapshot dir: %w", err)
} s, err = storage.CreateSnapshot(ctx, kind, key, parent, opts...)
rollback := true if err != nil {
defer func() { return fmt.Errorf("failed to create snapshot: %w", err)
if rollback { }
if rerr := t.Rollback(); rerr != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction") if len(s.ParentIDs) > 0 {
st, err := os.Stat(o.upperPath(s.ParentIDs[0]))
if err != nil {
return fmt.Errorf("failed to stat parent: %w", err)
}
stat := st.Sys().(*syscall.Stat_t)
if err := os.Lchown(filepath.Join(td, "fs"), int(stat.Uid), int(stat.Gid)); err != nil {
return fmt.Errorf("failed to chown: %w", err)
} }
} }
}()
s, err := storage.CreateSnapshot(ctx, kind, key, parent, opts...) path = filepath.Join(snapshotDir, s.ID)
if err != nil { if err = os.Rename(td, path); err != nil {
return nil, fmt.Errorf("failed to create snapshot: %w", err) return fmt.Errorf("failed to rename: %w", err)
}
if len(s.ParentIDs) > 0 {
st, err := os.Stat(o.upperPath(s.ParentIDs[0]))
if err != nil {
return nil, fmt.Errorf("failed to stat parent: %w", err)
} }
td = ""
stat := st.Sys().(*syscall.Stat_t) return nil
}); err != nil {
if err := os.Lchown(filepath.Join(td, "fs"), int(stat.Uid), int(stat.Gid)); err != nil { return nil, err
return nil, fmt.Errorf("failed to chown: %w", err)
}
}
path = filepath.Join(snapshotDir, s.ID)
if err = os.Rename(td, path); err != nil {
return nil, fmt.Errorf("failed to rename: %w", err)
}
td = ""
rollback = false
if err = t.Commit(); err != nil {
return nil, fmt.Errorf("commit failed: %w", err)
} }
return o.mounts(s), nil return o.mounts(s), nil