Skip to content

Commit

Permalink
Merge pull request #2652 from ktock/sharemounts-cleanup
Browse files Browse the repository at this point in the history
cache: Clean up temporary mount pool on restart
  • Loading branch information
tonistiigi committed Feb 23, 2022
2 parents 4411f53 + 3b74229 commit f5a831c
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 7 deletions.
1 change: 1 addition & 0 deletions cache/contenthash/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@ func setupCacheManager(t *testing.T, tmpdir string, snapshotterName string, snap
Applier: applier,
Differ: differ,
GarbageCollect: mdb.GarbageCollect,
MountPoolRoot: filepath.Join(tmpdir, "cachemounts"),
})
require.NoError(t, err)

Expand Down
9 changes: 9 additions & 0 deletions cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ManagerOpt struct {
Applier diff.Applier
Differ diff.Comparer
MetadataStore *metadata.Store
MountPoolRoot string
}

type Accessor interface {
Expand Down Expand Up @@ -90,6 +91,8 @@ type cacheManager struct {
Differ diff.Comparer
MetadataStore *metadata.Store

mountPool sharableMountPool

muPrune sync.Mutex // make sure parallel prune is not allowed so there will not be inconsistent results
unlazyG flightcontrol.Group
}
Expand All @@ -111,6 +114,12 @@ func NewManager(opt ManagerOpt) (Manager, error) {
return nil, err
}

p, err := newSharableMountPool(opt.MountPoolRoot)
if err != nil {
return nil, err
}
cm.mountPool = p

// cm.scheduleGC(5 * time.Minute)

return cm, nil
Expand Down
27 changes: 27 additions & 0 deletions cache/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func newCacheManager(ctx context.Context, opt cmOpt) (co *cmOut, cleanup func()
GarbageCollect: mdb.GarbageCollect,
Applier: applier,
Differ: differ,
MountPoolRoot: filepath.Join(tmpdir, "cachemounts"),
})
if err != nil {
return nil, nil, err
Expand All @@ -162,6 +163,32 @@ func newCacheManager(ctx context.Context, opt cmOpt) (co *cmOut, cleanup func()
}, cleanup, nil
}

func TestSharableMountPoolCleanup(t *testing.T) {
t.Parallel()
ctx := namespaces.WithNamespace(context.Background(), "buildkit-test")

tmpdir, err := ioutil.TempDir("", "cachemanager")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)

// Emulate the situation where the pool dir is dirty
mountPoolDir := filepath.Join(tmpdir, "cachemounts")
require.NoError(t, os.MkdirAll(mountPoolDir, 0700))
_, err = ioutil.TempDir(mountPoolDir, "buildkit")
require.NoError(t, err)

// Initialize cache manager and check if pool is cleaned up
_, cleanup, err := newCacheManager(ctx, cmOpt{
tmpdir: tmpdir,
})
require.NoError(t, err)
defer cleanup()

files, err := os.ReadDir(mountPoolDir)
require.NoError(t, err)
require.Equal(t, 0, len(files))
}

