Skip to content

Commit

Permalink
Update metadata snapshotter to lease on exists
Browse files Browse the repository at this point in the history
Currently the metadata snapshotter is not consistently adding keys to a
lease when already exists is returned. When a lease is provided, any
already exists errors should add the relevant key to the lease. It is
not expected that clients must explicitly lease a key after calling
Prepare/Commit.

Signed-off-by: Derek McGowan <derek@mcg.dev>
(cherry picked from commit c7fb8a9)
Signed-off-by: Derek McGowan <derek@mcg.dev>
  • Loading branch information
dmcgowan committed May 9, 2024
1 parent 6d429d6 commit b095401
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions metadata/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
bopts = []snapshots.Opt{
snapshots.WithLabels(snapshots.FilterInheritedLabels(base.Labels)),
}
rerr error
)

if err := update(ctx, s.db, func(tx *bolt.Tx) error {
Expand All @@ -318,12 +319,20 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
// Check if target exists, if so, return already exists
if target != "" {
if tbkt := bkt.Bucket([]byte(target)); tbkt != nil {
return fmt.Errorf("target snapshot %q: %w", target, errdefs.ErrAlreadyExists)
rerr = fmt.Errorf("target snapshot %q: %w", target, errdefs.ErrAlreadyExists)
if err := addSnapshotLease(ctx, tx, s.name, target); err != nil {
return err
}
return nil
}
}

if bbkt := bkt.Bucket([]byte(key)); bbkt != nil {
return fmt.Errorf("snapshot %q: %w", key, errdefs.ErrAlreadyExists)
rerr = fmt.Errorf("snapshot %q: %w", key, errdefs.ErrAlreadyExists)
if err := addSnapshotLease(ctx, tx, s.name, key); err != nil {
return err
}
return nil
}

if parent != "" {
Expand All @@ -344,11 +353,14 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
}); err != nil {
return nil, err
}
// Already exists and lease successfully added in transaction
if rerr != nil {
return nil, rerr
}

var (
m []mount.Mount
created string
rerr error
)
if readonly {
m, err = s.Snapshotter.View(ctx, bkey, bparent, bopts...)
Expand Down Expand Up @@ -511,24 +523,28 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
return err
}

var bname string
var (
bname string
rerr error
)
if err := update(ctx, s.db, func(tx *bolt.Tx) error {
bkt := getSnapshotterBucket(tx, ns, s.name)
if bkt == nil {
return fmt.Errorf("can not find snapshotter %q: %w",
s.name, errdefs.ErrNotFound)
}

if err := addSnapshotLease(ctx, tx, s.name, name); err != nil {
return err
}
bbkt, err := bkt.CreateBucket([]byte(name))
if err != nil {
if err == bolt.ErrBucketExists {
err = fmt.Errorf("snapshot %q: %w", name, errdefs.ErrAlreadyExists)
rerr = fmt.Errorf("snapshot %q: %w", name, errdefs.ErrAlreadyExists)
return nil
}
return err
}
if err := addSnapshotLease(ctx, tx, s.name, name); err != nil {
return err
}

obkt := bkt.Bucket([]byte(key))
if obkt == nil {
Expand Down Expand Up @@ -618,7 +634,7 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
return err
}

return nil
return rerr

}

Expand Down

0 comments on commit b095401

Please sign in to comment.