remotecache: Only visit each item once when walking results.

Before this change, walkAllResults did not skip an item if it had already been
visited, so walking the graph of results actually followed every single possible
path in the graph instead of just visiting each item once.

On small graphs this wasn't noticeable, but on sufficiently large remote cache
imports this rapidly scaled out of control. For example, I first encountered
this when importing a max-cache from a build of a full linux rootfs from source
(which takes about 30 minutes to build w/out cache on an 18-core machine) and
after 30 minutes the cache import was still running with all CPUs pegged at 100%

To fix this, walkAllResults now keeps a map of already visited items (keyed by
their pointer) and skips visiting an item if it's already been visited, making
it usable on large remote cache imports.

Signed-off-by: Erik Sipsma <erik@sipsma.dev>
v0.8
Erik Sipsma 2020-07-16 16:53:05 -07:00
parent 919ee62a78
commit 806fddd7f7
2 changed files with 8 additions and 3 deletions

View File

@ -220,6 +220,7 @@ func (cs *cacheResultStorage) LoadWithParents(ctx context.Context, res solver.Ca
m := map[string]solver.Result{} m := map[string]solver.Result{}
visited := make(map[*item]struct{})
if err := v.walkAllResults(func(i *item) error { if err := v.walkAllResults(func(i *item) error {
if i.result == nil { if i.result == nil {
return nil return nil
@ -236,7 +237,7 @@ func (cs *cacheResultStorage) LoadWithParents(ctx context.Context, res solver.Ca
m[id] = worker.NewWorkerRefResult(ref, cs.w) m[id] = worker.NewWorkerRefResult(ref, cs.w)
} }
return nil return nil
}); err != nil { }, visited); err != nil {
for _, v := range m { for _, v := range m {
v.Release(context.TODO()) v.Release(context.TODO())
} }

View File

@ -128,13 +128,17 @@ func (c *item) LinkFrom(rec solver.CacheExporterRecord, index int, selector stri
c.links[index][link{src: src, selector: selector}] = struct{}{} c.links[index][link{src: src, selector: selector}] = struct{}{}
} }
func (c *item) walkAllResults(fn func(i *item) error) error { func (c *item) walkAllResults(fn func(i *item) error, visited map[*item]struct{}) error {
if _, ok := visited[c]; ok {
return nil
}
visited[c] = struct{}{}
if err := fn(c); err != nil { if err := fn(c); err != nil {
return err return err
} }
for _, links := range c.links { for _, links := range c.links {
for l := range links { for l := range links {
if err := l.src.walkAllResults(fn); err != nil { if err := l.src.walkAllResults(fn, visited); err != nil {
return err return err
} }
} }