2018-01-02 18:06:50 +00:00
|
|
|
package solver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2019-01-10 16:43:27 +00:00
|
|
|
"time"
|
2018-01-02 18:06:50 +00:00
|
|
|
|
2018-05-11 05:58:41 +00:00
|
|
|
"github.com/moby/buildkit/solver/internal/pipe"
|
2021-07-09 07:48:03 +00:00
|
|
|
"github.com/moby/buildkit/util/bklog"
|
2018-01-02 18:06:50 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type edgeStatusType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
edgeStatusInitial edgeStatusType = iota
|
|
|
|
edgeStatusCacheFast
|
|
|
|
edgeStatusCacheSlow
|
|
|
|
edgeStatusComplete
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t edgeStatusType) String() string {
|
|
|
|
return []string{"initial", "cache-fast", "cache-slow", "complete"}[t]
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:32:26 +00:00
|
|
|
func newEdge(ed Edge, op activeOp, index *edgeIndex) *edge {
|
2018-01-02 18:06:50 +00:00
|
|
|
e := &edge{
|
2020-05-18 22:48:52 +00:00
|
|
|
edge: ed,
|
|
|
|
op: op,
|
|
|
|
depRequests: map[pipe.Receiver]*dep{},
|
|
|
|
keyMap: map[string]struct{}{},
|
|
|
|
cacheRecords: map[string]*CacheRecord{},
|
|
|
|
cacheRecordsLoaded: map[string]struct{}{},
|
|
|
|
index: index,
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
type edge struct {
|
|
|
|
edge Edge
|
|
|
|
op activeOp
|
|
|
|
|
|
|
|
edgeState
|
|
|
|
depRequests map[pipe.Receiver]*dep
|
|
|
|
deps []*dep
|
|
|
|
|
2020-05-18 22:48:52 +00:00
|
|
|
cacheMapReq pipe.Receiver
|
|
|
|
cacheMapDone bool
|
|
|
|
cacheMapIndex int
|
|
|
|
cacheMapDigests []digest.Digest
|
|
|
|
execReq pipe.Receiver
|
|
|
|
execCacheLoad bool
|
|
|
|
err error
|
|
|
|
cacheRecords map[string]*CacheRecord
|
|
|
|
cacheRecordsLoaded map[string]struct{}
|
|
|
|
keyMap map[string]struct{}
|
2018-01-02 18:06:50 +00:00
|
|
|
|
|
|
|
noCacheMatchPossible bool
|
|
|
|
allDepsCompletedCacheFast bool
|
|
|
|
allDepsCompletedCacheSlow bool
|
2018-03-01 19:09:21 +00:00
|
|
|
allDepsStateCacheSlow bool
|
2018-01-02 18:06:50 +00:00
|
|
|
allDepsCompleted bool
|
|
|
|
hasActiveOutgoing bool
|
|
|
|
|
|
|
|
releaserCount int
|
|
|
|
keysDidChange bool
|
2018-05-09 21:32:26 +00:00
|
|
|
index *edgeIndex
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
secondaryExporters []expDep
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dep holds state for a dependant edge
|
|
|
|
type dep struct {
|
|
|
|
req pipe.Receiver
|
|
|
|
edgeState
|
|
|
|
index Index
|
2018-04-13 20:52:27 +00:00
|
|
|
keyMap map[string]*CacheKey
|
2018-05-09 21:03:31 +00:00
|
|
|
slowCacheReq pipe.Receiver
|
2018-01-02 18:06:50 +00:00
|
|
|
slowCacheComplete bool
|
2018-03-01 19:09:21 +00:00
|
|
|
slowCacheFoundKey bool
|
2018-02-13 23:54:00 +00:00
|
|
|
slowCacheKey *ExportableCacheKey
|
2018-01-02 18:06:50 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:03:31 +00:00
|
|
|
// expDep holds secorndary exporter info for dependency
|
2018-04-13 20:52:27 +00:00
|
|
|
type expDep struct {
|
|
|
|
index int
|
|
|
|
cacheKey CacheKeyWithSelector
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
func newDep(i Index) *dep {
|
2018-04-13 20:52:27 +00:00
|
|
|
return &dep{index: i, keyMap: map[string]*CacheKey{}}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 21:03:31 +00:00
|
|
|
// edgePipe is a pipe for requests between two edges
|
2018-01-02 18:06:50 +00:00
|
|
|
type edgePipe struct {
|
|
|
|
*pipe.Pipe
|
|
|
|
From, Target *edge
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:03:31 +00:00
|
|
|
// edgeState hold basic mutable state info for an edge
|
2018-01-02 18:06:50 +00:00
|
|
|
type edgeState struct {
|
|
|
|
state edgeStatusType
|
2018-03-01 19:09:21 +00:00
|
|
|
result *SharedCachedResult
|
2018-01-02 18:06:50 +00:00
|
|
|
cacheMap *CacheMap
|
2018-02-13 23:54:00 +00:00
|
|
|
keys []ExportableCacheKey
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type edgeRequest struct {
|
|
|
|
desiredState edgeStatusType
|
|
|
|
currentState edgeState
|
2018-03-01 19:09:21 +00:00
|
|
|
currentKeys int
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// incrementReferenceCount increases the number of times release needs to be
|
|
|
|
// called to release the edge. Called on merging edges.
|
|
|
|
func (e *edge) incrementReferenceCount() {
|
2020-07-18 16:11:39 +00:00
|
|
|
e.releaserCount++
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// release releases the edge resources
|
|
|
|
func (e *edge) release() {
|
|
|
|
if e.releaserCount > 0 {
|
|
|
|
e.releaserCount--
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e.index.Release(e)
|
2018-03-01 19:09:21 +00:00
|
|
|
if e.result != nil {
|
2018-05-11 05:34:16 +00:00
|
|
|
go e.result.Release(context.TODO())
|
2018-03-01 19:09:21 +00:00
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// commitOptions returns parameters for the op execution
|
2018-04-24 19:01:50 +00:00
|
|
|
func (e *edge) commitOptions() ([]*CacheKey, []CachedResult) {
|
2018-04-13 20:52:27 +00:00
|
|
|
k := NewCacheKey(e.cacheMap.Digest, e.edge.Index)
|
2018-04-24 19:01:50 +00:00
|
|
|
if len(e.deps) == 0 {
|
|
|
|
keys := make([]*CacheKey, 0, len(e.cacheMapDigests))
|
|
|
|
for _, dgst := range e.cacheMapDigests {
|
|
|
|
keys = append(keys, NewCacheKey(dgst, e.edge.Index))
|
|
|
|
}
|
|
|
|
return keys, nil
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
inputs := make([][]CacheKeyWithSelector, len(e.deps))
|
2018-02-13 23:54:00 +00:00
|
|
|
results := make([]CachedResult, len(e.deps))
|
2018-01-02 18:06:50 +00:00
|
|
|
for i, dep := range e.deps {
|
2018-07-23 21:27:35 +00:00
|
|
|
for _, k := range dep.result.CacheKeys() {
|
|
|
|
inputs[i] = append(inputs[i], CacheKeyWithSelector{CacheKey: k, Selector: e.cacheMap.Deps[i].Selector})
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
if dep.slowCacheKey != nil {
|
2018-04-13 20:52:27 +00:00
|
|
|
inputs[i] = append(inputs[i], CacheKeyWithSelector{CacheKey: *dep.slowCacheKey})
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
results[i] = dep.result
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
k.deps = inputs
|
2018-04-24 19:01:50 +00:00
|
|
|
return []*CacheKey{k}, results
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isComplete returns true if edge state is final and will never change
|
|
|
|
func (e *edge) isComplete() bool {
|
|
|
|
return e.err != nil || e.result != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// finishIncoming finalizes the incoming pipe request
|
|
|
|
func (e *edge) finishIncoming(req pipe.Sender) {
|
|
|
|
err := e.err
|
|
|
|
if req.Request().Canceled && err == nil {
|
|
|
|
err = context.Canceled
|
|
|
|
}
|
|
|
|
if debugScheduler {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(context.TODO()).Debugf("finishIncoming %s %v %#v desired=%s", e.edge.Vertex.Name(), err, e.edgeState, req.Request().Payload.(*edgeRequest).desiredState)
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
req.Finalize(&e.edgeState, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateIncoming updates the current value of incoming pipe request
|
|
|
|
func (e *edge) updateIncoming(req pipe.Sender) {
|
2019-09-10 23:18:38 +00:00
|
|
|
if debugScheduler {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(context.TODO()).Debugf("updateIncoming %s %#v desired=%s", e.edge.Vertex.Name(), e.edgeState, req.Request().Payload.(*edgeRequest).desiredState)
|
2019-09-10 23:18:38 +00:00
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
req.Update(&e.edgeState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// probeCache is called with unprocessed cache keys for dependency
|
|
|
|
// if the key could match the edge, the cacheRecords for dependency are filled
|
2018-04-13 20:52:27 +00:00
|
|
|
func (e *edge) probeCache(d *dep, depKeys []CacheKeyWithSelector) bool {
|
|
|
|
if len(depKeys) == 0 {
|
2018-03-01 19:09:21 +00:00
|
|
|
return false
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
2018-02-05 16:06:51 +00:00
|
|
|
if e.op.IgnoreCache() {
|
2018-03-01 19:09:21 +00:00
|
|
|
return false
|
2018-02-05 16:06:51 +00:00
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
keys, err := e.op.Cache().Query(depKeys, d.index, e.cacheMap.Digest, e.edge.Index)
|
2018-01-02 18:06:50 +00:00
|
|
|
if err != nil {
|
|
|
|
e.err = errors.Wrap(err, "error on cache query")
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
found := false
|
|
|
|
for _, k := range keys {
|
|
|
|
if _, ok := d.keyMap[k.ID]; !ok {
|
|
|
|
d.keyMap[k.ID] = k
|
|
|
|
found = true
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
return found
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkDepMatchPossible checks if any cache matches are possible past this point
|
|
|
|
func (e *edge) checkDepMatchPossible(dep *dep) {
|
|
|
|
depHasSlowCache := e.cacheMap.Deps[dep.index].ComputeDigestFunc != nil
|
2018-08-27 20:25:14 +00:00
|
|
|
if !e.noCacheMatchPossible && (((!dep.slowCacheFoundKey && dep.slowCacheComplete && depHasSlowCache) || (!depHasSlowCache && dep.state >= edgeStatusCacheSlow)) && len(dep.keyMap) == 0) {
|
2018-01-02 18:06:50 +00:00
|
|
|
e.noCacheMatchPossible = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// slowCacheFunc returns the result based cache func for dependency if it exists
|
|
|
|
func (e *edge) slowCacheFunc(dep *dep) ResultBasedCacheFunc {
|
|
|
|
if e.cacheMap == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.cacheMap.Deps[int(dep.index)].ComputeDigestFunc
|
|
|
|
}
|
|
|
|
|
2020-10-31 03:55:33 +00:00
|
|
|
// preprocessFunc returns result based cache func
|
|
|
|
func (e *edge) preprocessFunc(dep *dep) PreprocessFunc {
|
|
|
|
if e.cacheMap == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.cacheMap.Deps[int(dep.index)].PreprocessFunc
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
// allDepsHaveKeys checks if all dependencies have at least one key. used for
|
|
|
|
// determining if there is enough data for combining cache key for edge
|
2018-08-27 20:25:14 +00:00
|
|
|
func (e *edge) allDepsHaveKeys(matching bool) bool {
|
2018-03-01 19:09:21 +00:00
|
|
|
if e.cacheMap == nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
for _, d := range e.deps {
|
2018-08-27 20:25:14 +00:00
|
|
|
cond := len(d.keys) == 0
|
|
|
|
if matching {
|
|
|
|
cond = len(d.keyMap) == 0
|
|
|
|
}
|
|
|
|
if cond && d.slowCacheKey == nil && d.result == nil {
|
2018-01-02 18:06:50 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// depKeys returns all current dependency cache keys
|
2018-04-13 20:52:27 +00:00
|
|
|
func (e *edge) currentIndexKey() *CacheKey {
|
|
|
|
if e.cacheMap == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([][]CacheKeyWithSelector, len(e.deps))
|
2018-01-02 18:06:50 +00:00
|
|
|
for i, d := range e.deps {
|
2018-04-13 20:52:27 +00:00
|
|
|
if len(d.keys) == 0 && d.result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-13 23:54:00 +00:00
|
|
|
for _, k := range d.keys {
|
2018-04-13 20:52:27 +00:00
|
|
|
keys[i] = append(keys[i], CacheKeyWithSelector{Selector: e.cacheMap.Deps[i].Selector, CacheKey: k})
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
if d.result != nil {
|
2018-07-23 21:27:35 +00:00
|
|
|
for _, rk := range d.result.CacheKeys() {
|
|
|
|
keys[i] = append(keys[i], CacheKeyWithSelector{Selector: e.cacheMap.Deps[i].Selector, CacheKey: rk})
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
if d.slowCacheKey != nil {
|
|
|
|
keys[i] = append(keys[i], CacheKeyWithSelector{CacheKey: ExportableCacheKey{CacheKey: d.slowCacheKey.CacheKey, Exporter: &exporter{k: d.slowCacheKey.CacheKey}}})
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
k := NewCacheKey(e.cacheMap.Digest, e.edge.Index)
|
|
|
|
k.deps = keys
|
|
|
|
|
|
|
|
return k
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// slow cache keys can be computed in 2 phases if there are multiple deps.
|
|
|
|
// first evaluate ones that didn't match any definition based keys
|
|
|
|
func (e *edge) skipPhase2SlowCache(dep *dep) bool {
|
|
|
|
isPhase1 := false
|
|
|
|
for _, dep := range e.deps {
|
2018-09-27 01:24:55 +00:00
|
|
|
if (!dep.slowCacheComplete && e.slowCacheFunc(dep) != nil || dep.state < edgeStatusCacheSlow) && len(dep.keyMap) == 0 {
|
2018-01-02 18:06:50 +00:00
|
|
|
isPhase1 = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
if isPhase1 && !dep.slowCacheComplete && e.slowCacheFunc(dep) != nil && len(dep.keyMap) > 0 {
|
2018-01-02 18:06:50 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-01 19:09:21 +00:00
|
|
|
func (e *edge) skipPhase2FastCache(dep *dep) bool {
|
|
|
|
isPhase1 := false
|
|
|
|
for _, dep := range e.deps {
|
2018-04-13 20:52:27 +00:00
|
|
|
if e.cacheMap == nil || len(dep.keyMap) == 0 && ((!dep.slowCacheComplete && e.slowCacheFunc(dep) != nil) || (dep.state < edgeStatusComplete && e.slowCacheFunc(dep) == nil)) {
|
2018-03-01 19:09:21 +00:00
|
|
|
isPhase1 = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
if isPhase1 && len(dep.keyMap) > 0 {
|
2018-03-01 19:09:21 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
// unpark is called by the scheduler with incoming requests and updates for
|
|
|
|
// previous calls.
|
|
|
|
// To avoid deadlocks and resource leaks this function needs to follow
|
|
|
|
// following rules:
|
|
|
|
// 1) this function needs to return unclosed outgoing requests if some incoming
|
|
|
|
// requests were not completed
|
|
|
|
// 2) this function may not return outgoing requests if it has completed all
|
|
|
|
// incoming requests
|
|
|
|
func (e *edge) unpark(incoming []pipe.Sender, updates, allPipes []pipe.Receiver, f *pipeFactory) {
|
|
|
|
// process all incoming changes
|
|
|
|
depChanged := false
|
|
|
|
for _, upt := range updates {
|
|
|
|
if changed := e.processUpdate(upt); changed {
|
|
|
|
depChanged = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if depChanged {
|
|
|
|
// the dep responses had changes. need to reevaluate edge state
|
|
|
|
e.recalcCurrentState()
|
|
|
|
}
|
|
|
|
|
|
|
|
desiredState, done := e.respondToIncoming(incoming, allPipes)
|
|
|
|
if done {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-29 00:07:50 +00:00
|
|
|
cacheMapReq := false
|
2018-01-02 18:06:50 +00:00
|
|
|
// set up new outgoing requests if needed
|
2018-04-24 19:01:50 +00:00
|
|
|
if e.cacheMapReq == nil && (e.cacheMap == nil || len(e.cacheRecords) == 0) {
|
|
|
|
index := e.cacheMapIndex
|
2018-01-02 18:06:50 +00:00
|
|
|
e.cacheMapReq = f.NewFuncRequest(func(ctx context.Context) (interface{}, error) {
|
2019-06-02 00:12:49 +00:00
|
|
|
cm, err := e.op.CacheMap(ctx, index)
|
2019-06-07 19:04:35 +00:00
|
|
|
return cm, errors.Wrap(err, "failed to load cache key")
|
2018-01-02 18:06:50 +00:00
|
|
|
})
|
2018-08-29 00:07:50 +00:00
|
|
|
cacheMapReq = true
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// execute op
|
|
|
|
if e.execReq == nil && desiredState == edgeStatusComplete {
|
2018-03-01 19:09:21 +00:00
|
|
|
if ok := e.execIfPossible(f); ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.execReq == nil {
|
2018-08-29 00:07:50 +00:00
|
|
|
if added := e.createInputRequests(desiredState, f, false); !added && !e.hasActiveOutgoing && !cacheMapReq {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(context.TODO()).Errorf("buildkit scheluding error: leaving incoming open. forcing solve. Please report this with BUILDKIT_SCHEDULER_DEBUG=1")
|
2020-12-01 22:10:45 +00:00
|
|
|
debugSchedulerPreUnpark(e, incoming, updates, allPipes)
|
2018-08-29 00:07:50 +00:00
|
|
|
e.createInputRequests(desiredState, f, true)
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
func (e *edge) makeExportable(k *CacheKey, records []*CacheRecord) ExportableCacheKey {
|
|
|
|
return ExportableCacheKey{
|
|
|
|
CacheKey: k,
|
2018-05-07 23:17:03 +00:00
|
|
|
Exporter: &exporter{k: k, records: records, override: e.edge.Vertex.Options().ExportCache},
|
2018-04-13 20:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 00:16:23 +00:00
|
|
|
func (e *edge) markFailed(f *pipeFactory, err error) {
|
|
|
|
e.err = err
|
|
|
|
e.postpone(f)
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
// processUpdate is called by unpark for every updated pipe request
|
|
|
|
func (e *edge) processUpdate(upt pipe.Receiver) (depChanged bool) {
|
|
|
|
// response for cachemap request
|
|
|
|
if upt == e.cacheMapReq && upt.Status().Completed {
|
|
|
|
if err := upt.Status().Err; err != nil {
|
2018-03-01 19:09:21 +00:00
|
|
|
e.cacheMapReq = nil
|
|
|
|
if !upt.Status().Canceled && e.err == nil {
|
2018-01-02 18:06:50 +00:00
|
|
|
e.err = err
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-24 19:01:50 +00:00
|
|
|
resp := upt.Status().Value.(*cacheMapResp)
|
|
|
|
e.cacheMap = resp.CacheMap
|
|
|
|
e.cacheMapDone = resp.complete
|
|
|
|
e.cacheMapIndex++
|
2018-01-02 18:06:50 +00:00
|
|
|
if len(e.deps) == 0 {
|
2018-04-24 19:01:50 +00:00
|
|
|
e.cacheMapDigests = append(e.cacheMapDigests, e.cacheMap.Digest)
|
2018-02-05 16:06:51 +00:00
|
|
|
if !e.op.IgnoreCache() {
|
2018-04-13 20:52:27 +00:00
|
|
|
keys, err := e.op.Cache().Query(nil, 0, e.cacheMap.Digest, e.edge.Index)
|
2018-02-05 16:06:51 +00:00
|
|
|
if err != nil {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(context.TODO()).Error(errors.Wrap(err, "invalid query response")) // make the build fail for this error
|
2018-02-05 16:06:51 +00:00
|
|
|
} else {
|
2018-04-13 20:52:27 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
records, err := e.op.Cache().Records(k)
|
|
|
|
if err != nil {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(context.TODO()).Errorf("error receiving cache records: %v", err)
|
2018-04-13 20:52:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range records {
|
2018-02-27 01:39:18 +00:00
|
|
|
e.cacheRecords[r.ID] = r
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
e.keys = append(e.keys, e.makeExportable(k, records))
|
2018-02-05 16:06:51 +00:00
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e.state = edgeStatusCacheSlow
|
|
|
|
}
|
2018-08-27 20:25:14 +00:00
|
|
|
if e.allDepsHaveKeys(false) {
|
2018-04-13 20:52:27 +00:00
|
|
|
e.keysDidChange = true
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
// probe keys that were loaded before cache map
|
2018-03-16 20:34:06 +00:00
|
|
|
for i, dep := range e.deps {
|
|
|
|
e.probeCache(dep, withSelector(dep.keys, e.cacheMap.Deps[i].Selector))
|
2018-01-02 18:06:50 +00:00
|
|
|
e.checkDepMatchPossible(dep)
|
|
|
|
}
|
2018-04-24 19:01:50 +00:00
|
|
|
if !e.cacheMapDone {
|
|
|
|
e.cacheMapReq = nil
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// response for exec request
|
|
|
|
if upt == e.execReq && upt.Status().Completed {
|
|
|
|
if err := upt.Status().Err; err != nil {
|
2018-03-01 19:09:21 +00:00
|
|
|
e.execReq = nil
|
2020-05-18 22:48:52 +00:00
|
|
|
if e.execCacheLoad {
|
|
|
|
for k := range e.cacheRecordsLoaded {
|
|
|
|
delete(e.cacheRecords, k)
|
|
|
|
}
|
|
|
|
} else if !upt.Status().Canceled && e.err == nil {
|
2018-01-02 18:06:50 +00:00
|
|
|
e.err = err
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-01 19:09:21 +00:00
|
|
|
e.result = NewSharedCachedResult(upt.Status().Value.(CachedResult))
|
2018-01-02 18:06:50 +00:00
|
|
|
e.state = edgeStatusComplete
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// response for requests to dependencies
|
2018-07-22 04:00:37 +00:00
|
|
|
if dep, ok := e.depRequests[upt]; ok {
|
2018-01-02 18:06:50 +00:00
|
|
|
if err := upt.Status().Err; !upt.Status().Canceled && upt.Status().Completed && err != nil {
|
|
|
|
if e.err == nil {
|
|
|
|
e.err = err
|
|
|
|
}
|
|
|
|
dep.err = err
|
|
|
|
}
|
|
|
|
|
2021-10-01 21:34:39 +00:00
|
|
|
if upt.Status().Value == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
state, isEdgeState := upt.Status().Value.(*edgeState)
|
|
|
|
if !isEdgeState {
|
|
|
|
bklog.G(context.TODO()).Warnf("invalid edgeState value for update: %T", state)
|
|
|
|
return
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
|
|
|
|
if len(dep.keys) < len(state.keys) {
|
|
|
|
newKeys := state.keys[len(dep.keys):]
|
|
|
|
if e.cacheMap != nil {
|
2018-03-16 20:34:06 +00:00
|
|
|
e.probeCache(dep, withSelector(newKeys, e.cacheMap.Deps[dep.index].Selector))
|
2018-07-22 04:00:37 +00:00
|
|
|
dep.edgeState.keys = state.keys
|
2018-08-27 20:25:14 +00:00
|
|
|
if e.allDepsHaveKeys(false) {
|
2018-01-02 18:06:50 +00:00
|
|
|
e.keysDidChange = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
depChanged = true
|
|
|
|
}
|
|
|
|
if dep.state != edgeStatusComplete && state.state == edgeStatusComplete {
|
|
|
|
e.keysDidChange = true
|
|
|
|
}
|
|
|
|
|
|
|
|
recheck := state.state != dep.state
|
|
|
|
|
|
|
|
dep.edgeState = *state
|
|
|
|
|
|
|
|
if recheck && e.cacheMap != nil {
|
|
|
|
e.checkDepMatchPossible(dep)
|
|
|
|
depChanged = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response for result based cache function
|
2018-03-01 19:09:21 +00:00
|
|
|
for i, dep := range e.deps {
|
2018-01-02 18:06:50 +00:00
|
|
|
if upt == dep.slowCacheReq && upt.Status().Completed {
|
|
|
|
if err := upt.Status().Err; err != nil {
|
2018-03-01 19:09:21 +00:00
|
|
|
dep.slowCacheReq = nil
|
|
|
|
if !upt.Status().Canceled && e.err == nil {
|
2018-01-02 18:06:50 +00:00
|
|
|
e.err = upt.Status().Err
|
|
|
|
}
|
|
|
|
} else if !dep.slowCacheComplete {
|
2020-10-31 03:55:33 +00:00
|
|
|
dgst := upt.Status().Value.(digest.Digest)
|
|
|
|
if e.cacheMap.Deps[int(dep.index)].ComputeDigestFunc != nil && dgst != "" {
|
|
|
|
k := NewCacheKey(dgst, -1)
|
|
|
|
dep.slowCacheKey = &ExportableCacheKey{CacheKey: k, Exporter: &exporter{k: k}}
|
|
|
|
slowKeyExp := CacheKeyWithSelector{CacheKey: *dep.slowCacheKey}
|
|
|
|
defKeys := make([]CacheKeyWithSelector, 0, len(dep.result.CacheKeys()))
|
|
|
|
for _, dk := range dep.result.CacheKeys() {
|
|
|
|
defKeys = append(defKeys, CacheKeyWithSelector{CacheKey: dk, Selector: e.cacheMap.Deps[i].Selector})
|
|
|
|
}
|
|
|
|
dep.slowCacheFoundKey = e.probeCache(dep, []CacheKeyWithSelector{slowKeyExp})
|
2018-05-09 04:47:41 +00:00
|
|
|
|
2020-10-31 03:55:33 +00:00
|
|
|
// connect def key to slow key
|
|
|
|
e.op.Cache().Query(append(defKeys, slowKeyExp), dep.index, e.cacheMap.Digest, e.edge.Index)
|
|
|
|
}
|
2018-05-09 04:47:41 +00:00
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
dep.slowCacheComplete = true
|
|
|
|
e.keysDidChange = true
|
2018-03-01 19:09:21 +00:00
|
|
|
e.checkDepMatchPossible(dep) // not matching key here doesn't set nocachematch possible to true
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// recalcCurrentState is called by unpark to recompute internal state after
|
|
|
|
// the state of dependencies has changed
|
|
|
|
func (e *edge) recalcCurrentState() {
|
|
|
|
// TODO: fast pass to detect incomplete results
|
2018-04-13 20:52:27 +00:00
|
|
|
newKeys := map[string]*CacheKey{}
|
2018-01-02 18:06:50 +00:00
|
|
|
|
|
|
|
for i, dep := range e.deps {
|
|
|
|
if i == 0 {
|
2018-04-13 20:52:27 +00:00
|
|
|
for id, k := range dep.keyMap {
|
|
|
|
if _, ok := e.keyMap[id]; ok {
|
2018-01-02 18:06:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
newKeys[id] = k
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-04-13 20:52:27 +00:00
|
|
|
for id := range newKeys {
|
|
|
|
if _, ok := dep.keyMap[id]; !ok {
|
|
|
|
delete(newKeys, id)
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
if len(newKeys) == 0 {
|
2018-01-02 18:06:50 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 22:34:18 +00:00
|
|
|
for key := range newKeys {
|
|
|
|
e.keyMap[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
for _, r := range newKeys {
|
|
|
|
// TODO: add all deps automatically
|
|
|
|
mergedKey := r.clone()
|
|
|
|
mergedKey.deps = make([][]CacheKeyWithSelector, len(e.deps))
|
|
|
|
for i, dep := range e.deps {
|
|
|
|
if dep.result != nil {
|
2018-07-23 21:27:35 +00:00
|
|
|
for _, dk := range dep.result.CacheKeys() {
|
|
|
|
mergedKey.deps[i] = append(mergedKey.deps[i], CacheKeyWithSelector{Selector: e.cacheMap.Deps[i].Selector, CacheKey: dk})
|
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
if dep.slowCacheKey != nil {
|
|
|
|
mergedKey.deps[i] = append(mergedKey.deps[i], CacheKeyWithSelector{CacheKey: *dep.slowCacheKey})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, k := range dep.keys {
|
|
|
|
mergedKey.deps[i] = append(mergedKey.deps[i], CacheKeyWithSelector{Selector: e.cacheMap.Deps[i].Selector, CacheKey: k})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
records, err := e.op.Cache().Records(mergedKey)
|
|
|
|
if err != nil {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(context.TODO()).Errorf("error receiving cache records: %v", err)
|
2018-04-13 20:52:27 +00:00
|
|
|
continue
|
2018-03-16 20:34:06 +00:00
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
for _, r := range records {
|
2020-05-18 22:48:52 +00:00
|
|
|
if _, ok := e.cacheRecordsLoaded[r.ID]; !ok {
|
|
|
|
e.cacheRecords[r.ID] = r
|
|
|
|
}
|
2018-02-27 01:39:18 +00:00
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
e.keys = append(e.keys, e.makeExportable(mergedKey, records))
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// detect lower/upper bound for current state
|
2018-03-01 19:09:21 +00:00
|
|
|
allDepsCompletedCacheFast := e.cacheMap != nil
|
|
|
|
allDepsCompletedCacheSlow := e.cacheMap != nil
|
|
|
|
allDepsStateCacheSlow := true
|
2018-01-02 18:06:50 +00:00
|
|
|
allDepsCompleted := true
|
2018-03-01 19:09:21 +00:00
|
|
|
stLow := edgeStatusInitial // minimal possible state
|
|
|
|
stHigh := edgeStatusCacheSlow // maximum possible state
|
2018-01-02 18:06:50 +00:00
|
|
|
if e.cacheMap != nil {
|
|
|
|
for _, dep := range e.deps {
|
2020-12-11 06:57:30 +00:00
|
|
|
isSlowCacheIncomplete := e.slowCacheFunc(dep) != nil && (dep.state == edgeStatusCacheSlow || (dep.state == edgeStatusComplete && !dep.slowCacheComplete))
|
2020-10-31 03:55:33 +00:00
|
|
|
isSlowIncomplete := (e.slowCacheFunc(dep) != nil || e.preprocessFunc(dep) != nil) && (dep.state == edgeStatusCacheSlow || (dep.state == edgeStatusComplete && !dep.slowCacheComplete))
|
2018-03-01 19:09:21 +00:00
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
if dep.state > stLow && len(dep.keyMap) == 0 && !isSlowIncomplete {
|
2018-01-02 18:06:50 +00:00
|
|
|
stLow = dep.state
|
|
|
|
if stLow > edgeStatusCacheSlow {
|
|
|
|
stLow = edgeStatusCacheSlow
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 19:09:21 +00:00
|
|
|
effectiveState := dep.state
|
2020-12-11 06:57:30 +00:00
|
|
|
if dep.state == edgeStatusCacheSlow && isSlowCacheIncomplete {
|
2018-03-01 19:09:21 +00:00
|
|
|
effectiveState = edgeStatusCacheFast
|
|
|
|
}
|
2020-12-11 06:57:30 +00:00
|
|
|
if dep.state == edgeStatusComplete && isSlowCacheIncomplete {
|
2018-03-01 19:09:21 +00:00
|
|
|
effectiveState = edgeStatusCacheFast
|
|
|
|
}
|
|
|
|
if effectiveState < stHigh {
|
|
|
|
stHigh = effectiveState
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
if isSlowIncomplete || dep.state < edgeStatusComplete {
|
|
|
|
allDepsCompleted = false
|
|
|
|
}
|
|
|
|
if dep.state < edgeStatusCacheFast {
|
|
|
|
allDepsCompletedCacheFast = false
|
|
|
|
}
|
2020-12-11 06:57:30 +00:00
|
|
|
if isSlowCacheIncomplete || dep.state < edgeStatusCacheSlow {
|
2018-01-02 18:06:50 +00:00
|
|
|
allDepsCompletedCacheSlow = false
|
|
|
|
}
|
2018-08-27 20:25:14 +00:00
|
|
|
if dep.state < edgeStatusCacheSlow && len(dep.keyMap) == 0 {
|
2018-03-01 19:09:21 +00:00
|
|
|
allDepsStateCacheSlow = false
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
if stLow > e.state {
|
|
|
|
e.state = stLow
|
|
|
|
}
|
2018-03-01 19:09:21 +00:00
|
|
|
if stHigh > e.state {
|
|
|
|
e.state = stHigh
|
|
|
|
}
|
2018-04-24 19:01:50 +00:00
|
|
|
if !e.cacheMapDone && len(e.keys) == 0 {
|
|
|
|
e.state = edgeStatusInitial
|
|
|
|
}
|
2018-03-01 19:09:21 +00:00
|
|
|
|
2018-04-24 19:01:50 +00:00
|
|
|
e.allDepsCompletedCacheFast = e.cacheMapDone && allDepsCompletedCacheFast
|
|
|
|
e.allDepsCompletedCacheSlow = e.cacheMapDone && allDepsCompletedCacheSlow
|
|
|
|
e.allDepsStateCacheSlow = e.cacheMapDone && allDepsStateCacheSlow
|
|
|
|
e.allDepsCompleted = e.cacheMapDone && allDepsCompleted
|
2019-03-13 00:49:20 +00:00
|
|
|
|
|
|
|
if e.allDepsStateCacheSlow && len(e.cacheRecords) > 0 && e.state == edgeStatusCacheFast {
|
2019-03-13 22:34:18 +00:00
|
|
|
openKeys := map[string]struct{}{}
|
|
|
|
for _, dep := range e.deps {
|
|
|
|
isSlowIncomplete := e.slowCacheFunc(dep) != nil && (dep.state == edgeStatusCacheSlow || (dep.state == edgeStatusComplete && !dep.slowCacheComplete))
|
|
|
|
if !isSlowIncomplete {
|
|
|
|
openDepKeys := map[string]struct{}{}
|
|
|
|
for key := range dep.keyMap {
|
|
|
|
if _, ok := e.keyMap[key]; !ok {
|
|
|
|
openDepKeys[key] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(openKeys) != 0 {
|
|
|
|
for k := range openKeys {
|
|
|
|
if _, ok := openDepKeys[k]; !ok {
|
|
|
|
delete(openKeys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
openKeys = openDepKeys
|
|
|
|
}
|
|
|
|
if len(openKeys) == 0 {
|
|
|
|
e.state = edgeStatusCacheSlow
|
|
|
|
if debugScheduler {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(context.TODO()).Debugf("upgrade to cache-slow because no open keys")
|
2019-03-13 22:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 00:49:20 +00:00
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// respondToIncoming responds to all incoming requests. completing or
|
|
|
|
// updating them when possible
|
|
|
|
func (e *edge) respondToIncoming(incoming []pipe.Sender, allPipes []pipe.Receiver) (edgeStatusType, bool) {
|
|
|
|
// detect the result state for the requests
|
|
|
|
allIncomingCanComplete := true
|
|
|
|
desiredState := e.state
|
2018-03-01 19:09:21 +00:00
|
|
|
allCanceled := true
|
2018-01-02 18:06:50 +00:00
|
|
|
|
|
|
|
// check incoming requests
|
2018-03-01 19:09:21 +00:00
|
|
|
// check if all requests can be either answered or canceled
|
2018-01-02 18:06:50 +00:00
|
|
|
if !e.isComplete() {
|
|
|
|
for _, req := range incoming {
|
|
|
|
if !req.Request().Canceled {
|
2018-03-01 19:09:21 +00:00
|
|
|
allCanceled = false
|
2018-01-02 18:06:50 +00:00
|
|
|
if r := req.Request().Payload.(*edgeRequest); desiredState < r.desiredState {
|
|
|
|
desiredState = r.desiredState
|
2018-03-01 19:09:21 +00:00
|
|
|
if e.hasActiveOutgoing || r.desiredState == edgeStatusComplete || r.currentKeys == len(e.keys) {
|
|
|
|
allIncomingCanComplete = false
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 19:09:21 +00:00
|
|
|
// do not set allIncomingCanComplete if active ongoing can modify the state
|
|
|
|
if !allCanceled && e.state < edgeStatusComplete && len(e.keys) == 0 && e.hasActiveOutgoing {
|
2018-01-02 18:06:50 +00:00
|
|
|
allIncomingCanComplete = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if debugScheduler {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(context.TODO()).Debugf("status state=%s cancomplete=%v hasouts=%v noPossibleCache=%v depsCacheFast=%v keys=%d cacheRecords=%d", e.state, allIncomingCanComplete, e.hasActiveOutgoing, e.noCacheMatchPossible, e.allDepsCompletedCacheFast, len(e.keys), len(e.cacheRecords))
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if allIncomingCanComplete && e.hasActiveOutgoing {
|
|
|
|
// cancel all current requests
|
|
|
|
for _, p := range allPipes {
|
|
|
|
p.Cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// can close all but one requests
|
|
|
|
var leaveOpen pipe.Sender
|
|
|
|
for _, req := range incoming {
|
|
|
|
if !req.Request().Canceled {
|
|
|
|
leaveOpen = req
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, req := range incoming {
|
|
|
|
if leaveOpen == nil || leaveOpen == req {
|
|
|
|
leaveOpen = req
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
e.finishIncoming(req)
|
|
|
|
}
|
|
|
|
return desiredState, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// can complete, finish and return
|
|
|
|
if allIncomingCanComplete && !e.hasActiveOutgoing {
|
|
|
|
for _, req := range incoming {
|
|
|
|
e.finishIncoming(req)
|
|
|
|
}
|
|
|
|
return desiredState, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// update incoming based on current state
|
|
|
|
for _, req := range incoming {
|
|
|
|
r := req.Request().Payload.(*edgeRequest)
|
2018-03-01 19:09:21 +00:00
|
|
|
if req.Request().Canceled {
|
|
|
|
e.finishIncoming(req)
|
|
|
|
} else if !e.hasActiveOutgoing && e.state >= r.desiredState {
|
2018-01-02 18:06:50 +00:00
|
|
|
e.finishIncoming(req)
|
|
|
|
} else if !isEqualState(r.currentState, e.edgeState) && !req.Request().Canceled {
|
|
|
|
e.updateIncoming(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return desiredState, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// createInputRequests creates new requests for dependencies or async functions
|
|
|
|
// that need to complete to continue processing the edge
|
2018-08-29 00:07:50 +00:00
|
|
|
func (e *edge) createInputRequests(desiredState edgeStatusType, f *pipeFactory, force bool) bool {
|
|
|
|
addedNew := false
|
|
|
|
|
2018-01-02 18:06:50 +00:00
|
|
|
// initialize deps state
|
|
|
|
if e.deps == nil {
|
|
|
|
e.depRequests = make(map[pipe.Receiver]*dep)
|
|
|
|
e.deps = make([]*dep, 0, len(e.edge.Vertex.Inputs()))
|
|
|
|
for i := range e.edge.Vertex.Inputs() {
|
|
|
|
e.deps = append(e.deps, newDep(Index(i)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cycle all dependencies. set up outgoing requests if needed
|
|
|
|
for _, dep := range e.deps {
|
|
|
|
desiredStateDep := dep.state
|
|
|
|
|
2018-08-29 00:07:50 +00:00
|
|
|
if e.noCacheMatchPossible || force {
|
2018-03-01 19:09:21 +00:00
|
|
|
desiredStateDep = edgeStatusComplete
|
2018-01-02 18:06:50 +00:00
|
|
|
} else if dep.state == edgeStatusInitial && desiredState > dep.state {
|
|
|
|
desiredStateDep = edgeStatusCacheFast
|
|
|
|
} else if dep.state == edgeStatusCacheFast && desiredState > dep.state {
|
2018-03-01 19:09:21 +00:00
|
|
|
// wait all deps to complete cache fast before continuing with slow cache
|
2018-08-27 20:25:14 +00:00
|
|
|
if (e.allDepsCompletedCacheFast && len(e.keys) == 0) || len(dep.keyMap) == 0 || e.allDepsHaveKeys(true) {
|
|
|
|
if !e.skipPhase2FastCache(dep) && e.cacheMap != nil {
|
2018-03-01 19:09:21 +00:00
|
|
|
desiredStateDep = edgeStatusCacheSlow
|
|
|
|
}
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
2018-08-27 20:25:14 +00:00
|
|
|
} else if e.cacheMap != nil && dep.state == edgeStatusCacheSlow && desiredState == edgeStatusComplete {
|
2018-03-01 19:09:21 +00:00
|
|
|
// if all deps have completed cache-slow or content based cache for input is available
|
2018-08-27 20:25:14 +00:00
|
|
|
if (len(dep.keyMap) == 0 || e.allDepsCompletedCacheSlow || (!e.skipPhase2FastCache(dep) && e.slowCacheFunc(dep) != nil)) && (len(e.cacheRecords) == 0) {
|
|
|
|
if len(dep.keyMap) == 0 || !e.skipPhase2SlowCache(dep) {
|
2018-01-02 18:06:50 +00:00
|
|
|
desiredStateDep = edgeStatusComplete
|
|
|
|
}
|
|
|
|
}
|
2018-08-27 20:25:14 +00:00
|
|
|
} else if e.cacheMap != nil && dep.state == edgeStatusCacheSlow && e.slowCacheFunc(dep) != nil && desiredState == edgeStatusCacheSlow {
|
|
|
|
if len(dep.keyMap) == 0 || !e.skipPhase2SlowCache(dep) {
|
2018-01-02 18:06:50 +00:00
|
|
|
desiredStateDep = edgeStatusComplete
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// outgoing request is needed
|
|
|
|
if dep.state < desiredStateDep {
|
|
|
|
addNew := true
|
|
|
|
if dep.req != nil && !dep.req.Status().Completed {
|
|
|
|
if dep.req.Request().(*edgeRequest).desiredState != desiredStateDep {
|
|
|
|
dep.req.Cancel()
|
|
|
|
} else {
|
|
|
|
addNew = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if addNew {
|
|
|
|
req := f.NewInputRequest(e.edge.Vertex.Inputs()[int(dep.index)], &edgeRequest{
|
|
|
|
currentState: dep.edgeState,
|
|
|
|
desiredState: desiredStateDep,
|
2018-03-01 19:09:21 +00:00
|
|
|
currentKeys: len(dep.keys),
|
2018-01-02 18:06:50 +00:00
|
|
|
})
|
|
|
|
e.depRequests[req] = dep
|
|
|
|
dep.req = req
|
2018-08-29 00:07:50 +00:00
|
|
|
addedNew = true
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// initialize function to compute cache key based on dependency result
|
2020-10-31 03:55:33 +00:00
|
|
|
if dep.state == edgeStatusComplete && dep.slowCacheReq == nil && (e.slowCacheFunc(dep) != nil || e.preprocessFunc(dep) != nil) && e.cacheMap != nil {
|
|
|
|
pfn := e.preprocessFunc(dep)
|
2018-01-02 18:06:50 +00:00
|
|
|
fn := e.slowCacheFunc(dep)
|
|
|
|
res := dep.result
|
2020-10-31 03:55:33 +00:00
|
|
|
func(pfn PreprocessFunc, fn ResultBasedCacheFunc, res Result, index Index) {
|
2018-01-02 18:06:50 +00:00
|
|
|
dep.slowCacheReq = f.NewFuncRequest(func(ctx context.Context) (interface{}, error) {
|
2020-10-31 03:55:33 +00:00
|
|
|
v, err := e.op.CalcSlowCache(ctx, index, pfn, fn, res)
|
2019-06-07 19:04:35 +00:00
|
|
|
return v, errors.Wrap(err, "failed to compute cache key")
|
2018-01-02 18:06:50 +00:00
|
|
|
})
|
2020-10-31 03:55:33 +00:00
|
|
|
}(pfn, fn, res, dep.index)
|
2018-08-29 00:07:50 +00:00
|
|
|
addedNew = true
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-29 00:07:50 +00:00
|
|
|
return addedNew
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// execIfPossible creates a request for getting the edge result if there is
|
|
|
|
// enough state
|
2018-03-01 19:09:21 +00:00
|
|
|
func (e *edge) execIfPossible(f *pipeFactory) bool {
|
2018-02-27 01:39:18 +00:00
|
|
|
if len(e.cacheRecords) > 0 {
|
2018-01-02 18:06:50 +00:00
|
|
|
if e.keysDidChange {
|
|
|
|
e.postpone(f)
|
2018-03-01 19:09:21 +00:00
|
|
|
return true
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
e.execReq = f.NewFuncRequest(e.loadCache)
|
2020-05-18 22:48:52 +00:00
|
|
|
e.execCacheLoad = true
|
2018-01-02 18:06:50 +00:00
|
|
|
for req := range e.depRequests {
|
|
|
|
req.Cancel()
|
|
|
|
}
|
2018-03-01 19:09:21 +00:00
|
|
|
return true
|
2018-01-02 18:06:50 +00:00
|
|
|
} else if e.allDepsCompleted {
|
|
|
|
if e.keysDidChange {
|
|
|
|
e.postpone(f)
|
2018-03-01 19:09:21 +00:00
|
|
|
return true
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
e.execReq = f.NewFuncRequest(e.execOp)
|
2020-05-18 22:48:52 +00:00
|
|
|
e.execCacheLoad = false
|
2018-03-01 19:09:21 +00:00
|
|
|
return true
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
2018-03-01 19:09:21 +00:00
|
|
|
return false
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// postpone delays exec to next unpark invocation if we have unprocessed keys
|
|
|
|
func (e *edge) postpone(f *pipeFactory) {
|
|
|
|
f.NewFuncRequest(func(context.Context) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadCache creates a request to load edge result from cache
|
|
|
|
func (e *edge) loadCache(ctx context.Context) (interface{}, error) {
|
2018-04-13 20:52:27 +00:00
|
|
|
recs := make([]*CacheRecord, 0, len(e.cacheRecords))
|
2018-02-02 00:10:38 +00:00
|
|
|
for _, r := range e.cacheRecords {
|
2018-04-13 20:52:27 +00:00
|
|
|
recs = append(recs, r)
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
|
|
|
|
rec := getBestResult(recs)
|
2020-05-18 22:48:52 +00:00
|
|
|
e.cacheRecordsLoaded[rec.ID] = struct{}{}
|
2018-04-13 20:52:27 +00:00
|
|
|
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(ctx).Debugf("load cache for %s with %s", e.edge.Vertex.Name(), rec.ID)
|
2018-04-13 20:52:27 +00:00
|
|
|
res, err := e.op.LoadCache(ctx, rec)
|
2018-01-02 18:06:50 +00:00
|
|
|
if err != nil {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(ctx).Debugf("load cache for %s err: %v", e.edge.Vertex.Name(), err)
|
2019-06-07 19:04:35 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to load cache")
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
|
2018-07-23 21:27:35 +00:00
|
|
|
return NewCachedResult(res, []ExportableCacheKey{{CacheKey: rec.key, Exporter: &exporter{k: rec.key, record: rec, edge: e}}}), nil
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// execOp creates a request to execute the vertex operation
|
|
|
|
func (e *edge) execOp(ctx context.Context) (interface{}, error) {
|
2018-04-24 19:01:50 +00:00
|
|
|
cacheKeys, inputs := e.commitOptions()
|
2018-04-13 20:52:27 +00:00
|
|
|
results, subExporters, err := e.op.Exec(ctx, toResultSlice(inputs))
|
2018-01-02 18:06:50 +00:00
|
|
|
if err != nil {
|
2019-06-02 00:12:49 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
index := e.edge.Index
|
|
|
|
if len(results) <= int(index) {
|
|
|
|
return nil, errors.Errorf("invalid response from exec need %d index but %d results received", index, len(results))
|
|
|
|
}
|
|
|
|
|
|
|
|
res := results[int(index)]
|
|
|
|
|
2018-03-01 19:09:21 +00:00
|
|
|
for i := range results {
|
|
|
|
if i != int(index) {
|
|
|
|
go results[i].Release(context.TODO())
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 23:54:00 +00:00
|
|
|
|
2018-05-07 21:19:02 +00:00
|
|
|
var exporters []CacheExporter
|
2018-04-24 19:01:50 +00:00
|
|
|
|
|
|
|
for _, cacheKey := range cacheKeys {
|
2019-01-10 16:43:27 +00:00
|
|
|
ck, err := e.op.Cache().Save(cacheKey, res, time.Now())
|
2018-04-24 19:01:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-13 23:54:00 +00:00
|
|
|
|
2018-05-08 18:18:06 +00:00
|
|
|
if exp, ok := ck.Exporter.(*exporter); ok {
|
|
|
|
exp.edge = e
|
|
|
|
}
|
|
|
|
|
2018-05-07 21:19:02 +00:00
|
|
|
exps := make([]CacheExporter, 0, len(subExporters))
|
2018-04-24 19:01:50 +00:00
|
|
|
for _, exp := range subExporters {
|
|
|
|
exps = append(exps, exp.Exporter)
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|
2018-04-24 19:01:50 +00:00
|
|
|
|
|
|
|
exporters = append(exporters, ck.Exporter)
|
|
|
|
exporters = append(exporters, exps...)
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:27:35 +00:00
|
|
|
ek := make([]ExportableCacheKey, 0, len(cacheKeys))
|
|
|
|
for _, ck := range cacheKeys {
|
|
|
|
ek = append(ek, ExportableCacheKey{
|
|
|
|
CacheKey: ck,
|
|
|
|
Exporter: &mergedExporter{exporters: exporters},
|
|
|
|
})
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 21:27:35 +00:00
|
|
|
return NewCachedResult(res, ek), nil
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 02:55:03 +00:00
|
|
|
func (e *edge) isDep(e2 *edge) bool {
|
|
|
|
return isDep(e.edge.Vertex, e2.edge.Vertex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDep(vtx, vtx2 Vertex) bool {
|
|
|
|
if vtx.Digest() == vtx2.Digest() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, e := range vtx.Inputs() {
|
|
|
|
if isDep(e.Vertex, vtx2) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:52:27 +00:00
|
|
|
func toResultSlice(cres []CachedResult) (out []Result) {
|
|
|
|
out = make([]Result, len(cres))
|
|
|
|
for i := range cres {
|
|
|
|
out[i] = cres[i].(Result)
|
2018-02-13 23:54:00 +00:00
|
|
|
}
|
2018-04-13 20:52:27 +00:00
|
|
|
return out
|
2018-01-02 18:06:50 +00:00
|
|
|
}
|
2018-05-09 21:03:31 +00:00
|
|
|
|
|
|
|
func isEqualState(s1, s2 edgeState) bool {
|
|
|
|
if s1.state != s2.state || s1.result != s2.result || s1.cacheMap != s2.cacheMap || len(s1.keys) != len(s2.keys) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func withSelector(keys []ExportableCacheKey, selector digest.Digest) []CacheKeyWithSelector {
|
|
|
|
out := make([]CacheKeyWithSelector, len(keys))
|
|
|
|
for i, k := range keys {
|
|
|
|
out[i] = CacheKeyWithSelector{Selector: selector, CacheKey: k}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|