func TestManager(t *testing.T) {
t.Parallel()

Expand Down
50 changes: 44 additions & 6 deletions cache/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
Expand All @@ -22,11 +23,13 @@ import (
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/flightcontrol"
"github.com/moby/buildkit/util/leaseutil"
"github.com/moby/buildkit/util/progress"
"github.com/moby/buildkit/util/winlayers"
"github.com/moby/sys/mountinfo"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -1422,7 +1425,7 @@ func (sr *mutableRef) Mount(ctx context.Context, readonly bool, s session.Group)

// Make the mounts sharable. We don't do this for immutableRef mounts because
// it requires the raw []mount.Mount for computing diff on overlayfs.
mnt = setSharable(mnt)
mnt = sr.cm.mountPool.setSharable(mnt)
sr.mountCache = mnt
if readonly {
mnt = setReadonly(mnt)
Expand Down Expand Up @@ -1524,8 +1527,42 @@ func readonlyOverlay(opt []string) []string {
return out
}

func setSharable(mounts snapshot.Mountable) snapshot.Mountable {
return &sharableMountable{Mountable: mounts}
func newSharableMountPool(tmpdirRoot string) (sharableMountPool, error) {
if tmpdirRoot != "" {
if err := os.MkdirAll(tmpdirRoot, 0700); err != nil {
return sharableMountPool{}, fmt.Errorf("failed to prepare mount pool: %w", err)
}
// If tmpdirRoot is specified, remove existing mounts to avoid conflict.
files, err := os.ReadDir(tmpdirRoot)
if err != nil {
return sharableMountPool{}, fmt.Errorf("failed to read mount pool: %w", err)
}
for _, file := range files {
if file.IsDir() {
dir := filepath.Join(tmpdirRoot, file.Name())
bklog.G(context.Background()).Debugf("cleaning up existing temporary mount %q", dir)
if err := mount.Unmount(dir, 0); err != nil {
if mounted, merr := mountinfo.Mounted(dir); merr != nil || mounted {
bklog.G(context.Background()).WithError(err).WithError(merr).
WithField("mounted", mounted).Warnf("failed to unmount existing temporary mount %q", dir)
continue
}
}
if err := os.Remove(dir); err != nil {
bklog.G(context.Background()).WithError(err).Warnf("failed to remove existing temporary mount %q", dir)
}
}
}
}
return sharableMountPool{tmpdirRoot}, nil
}

type sharableMountPool struct {
tmpdirRoot string
}

func (p sharableMountPool) setSharable(mounts snapshot.Mountable) snapshot.Mountable {
return &sharableMountable{Mountable: mounts, mountPoolRoot: p.tmpdirRoot}
}

// sharableMountable allows sharing underlying (possibly writable) mounts among callers.
Expand All @@ -1538,8 +1575,9 @@ func setSharable(mounts snapshot.Mountable) snapshot.Mountable {
type sharableMountable struct {
snapshot.Mountable

count int32
mu sync.Mutex
count int32
mu sync.Mutex
mountPoolRoot string

curMounts []mount.Mount
curMountPoint string
Expand Down Expand Up @@ -1571,7 +1609,7 @@ func (sm *sharableMountable) Mount() (_ []mount.Mount, _ func() error, retErr er
// Don't need temporary mount wrapper for non-overlayfs mounts
return mounts, release, nil
}
dir, err := ioutil.TempDir("", "buildkit")
dir, err := ioutil.TempDir(sm.mountPoolRoot, "buildkit")
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/klauspost/compress v1.14.3
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/moby/locker v1.0.1
github.com/moby/sys/mountinfo v0.6.0
github.com/moby/sys/signal v0.6.0
github.com/morikuni/aec v1.0.0
github.com/opencontainers/go-digest v1.0.0
Expand Down Expand Up @@ -104,7 +105,6 @@ require (
github.com/ishidawataru/sctp v0.0.0-20210226210310-f2269e66cdee // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/moby/sys/mount v0.3.0 // indirect
github.com/moby/sys/mountinfo v0.6.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions solver/llbsolver/mounts/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func newCacheManager(ctx context.Context, opt cmOpt) (co *cmOut, cleanup func()
Differ: differ,
LeaseManager: lm,
GarbageCollect: mdb.GarbageCollect,
MountPoolRoot: filepath.Join(tmpdir, "cachemounts"),
})
if err != nil {
return nil, nil, err
Expand Down
1 change: 1 addition & 0 deletions source/git/gitsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ func setupGitSource(t *testing.T, tmpdir string) source.Source {
Applier: applier,
Differ: differ,
GarbageCollect: mdb.GarbageCollect,
MountPoolRoot: filepath.Join(tmpdir, "cachemounts"),
})
require.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions source/http/httpsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ func newHTTPSource(tmpdir string) (source.Source, error) {
Applier: applier,
Differ: differ,
GarbageCollect: mdb.GarbageCollect,
MountPoolRoot: filepath.Join(tmpdir, "cachemounts"),
})
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions worker/base/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type WorkerOpt struct {
GarbageCollect func(context.Context) (gc.Stats, error)
ParallelismSem *semaphore.Weighted
MetadataStore *metadata.Store
MountPoolRoot string
}

// Worker is a local worker instance with dedicated snapshotter, cache, and so on.
Expand Down Expand Up @@ -103,6 +104,7 @@ func NewWorker(ctx context.Context, opt WorkerOpt) (*Worker, error) {
ContentStore: opt.ContentStore,
Differ: opt.Differ,
MetadataStore: opt.MetadataStore,
MountPoolRoot: opt.MountPoolRoot,
})
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions worker/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns s
LeaseManager: lm,
GarbageCollect: gc,
ParallelismSem: parallelismSem,
MountPoolRoot: filepath.Join(root, "cachemounts"),
}
return opt, nil
}
1 change: 1 addition & 0 deletions worker/runc/runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func NewWorkerOpt(root string, snFactory SnapshotterFactory, rootless bool, proc
LeaseManager: lm,
GarbageCollect: mdb.GarbageCollect,
ParallelismSem: parallelismSem,
MountPoolRoot: filepath.Join(root, "cachemounts"),
}
return opt, nil
}

0 comments on commit f5a831c

Please sign in to comment.