Skip to content

Commit 0ca6c97

Browse files
Make resolve and dq caches local
This makes the shamefully global caches no longer shamefully global if a cache is configured. This allows for better scoping of the respective caches when apko is used as a library.
1 parent 3af598f commit 0ca6c97

File tree

6 files changed

+35
-20
lines changed

6 files changed

+35
-20
lines changed

pkg/apk/apk/cache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ type cache struct {
145145
dir string
146146
offline bool
147147

148-
shared *Cache
148+
shared *Cache
149+
resolverCache *resolverCache
150+
disqualifyCache *disqualifyCache
149151
}
150152

151153
// client return an http.Client that knows how to read from and write to the cache

pkg/apk/apk/implementation.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,12 @@ func (a *APK) ResolveWorld(ctx context.Context) (toInstall []*RepositoryPackage,
610610
if err != nil {
611611
return toInstall, conflicts, fmt.Errorf("error getting world packages: %w", err)
612612
}
613-
resolver := NewPkgResolver(ctx, indexes)
613+
614+
resolverCache := globalResolverCache
615+
if a.cache != nil {
616+
resolverCache = a.cache.resolverCache
617+
}
618+
resolver := resolverCache.get(ctx, indexes)
614619

615620
// For other architectures we're building (if any), we want to disqualify any packages not present in all archs.
616621
allArchs := map[string][]NamedIndex{}
@@ -622,7 +627,13 @@ func (a *APK) ResolveWorld(ctx context.Context) (toInstall []*RepositoryPackage,
622627
allArchs[otherArch] = indexes
623628
}
624629

625-
toInstall, conflicts, err = resolver.GetPackagesWithDependencies(ctx, directPkgs, allArchs)
630+
dqCache := globalDisqualifyCache
631+
if a.cache != nil {
632+
dqCache = a.cache.disqualifyCache
633+
}
634+
dq := dqCache.get(ctx, allArchs, resolverCache)
635+
636+
toInstall, conflicts, err = resolver.GetPackagesWithDependencies(ctx, directPkgs, dq)
626637
if err != nil {
627638
return
628639
}

pkg/apk/apk/options.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ func WithCache(cacheDir string, offline bool, shared *Cache) Option {
103103
}
104104
}
105105
o.cache = &cache{
106-
dir: cacheDir,
107-
offline: offline,
108-
shared: shared,
106+
dir: cacheDir,
107+
offline: offline,
108+
shared: shared,
109+
resolverCache: &resolverCache{},
110+
disqualifyCache: &disqualifyCache{},
109111
}
110112
return nil
111113
}

