2018-02-21 19:01:43 +00:00
|
|
|
package solver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/identity"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
type internalMemoryKeyT string
|
|
|
|
|
|
|
|
var internalMemoryKey = internalMemoryKeyT("buildkit/memory-cache-id")
|
|
|
|
|
|
|
|
var NoSelector = digest.FromBytes(nil)
|
|
|
|
|
|
|
|
func NewInMemoryCacheManager() CacheManager {
|
2018-02-27 01:39:18 +00:00
|
|
|
return NewCacheManager(identity.NewID(), NewInMemoryCacheStorage(), NewInMemoryResultStorage())
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCacheManager(id string, storage CacheKeyStorage, results CacheResultStorage) CacheManager {
|
|
|
|
cm := &inMemoryCacheManager{
|
|
|
|
id: id,
|
|
|
|
backend: storage,
|
|
|
|
results: results,
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
2018-02-27 01:39:18 +00:00
|
|
|
|
|
|
|
storage.Walk(func(id string) error {
|
|
|
|
return storage.WalkResults(id, func(cr CacheResult) error {
|
|
|
|
if !results.Exists(cr.ID) {
|
|
|
|
storage.Release(cr.ID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return cm
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type inMemoryCacheKey struct {
|
2018-03-17 04:07:18 +00:00
|
|
|
manager *inMemoryCacheManager
|
|
|
|
cacheResult CacheResult
|
|
|
|
deps []CacheKeyWithSelector // only []*inMemoryCacheKey
|
|
|
|
digest digest.Digest
|
|
|
|
output Index
|
|
|
|
id string
|
2018-02-21 19:01:43 +00:00
|
|
|
CacheKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ck *inMemoryCacheKey) Deps() []CacheKeyWithSelector {
|
|
|
|
return ck.deps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ck *inMemoryCacheKey) Digest() digest.Digest {
|
2018-03-17 04:07:18 +00:00
|
|
|
return ck.digest
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
func (ck *inMemoryCacheKey) Output() Index {
|
2018-03-17 04:07:18 +00:00
|
|
|
return ck.output
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
func withExporter(ck *inMemoryCacheKey, cacheResult *CacheResult, deps []CacheKeyWithSelector) ExportableCacheKey {
|
2018-02-21 19:01:43 +00:00
|
|
|
return ExportableCacheKey{ck, &cacheExporter{
|
|
|
|
inMemoryCacheKey: ck,
|
|
|
|
cacheResult: cacheResult,
|
2018-03-16 20:34:06 +00:00
|
|
|
deps: deps,
|
2018-02-21 19:01:43 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
type cacheExporter struct {
|
|
|
|
*inMemoryCacheKey
|
|
|
|
cacheResult *CacheResult
|
2018-03-16 20:34:06 +00:00
|
|
|
deps []CacheKeyWithSelector
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ce *cacheExporter) Export(ctx context.Context, m map[digest.Digest]*ExportRecord, converter func(context.Context, Result) (*Remote, error)) (*ExportRecord, error) {
|
|
|
|
var res Result
|
|
|
|
if ce.cacheResult == nil {
|
2018-03-17 04:07:18 +00:00
|
|
|
cr, err := ce.inMemoryCacheKey.manager.getBestResult(ce.inMemoryCacheKey.id)
|
2018-02-21 19:01:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ce.cacheResult = cr
|
|
|
|
}
|
|
|
|
|
|
|
|
var remote *Remote
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if ce.cacheResult != nil {
|
|
|
|
remote, err = ce.inMemoryCacheKey.manager.results.LoadRemote(ctx, *ce.cacheResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if remote == nil {
|
|
|
|
res, err = ce.inMemoryCacheKey.manager.results.Load(ctx, *ce.cacheResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if res != nil && remote == nil {
|
|
|
|
remote, err = converter(ctx, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
cacheID := digest.FromBytes([]byte(ce.inMemoryCacheKey.id))
|
2018-02-21 19:01:43 +00:00
|
|
|
if remote != nil && len(remote.Descriptors) > 0 && remote.Descriptors[0].Digest != "" {
|
|
|
|
cacheID = remote.Descriptors[0].Digest
|
|
|
|
}
|
|
|
|
|
|
|
|
rec, ok := m[cacheID]
|
|
|
|
if !ok {
|
|
|
|
rec = &ExportRecord{
|
|
|
|
Digest: cacheID,
|
|
|
|
Remote: remote,
|
|
|
|
Links: make(map[CacheLink]struct{}),
|
|
|
|
}
|
|
|
|
m[cacheID] = rec
|
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
if len(ce.Deps()) == 0 {
|
2018-02-21 19:01:43 +00:00
|
|
|
rec.Links[CacheLink{
|
|
|
|
Output: ce.Output(),
|
|
|
|
Base: ce.Digest(),
|
|
|
|
}] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
for i, dep := range ce.deps {
|
|
|
|
if dep.CacheKey.Exporter == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-02-21 19:01:43 +00:00
|
|
|
r, err := dep.CacheKey.Export(ctx, m, converter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
link := CacheLink{
|
|
|
|
Source: r.Digest,
|
|
|
|
Input: Index(i),
|
|
|
|
Output: ce.Output(),
|
|
|
|
Base: ce.Digest(),
|
|
|
|
Selector: dep.Selector,
|
|
|
|
}
|
|
|
|
rec.Links[link] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type inMemoryCacheManager struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
id string
|
|
|
|
|
|
|
|
backend CacheKeyStorage
|
|
|
|
results CacheResultStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *inMemoryCacheManager) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
func (c *inMemoryCacheManager) toInMemoryCacheKey(id string, dgst digest.Digest, output Index, deps []CacheKeyWithSelector) *inMemoryCacheKey {
|
2018-03-16 20:34:06 +00:00
|
|
|
ck := &inMemoryCacheKey{
|
2018-03-17 04:07:18 +00:00
|
|
|
id: id,
|
|
|
|
output: output,
|
|
|
|
digest: dgst,
|
|
|
|
manager: c,
|
|
|
|
CacheKey: NewCacheKey("", 0, nil),
|
|
|
|
deps: deps,
|
|
|
|
}
|
|
|
|
ck.SetValue(internalMemoryKey, id)
|
2018-03-16 20:34:06 +00:00
|
|
|
return ck
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
func (c *inMemoryCacheManager) getBestResult(id string) (*CacheResult, error) {
|
2018-02-21 19:01:43 +00:00
|
|
|
var results []*CacheResult
|
2018-03-17 04:07:18 +00:00
|
|
|
if err := c.backend.WalkResults(id, func(res CacheResult) error {
|
2018-02-21 19:01:43 +00:00
|
|
|
results = append(results, &res)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(results, func(i, j int) bool {
|
2018-02-27 01:39:18 +00:00
|
|
|
return results[i].CreatedAt.After(results[j].CreatedAt)
|
2018-02-21 19:01:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if len(results) > 0 {
|
|
|
|
return results[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
func (c *inMemoryCacheManager) Query(deps []CacheKeyWithSelector, input Index, dgst digest.Digest, output Index) ([]*CacheRecord, error) {
|
2018-02-21 19:01:43 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
type dep struct {
|
|
|
|
results map[string]struct{}
|
|
|
|
key CacheKeyWithSelector
|
|
|
|
internalKey *inMemoryCacheKey
|
|
|
|
}
|
2018-02-21 19:01:43 +00:00
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
formatDeps := func(deps []dep, index int) []CacheKeyWithSelector {
|
|
|
|
keys := make([]CacheKeyWithSelector, index+1)
|
|
|
|
if len(deps) == 1 {
|
|
|
|
keys[index] = deps[0].key
|
2018-03-01 19:09:21 +00:00
|
|
|
} else {
|
2018-03-16 20:34:06 +00:00
|
|
|
k2 := make([]CacheKeyWithSelector, 0, len(deps))
|
|
|
|
for _, d := range deps {
|
|
|
|
k2 = append(k2, d.key)
|
|
|
|
}
|
|
|
|
keys[index] = CacheKeyWithSelector{CacheKey: ExportableCacheKey{CacheKey: NewCacheKey("", 0, k2)}}
|
2018-03-01 19:09:21 +00:00
|
|
|
}
|
2018-03-16 20:34:06 +00:00
|
|
|
return keys
|
|
|
|
}
|
2018-02-21 19:01:43 +00:00
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
allDeps := make([]dep, 0, len(deps))
|
2018-03-01 19:09:21 +00:00
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
for _, d := range deps {
|
|
|
|
for _, k := range c.getAllKeys(d) {
|
|
|
|
d := dep{key: k, results: map[string]struct{}{}}
|
|
|
|
internalKey, err := c.getInternalKey(k.CacheKey, false)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == ErrNotFound {
|
|
|
|
allDeps = append(allDeps, d)
|
|
|
|
} else {
|
2018-03-01 19:09:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-16 20:34:06 +00:00
|
|
|
} else {
|
|
|
|
d.internalKey = internalKey
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
2018-03-16 20:34:06 +00:00
|
|
|
allDeps = append(allDeps, d)
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
allRes := map[string]struct{}{}
|
|
|
|
for _, d := range allDeps {
|
|
|
|
if d.internalKey != nil {
|
2018-03-17 04:07:18 +00:00
|
|
|
if err := c.backend.WalkLinks(d.internalKey.id, CacheInfoLink{input, output, dgst, d.key.Selector}, func(id string) error {
|
2018-03-16 20:34:06 +00:00
|
|
|
d.results[id] = struct{}{}
|
|
|
|
allRes[id] = struct{}{}
|
2018-02-21 19:01:43 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(deps) == 0 {
|
2018-03-16 20:34:06 +00:00
|
|
|
allRes[digest.FromBytes([]byte(fmt.Sprintf("%s@%d", dgst, output))).String()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
outs := make([]*CacheRecord, 0, len(allRes))
|
|
|
|
|
|
|
|
for res := range allRes {
|
|
|
|
for _, d := range allDeps {
|
|
|
|
if d.internalKey == nil {
|
|
|
|
internalKey, err := c.getInternalKey(d.key.CacheKey, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.internalKey = internalKey
|
|
|
|
}
|
|
|
|
if _, ok := d.results[res]; !ok {
|
2018-03-17 04:07:18 +00:00
|
|
|
if err := c.backend.AddLink(d.internalKey.id, CacheInfoLink{
|
2018-03-16 20:34:06 +00:00
|
|
|
Input: input,
|
|
|
|
Output: output,
|
|
|
|
Digest: dgst,
|
|
|
|
Selector: d.key.Selector,
|
|
|
|
}, res); err != nil {
|
|
|
|
return nil, err
|
2018-03-01 19:09:21 +00:00
|
|
|
}
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-16 20:34:06 +00:00
|
|
|
hadResults := false
|
2018-02-21 19:01:43 +00:00
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
fdeps := formatDeps(allDeps, int(input))
|
|
|
|
k := c.toInMemoryCacheKey(res, dgst, output, fdeps)
|
2018-03-16 20:34:06 +00:00
|
|
|
// TODO: invoke this only once per input
|
|
|
|
if err := c.backend.WalkResults(res, func(r CacheResult) error {
|
|
|
|
if c.results.Exists(r.ID) {
|
2018-03-01 19:09:21 +00:00
|
|
|
outs = append(outs, &CacheRecord{
|
2018-03-16 20:34:06 +00:00
|
|
|
ID: res + "@" + r.ID,
|
2018-03-17 04:07:18 +00:00
|
|
|
CacheKey: withExporter(k, &r, fdeps),
|
2018-03-01 19:09:21 +00:00
|
|
|
CacheManager: c,
|
2018-03-16 20:34:06 +00:00
|
|
|
Loadable: true,
|
|
|
|
CreatedAt: r.CreatedAt,
|
2018-03-01 19:09:21 +00:00
|
|
|
})
|
2018-03-16 20:34:06 +00:00
|
|
|
hadResults = true
|
|
|
|
} else {
|
|
|
|
c.backend.Release(r.ID)
|
2018-03-01 19:09:21 +00:00
|
|
|
}
|
2018-03-16 20:34:06 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hadResults {
|
2018-03-17 04:07:18 +00:00
|
|
|
if len(deps) == 0 {
|
|
|
|
if !c.backend.Exists(res) {
|
2018-03-16 20:34:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outs = append(outs, &CacheRecord{
|
|
|
|
ID: res,
|
2018-03-17 04:07:18 +00:00
|
|
|
CacheKey: withExporter(k, nil, fdeps),
|
2018-03-16 20:34:06 +00:00
|
|
|
CacheManager: c,
|
|
|
|
Loadable: false,
|
|
|
|
})
|
2018-02-27 01:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 19:01:43 +00:00
|
|
|
return outs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *inMemoryCacheManager) Load(ctx context.Context, rec *CacheRecord) (Result, error) {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
|
|
|
keyParts := strings.Split(rec.ID, "@")
|
|
|
|
if len(keyParts) != 2 {
|
|
|
|
return nil, errors.Errorf("invalid cache record ID")
|
|
|
|
}
|
|
|
|
ck, err := c.getInternalKey(rec.CacheKey, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
res, err := c.backend.Load(ck.id, keyParts[1])
|
2018-02-21 19:01:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.results.Load(ctx, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *inMemoryCacheManager) Save(k CacheKey, r Result) (ExportableCacheKey, error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
empty := ExportableCacheKey{}
|
|
|
|
|
|
|
|
ck, err := c.getInternalKey(k, true)
|
|
|
|
if err != nil {
|
|
|
|
return empty, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := c.results.Save(r)
|
|
|
|
if err != nil {
|
|
|
|
return empty, err
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
if err := c.backend.AddResult(ck.id, res); err != nil {
|
2018-02-21 19:01:43 +00:00
|
|
|
return empty, err
|
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
return withExporter(ck, &res, ck.Deps()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *inMemoryCacheManager) getInternalKeys(d CacheKeyWithSelector, createIfNotExist bool) ([]CacheKeyWithSelector, error) {
|
|
|
|
keys := make([]CacheKeyWithSelector, 0, 1)
|
|
|
|
if d.CacheKey.Digest() == "" {
|
|
|
|
for _, d := range d.CacheKey.Deps() {
|
|
|
|
k, err := c.getInternalKey(d.CacheKey, createIfNotExist)
|
|
|
|
if err != nil {
|
|
|
|
if !createIfNotExist && errors.Cause(err) == ErrNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keys = append(keys, CacheKeyWithSelector{Selector: d.Selector, CacheKey: ExportableCacheKey{CacheKey: k, Exporter: d.CacheKey.Exporter}})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
k, err := c.getInternalKey(d.CacheKey, createIfNotExist)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keys = append(keys, CacheKeyWithSelector{Selector: d.Selector, CacheKey: ExportableCacheKey{CacheKey: k, Exporter: d.CacheKey.Exporter}})
|
|
|
|
}
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *inMemoryCacheManager) getAllKeys(d CacheKeyWithSelector) []CacheKeyWithSelector {
|
|
|
|
keys := make([]CacheKeyWithSelector, 0, 1)
|
|
|
|
if d.CacheKey.Digest() == "" {
|
|
|
|
for _, d := range d.CacheKey.Deps() {
|
|
|
|
keys = append(keys, d)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keys = append(keys, d)
|
|
|
|
}
|
|
|
|
return keys
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *inMemoryCacheManager) getInternalKey(k CacheKey, createIfNotExist bool) (*inMemoryCacheKey, error) {
|
|
|
|
if ck, ok := k.(ExportableCacheKey); ok {
|
|
|
|
k = ck.CacheKey
|
|
|
|
}
|
|
|
|
if ck, ok := k.(*inMemoryCacheKey); ok {
|
|
|
|
return ck, nil
|
|
|
|
}
|
|
|
|
internalV := k.GetValue(internalMemoryKey)
|
|
|
|
if internalV != nil {
|
2018-03-17 04:07:18 +00:00
|
|
|
return c.toInMemoryCacheKey(internalV.(string), k.Digest(), k.Output(), k.Deps()), nil
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
matches := make(map[string]struct{})
|
|
|
|
deps := make([][]CacheKeyWithSelector, 0, len(k.Deps()))
|
2018-02-21 19:01:43 +00:00
|
|
|
for i, inp := range k.Deps() {
|
2018-03-16 20:34:06 +00:00
|
|
|
allKeys := c.getAllKeys(inp)
|
|
|
|
cks := make([]CacheKeyWithSelector, 0, len(allKeys))
|
|
|
|
for _, k := range allKeys {
|
|
|
|
internalKey, err := c.getInternalKey(k.CacheKey, createIfNotExist)
|
|
|
|
if err == nil {
|
|
|
|
cks = append(cks, CacheKeyWithSelector{Selector: k.Selector, CacheKey: ExportableCacheKey{CacheKey: internalKey, Exporter: k.CacheKey.Exporter}})
|
|
|
|
}
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
if len(cks) == 0 {
|
|
|
|
return nil, errors.WithStack(ErrNotFound)
|
|
|
|
}
|
2018-02-21 19:01:43 +00:00
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
if i == 0 || len(matches) > 0 {
|
|
|
|
for _, ck := range cks {
|
|
|
|
internalCk := ck.CacheKey.CacheKey.(*inMemoryCacheKey)
|
|
|
|
m2 := make(map[string]struct{})
|
2018-03-17 04:07:18 +00:00
|
|
|
if err := c.backend.WalkLinks(internalCk.id, CacheInfoLink{
|
2018-03-16 20:34:06 +00:00
|
|
|
Input: Index(i),
|
|
|
|
Output: Index(k.Output()),
|
|
|
|
Digest: k.Digest(),
|
|
|
|
Selector: ck.Selector,
|
|
|
|
}, func(id string) error {
|
|
|
|
if i == 0 {
|
|
|
|
matches[id] = struct{}{}
|
|
|
|
} else {
|
|
|
|
m2[id] = struct{}{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if i != 0 {
|
|
|
|
for id := range matches {
|
|
|
|
if _, ok := m2[id]; !ok {
|
|
|
|
delete(matches, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deps = append(deps, cks)
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
var internalKey string
|
|
|
|
if len(matches) == 0 && len(k.Deps()) > 0 {
|
|
|
|
if createIfNotExist {
|
|
|
|
internalKey = identity.NewID()
|
|
|
|
} else {
|
|
|
|
return nil, errors.WithStack(ErrNotFound)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k := range matches {
|
|
|
|
internalKey = k
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(k.Deps()) == 0 {
|
|
|
|
internalKey = digest.FromBytes([]byte(fmt.Sprintf("%s@%d", k.Digest(), k.Output()))).String()
|
|
|
|
}
|
2018-03-17 04:07:18 +00:00
|
|
|
return c.toInMemoryCacheKey(internalKey, k.Digest(), k.Output(), k.Deps()), nil
|
2018-03-16 20:34:06 +00:00
|
|
|
}
|
2018-02-21 19:01:43 +00:00
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
for i, dep := range deps {
|
|
|
|
for _, ck := range dep {
|
|
|
|
internalCk := ck.CacheKey.CacheKey.(*inMemoryCacheKey)
|
2018-03-17 04:07:18 +00:00
|
|
|
err := c.backend.AddLink(internalCk.id, CacheInfoLink{
|
2018-02-21 19:01:43 +00:00
|
|
|
Input: Index(i),
|
2018-03-17 04:07:18 +00:00
|
|
|
Output: k.Output(),
|
|
|
|
Digest: k.Digest(),
|
2018-03-16 20:34:06 +00:00
|
|
|
Selector: ck.Selector,
|
2018-03-17 04:07:18 +00:00
|
|
|
}, internalKey)
|
2018-02-21 19:01:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
return c.toInMemoryCacheKey(internalKey, k.Digest(), k.Output(), k.Deps()), nil
|
2018-02-21 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCombinedCacheManager(cms []CacheManager, main CacheManager) CacheManager {
|
|
|
|
return &combinedCacheManager{cms: cms, main: main}
|
|
|
|
}
|
|
|
|
|
|
|
|
type combinedCacheManager struct {
|
|
|
|
cms []CacheManager
|
|
|
|
main CacheManager
|
|
|
|
id string
|
|
|
|
idOnce sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *combinedCacheManager) ID() string {
|
|
|
|
cm.idOnce.Do(func() {
|
|
|
|
ids := make([]string, len(cm.cms))
|
|
|
|
for i, c := range cm.cms {
|
|
|
|
ids[i] = c.ID()
|
|
|
|
}
|
|
|
|
cm.id = digest.FromBytes([]byte(strings.Join(ids, ","))).String()
|
|
|
|
})
|
|
|
|
return cm.id
|
|
|
|
}
|
|
|
|
|
2018-03-16 20:34:06 +00:00
|
|
|
func (cm *combinedCacheManager) Query(inp []CacheKeyWithSelector, inputIndex Index, dgst digest.Digest, outputIndex Index) ([]*CacheRecord, error) {
|
2018-02-21 19:01:43 +00:00
|
|
|
eg, _ := errgroup.WithContext(context.TODO())
|
|
|
|
res := make(map[string]*CacheRecord, len(cm.cms))
|
|
|
|
var mu sync.Mutex
|
|
|
|
for i, c := range cm.cms {
|
|
|
|
func(i int, c CacheManager) {
|
|
|
|
eg.Go(func() error {
|
2018-03-16 20:34:06 +00:00
|
|
|
recs, err := c.Query(inp, inputIndex, dgst, outputIndex)
|
2018-02-21 19:01:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mu.Lock()
|
|
|
|
for _, r := range recs {
|
|
|
|
if _, ok := res[r.ID]; !ok || c == cm.main {
|
|
|
|
r.CacheManager = c
|
|
|
|
if c == cm.main {
|
|
|
|
r.Priority = 1
|
|
|
|
}
|
|
|
|
res[r.ID] = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mu.Unlock()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}(i, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*CacheRecord, 0, len(res))
|
|
|
|
for _, r := range res {
|
|
|
|
out = append(out, r)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *combinedCacheManager) Load(ctx context.Context, rec *CacheRecord) (Result, error) {
|
|
|
|
return rec.CacheManager.Load(ctx, rec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *combinedCacheManager) Save(key CacheKey, s Result) (ExportableCacheKey, error) {
|
|
|
|
return cm.main.Save(key, s)
|
|
|
|
}
|