snapshots: refactor metastore transaction

Signed-off-by: Junyu Liu <ljyngup@gmail.com>
This commit is contained in:
iyear 2023-01-04 19:05:52 +08:00
parent a0b8401ea9
commit 9df5a1714d
3 changed files with 330 additions and 383 deletions

View File

@ -105,44 +105,40 @@ func NewSnapshotter(root string) (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 (s *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, error) { func (s *snapshotter) Stat(ctx context.Context, key string) (info snapshots.Info, err error) {
ctx, t, err := s.ms.TransactionContext(ctx, false) err = s.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
_, info, _, err = storage.GetInfo(ctx, key)
return err
})
if err != nil { if err != nil {
return snapshots.Info{}, err return snapshots.Info{}, err
} }
defer t.Rollback()
_, info, _, err := storage.GetInfo(ctx, key)
return info, err
}
func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error) {
ctx, t, err := s.ms.TransactionContext(ctx, true)
if err != nil {
return snapshots.Info{}, err
}
defer t.Rollback()
info, err = storage.UpdateInfo(ctx, info, fieldpaths...)
if err != nil {
return snapshots.Info{}, err
}
if err := t.Commit(); err != nil {
return snapshots.Info{}, err
}
return info, nil return info, nil
} }
func (s *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, error) { func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (_ snapshots.Info, err error) {
ctx, t, err := s.ms.TransactionContext(ctx, false) err = s.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
info, err = storage.UpdateInfo(ctx, info, fieldpaths...)
return err
})
if err != nil { if err != nil {
return snapshots.Usage{}, err return snapshots.Info{}, err
} }
id, info, usage, err := storage.GetInfo(ctx, key)
t.Rollback() // transaction no longer needed at this point.
return info, nil
}
func (s *snapshotter) Usage(ctx context.Context, key string) (usage snapshots.Usage, err error) {
var (
id string
info snapshots.Info
)
err = s.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
id, info, usage, err = storage.GetInfo(ctx, key)
return err
})
if err != nil { if err != nil {
return snapshots.Usage{}, err return snapshots.Usage{}, err
} }
@ -170,33 +166,25 @@ func (s *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 (s *snapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) { func (s *snapshotter) Mounts(ctx context.Context, key string) (_ []mount.Mount, err error) {
ctx, t, err := s.ms.TransactionContext(ctx, false) var snapshot storage.Snapshot
err = s.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
snapshot, err = storage.GetSnapshot(ctx, key)
if err != nil {
return fmt.Errorf("failed to get snapshot mount: %w", err)
}
return nil
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer t.Rollback()
snapshot, err := storage.GetSnapshot(ctx, key)
if err != nil {
return nil, fmt.Errorf("failed to get snapshot mount: %w", err)
}
return s.mounts(snapshot), nil return s.mounts(snapshot), nil
} }
func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error { func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
ctx, t, err := s.ms.TransactionContext(ctx, true) return s.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
if err != nil {
return err
}
defer func() {
if err != nil {
if rerr := t.Rollback(); rerr != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
}
}()
// grab the existing id // grab the existing id
id, _, _, err := storage.GetInfo(ctx, key) id, _, _, err := storage.GetInfo(ctx, key)
if err != nil { if err != nil {
@ -211,39 +199,44 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
if _, err = storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil { if _, err = storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil {
return fmt.Errorf("failed to commit snapshot: %w", err) return fmt.Errorf("failed to commit snapshot: %w", err)
} }
return nil
return t.Commit() })
} }
// Remove abandons the transaction identified by key. All resources // Remove abandons the transaction identified by key. All resources
// associated with the key will be removed. // associated with the key will be removed.
func (s *snapshotter) Remove(ctx context.Context, key string) error { func (s *snapshotter) Remove(ctx context.Context, key string) (err error) {
ctx, t, err := s.ms.TransactionContext(ctx, true) var (
if err != nil { renamed, path string
return err restore bool
} )
defer t.Rollback()
err = s.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
id, _, err := storage.Remove(ctx, key) id, _, err := storage.Remove(ctx, key)
if err != nil { if err != nil {
return fmt.Errorf("failed to remove: %w", err) return fmt.Errorf("failed to remove: %w", err)
} }
path := s.getSnapshotDir(id) path = s.getSnapshotDir(id)
renamed := s.getSnapshotDir("rm-" + id) renamed = s.getSnapshotDir("rm-" + id)
if err := os.Rename(path, renamed); err != nil && !os.IsNotExist(err) { if err = os.Rename(path, renamed); err != nil && !os.IsNotExist(err) {
return err return err
} }
if err := t.Commit(); err != nil { restore = true
return nil
})
if err != nil {
if restore { // failed to commit
if err1 := os.Rename(renamed, path); err1 != nil { if err1 := os.Rename(renamed, path); err1 != nil {
// May cause inconsistent data on disk // May cause inconsistent data on disk
log.G(ctx).WithError(err1).WithField("path", renamed).Error("Failed to rename after failed commit") log.G(ctx).WithError(err1).WithField("path", renamed).Error("Failed to rename after failed commit")
} }
return fmt.Errorf("failed to commit: %w", err) }
return err
} }
if err := os.RemoveAll(renamed); err != nil { if err = os.RemoveAll(renamed); err != nil {
// Must be cleaned up, any "rm-*" could be removed if no active transactions // Must be cleaned up, any "rm-*" could be removed if no active transactions
log.G(ctx).WithError(err).WithField("path", renamed).Warnf("Failed to remove root filesystem") log.G(ctx).WithError(err).WithField("path", renamed).Warnf("Failed to remove root filesystem")
} }
@ -253,13 +246,9 @@ func (s *snapshotter) Remove(ctx context.Context, key string) error {
// Walk the committed snapshots. // Walk the committed snapshots.
func (s *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...string) error { func (s *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...string) error {
ctx, t, err := s.ms.TransactionContext(ctx, false) return s.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
if err != nil {
return err
}
defer t.Rollback()
return storage.WalkInfo(ctx, fn, fs...) return storage.WalkInfo(ctx, fn, fs...)
})
} }
// Close closes the snapshotter // Close closes the snapshotter
@ -309,24 +298,23 @@ func (s *snapshotter) getSnapshotDir(id string) string {
return filepath.Join(s.root, "snapshots", id) return filepath.Join(s.root, "snapshots", id)
} }
func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) (_ []mount.Mount, err error) { func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) ([]mount.Mount, error) {
ctx, t, err := s.ms.TransactionContext(ctx, true) var newSnapshot storage.Snapshot
err := s.ms.WithTransaction(ctx, true, func(ctx context.Context) (err error) {
newSnapshot, err = storage.CreateSnapshot(ctx, kind, key, parent, opts...)
if err != nil { if err != nil {
return nil, err return fmt.Errorf("failed to create snapshot: %w", err)
} }
defer t.Rollback()
if kind != snapshots.KindActive {
newSnapshot, err := storage.CreateSnapshot(ctx, kind, key, parent, opts...) return nil
if err != nil {
return nil, fmt.Errorf("failed to create snapshot: %w", err)
} }
if kind == snapshots.KindActive {
log.G(ctx).Debug("createSnapshot active") log.G(ctx).Debug("createSnapshot active")
// Create the new snapshot dir // Create the new snapshot dir
snDir := s.getSnapshotDir(newSnapshot.ID) snDir := s.getSnapshotDir(newSnapshot.ID)
if err := os.MkdirAll(snDir, 0700); err != nil { if err = os.MkdirAll(snDir, 0700); err != nil {
return nil, err return err
} }
var snapshotInfo snapshots.Info var snapshotInfo snapshots.Info
@ -364,7 +352,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
ownerKey := snapshotInfo.Labels[reuseScratchOwnerKeyLabel] ownerKey := snapshotInfo.Labels[reuseScratchOwnerKeyLabel]
if shareScratch == "true" && ownerKey != "" { if shareScratch == "true" && ownerKey != "" {
if err = s.handleSharing(ctx, ownerKey, snDir); err != nil { if err = s.handleSharing(ctx, ownerKey, snDir); err != nil {
return nil, err return err
} }
} else { } else {
var sizeGB int var sizeGB int
@ -376,7 +364,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
scratchLocation := snapshotInfo.Labels[rootfsLocLabel] scratchLocation := snapshotInfo.Labels[rootfsLocLabel]
scratchSource, err := s.openOrCreateScratch(ctx, sizeGB, scratchLocation) scratchSource, err := s.openOrCreateScratch(ctx, sizeGB, scratchLocation)
if err != nil { if err != nil {
return nil, err return err
} }
defer scratchSource.Close() defer scratchSource.Close()
@ -384,20 +372,21 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
destPath := filepath.Join(snDir, "sandbox.vhdx") destPath := filepath.Join(snDir, "sandbox.vhdx")
dest, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE, 0700) dest, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE, 0700)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create sandbox.vhdx in snapshot: %w", err) return fmt.Errorf("failed to create sandbox.vhdx in snapshot: %w", err)
} }
defer dest.Close() defer dest.Close()
if _, err := io.Copy(dest, scratchSource); err != nil { if _, err := io.Copy(dest, scratchSource); err != nil {
dest.Close() dest.Close()
os.Remove(destPath) os.Remove(destPath)
return nil, fmt.Errorf("failed to copy cached scratch.vhdx to sandbox.vhdx in snapshot: %w", err) return fmt.Errorf("failed to copy cached scratch.vhdx to sandbox.vhdx in snapshot: %w", err)
}
} }
} }
} }
if err := t.Commit(); err != nil { return nil
return nil, fmt.Errorf("commit failed: %w", err) })
if err != nil {
return nil, err
} }
return s.mounts(newSnapshot), nil return s.mounts(newSnapshot), nil

