Skip to content

Commit

Permalink
remove complex blob cache GC code
Browse files Browse the repository at this point in the history
Containers can contorl if a committed snapshot should
be removed. We don't have to consider if snapshot belongs
to multiple images.

Just respond to containerd a committed snapshot shoud be remove.

Signed-off-by: Changwei Ge <[email protected]>
  • Loading branch information
changweige committed Nov 10, 2022
1 parent e53fe6a commit b707479
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 468 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/docker/cli v20.10.0-beta1.0.20201029214301-1d20b15adc38+incompatible
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/google/go-containerregistry v0.5.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-retryablehttp v0.7.1
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799
Expand Down Expand Up @@ -96,10 +97,10 @@ require (
)

retract (
v0.11.0
v0.11.1
v0.11.2
v0.11.3
v0.11.2
v0.11.1
v0.11.0
)

replace (
Expand Down
13 changes: 0 additions & 13 deletions pkg/cache/db.go

This file was deleted.

98 changes: 35 additions & 63 deletions pkg/cache/manager.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Copyright (c) 2022. Nydus Developers. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

package cache

import (
Expand All @@ -11,13 +17,17 @@ import (
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/snapshots"
"github.com/containerd/continuity/fs"
"github.com/containerd/nydus-snapshotter/config"
"github.com/containerd/nydus-snapshotter/pkg/store"
)

const (
chunkMapFileSuffix = ".chunk_map"
metaFileSuffix = ".blob.meta"
// Blob cache is suffixed after nydus v2.1
dataFileSuffix = ".blob.data"
)

type Manager struct {
db DB
store *Store
cacheDir string
period time.Duration
eventCh chan struct{}
Expand All @@ -37,81 +47,21 @@ func NewManager(opt Opt) (*Manager, error) {
return nil, errors.Wrapf(err, "failed to create cache dir %s", opt.CacheDir)
}

db, err := store.NewCacheStore(opt.Database)
if err != nil {
return nil, err
}
s := NewStore(opt.CacheDir)

eventCh := make(chan struct{})
m := &Manager{
db: db,
store: s,
cacheDir: opt.CacheDir,
period: opt.Period,
eventCh: eventCh,
fsDriver: opt.FsDriver,
}

// For fscache backend, the cache is maintained by the kernel fscache module,
// so here we ignore gc for now, and in the future we need another design
// to remove the cache.
if opt.FsDriver == config.FsDriverFscache {
return m, nil
}

go m.runGC()
log.L.Info("gc goroutine start...")

return m, nil
}

func (m *Manager) CacheDir() string {
return m.cacheDir
}

func (m *Manager) SchedGC() {
if m.fsDriver == config.FsDriverFscache {
return
}
m.eventCh <- struct{}{}
}

func (m *Manager) runGC() {
tick := time.NewTicker(m.period)
defer tick.Stop()
for {
select {
case <-m.eventCh:
if err := m.gc(); err != nil {
log.L.Infof("[event] cache gc err, %v", err)
}
tick.Reset(m.period)
case <-tick.C:
if err := m.gc(); err != nil {
log.L.Infof("[tick] cache gc err, %v", err)
}
}
}
}

func (m *Manager) gc() error {
delBlobs, err := m.db.GC(m.store.DelBlob)
if err != nil {
return errors.Wrapf(err, "cache gc err")
}
log.L.Debugf("remove %d unused blobs successfully", len(delBlobs))
return nil
}

func (m *Manager) AddSnapshot(imageID string, blobs []string) error {
return m.db.AddSnapshot(imageID, blobs)
}

func (m *Manager) DelSnapshot(imageID string) error {
return m.db.DelSnapshot(imageID)
}

// Report each blob disk usage
func (m *Manager) CacheUsage(ctx context.Context, blobID string) (snapshots.Usage, error) {
var usage snapshots.Usage
Expand All @@ -136,3 +86,25 @@ func (m *Manager) CacheUsage(ctx context.Context, blobID string) (snapshots.Usag

return usage, nil
}

func (m *Manager) RemoveBlobCache(blobID string) error {
blobCachePath := path.Join(m.cacheDir, blobID)
blobCacheSuffixedPath := path.Join(m.cacheDir, blobID+dataFileSuffix)
blobChunkMap := path.Join(m.cacheDir, blobID+chunkMapFileSuffix)
blobMeta := path.Join(m.cacheDir, blobID+metaFileSuffix)

// NOTE: Delete chunk bitmap file before data blob
stuffs := []string{blobChunkMap, blobMeta, blobCachePath, blobCacheSuffixedPath}

for _, f := range stuffs {
err := os.Remove(f)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.L.Debugf("file %s doest not exist.", f)
continue
}
return err
}
}
return nil
}
51 changes: 0 additions & 51 deletions pkg/cache/store.go

This file was deleted.

37 changes: 9 additions & 28 deletions pkg/filesystem/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,15 @@ func (fs *Filesystem) CacheUsage(ctx context.Context, blobDigest string) (snapsh
return fs.cacheMgr.CacheUsage(ctx, blobID)
}

func (fs *Filesystem) RemoveCache(blobDigest string) error {
digest := digest.Digest(blobDigest)
if err := digest.Validate(); err != nil {
return errors.Wrapf(err, "invalid blob digest from label %s", label.CRILayerDigest)
}
blobID := digest.Hex()
return fs.cacheMgr.RemoveBlobCache(blobID)
}

// WaitUntilReady wait until daemon ready by snapshotID, it will wait until nydus domain socket established
// and the status of nydusd daemon must be ready
func (fs *Filesystem) WaitUntilReady(snapshotID string) error {
Expand All @@ -597,19 +606,6 @@ func (fs *Filesystem) WaitUntilReady(snapshotID string) error {
return d.WaitUntilReady()
}

func (fs *Filesystem) DelSnapshot(imageID string) error {
if fs.cacheMgr == nil {
return nil
}

if err := fs.cacheMgr.DelSnapshot(imageID); err != nil {
return errors.Wrap(err, "del snapshot err")
}
log.L.Debugf("remove snapshot %s\n", imageID)
fs.cacheMgr.SchedGC()
return nil
}

func (fs *Filesystem) Umount(ctx context.Context, mountPoint string) error {
if !fs.hasDaemon() {
return nil
Expand Down Expand Up @@ -709,21 +705,6 @@ func (fs *Filesystem) mount(d *daemon.Daemon, labels map[string]string) error {
return nil
}

func (fs *Filesystem) AddSnapshot(labels map[string]string) error {
// Do nothing if there's no cacheMgr
if fs.cacheMgr == nil {
return nil
}

imageID, _ := registry.ParseLabels(labels)
blobs, err := fs.getBlobIDs(labels)
if err != nil {
return err
}
log.L.Infof("image %s with blob caches %v", imageID, blobs)
return fs.cacheMgr.AddSnapshot(imageID, blobs)
}

// 1. Create a daemon instance
// 2. Build command line
// 3. Start daemon
Expand Down
92 changes: 0 additions & 92 deletions pkg/store/cachestore.go

This file was deleted.

Loading

0 comments on commit b707479

Please sign in to comment.