pkg/apk/apk/repo.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,6 @@ func (p *PkgResolver) Clone() *PkgResolver {
221221
// NewPkgResolver creates a new pkgResolver from a list of indexes.
222222
// The indexes are anything that implements NamedIndex.
223223
func NewPkgResolver(ctx context.Context, indexes []NamedIndex) *PkgResolver {
224-
return globalResolverCache.Get(ctx, indexes)
225-
}
226-
227-
func newPkgResolver(ctx context.Context, indexes []NamedIndex) *PkgResolver {
228224
_, span := otel.Tracer("go-apk").Start(ctx, "NewPkgResolver")
229225
defer span.End()
230226

@@ -485,12 +481,15 @@ func (p *PkgResolver) constrain(constraints []string, dq map[*RepositoryPackage]
485481

486482
// GetPackagesWithDependencies get all of the dependencies for the given packages based on the
487483
// indexes. Does not filter for installed already or not.
488-
func (p *PkgResolver) GetPackagesWithDependencies(ctx context.Context, packages []string, allArchs map[string][]NamedIndex) (toInstall []*RepositoryPackage, conflicts []string, err error) {
484+
func (p *PkgResolver) GetPackagesWithDependencies(ctx context.Context, packages []string, dq map[*RepositoryPackage]string) (toInstall []*RepositoryPackage, conflicts []string, err error) {
489485
_, span := otel.Tracer("go-apk").Start(ctx, "GetPackagesWithDependencies")
490486
defer span.End()
491487

492-
// Tracks all the packages we have disqualified and the reason we disqualified them.
493-
dq := globalDisqualifyCache.Get(ctx, allArchs)
488+
if dq == nil {
489+
// Make sure we have a map to avoid panics. The code below mutates this map, so we need
490+
// it to not be nil in all cases.
491+
dq = map[*RepositoryPackage]string{}
492+
}
494493

495494
// We're going to mutate this as our set of input packages to install, so make a copy.
496495
constraints := slices.Clone(packages)
@@ -1136,7 +1135,7 @@ func maybedqerror(pkgs []*repositoryPackage, dq map[*RepositoryPackage]string) e
11361135
return errors.New("not in indexes")
11371136
}
11381137

1139-
func disqualifyDifference(ctx context.Context, byArch map[string][]NamedIndex) map[*RepositoryPackage]string {
1138+
func disqualifyDifference(ctx context.Context, byArch map[string][]NamedIndex, resolverCache *resolverCache) map[*RepositoryPackage]string {
11401139
dq := map[*RepositoryPackage]string{}
11411140

11421141
if len(byArch) == 1 {
@@ -1164,7 +1163,7 @@ func disqualifyDifference(ctx context.Context, byArch map[string][]NamedIndex) m
11641163
}
11651164

11661165
for arch := range allowablePackages {
1167-
p := globalResolverCache.Get(ctx, byArch[arch])
1166+
p := resolverCache.get(ctx, byArch[arch])
11681167
for otherArch, allowed := range allowablePackages {
11691168
if otherArch == arch {
11701169
continue

pkg/apk/apk/repo_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,9 @@ func TestDisqualifyingOtherArchitectures(t *testing.T) {
10261026
"x86_64": testNamedRepositoryFromIndexes(index),
10271027
"aarch64": armIndex,
10281028
}
1029+
dq := disqualifyDifference(context.Background(), byArch, globalResolverCache)
10291030

10301031
resolver := NewPkgResolver(context.Background(), armIndex)
1031-
_, _, err := resolver.GetPackagesWithDependencies(context.Background(), names, byArch)
1032+
_, _, err := resolver.GetPackagesWithDependencies(context.Background(), names, dq)
10321033
require.ErrorContains(t, err, "package \"onlyinarm64-1.0.0.apk\" not available for arch \"x86_64\"")
10331034
}

pkg/apk/apk/shameful_global_caches.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ func (r *resolverCache) fill(indexes []NamedIndex, pr *PkgResolver) {
6868
child.fill(indexes[1:], pr)
6969
}
7070

71-
func (r *resolverCache) Get(ctx context.Context, indexes []NamedIndex) *PkgResolver {
71+
func (r *resolverCache) get(ctx context.Context, indexes []NamedIndex) *PkgResolver {
7272
r.Lock()
7373
defer r.Unlock()
7474

7575
if pr := r.find(indexes); pr != nil {
7676
return pr.Clone()
7777
}
7878

79-
pr := newPkgResolver(ctx, indexes)
79+
pr := NewPkgResolver(ctx, indexes)
8080
r.fill(indexes, pr)
8181

8282
return pr.Clone()
@@ -130,7 +130,7 @@ func (r *disqualifyCache) fill(indexes []NamedIndex, dq map[*RepositoryPackage]s
130130

131131
// It is expensive to compute the difference between every architecture.
132132
// This caches that difference based on the input []NamedIndex for every architecture.
133-
func (r *disqualifyCache) Get(ctx context.Context, byArch map[string][]NamedIndex) map[*RepositoryPackage]string {
133+
func (r *disqualifyCache) get(ctx context.Context, byArch map[string][]NamedIndex, resolverCache *resolverCache) map[*RepositoryPackage]string {
134134
r.Lock()
135135
defer r.Unlock()
136136

@@ -142,7 +142,7 @@ func (r *disqualifyCache) Get(ctx context.Context, byArch map[string][]NamedInde
142142
return maps.Clone(dq)
143143
}
144144

145-
dq := disqualifyDifference(ctx, byArch)
145+
dq := disqualifyDifference(ctx, byArch, resolverCache)
146146
r.fill(indexes, dq)
147147

148148
return maps.Clone(dq)

0 commit comments

Comments
 (0)