Merge pull request #1728 from AkihiroSuda/snapshot-closable

snapshot: add Close()
This commit is contained in:
Stephen Day 2017-11-14 14:41:50 -08:00 committed by GitHub
commit c78c156feb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 66 additions and 2 deletions

View File

@ -724,3 +724,8 @@ func (s *snapshotter) pruneBranch(ctx context.Context, node *treeNode) error {
return nil return nil
} }
// Close closes s.Snapshotter but not db
func (s *snapshotter) Close() error {
return s.Snapshotter.Close()
}

View File

@ -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") sn := NewDB(db, nil, map[string]snapshot.Snapshotter{"naive": snapshotter}).Snapshotter("naive")
return sn, func() error { return sn, func() error {
if err := sn.Close(); err != nil {
return err
}
return db.Close() return db.Close()
}, nil }, nil
} }

View File

@ -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 { func toKind(kind snapshotapi.Kind) snapshot.Kind {
if kind == snapshotapi.KindActive { if kind == snapshotapi.KindActive {
return snapshot.KindActive return snapshot.KindActive

View File

@ -370,3 +370,8 @@ func (b *snapshotter) Remove(ctx context.Context, key string) (err error) {
return nil return nil
} }
// Close closes the snapshotter
func (b *snapshotter) Close() error {
return b.ms.Close()
}

View File

@ -47,6 +47,9 @@ func boltSnapshotter(t *testing.T) func(context.Context, string) (snapshot.Snaps
} }
return snapshotter, func() error { return snapshotter, func() error {
if err := snapshotter.Close(); err != nil {
return err
}
err := mount.UnmountAll(root, unix.MNT_DETACH) err := mount.UnmountAll(root, unix.MNT_DETACH)
if cerr := cleanupDevice(); cerr != nil { if cerr := cleanupDevice(); cerr != nil {
err = errors.Wrap(cerr, "device cleanup failed") err = errors.Wrap(cerr, "device cleanup failed")

View File

@ -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()
}

View File

@ -15,7 +15,7 @@ func newSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, fun
return nil, nil, err return nil, nil, err
} }
return snapshotter, nil, nil return snapshotter, func() error { return snapshotter.Close() }, nil
} }
func TestNaive(t *testing.T) { func TestNaive(t *testing.T) {

View File

@ -401,3 +401,8 @@ func (o *snapshotter) upperPath(id string) string {
func (o *snapshotter) workPath(id string) string { func (o *snapshotter) workPath(id string) string {
return filepath.Join(o.root, "snapshots", id, "work") return filepath.Join(o.root, "snapshots", id, "work")
} }
// Close closes the snapshotter
func (o *snapshotter) Close() error {
return o.ms.Close()
}

View File

@ -24,7 +24,7 @@ func newSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, fun
return nil, nil, err return nil, nil, err
} }
return snapshotter, nil, nil return snapshotter, func() error { return snapshotter.Close() }, nil
} }
func TestOverlay(t *testing.T) { func TestOverlay(t *testing.T) {

View File

@ -296,6 +296,14 @@ type Snapshotter interface {
// Walk all snapshots in the snapshotter. For each snapshot in the // Walk all snapshots in the snapshotter. For each snapshot in the
// snapshotter, the function will be called. // snapshotter, the function will be called.
Walk(ctx context.Context, fn func(context.Context, Info) error) error 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 // Opt allows setting mutable snapshot properties on creation

View File

@ -44,6 +44,7 @@ func SnapshotterSuite(t *testing.T, name string, snapshotterFn func(ctx context.
t.Run("ViewReadonly", makeTest(name, snapshotterFn, checkSnapshotterViewReadonly)) t.Run("ViewReadonly", makeTest(name, snapshotterFn, checkSnapshotterViewReadonly))
t.Run("StatInWalk", makeTest(name, snapshotterFn, checkStatInWalk)) 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) { 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) {
@ -786,3 +787,22 @@ func checkFileFromLowerLayer(ctx context.Context, t *testing.T, snapshotter snap
t.Fatalf("Check snapshots failed: %+v", err) 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)
}
}

View File

@ -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 { func (o *snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshot.Info) error) error {
panic("not implemented") panic("not implemented")
} }
// Close closes the snapshotter
func (o *snapshotter) Close() error {
panic("not implemented")
}

View File

@ -18,6 +18,7 @@ func newSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, fun
sn := client.SnapshotService(DefaultSnapshotter) sn := client.SnapshotService(DefaultSnapshotter)
return sn, func() error { return sn, func() error {
// no need to close remote snapshotter
return client.Close() return client.Close()
}, nil }, nil
} }