Add parent cache to overlayfs storage driver

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-12-12 13:35:02 -08:00
parent e61c869446
commit 934940a96c

View file

@ -7,6 +7,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"github.com/docker/containerd" "github.com/docker/containerd"
) )
@ -24,12 +25,14 @@ func NewOverlayfs(root string) (*Overlayfs, error) {
} }
} }
return &Overlayfs{ return &Overlayfs{
root: root, root: root,
cache: newCache(),
}, nil }, nil
} }
type Overlayfs struct { type Overlayfs struct {
root string root string
cache *cache
} }
func (o *Overlayfs) Prepare(key string, parentName string) ([]containerd.Mount, error) { func (o *Overlayfs) Prepare(key string, parentName string) ([]containerd.Mount, error) {
@ -45,7 +48,7 @@ func (o *Overlayfs) Prepare(key string, parentName string) ([]containerd.Mount,
return nil, err return nil, err
} }
} }
return active.mounts() return active.mounts(o.cache)
} }
func (o *Overlayfs) Commit(key string, name string) error { func (o *Overlayfs) Commit(key string, name string) error {
@ -112,21 +115,20 @@ func (a *activeDir) commit(name string) error {
return os.Rename(a.path, filepath.Join(a.snapshotsDir, name)) return os.Rename(a.path, filepath.Join(a.snapshotsDir, name))
} }
func (a *activeDir) mounts() ([]containerd.Mount, error) { func (a *activeDir) mounts(c *cache) ([]containerd.Mount, error) {
var ( var (
parentLink = filepath.Join(a.path, "parent") parents []string
parents []string err error
current = a.path
) )
for { for {
snapshot, err := os.Readlink(parentLink) if current, err = c.get(current); err != nil {
if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
break break
} }
return nil, err return nil, err
} }
parents = append(parents, filepath.Join(snapshot, "fs")) parents = append(parents, filepath.Join(current, "fs"))
parentLink = filepath.Join(snapshot, "parent")
} }
if len(parents) == 0 { if len(parents) == 0 {
// if we only have one layer/no parents then just return a bind mount as overlay // if we only have one layer/no parents then just return a bind mount as overlay
@ -155,3 +157,28 @@ func (a *activeDir) mounts() ([]containerd.Mount, error) {
}, },
}, nil }, nil
} }
func newCache() *cache {
return &cache{
parents: make(map[string]string),
}
}
type cache struct {
mu sync.Mutex
parents map[string]string
}
func (c *cache) get(path string) (string, error) {
c.mu.Lock()
defer c.mu.Unlock()
parentRoot, ok := c.parents[path]
if !ok {
link, err := os.Readlink(filepath.Join(path, "parent"))
if err != nil {
return "", err
}
c.parents[path], parentRoot = link, link
}
return parentRoot, nil
}