Skip to content

Commit

Permalink
snapshot: add Close()
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed Nov 14, 2017
1 parent 17093c2 commit 4feb6f2
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 2 deletions.
5 changes: 5 additions & 0 deletions metadata/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,8 @@ func (s *snapshotter) pruneBranch(ctx context.Context, node *treeNode) error {

return nil
}

// Close closes s.Snapshotter but not db
func (s *snapshotter) Close() error {
return s.Snapshotter.Close()
}
3 changes: 3 additions & 0 deletions metadata/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func newTestSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter,
sn := NewDB(db, nil, map[string]snapshot.Snapshotter{"naive": snapshotter}).Snapshotter("naive")

return sn, func() error {
if err := sn.Close(); err != nil {
return err
}
return db.Close()
}, nil
}
Expand Down
4 changes: 4 additions & 0 deletions services/snapshot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (r *remoteSnapshotter) Walk(ctx context.Context, fn func(context.Context, s
}
}

func (r *remoteSnapshotter) Close() error {
return nil
}

func toKind(kind snapshotapi.Kind) snapshot.Kind {
if kind == snapshotapi.KindActive {
return snapshot.KindActive
Expand Down
5 changes: 5 additions & 0 deletions snapshot/btrfs/btrfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,8 @@ func (b *snapshotter) Remove(ctx context.Context, key string) (err error) {

return nil
}

// Close closes the snapshotter
func (b *snapshotter) Close() error {
return b.ms.Close()
}
3 changes: 3 additions & 0 deletions snapshot/btrfs/btrfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func boltSnapshotter(t *testing.T) func(context.Context, string) (snapshot.Snaps
}

return snapshotter, func() error {
if err := snapshotter.Close(); err != nil {
return err
}
err := mount.UnmountAll(root, unix.MNT_DETACH)
if cerr := cleanupDevice(); cerr != nil {
err = errors.Wrap(cerr, "device cleanup failed")
Expand Down
5 changes: 5 additions & 0 deletions snapshot/naive/naive.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,8 @@ func (o *snapshotter) mounts(s storage.Snapshot) []mount.Mount {
},
}
}

// Close closes the snapshotter
func (o *snapshotter) Close() error {
return o.ms.Close()
}
2 changes: 1 addition & 1 deletion snapshot/naive/naive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func newSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, fun
return nil, nil, err
}

return snapshotter, nil, nil
return snapshotter, func() error { return snapshotter.Close() }, nil
}

func TestNaive(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions snapshot/overlay/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,8 @@ func (o *snapshotter) upperPath(id string) string {
func (o *snapshotter) workPath(id string) string {
return filepath.Join(o.root, "snapshots", id, "work")
}

// Close closes the snapshotter
func (o *snapshotter) Close() error {
return o.ms.Close()
}
2 changes: 1 addition & 1 deletion snapshot/overlay/overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func newSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, fun
return nil, nil, err
}

return snapshotter, nil, nil
return snapshotter, func() error { return snapshotter.Close() }, nil
}

func TestOverlay(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions snapshot/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ type Snapshotter interface {
// Walk all snapshots in the snapshotter. For each snapshot in the
// snapshotter, the function will be called.
Walk(ctx context.Context, fn func(context.Context, Info) error) error

// Close releases the internal resources.
//
// Close is expected to be called on the end of the lifecycle of the snapshotter,
// but not mandatory.
//
// Close returns nil when it is already closed.
Close() error
}

// Opt allows setting mutable snapshot properties on creation
Expand Down
20 changes: 20 additions & 0 deletions snapshot/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func SnapshotterSuite(t *testing.T, name string, snapshotterFn func(ctx context.
t.Run("ViewReadonly", makeTest(name, snapshotterFn, checkSnapshotterViewReadonly))

t.Run("StatInWalk", makeTest(name, snapshotterFn, checkStatInWalk))
t.Run("CloseTwice", makeTest(name, snapshotterFn, closeTwice))
}

func makeTest(name string, snapshotterFn func(ctx context.Context, root string) (snapshot.Snapshotter, func() error, error), fn func(ctx context.Context, t *testing.T, snapshotter snapshot.Snapshotter, work string)) func(t *testing.T) {
Expand Down Expand Up @@ -786,3 +787,22 @@ func checkFileFromLowerLayer(ctx context.Context, t *testing.T, snapshotter snap
t.Fatalf("Check snapshots failed: %+v", err)
}
}

func closeTwice(ctx context.Context, t *testing.T, snapshotter snapshot.Snapshotter, work string) {
// do some dummy ops to modify the snapshotter internal state
if _, err := snapshotter.Prepare(ctx, "dummy", ""); err != nil {
t.Fatal(err)
}
if err := snapshotter.Commit(ctx, "dummy-1", "dummy"); err != nil {
t.Fatal(err)
}
if err := snapshotter.Remove(ctx, "dummy-1"); err != nil {
t.Fatal(err)
}
if err := snapshotter.Close(); err != nil {
t.Fatalf("The first close failed: %+v", err)
}
if err := snapshotter.Close(); err != nil {
t.Fatalf("The second close failed: %+v", err)
}
}
5 changes: 5 additions & 0 deletions snapshot/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ func (o *snapshotter) Remove(ctx context.Context, key string) error {
func (o *snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshot.Info) error) error {
panic("not implemented")
}

// Close closes the snapshotter
func (o *snapshotter) Close() error {
panic("not implemented")
}
1 change: 1 addition & 0 deletions snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func newSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, fun
sn := client.SnapshotService(DefaultSnapshotter)

return sn, func() error {
// no need to close remote snapshotter
return client.Close()
}, nil
}
Expand Down

0 comments on commit 4feb6f2

Please sign in to comment.