snapshots: refactor metastore transaction
Signed-off-by: Junyu Liu <ljyngup@gmail.com>
This commit is contained in:
		| @@ -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,80 +166,77 @@ 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 { | 		// 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, s.getSnapshotDir(id)) | ||||||
| 	id, _, _, err := storage.GetInfo(ctx, key) | 		if err != nil { | ||||||
| 	if err != nil { | 			return err | ||||||
| 		return err | 		} | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	usage, err := fs.DiskUsage(ctx, s.getSnapshotDir(id)) | 		if _, err = storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil { | ||||||
| 	if err != nil { | 			return fmt.Errorf("failed to commit snapshot: %w", 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 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() |  | ||||||
|  |  | ||||||
| 	id, _, err := storage.Remove(ctx, key) | 	err = s.ms.WithTransaction(ctx, true, func(ctx context.Context) error { | ||||||
| 	if err != nil { | 		id, _, err := storage.Remove(ctx, key) | ||||||
| 		return fmt.Errorf("failed to remove: %w", err) | 		if err != nil { | ||||||
| 	} | 			return fmt.Errorf("failed to remove: %w", err) | ||||||
|  |  | ||||||
| 	path := s.getSnapshotDir(id) |  | ||||||
| 	renamed := s.getSnapshotDir("rm-" + id) |  | ||||||
| 	if err := os.Rename(path, renamed); err != nil && !os.IsNotExist(err) { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if err := t.Commit(); err != nil { |  | ||||||
| 		if err1 := os.Rename(renamed, path); err1 != nil { |  | ||||||
| 			// May cause inconsistent data on disk |  | ||||||
| 			log.G(ctx).WithError(err1).WithField("path", renamed).Error("Failed to rename after failed commit") |  | ||||||
| 		} | 		} | ||||||
| 		return fmt.Errorf("failed to commit: %w", err) |  | ||||||
|  | 		path = s.getSnapshotDir(id) | ||||||
|  | 		renamed = s.getSnapshotDir("rm-" + id) | ||||||
|  | 		if err = os.Rename(path, renamed); err != nil && !os.IsNotExist(err) { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		restore = true | ||||||
|  | 		return nil | ||||||
|  | 	}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		if restore { // failed to commit | ||||||
|  | 			if err1 := os.Rename(renamed, path); err1 != nil { | ||||||
|  | 				// May cause inconsistent data on disk | ||||||
|  | 				log.G(ctx).WithError(err1).WithField("path", renamed).Error("Failed to rename after failed commit") | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		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 storage.WalkInfo(ctx, fn, fs...) | ||||||
| 		return err | 	}) | ||||||
| 	} |  | ||||||
| 	defer t.Rollback() |  | ||||||
|  |  | ||||||
| 	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 | ||||||
| 	if err != nil { | 	err := s.ms.WithTransaction(ctx, true, func(ctx context.Context) (err error) { | ||||||
| 		return nil, err | 		newSnapshot, err = storage.CreateSnapshot(ctx, kind, key, parent, opts...) | ||||||
| 	} | 		if err != nil { | ||||||
| 	defer t.Rollback() | 			return fmt.Errorf("failed to create snapshot: %w", err) | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 	newSnapshot, err := storage.CreateSnapshot(ctx, kind, key, parent, opts...) | 		if kind != snapshots.KindActive { | ||||||
| 	if err != nil { | 			return 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 | ||||||
|   | |||||||
| @@ -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 { | ||||||
|  | 		info, err = storage.UpdateInfo(ctx, info, fieldpaths...) | ||||||
|  | 		return err | ||||||
|  | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return snapshots.Info{}, err | 		return snapshots.Info{}, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	info, err = storage.UpdateInfo(ctx, info, fieldpaths...) |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Rollback() |  | ||||||
| 		return snapshots.Info{}, err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if err := t.Commit(); err != nil { |  | ||||||
| 		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 { | 		id, _, _, err := storage.GetInfo(ctx, key) | ||||||
| 		return err | 		if err != nil { | ||||||
| 	} | 			return err | ||||||
|  |  | ||||||
| 	id, _, _, err := storage.GetInfo(ctx, key) |  | ||||||
| 	if err != nil { |  | ||||||
| 		if rerr := t.Rollback(); rerr != nil { |  | ||||||
| 			log.G(ctx).WithError(rerr).Warn("failed to rollback transaction") |  | ||||||
| 		} | 		} | ||||||
| 		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 { | 			return err | ||||||
| 			log.G(ctx).WithError(rerr).Warn("failed to rollback transaction") |  | ||||||
| 		} | 		} | ||||||
| 		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 { | 			return fmt.Errorf("failed to commit snapshot: %w", err) | ||||||
| 			log.G(ctx).WithError(rerr).Warn("failed to rollback transaction") |  | ||||||
| 		} | 		} | ||||||
| 		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 (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 { | 	err = o.ms.WithTransaction(ctx, true, func(ctx context.Context) error { | ||||||
| 			if rerr := t.Rollback(); rerr != nil { | 		id, _, err := storage.Remove(ctx, key) | ||||||
| 				log.G(ctx).WithError(rerr).Warn("failed to rollback transaction") | 		if err != nil { | ||||||
|  | 			return fmt.Errorf("failed to remove: %w", err) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		path = o.getSnapshotDir(id) | ||||||
|  | 		renamed = filepath.Join(o.root, "snapshots", "rm-"+id) | ||||||
|  | 		if err = os.Rename(path, renamed); err != nil { | ||||||
|  | 			if !os.IsNotExist(err) { | ||||||
|  | 				return fmt.Errorf("failed to rename: %w", err) | ||||||
| 			} | 			} | ||||||
|  | 			renamed = "" | ||||||
| 		} | 		} | ||||||
| 	}() |  | ||||||
|  |  | ||||||
| 	id, _, err := storage.Remove(ctx, key) | 		restore = true | ||||||
|  | 		return nil | ||||||
|  | 	}) | ||||||
|  |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return fmt.Errorf("failed to remove: %w", err) | 		if renamed != "" && restore { | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	path := o.getSnapshotDir(id) |  | ||||||
| 	renamed := filepath.Join(o.root, "snapshots", "rm-"+id) |  | ||||||
| 	if err := os.Rename(path, renamed); err != nil { |  | ||||||
| 		if !os.IsNotExist(err) { |  | ||||||
| 			return fmt.Errorf("failed to rename: %w", err) |  | ||||||
| 		} |  | ||||||
| 		renamed = "" |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	err = t.Commit() |  | ||||||
| 	t = nil |  | ||||||
| 	if err != nil { |  | ||||||
| 		if renamed != "" { |  | ||||||
| 			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 storage.WalkInfo(ctx, fn, fs...) | ||||||
| 		return err | 	}) | ||||||
| 	} |  | ||||||
| 	defer t.Rollback() |  | ||||||
| 	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,52 +241,41 @@ 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 { | ||||||
|  | 			return fmt.Errorf("failed to create snapshot: %w", err) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		if td != "" { | ||||||
|  | 			if len(s.ParentIDs) > 0 { | ||||||
|  | 				parent := o.getSnapshotDir(s.ParentIDs[0]) | ||||||
|  | 				xattrErrorHandler := func(dst, src, xattrKey string, copyErr error) error { | ||||||
|  | 					// security.* xattr cannot be copied in most cases (moby/buildkit#1189) | ||||||
|  | 					log.G(ctx).WithError(copyErr).Debugf("failed to copy xattr %q", xattrKey) | ||||||
|  | 					return nil | ||||||
|  | 				} | ||||||
|  | 				copyDirOpts := []fs.CopyDirOpt{ | ||||||
|  | 					fs.WithXAttrErrorHandler(xattrErrorHandler), | ||||||
|  | 				} | ||||||
|  | 				if err = fs.CopyDir(td, parent, copyDirOpts...); err != nil { | ||||||
|  | 					return fmt.Errorf("copying of parent failed: %w", err) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			path = o.getSnapshotDir(s.ID) | ||||||
|  | 			if err = os.Rename(td, path); err != nil { | ||||||
|  | 				return fmt.Errorf("failed to rename: %w", err) | ||||||
|  | 			} | ||||||
|  | 			td = "" | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		return nil | ||||||
|  | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, 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 len(s.ParentIDs) > 0 { |  | ||||||
| 			parent := o.getSnapshotDir(s.ParentIDs[0]) |  | ||||||
| 			xattrErrorHandler := func(dst, src, xattrKey string, copyErr error) error { |  | ||||||
| 				// security.* xattr cannot be copied in most cases (moby/buildkit#1189) |  | ||||||
| 				log.G(ctx).WithError(copyErr).Debugf("failed to copy xattr %q", xattrKey) |  | ||||||
| 				return nil |  | ||||||
| 			} |  | ||||||
| 			copyDirOpts := []fs.CopyDirOpt{ |  | ||||||
| 				fs.WithXAttrErrorHandler(xattrErrorHandler), |  | ||||||
| 			} |  | ||||||
| 			if err := fs.CopyDir(td, parent, copyDirOpts...); err != nil { |  | ||||||
| 				if rerr := t.Rollback(); rerr != nil { |  | ||||||
| 					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) |  | ||||||
| 		if err := os.Rename(td, path); err != nil { |  | ||||||
| 			if rerr := t.Rollback(); rerr != nil { |  | ||||||
| 				log.G(ctx).WithError(rerr).Warn("failed to rollback transaction") |  | ||||||
| 			} |  | ||||||
| 			return nil, fmt.Errorf("failed to rename: %w", err) |  | ||||||
| 		} |  | ||||||
| 		td = "" |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if err := t.Commit(); err != nil { |  | ||||||
| 		return nil, fmt.Errorf("commit failed: %w", err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return o.mounts(s), nil | 	return o.mounts(s), nil | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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,115 +173,113 @@ 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 { | 		// grab the existing id | ||||||
| 		return err | 		id, _, _, err := storage.GetInfo(ctx, key) | ||||||
| 	} | 		if err != nil { | ||||||
|  | 			return fmt.Errorf("failed to get storage info for %s: %w", key, 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 | 		snapshot, err := storage.GetSnapshot(ctx, key) | ||||||
| 	id, _, _, err := storage.GetInfo(ctx, key) | 		if err != nil { | ||||||
| 	if err != nil { |  | ||||||
| 		return fmt.Errorf("failed to get storage info for %s: %w", key, err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	snapshot, err := storage.GetSnapshot(ctx, key) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	path := s.getSnapshotDir(id) |  | ||||||
|  |  | ||||||
| 	// If (windowsDiff).Apply was used to populate this layer, then it's already in the 'committed' state. |  | ||||||
| 	// See createSnapshot below for more details |  | ||||||
| 	if !strings.Contains(key, snapshots.UnpackKeyPrefix) { |  | ||||||
| 		if err := s.convertScratchToReadOnlyLayer(ctx, snapshot, path); err != nil { |  | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	usage, err := fs.DiskUsage(ctx, path) | 		path := s.getSnapshotDir(id) | ||||||
| 	if err != nil { |  | ||||||
| 		return fmt.Errorf("failed to collect disk usage of snapshot storage: %s: %w", path, err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if _, err := storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil { | 		// If (windowsDiff).Apply was used to populate this layer, then it's already in the 'committed' state. | ||||||
| 		return fmt.Errorf("failed to commit snapshot: %w", err) | 		// See createSnapshot below for more details | ||||||
| 	} | 		if !strings.Contains(key, snapshots.UnpackKeyPrefix) { | ||||||
| 	return t.Commit() | 			if err := s.convertScratchToReadOnlyLayer(ctx, snapshot, path); err != nil { | ||||||
|  | 				return err | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		usage, err := fs.DiskUsage(ctx, path) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return fmt.Errorf("failed to collect disk usage of snapshot storage: %s: %w", path, err) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		if _, err := storage.CommitActive(ctx, key, name, snapshots.Usage(usage), opts...); err != nil { | ||||||
|  | 			return fmt.Errorf("failed to commit snapshot: %w", err) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		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 ( | ||||||
|  | 		renamed, path, renamedID string | ||||||
|  | 		restore                  bool | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	err := s.ms.WithTransaction(ctx, true, func(ctx context.Context) error { | ||||||
|  | 		id, _, err := storage.Remove(ctx, key) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return fmt.Errorf("failed to remove: %w", err) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		path = s.getSnapshotDir(id) | ||||||
|  | 		renamedID = "rm-" + id | ||||||
|  | 		renamed = s.getSnapshotDir(renamedID) | ||||||
|  | 		if err = os.Rename(path, renamed); err != nil && !os.IsNotExist(err) { | ||||||
|  | 			if !os.IsPermission(err) { | ||||||
|  | 				return err | ||||||
|  | 			} | ||||||
|  | 			// If permission denied, it's possible that the scratch is still mounted, an | ||||||
|  | 			// artifact after a hard daemon crash for example. Worth a shot to try deactivating it | ||||||
|  | 			// before retrying the rename. | ||||||
|  | 			var ( | ||||||
|  | 				home, layerID = filepath.Split(path) | ||||||
|  | 				di            = hcsshim.DriverInfo{ | ||||||
|  | 					HomeDir: home, | ||||||
|  | 				} | ||||||
|  | 			) | ||||||
|  |  | ||||||
|  | 			if deactivateErr := hcsshim.DeactivateLayer(di, layerID); deactivateErr != nil { | ||||||
|  | 				return fmt.Errorf("failed to deactivate layer following failed rename: %s: %w", deactivateErr, err) | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			if renameErr := os.Rename(path, renamed); renameErr != nil && !os.IsNotExist(renameErr) { | ||||||
|  | 				return fmt.Errorf("second rename attempt following detach failed: %s: %w", renameErr, err) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		restore = true | ||||||
|  | 		return nil | ||||||
|  | 	}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | 		if restore { // failed to commit | ||||||
|  | 			if err1 := os.Rename(renamed, path); err1 != nil { | ||||||
|  | 				// May cause inconsistent data on disk | ||||||
|  | 				log.G(ctx).WithError(err1).WithField("path", renamed).Error("Failed to rename after failed commit") | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	defer t.Rollback() |  | ||||||
|  |  | ||||||
| 	id, _, err := storage.Remove(ctx, key) | 	if err = hcsshim.DestroyLayer(s.info, renamedID); err != nil { | ||||||
| 	if err != nil { |  | ||||||
| 		return fmt.Errorf("failed to remove: %w", err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	path := s.getSnapshotDir(id) |  | ||||||
| 	renamedID := "rm-" + id |  | ||||||
| 	renamed := s.getSnapshotDir(renamedID) |  | ||||||
| 	if err := os.Rename(path, renamed); err != nil && !os.IsNotExist(err) { |  | ||||||
| 		if !os.IsPermission(err) { |  | ||||||
| 			return err |  | ||||||
| 		} |  | ||||||
| 		// If permission denied, it's possible that the scratch is still mounted, an |  | ||||||
| 		// artifact after a hard daemon crash for example. Worth a shot to try deactivating it |  | ||||||
| 		// before retrying the rename. |  | ||||||
| 		var ( |  | ||||||
| 			home, layerID = filepath.Split(path) |  | ||||||
| 			di            = hcsshim.DriverInfo{ |  | ||||||
| 				HomeDir: home, |  | ||||||
| 			} |  | ||||||
| 		) |  | ||||||
|  |  | ||||||
| 		if deactivateErr := hcsshim.DeactivateLayer(di, layerID); deactivateErr != nil { |  | ||||||
| 			return fmt.Errorf("failed to deactivate layer following failed rename: %s: %w", deactivateErr, err) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if renameErr := os.Rename(path, renamed); renameErr != nil && !os.IsNotExist(renameErr) { |  | ||||||
| 			return fmt.Errorf("second rename attempt following detach failed: %s: %w", renameErr, err) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if err := t.Commit(); err != nil { |  | ||||||
| 		if err1 := os.Rename(renamed, path); err1 != nil { |  | ||||||
| 			// May cause inconsistent data on disk |  | ||||||
| 			log.G(ctx).WithError(err1).WithField("path", renamed).Error("Failed to rename after failed commit") |  | ||||||
| 		} |  | ||||||
| 		return fmt.Errorf("failed to commit: %w", err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	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 storage.WalkInfo(ctx, fn, fs...) | ||||||
| 		return err | 	}) | ||||||
| 	} |  | ||||||
| 	defer t.Rollback() |  | ||||||
|  |  | ||||||
| 	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 | ||||||
| 	if err != nil { | 	err = s.ms.WithTransaction(ctx, true, func(ctx context.Context) error { | ||||||
| 		return nil, err | 		newSnapshot, err = storage.CreateSnapshot(ctx, kind, key, parent, opts...) | ||||||
| 	} | 		if err != nil { | ||||||
| 	defer t.Rollback() | 			return fmt.Errorf("failed to create snapshot: %w", err) | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 	newSnapshot, err := storage.CreateSnapshot(ctx, kind, key, parent, opts...) | 		if kind != snapshots.KindActive { | ||||||
| 	if err != nil { | 			return 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 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 iyear
					iyear