Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/cache: configurable backend preference #1424

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type backend interface {
}

type CacheOptions struct {
Log *logrus.Entry
Log *logrus.Entry
Format string
}

func WithLog(log *logrus.Entry) CacheOption {
Expand All @@ -60,6 +61,12 @@ func WithLog(log *logrus.Entry) CacheOption {
}
}

func WithFormat(format string) CacheOption {
return func(o *CacheOptions) {
o.Format = format
}
}

type CacheOption func(*CacheOptions)

// New creates a new Cache. It chooses a cache implementation based
Expand All @@ -73,7 +80,7 @@ func New(cacheDir string, cacheOpts ...CacheOption) (Cache, error) {
for _, opt := range cacheOpts {
opt(opts)
}
cacheBackend, err := getDefaultBackend(cacheDir, opts.Log)
cacheBackend, err := getBackend(cacheDir, opts.Format, opts.Log)
if err != nil {
return nil, err
}
Expand All @@ -84,7 +91,7 @@ func New(cacheDir string, cacheOpts ...CacheOption) (Cache, error) {
return &cache{backend: cacheBackend, log: opts.Log}, nil
}

func getDefaultBackend(cacheDir string, log *logrus.Entry) (backend, error) {
func getBackend(cacheDir string, backendName string, log *logrus.Entry) (backend, error) {
entries, err := os.ReadDir(cacheDir)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("detect cache format: read cache directory: %v", err)
Expand All @@ -96,12 +103,21 @@ func getDefaultBackend(cacheDir string, log *logrus.Entry) (backend, error) {
}

if len(entries) == 0 {
log.WithField("backend", backends[0].Name()).Info("cache directory is empty, using preferred backend")
return backends[0], nil
if backendName == "" {
log.WithField("backend", backends[0].Name()).Info("cache directory is empty, using preferred backend")
return backends[0], nil
}
for _, b := range backends {
if b.Name() == backendName {
log.WithField("backend", backendName).Info("using preferred backend")
return b, nil
}
}
return nil, fmt.Errorf("preferred backend %q not found", backendName)
}

for _, backend := range backends {
if backend.IsCachePresent() {
if (backendName == "" || backend.Name() == backendName) && backend.IsCachePresent() {
log.WithField("backend", backend.Name()).Info("found existing cache contents")
return backend, nil
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,11 @@ func TestCache_ListPackages(t *testing.T) {
func genTestCaches(t *testing.T, fbcFS fs.FS) map[string]Cache {
t.Helper()

caches := map[string]Cache{
"json": &cache{backend: newJSONBackend(t.TempDir()), log: log.Null()},
"pogreb.v1": &cache{backend: newPogrebV1Backend(t.TempDir()), log: log.Null()},
caches := make(map[string]Cache)
for _, format := range []string{FormatJSON, FormatPogrebV1} {
c, err := New(t.TempDir(), WithFormat(format), WithLog(log.Null()))
require.NoError(t, err)
caches[format] = c
}

for _, c := range caches {
Expand Down
4 changes: 3 additions & 1 deletion pkg/cache/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ type jsonBackend struct {
bundles bundleKeys
}

const FormatJSON = "json"

func (q *jsonBackend) Name() string {
return "json"
return FormatJSON
}

func (q *jsonBackend) IsCachePresent() bool {
Expand Down
6 changes: 4 additions & 2 deletions pkg/cache/pogrebv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ func newPogrebV1Backend(baseDir string) *pogrebV1Backend {
}

const (
FormatPogrebV1 = "pogreb.v1"

pogrebV1CacheModeDir = 0770
pogrebV1CacheModeFile = 0660

pograbV1CacheDir = "pogreb.v1"
pograbV1CacheDir = FormatPogrebV1
pogrebDigestFile = pograbV1CacheDir + "/digest"
pogrebDbDir = pograbV1CacheDir + "/db"
)
Expand All @@ -46,7 +48,7 @@ type pogrebV1Backend struct {
}

func (q *pogrebV1Backend) Name() string {
return pograbV1CacheDir
return FormatPogrebV1
}

func (q *pogrebV1Backend) IsCachePresent() bool {
Expand Down