Skip to content

Commit

Permalink
internal/dnfjson: fix caching bug with multidir caches
Browse files Browse the repository at this point in the history
Part of the fix for COMPOSER-2068
  • Loading branch information
diaasami committed Nov 9, 2023
1 parent 98f2882 commit bc27278
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 10 additions & 3 deletions internal/dnfjson/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func newRPMCache(path string, maxSize uint64) *rpmCache {
// later use of the cache. eg. touchRepo
func (r *rpmCache) updateInfo() {
dirs, _ := os.ReadDir(r.root)
// reset to make sure accumulation works properly
r.size = 0
r.repoElements = make(map[string]pathInfo)
r.repoRecency = nil
for _, d := range dirs {
r.updateCacheDirInfo(filepath.Join(r.root, d.Name()))
}
Expand Down Expand Up @@ -186,9 +190,12 @@ func (r *rpmCache) updateCacheDirInfo(path string) {
// sort IDs by mtime (oldest first)
sort.Slice(repoIDs, sortFunc)

r.size = totalSize
r.repoElements = repos
r.repoRecency = repoIDs
r.size += totalSize
// TODO: replace loop with maps.Copy() introduced in Go 1.21
for k, v := range repos {
r.repoElements[k] = v
}
copy(r.repoRecency, repoIDs)
}

func (r *rpmCache) shrink() error {
Expand Down
18 changes: 18 additions & 0 deletions internal/dnfjson/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ func TestCacheRead(t *testing.T) {
}
}

func TestMultiDirCacheRead(t *testing.T) {
assert := assert.New(t)
t.Run("MultiDir", func(t *testing.T) {
testCacheRoot := t.TempDir()
// Cache is now per-distro, use the name of the config as a distro name
size1 := createTestCache(filepath.Join(testCacheRoot, "rhel84-aarch64"), testCfgs["rhel84-aarch64"])
size2 := createTestCache(filepath.Join(testCacheRoot, "fake-real"), testCfgs["fake-real"])

// Cache covers all distros, pass in top directory
cache := newRPMCache(testCacheRoot, 1048576) // 1 MiB, but doesn't matter for this test

nrepos := len(getRepoIDs(testCfgs["rhel84-aarch64"])) + len(getRepoIDs(testCfgs["fake-real"]))
assert.Equal(size1+size2, cache.size)
assert.Equal(nrepos, len(cache.repoElements))
assert.Equal(nrepos, len(cache.repoRecency))
})
}

func sizeSum(cfg testCache, repoIDFilter ...string) uint64 {
var sum uint64
for path, info := range cfg {
Expand Down

0 comments on commit bc27278

Please sign in to comment.