2017-06-14 00:15:55 +00:00
|
|
|
package solver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-06-16 22:49:03 +00:00
|
|
|
"io"
|
2017-06-14 00:15:55 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
"github.com/moby/buildkit/util/progress"
|
2017-06-14 00:15:55 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-14 00:15:55 +00:00
|
|
|
)
|
|
|
|
|
2017-07-21 17:58:24 +00:00
|
|
|
type jobKeyT string
|
|
|
|
|
|
|
|
var jobKey = jobKeyT("buildkit/solver/job")
|
|
|
|
|
2017-06-14 00:15:55 +00:00
|
|
|
type jobList struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
refs map[string]*job
|
|
|
|
updateCond *sync.Cond
|
|
|
|
}
|
|
|
|
|
|
|
|
func newJobList() *jobList {
|
|
|
|
jl := &jobList{
|
|
|
|
refs: make(map[string]*job),
|
|
|
|
}
|
|
|
|
jl.updateCond = sync.NewCond(jl.mu.RLocker())
|
|
|
|
return jl
|
|
|
|
}
|
|
|
|
|
2017-07-21 17:58:24 +00:00
|
|
|
func (jl *jobList) new(ctx context.Context, id string, g *vertex, pr progress.Reader) (context.Context, *job, error) {
|
2017-06-14 00:15:55 +00:00
|
|
|
jl.mu.Lock()
|
|
|
|
defer jl.mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := jl.refs[id]; ok {
|
2017-07-21 17:58:24 +00:00
|
|
|
return nil, nil, errors.Errorf("id %s exists", id)
|
2017-06-14 00:15:55 +00:00
|
|
|
}
|
|
|
|
j := &job{g: g, pr: progress.NewMultiReader(pr)}
|
|
|
|
jl.refs[id] = j
|
|
|
|
jl.updateCond.Broadcast()
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
jl.mu.Lock()
|
|
|
|
defer jl.mu.Unlock()
|
|
|
|
delete(jl.refs, id)
|
|
|
|
}()
|
|
|
|
|
2017-07-21 17:58:24 +00:00
|
|
|
return context.WithValue(ctx, jobKey, jl.refs[id]), jl.refs[id], nil
|
2017-06-14 00:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (jl *jobList) get(id string) (*job, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
jl.updateCond.Broadcast()
|
|
|
|
}()
|
|
|
|
|
|
|
|
jl.mu.RLock()
|
|
|
|
defer jl.mu.RUnlock()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, errors.Errorf("no such job %s", id)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
j, ok := jl.refs[id]
|
|
|
|
if !ok {
|
|
|
|
jl.updateCond.Wait()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return j, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type job struct {
|
|
|
|
mu sync.Mutex
|
2017-07-06 04:25:51 +00:00
|
|
|
g *vertex
|
2017-06-14 00:15:55 +00:00
|
|
|
pr *progress.MultiReader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *job) pipe(ctx context.Context, ch chan *client.SolveStatus) error {
|
|
|
|
pr := j.pr.Reader(ctx)
|
|
|
|
for v := range walk(j.g) {
|
2017-07-06 04:25:51 +00:00
|
|
|
vv := v.(*vertex)
|
2017-06-14 00:15:55 +00:00
|
|
|
ss := &client.SolveStatus{
|
2017-07-06 04:25:51 +00:00
|
|
|
Vertexes: []*client.Vertex{&vv.clientVertex},
|
2017-06-14 00:15:55 +00:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case ch <- ss:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
2017-06-15 23:08:20 +00:00
|
|
|
p, err := pr.Read(ctx)
|
2017-06-14 00:15:55 +00:00
|
|
|
if err != nil {
|
2017-06-16 22:49:03 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-14 00:15:55 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-06-16 22:49:03 +00:00
|
|
|
ss := &client.SolveStatus{}
|
|
|
|
for _, p := range p {
|
|
|
|
switch v := p.Sys.(type) {
|
|
|
|
case client.Vertex:
|
2017-07-21 17:58:24 +00:00
|
|
|
vtx, ok := p.Meta("parentVertex")
|
|
|
|
if ok {
|
|
|
|
v.Parent = vtx.(digest.Digest)
|
|
|
|
}
|
2017-06-16 22:49:03 +00:00
|
|
|
ss.Vertexes = append(ss.Vertexes, &v)
|
|
|
|
|
|
|
|
case progress.Status:
|
2017-06-17 05:12:27 +00:00
|
|
|
vtx, ok := p.Meta("vertex")
|
|
|
|
if !ok {
|
|
|
|
logrus.Warnf("progress %s status without vertex info", p.ID)
|
|
|
|
continue
|
|
|
|
}
|
2017-06-16 22:49:03 +00:00
|
|
|
vs := &client.VertexStatus{
|
2017-06-17 05:12:27 +00:00
|
|
|
ID: p.ID,
|
|
|
|
Vertex: vtx.(digest.Digest),
|
2017-06-16 22:49:03 +00:00
|
|
|
Name: v.Action,
|
|
|
|
Total: int64(v.Total),
|
|
|
|
Current: int64(v.Current),
|
|
|
|
Timestamp: p.Timestamp,
|
|
|
|
Started: v.Started,
|
|
|
|
Completed: v.Completed,
|
|
|
|
}
|
|
|
|
ss.Statuses = append(ss.Statuses, vs)
|
2017-06-19 05:24:41 +00:00
|
|
|
case client.VertexLog:
|
|
|
|
vtx, ok := p.Meta("vertex")
|
|
|
|
if !ok {
|
2017-06-30 03:34:29 +00:00
|
|
|
logrus.Warnf("progress %s log without vertex info", p.ID)
|
2017-06-19 05:24:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.Vertex = vtx.(digest.Digest)
|
|
|
|
v.Timestamp = p.Timestamp
|
|
|
|
ss.Logs = append(ss.Logs, &v)
|
2017-06-15 23:08:20 +00:00
|
|
|
}
|
2017-06-14 00:15:55 +00:00
|
|
|
}
|
2017-06-16 22:49:03 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case ch <- ss:
|
|
|
|
}
|
2017-06-14 00:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 04:25:51 +00:00
|
|
|
func walk(v Vertex) chan Vertex {
|
2017-06-14 00:15:55 +00:00
|
|
|
cache := make(map[digest.Digest]struct{})
|
2017-07-06 04:25:51 +00:00
|
|
|
ch := make(chan Vertex, 32)
|
2017-06-14 00:15:55 +00:00
|
|
|
|
2017-07-06 04:25:51 +00:00
|
|
|
var send func(v Vertex)
|
|
|
|
send = func(v Vertex) {
|
|
|
|
for _, v := range v.Inputs() {
|
|
|
|
send(v.Vertex)
|
2017-06-14 00:15:55 +00:00
|
|
|
}
|
2017-07-06 04:25:51 +00:00
|
|
|
if _, ok := cache[v.Digest()]; !ok {
|
|
|
|
ch <- v
|
|
|
|
cache[v.Digest()] = struct{}{}
|
2017-06-14 00:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2017-07-06 04:25:51 +00:00
|
|
|
send(v)
|
2017-06-14 00:15:55 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|