View File

@ -61,13 +61,11 @@ func NewSnapshotter(root string) (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) err = o.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
if err != nil { _, info, _, err = storage.GetInfo(ctx, key)
return snapshots.Info{}, err return err
} })
defer t.Rollback()
_, info, _, err := storage.GetInfo(ctx, key)
if err != nil { if err != nil {
return snapshots.Info{}, err return snapshots.Info{}, err
} }
@ -75,33 +73,28 @@ func (o *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, err
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) (_ 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 {
return snapshots.Info{}, err
}
info, err = storage.UpdateInfo(ctx, info, fieldpaths...) info, err = storage.UpdateInfo(ctx, info, fieldpaths...)
return err
})
if err != nil { if err != nil {
t.Rollback()
return snapshots.Info{}, err
}
if err := t.Commit(); err != nil {
return snapshots.Info{}, err return snapshots.Info{}, err
} }
return info, nil return info, nil
} }
func (o *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, error) { func (o *snapshotter) Usage(ctx context.Context, key string) (usage snapshots.Usage, err error) {
ctx, t, err := o.ms.TransactionContext(ctx, false) var (
if err != nil { id string
return snapshots.Usage{}, err info snapshots.Info
} )
defer t.Rollback()
id, info, usage, err := storage.GetInfo(ctx, key) err = o.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
id, info, usage, err = storage.GetInfo(ctx, key)
return err
})
if err != nil { if err != nil {
return snapshots.Usage{}, err return snapshots.Usage{}, err
} }
@ -129,89 +122,77 @@ 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
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 snapshot mount: %w", err)
}
return nil
})
if err != nil { if 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 snapshot 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 {
return err
}
id, _, _, err := storage.GetInfo(ctx, key) id, _, _, err := storage.GetInfo(ctx, key)
if err != nil { if err != nil {
if rerr := t.Rollback(); rerr != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
return err return err
} }
usage, err := fs.DiskUsage(ctx, o.getSnapshotDir(id)) usage, err := fs.DiskUsage(ctx, o.getSnapshotDir(id))
if err != nil { if err != nil {
if rerr := t.Rollback(); rerr != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
return err return err
} }
if _, err := storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil { if _, err = storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil {
if rerr := t.Rollback(); rerr != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
return fmt.Errorf("failed to commit snapshot: %w", err) return fmt.Errorf("failed to commit snapshot: %w", err)
} }
return t.Commit() return nil
})
} }
// Remove abandons the transaction identified by key. All resources // Remove abandons the transaction identified by key. All resources
// associated with the key will be removed. // associated with the key will be removed.
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 (
if err != nil { renamed, path string
return err restore bool
} )
defer func() {
if err != nil && t != nil {
if rerr := t.Rollback(); rerr != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
}
}()
err = o.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
id, _, err := storage.Remove(ctx, key) id, _, err := storage.Remove(ctx, key)
if err != nil { if err != nil {
return fmt.Errorf("failed to remove: %w", err) return fmt.Errorf("failed to remove: %w", err)
} }
path := o.getSnapshotDir(id) path = o.getSnapshotDir(id)
renamed := filepath.Join(o.root, "snapshots", "rm-"+id) renamed = filepath.Join(o.root, "snapshots", "rm-"+id)
if err := os.Rename(path, renamed); err != nil { if err = os.Rename(path, renamed); err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return fmt.Errorf("failed to rename: %w", err) return fmt.Errorf("failed to rename: %w", err)
} }
renamed = "" renamed = ""
} }
err = t.Commit() restore = true
t = nil return nil
})
if err != nil { if err != nil {
if renamed != "" { if renamed != "" && restore {
if err1 := os.Rename(renamed, path); err1 != nil { if err1 := os.Rename(renamed, path); err1 != nil {
// May cause inconsistent data on disk // May cause inconsistent data on disk
log.G(ctx).WithError(err1).WithField("path", renamed).Error("failed to rename after failed commit") log.G(ctx).WithError(err1).WithField("path", renamed).Error("failed to rename after failed commit")
} }
} }
return fmt.Errorf("failed to commit: %w", err) return err
} }
if renamed != "" { if renamed != "" {
if err := os.RemoveAll(renamed); err != nil { if err := os.RemoveAll(renamed); err != nil {
@ -225,17 +206,15 @@ func (o *snapshotter) Remove(ctx context.Context, key string) (err error) {
// Walk the committed snapshots. // Walk the committed 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 {
return err
}
defer t.Rollback()
return storage.WalkInfo(ctx, fn, fs...) return storage.WalkInfo(ctx, fn, fs...)
})
} }
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) {
var ( var (
path, td string path, td string
s storage.Snapshot
) )
if kind == snapshots.KindActive || parent == "" { if kind == snapshots.KindActive || parent == "" {
@ -262,17 +241,10 @@ func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
}() }()
} }
ctx, t, err := o.ms.TransactionContext(ctx, true) err = o.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
s, err = storage.CreateSnapshot(ctx, kind, key, parent, opts...)
if err != nil { if err != nil {
return nil, err return fmt.Errorf("failed to create snapshot: %w", err)
}
s, err := storage.CreateSnapshot(ctx, kind, key, parent, opts...)
if err != nil {
if rerr := t.Rollback(); rerr != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
return nil, fmt.Errorf("failed to create snapshot: %w", err)
} }
if td != "" { if td != "" {
@ -286,26 +258,22 @@ func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
copyDirOpts := []fs.CopyDirOpt{ copyDirOpts := []fs.CopyDirOpt{
fs.WithXAttrErrorHandler(xattrErrorHandler), fs.WithXAttrErrorHandler(xattrErrorHandler),
} }
if err := fs.CopyDir(td, parent, copyDirOpts...); err != nil { if err = fs.CopyDir(td, parent, copyDirOpts...); err != nil {
if rerr := t.Rollback(); rerr != nil { return fmt.Errorf("copying of parent failed: %w", err)
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
return nil, fmt.Errorf("copying of parent failed: %w", err)
} }
} }
path = o.getSnapshotDir(s.ID) path = o.getSnapshotDir(s.ID)
if err := os.Rename(td, path); err != nil { if err = os.Rename(td, path); err != nil {
if rerr := t.Rollback(); rerr != nil { return fmt.Errorf("failed to rename: %w", err)
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
return nil, fmt.Errorf("failed to rename: %w", err)
} }
td = "" td = ""
} }
if err := t.Commit(); err != nil { return nil
return nil, fmt.Errorf("commit failed: %w", err) })
if err != nil {
return nil, err
} }
return o.mounts(s), nil return o.mounts(s), nil

