2018-04-13 20:52:27 +00:00
|
|
|
package solver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type exporter struct {
|
|
|
|
k *CacheKey
|
|
|
|
records []*CacheRecord
|
|
|
|
record *CacheRecord
|
|
|
|
|
2018-05-07 23:17:03 +00:00
|
|
|
res []CacheExporterRecord
|
|
|
|
edge *edge // for secondaryExporters
|
|
|
|
override *bool
|
2018-04-13 20:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 21:19:02 +00:00
|
|
|
func addBacklinks(t CacheExporterTarget, rec CacheExporterRecord, cm *cacheManager, id string, bkm map[string]CacheExporterRecord) (CacheExporterRecord, error) {
|
2018-04-13 20:52:27 +00:00
|
|
|
if rec == nil {
|
|
|
|
var ok bool
|
|
|
|
rec, ok = bkm[id]
|
2020-02-28 06:36:13 +00:00
|
|
|
if ok && rec != nil {
|
2018-04-13 20:52:27 +00:00
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
_ = ok
|
|
|
|
}
|
2020-02-28 06:36:13 +00:00
|
|
|
bkm[id] = nil
|
2018-04-13 20:52:27 +00:00
|
|
|
if err := cm.backend.WalkBacklinks(id, func(id string, link CacheInfoLink) error {
|
|
|
|
if rec == nil {
|
|
|
|
rec = t.Add(link.Digest)
|
|
|
|
}
|
|
|
|
r, ok := bkm[id]
|
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
r, err = addBacklinks(t, nil, cm, id, bkm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 06:36:13 +00:00
|
|
|
if r != nil {
|
|
|
|
rec.LinkFrom(r, int(link.Input), link.Selector.String())
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rec == nil {
|
|
|
|
rec = t.Add(digest.Digest(id))
|
|
|
|
}
|
|
|
|
bkm[id] = rec
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type backlinkT struct{}
|
|
|
|
|
|
|
|
var backlinkKey = backlinkT{}
|
|
|
|
|
2018-05-07 21:19:02 +00:00
|
|
|
func (e *exporter) ExportTo(ctx context.Context, t CacheExporterTarget, opt CacheExportOpt) ([]CacheExporterRecord, error) {
|
|
|
|
var bkm map[string]CacheExporterRecord
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
if bk := ctx.Value(backlinkKey); bk == nil {
|
2018-05-07 21:19:02 +00:00
|
|
|
bkm = map[string]CacheExporterRecord{}
|
2018-04-13 20:52:27 +00:00
|
|
|
ctx = context.WithValue(ctx, backlinkKey, bkm)
|
|
|
|
} else {
|
2018-05-07 21:19:02 +00:00
|
|
|
bkm = bk.(map[string]CacheExporterRecord)
|
2018-04-13 20:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if t.Visited(e) {
|
|
|
|
return e.res, nil
|
|
|
|
}
|
2020-02-28 06:36:13 +00:00
|
|
|
t.Visit(e)
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
deps := e.k.Deps()
|
|
|
|
|
|
|
|
type expr struct {
|
2018-05-07 21:19:02 +00:00
|
|
|
r CacheExporterRecord
|
2018-04-13 20:52:27 +00:00
|
|
|
selector digest.Digest
|
|
|
|
}
|
|
|
|
|
2018-05-07 23:17:03 +00:00
|
|
|
rec := t.Add(rootKey(e.k.Digest(), e.k.Output()))
|
|
|
|
allRec := []CacheExporterRecord{rec}
|
|
|
|
|
|
|
|
addRecord := true
|
|
|
|
|
|
|
|
if e.override != nil {
|
|
|
|
addRecord = *e.override
|
|
|
|
}
|
|
|
|
|
2018-05-08 00:11:42 +00:00
|
|
|
if e.record == nil && len(e.k.Deps()) > 0 {
|
|
|
|
e.record = getBestResult(e.records)
|
|
|
|
}
|
|
|
|
|
2018-05-07 23:17:03 +00:00
|
|
|
var remote *Remote
|
|
|
|
if v := e.record; v != nil && len(e.k.Deps()) > 0 && addRecord {
|
|
|
|
cm := v.cacheManager
|
|
|
|
key := cm.getID(v.key)
|
|
|
|
res, err := cm.backend.Load(key, v.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remote, err = cm.results.LoadRemote(ctx, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if remote == nil && opt.Mode != CacheExportModeRemoteOnly {
|
|
|
|
res, err := cm.results.Load(ctx, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
remote, err = opt.Convert(ctx, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Release(context.TODO())
|
|
|
|
}
|
|
|
|
|
|
|
|
if remote != nil {
|
|
|
|
for _, rec := range allRec {
|
|
|
|
rec.AddResult(v.CreatedAt, remote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if remote != nil && opt.Mode == CacheExportModeMin {
|
|
|
|
opt.Mode = CacheExportModeRemoteOnly
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
srcs := make([][]expr, len(deps))
|
|
|
|
|
|
|
|
for i, deps := range deps {
|
|
|
|
for _, dep := range deps {
|
2018-05-07 21:19:02 +00:00
|
|
|
recs, err := dep.CacheKey.Exporter.ExportTo(ctx, t, opt)
|
2018-04-13 20:52:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
for _, r := range recs {
|
|
|
|
srcs[i] = append(srcs[i], expr{r: r, selector: dep.Selector})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.edge != nil {
|
|
|
|
for _, de := range e.edge.secondaryExporters {
|
2018-05-07 21:19:02 +00:00
|
|
|
recs, err := de.cacheKey.CacheKey.Exporter.ExportTo(ctx, t, opt)
|
2018-04-13 20:52:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
for _, r := range recs {
|
|
|
|
srcs[de.index] = append(srcs[de.index], expr{r: r, selector: de.cacheKey.Selector})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, srcs := range srcs {
|
|
|
|
for _, src := range srcs {
|
|
|
|
rec.LinkFrom(src.r, i, src.selector.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for cm, id := range e.k.ids {
|
|
|
|
if _, err := addBacklinks(t, rec, cm, id, bkm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:01:50 +00:00
|
|
|
if v := e.record; v != nil && len(deps) == 0 {
|
|
|
|
cm := v.cacheManager
|
|
|
|
key := cm.getID(v.key)
|
|
|
|
if err := cm.backend.WalkIDsByResult(v.ID, func(id string) error {
|
|
|
|
if id == key {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
allRec = append(allRec, t.Add(digest.Digest(id)))
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.res = allRec
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
return e.res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBestResult(records []*CacheRecord) *CacheRecord {
|
|
|
|
var rec *CacheRecord
|
|
|
|
for _, r := range records {
|
|
|
|
if rec == nil || rec.CreatedAt.Before(r.CreatedAt) || (rec.CreatedAt.Equal(r.CreatedAt) && rec.Priority < r.Priority) {
|
|
|
|
rec = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
|
|
|
type mergedExporter struct {
|
2018-05-07 21:19:02 +00:00
|
|
|
exporters []CacheExporter
|
2018-04-13 20:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 21:19:02 +00:00
|
|
|
func (e *mergedExporter) ExportTo(ctx context.Context, t CacheExporterTarget, opt CacheExportOpt) (er []CacheExporterRecord, err error) {
|
2018-04-13 20:52:27 +00:00
|
|
|
for _, e := range e.exporters {
|
2018-05-07 21:19:02 +00:00
|
|
|
r, err := e.ExportTo(ctx, t, opt)
|
2018-04-13 20:52:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
er = append(er, r...)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|