Update metadata plugin initialization
Updates metadata plugin to require content and snapshotter plugins be loaded and initializes with those plugins, keeping the metadata database structure static after initialization. Service plugins now only require metadata plugin access snapshotter or content stores through metadata, which was already required behavior of the services. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
@@ -17,9 +17,14 @@ func WithTransactionContext(ctx context.Context, tx *bolt.Tx) context.Context {
|
||||
return context.WithValue(ctx, transactionKey{}, tx)
|
||||
}
|
||||
|
||||
type transactor interface {
|
||||
View(fn func(*bolt.Tx) error) error
|
||||
Update(fn func(*bolt.Tx) error) error
|
||||
}
|
||||
|
||||
// view gets a bolt db transaction either from the context
|
||||
// or starts a new one with the provided bolt database.
|
||||
func view(ctx context.Context, db *DB, fn func(*bolt.Tx) error) error {
|
||||
func view(ctx context.Context, db transactor, fn func(*bolt.Tx) error) error {
|
||||
tx, ok := ctx.Value(transactionKey{}).(*bolt.Tx)
|
||||
if !ok {
|
||||
return db.View(fn)
|
||||
@@ -29,7 +34,7 @@ func view(ctx context.Context, db *DB, fn func(*bolt.Tx) error) error {
|
||||
|
||||
// update gets a writable bolt db transaction either from the context
|
||||
// or starts a new one with the provided bolt database.
|
||||
func update(ctx context.Context, db *DB, fn func(*bolt.Tx) error) error {
|
||||
func update(ctx context.Context, db transactor, fn func(*bolt.Tx) error) error {
|
||||
tx, ok := ctx.Value(transactionKey{}).(*bolt.Tx)
|
||||
if !ok {
|
||||
return db.Update(fn)
|
||||
|
||||
@@ -19,23 +19,16 @@ import (
|
||||
|
||||
type contentStore struct {
|
||||
content.Store
|
||||
db *DB
|
||||
db transactor
|
||||
}
|
||||
|
||||
// NewContentStore returns a namespaced content store using an existing
|
||||
// newContentStore returns a namespaced content store using an existing
|
||||
// content store interface.
|
||||
func NewContentStore(db *DB, cs content.Store) content.Store {
|
||||
db.storeL.Lock()
|
||||
defer db.storeL.Unlock()
|
||||
|
||||
if db.cs == nil {
|
||||
db.cs = &contentStore{
|
||||
Store: cs,
|
||||
db: db,
|
||||
}
|
||||
func newContentStore(db transactor, cs content.Store) content.Store {
|
||||
return &contentStore{
|
||||
Store: cs,
|
||||
db: db,
|
||||
}
|
||||
return db.cs
|
||||
|
||||
}
|
||||
|
||||
func (cs *contentStore) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
|
||||
@@ -360,7 +353,7 @@ type namespacedWriter struct {
|
||||
content.Writer
|
||||
ref string
|
||||
namespace string
|
||||
db *DB
|
||||
db transactor
|
||||
}
|
||||
|
||||
func (nw *namespacedWriter) Commit(ctx context.Context, size int64, expected digest.Digest, opts ...content.Opt) error {
|
||||
|
||||
@@ -23,7 +23,7 @@ func createContentStore(ctx context.Context, root string) (content.Store, func()
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return NewContentStore(NewDB(db), cs), func() error {
|
||||
return NewDB(db, cs, nil).ContentStore(), func() error {
|
||||
return db.Close()
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -1,26 +1,40 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/containerd/containerd/content"
|
||||
"github.com/containerd/containerd/snapshot"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
db *bolt.DB
|
||||
|
||||
storeL sync.Mutex
|
||||
ss map[string]*snapshotter
|
||||
cs *contentStore
|
||||
ss map[string]snapshot.Snapshotter
|
||||
cs content.Store
|
||||
}
|
||||
|
||||
func NewDB(db *bolt.DB) *DB {
|
||||
func NewDB(db *bolt.DB, cs content.Store, ss map[string]snapshot.Snapshotter) *DB {
|
||||
return &DB{
|
||||
db: db,
|
||||
ss: map[string]*snapshotter{},
|
||||
ss: ss,
|
||||
cs: cs,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *DB) ContentStore() content.Store {
|
||||
if m.cs == nil {
|
||||
return nil
|
||||
}
|
||||
return newContentStore(m, m.cs)
|
||||
}
|
||||
|
||||
func (m *DB) Snapshotter(name string) snapshot.Snapshotter {
|
||||
sn, ok := m.ss[name]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return newSnapshotter(m, name, sn)
|
||||
}
|
||||
|
||||
func (m *DB) View(fn func(*bolt.Tx) error) error {
|
||||
return m.db.View(fn)
|
||||
}
|
||||
|
||||
@@ -19,26 +19,17 @@ import (
|
||||
type snapshotter struct {
|
||||
snapshot.Snapshotter
|
||||
name string
|
||||
db *DB
|
||||
db transactor
|
||||
}
|
||||
|
||||
// NewSnapshotter returns a new Snapshotter which namespaces the given snapshot
|
||||
// using the provided name and metadata store.
|
||||
func NewSnapshotter(db *DB, name string, sn snapshot.Snapshotter) snapshot.Snapshotter {
|
||||
db.storeL.Lock()
|
||||
defer db.storeL.Unlock()
|
||||
|
||||
ss, ok := db.ss[name]
|
||||
if !ok {
|
||||
ss = &snapshotter{
|
||||
Snapshotter: sn,
|
||||
name: name,
|
||||
db: db,
|
||||
}
|
||||
db.ss[name] = ss
|
||||
// newSnapshotter returns a new Snapshotter which namespaces the given snapshot
|
||||
// using the provided name and database.
|
||||
func newSnapshotter(db transactor, name string, sn snapshot.Snapshotter) snapshot.Snapshotter {
|
||||
return &snapshotter{
|
||||
Snapshotter: sn,
|
||||
name: name,
|
||||
db: db,
|
||||
}
|
||||
|
||||
return ss
|
||||
}
|
||||
|
||||
func createKey(id uint64, namespace, key string) string {
|
||||
@@ -466,11 +457,7 @@ func (s *snapshotter) Remove(ctx context.Context, key string) error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := bkt.DeleteBucket([]byte(key)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return bkt.DeleteBucket([]byte(key))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/containerd/containerd/testutil"
|
||||
)
|
||||
|
||||
func newSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, func() error, error) {
|
||||
func newTestSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, func() error, error) {
|
||||
naiveRoot := filepath.Join(root, "naive")
|
||||
if err := os.Mkdir(naiveRoot, 0770); err != nil {
|
||||
return nil, nil, err
|
||||
@@ -28,7 +28,7 @@ func newSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, fun
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
sn := NewSnapshotter(NewDB(db), "naive", snapshotter)
|
||||
sn := NewDB(db, nil, map[string]snapshot.Snapshotter{"naive": snapshotter}).Snapshotter("naive")
|
||||
|
||||
return sn, func() error {
|
||||
return db.Close()
|
||||
@@ -38,5 +38,5 @@ func newSnapshotter(ctx context.Context, root string) (snapshot.Snapshotter, fun
|
||||
func TestMetadata(t *testing.T) {
|
||||
// Snapshot tests require mounting, still requires root
|
||||
testutil.RequiresRoot(t)
|
||||
testsuite.SnapshotterSuite(t, "Metadata", newSnapshotter)
|
||||
testsuite.SnapshotterSuite(t, "Metadata", newTestSnapshotter)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user