View File

@ -110,44 +110,40 @@ func NewSnapshotter(root string) (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 (s *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, error) { func (s *snapshotter) Stat(ctx context.Context, key string) (info snapshots.Info, err error) {
ctx, t, err := s.ms.TransactionContext(ctx, false) err = s.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
_, info, _, err = storage.GetInfo(ctx, key)
return err
})
if err != nil { if err != nil {
return snapshots.Info{}, err return snapshots.Info{}, err
} }
defer t.Rollback()
_, info, _, err := storage.GetInfo(ctx, key)
return info, err
}
func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error) {
ctx, t, err := s.ms.TransactionContext(ctx, true)
if err != nil {
return snapshots.Info{}, err
}
defer t.Rollback()
info, err = storage.UpdateInfo(ctx, info, fieldpaths...)
if err != nil {
return snapshots.Info{}, err
}
if err := t.Commit(); err != nil {
return snapshots.Info{}, err
}
return info, nil return info, nil
} }
func (s *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, error) { func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (_ snapshots.Info, err error) {
ctx, t, err := s.ms.TransactionContext(ctx, false) err = s.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
info, err = storage.UpdateInfo(ctx, info, fieldpaths...)
return err
})
if err != nil { if err != nil {
return snapshots.Usage{}, err return snapshots.Info{}, err
} }
id, info, usage, err := storage.GetInfo(ctx, key)
t.Rollback() // transaction no longer needed at this point.
return info, nil
}
func (s *snapshotter) Usage(ctx context.Context, key string) (usage snapshots.Usage, err error) {
var (
id string
info snapshots.Info
)
err = s.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
id, info, usage, err = storage.GetInfo(ctx, key)
return err
})
if err != nil { if err != nil {
return snapshots.Usage{}, err return snapshots.Usage{}, err
} }
@ -177,34 +173,25 @@ func (s *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 (s *snapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) { func (s *snapshotter) Mounts(ctx context.Context, key string) (_ []mount.Mount, err error) {
ctx, t, err := s.ms.TransactionContext(ctx, false) var snapshot storage.Snapshot
err = s.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
snapshot, err = storage.GetSnapshot(ctx, key)
if err != nil {
return fmt.Errorf("failed to get snapshot mount: %w", err)
}
return nil
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer t.Rollback()
snapshot, err := storage.GetSnapshot(ctx, key)
if err != nil {
return nil, fmt.Errorf("failed to get snapshot mount: %w", err)
}
return s.mounts(snapshot), nil return s.mounts(snapshot), nil
} }
func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) (retErr error) { func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) (retErr error) {
ctx, t, err := s.ms.TransactionContext(ctx, true) return s.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
if err != nil {
return err
}
defer func() {
if retErr != nil {
if rerr := t.Rollback(); rerr != nil {
log.G(ctx).WithError(rerr).Warn("failed to rollback transaction")
}
}
}()
// grab the existing id // grab the existing id
id, _, _, err := storage.GetInfo(ctx, key) id, _, _, err := storage.GetInfo(ctx, key)
if err != nil { if err != nil {
@ -234,27 +221,29 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
if _, err := storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil { if _, err := storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil {
return fmt.Errorf("failed to commit snapshot: %w", err) return fmt.Errorf("failed to commit snapshot: %w", err)
} }
return t.Commit()
return nil
})
} }
// Remove abandons the transaction identified by key. All resources // Remove abandons the transaction identified by key. All resources
// associated with the key will be removed. // associated with the key will be removed.
func (s *snapshotter) Remove(ctx context.Context, key string) error { func (s *snapshotter) Remove(ctx context.Context, key string) error {
ctx, t, err := s.ms.TransactionContext(ctx, true) var (
if err != nil { renamed, path, renamedID string
return err restore bool
} )
defer t.Rollback()
err := s.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
id, _, err := storage.Remove(ctx, key) id, _, err := storage.Remove(ctx, key)
if err != nil { if err != nil {
return fmt.Errorf("failed to remove: %w", err) return fmt.Errorf("failed to remove: %w", err)
} }
path := s.getSnapshotDir(id) path = s.getSnapshotDir(id)
renamedID := "rm-" + id renamedID = "rm-" + id
renamed := s.getSnapshotDir(renamedID) renamed = s.getSnapshotDir(renamedID)
if err := os.Rename(path, renamed); err != nil && !os.IsNotExist(err) { if err = os.Rename(path, renamed); err != nil && !os.IsNotExist(err) {
if !os.IsPermission(err) { if !os.IsPermission(err) {
return err return err
} }
@ -277,15 +266,20 @@ func (s *snapshotter) Remove(ctx context.Context, key string) error {
} }
} }
if err := t.Commit(); err != nil { restore = true
return nil
})
if err != nil {
if restore { // failed to commit
if err1 := os.Rename(renamed, path); err1 != nil { if err1 := os.Rename(renamed, path); err1 != nil {
// May cause inconsistent data on disk // May cause inconsistent data on disk
log.G(ctx).WithError(err1).WithField("path", renamed).Error("Failed to rename after failed commit") log.G(ctx).WithError(err1).WithField("path", renamed).Error("Failed to rename after failed commit")
} }
return fmt.Errorf("failed to commit: %w", err) }
return err
} }
if err := hcsshim.DestroyLayer(s.info, renamedID); err != nil { if err = hcsshim.DestroyLayer(s.info, renamedID); err != nil {
// Must be cleaned up, any "rm-*" could be removed if no active transactions // Must be cleaned up, any "rm-*" could be removed if no active transactions
log.G(ctx).WithError(err).WithField("path", renamed).Warnf("Failed to remove root filesystem") log.G(ctx).WithError(err).WithField("path", renamed).Warnf("Failed to remove root filesystem")
} }
@ -295,13 +289,9 @@ func (s *snapshotter) Remove(ctx context.Context, key string) error {
// Walk the committed snapshots. // Walk the committed snapshots.
func (s *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...string) error { func (s *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...string) error {
ctx, t, err := s.ms.TransactionContext(ctx, false) return s.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
if err != nil {
return err
}
defer t.Rollback()
return storage.WalkInfo(ctx, fn, fs...) return storage.WalkInfo(ctx, fn, fs...)
})
} }
// Close closes the snapshotter // Close closes the snapshotter
@ -351,24 +341,23 @@ func (s *snapshotter) getSnapshotDir(id string) string {
return filepath.Join(s.root, "snapshots", id) return filepath.Join(s.root, "snapshots", id)
} }
func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) ([]mount.Mount, error) { func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) (_ []mount.Mount, err error) {
ctx, t, err := s.ms.TransactionContext(ctx, true) var newSnapshot storage.Snapshot
err = s.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
newSnapshot, err = storage.CreateSnapshot(ctx, kind, key, parent, opts...)
if err != nil { if err != nil {
return nil, err return fmt.Errorf("failed to create snapshot: %w", err)
} }
defer t.Rollback()
if kind != snapshots.KindActive {
newSnapshot, err := storage.CreateSnapshot(ctx, kind, key, parent, opts...) return nil
if err != nil {
return nil, fmt.Errorf("failed to create snapshot: %w", err)
} }
if kind == snapshots.KindActive {
log.G(ctx).Debug("createSnapshot active") log.G(ctx).Debug("createSnapshot active")
// Create the new snapshot dir // Create the new snapshot dir
snDir := s.getSnapshotDir(newSnapshot.ID) snDir := s.getSnapshotDir(newSnapshot.ID)
if err := os.MkdirAll(snDir, 0700); err != nil { if err = os.MkdirAll(snDir, 0700); err != nil {
return nil, err return err
} }
// IO/disk space optimization // IO/disk space optimization
@ -391,7 +380,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
sizeInGB, err := strconv.ParseUint(sizeGBstr, 10, 32) sizeInGB, err := strconv.ParseUint(sizeGBstr, 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse label %q=%q: %w", rootfsSizeInGBLabel, sizeGBstr, err) return fmt.Errorf("failed to parse label %q=%q: %w", rootfsSizeInGBLabel, sizeGBstr, err)
} }
sizeInBytes = sizeInGB * 1024 * 1024 * 1024 sizeInBytes = sizeInGB * 1024 * 1024 * 1024
} }
@ -400,7 +389,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
if sizeBytesStr, ok := snapshotInfo.Labels[rootfsSizeInBytesLabel]; ok { if sizeBytesStr, ok := snapshotInfo.Labels[rootfsSizeInBytesLabel]; ok {
sizeInBytes, err = strconv.ParseUint(sizeBytesStr, 10, 64) sizeInBytes, err = strconv.ParseUint(sizeBytesStr, 10, 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse label %q=%q: %w", rootfsSizeInBytesLabel, sizeBytesStr, err) return fmt.Errorf("failed to parse label %q=%q: %w", rootfsSizeInBytesLabel, sizeBytesStr, err)
} }
} }
@ -411,18 +400,19 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
// This has to be run first to avoid clashing with the containers sandbox.vhdx. // This has to be run first to avoid clashing with the containers sandbox.vhdx.
if makeUVMScratch { if makeUVMScratch {
if err := s.createUVMScratchLayer(ctx, snDir, parentLayerPaths); err != nil { if err = s.createUVMScratchLayer(ctx, snDir, parentLayerPaths); err != nil {
return nil, fmt.Errorf("failed to make UVM's scratch layer: %w", err) return fmt.Errorf("failed to make UVM's scratch layer: %w", err)
} }
} }
if err := s.createScratchLayer(ctx, snDir, parentLayerPaths, sizeInBytes); err != nil { if err = s.createScratchLayer(ctx, snDir, parentLayerPaths, sizeInBytes); err != nil {
return nil, fmt.Errorf("failed to create scratch layer: %w", err) return fmt.Errorf("failed to create scratch layer: %w", err)
}
} }
} }
if err := t.Commit(); err != nil { return nil
return nil, fmt.Errorf("commit failed: %w", err) })
if err != nil {
return nil, err
} }
return s.mounts(newSnapshot), nil return s.mounts(newSnapshot), nil