Skip to content

Commit

Permalink
Merge pull request #977 from tonistiigi/logs-dupes
Browse files Browse the repository at this point in the history
progress: avoid double logs when multiple targets build same step
  • Loading branch information
tonistiigi authored Mar 5, 2022
2 parents 1416bc1 + b77d786 commit 9fcea76
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
10 changes: 5 additions & 5 deletions util/progress/multiwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

func WithPrefix(w Writer, pfx string, force bool) Writer {
return &prefixed{
main: w,
pfx: pfx,
force: force,
Writer: w,
pfx: pfx,
force: force,
}
}

type prefixed struct {
main Writer
Writer
pfx string
force bool
}
Expand All @@ -26,7 +26,7 @@ func (p *prefixed) Write(v *client.SolveStatus) {
v.Name = addPrefix(p.pfx, v.Name)
}
}
p.main.Write(v)
p.Writer.Write(v)
}

func addPrefix(pfx, name string) string {
Expand Down
42 changes: 36 additions & 6 deletions util/progress/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"io"
"io/ioutil"
"os"
"sync"

"github.com/containerd/console"
"github.com/docker/buildx/util/logutil"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
)

Expand All @@ -21,10 +23,12 @@ const (
)

type Printer struct {
status chan *client.SolveStatus
done <-chan struct{}
err error
warnings []client.VertexWarning
status chan *client.SolveStatus
done <-chan struct{}
err error
warnings []client.VertexWarning
logMu sync.Mutex
logSourceMap map[digest.Digest]interface{}
}

func (p *Printer) Wait() error {
Expand All @@ -41,13 +45,39 @@ func (p *Printer) Warnings() []client.VertexWarning {
return p.warnings
}

func (p *Printer) ValidateLogSource(dgst digest.Digest, v interface{}) bool {
p.logMu.Lock()
defer p.logMu.Unlock()
src, ok := p.logSourceMap[dgst]
if ok {
if src == v {
return true
}
} else {
p.logSourceMap[dgst] = v
return true
}
return false
}

func (p *Printer) ClearLogSource(v interface{}) {
p.logMu.Lock()
defer p.logMu.Unlock()
for d := range p.logSourceMap {
if p.logSourceMap[d] == v {
delete(p.logSourceMap, d)
}
}
}

func NewPrinter(ctx context.Context, w io.Writer, out console.File, mode string) *Printer {
statusCh := make(chan *client.SolveStatus)
doneCh := make(chan struct{})

pw := &Printer{
status: statusCh,
done: doneCh,
status: statusCh,
done: doneCh,
logSourceMap: map[digest.Digest]interface{}{},
}

if v := os.Getenv("BUILDKIT_PROGRESS"); v != "" && mode == PrinterModeAuto {
Expand Down
14 changes: 14 additions & 0 deletions util/progress/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

type Writer interface {
Write(*client.SolveStatus)
ValidateLogSource(digest.Digest, interface{}) bool
ClearLogSource(interface{})
}

func Write(w Writer, name string, f func() error) {
Expand Down Expand Up @@ -47,8 +49,20 @@ func NewChannel(w Writer) (chan *client.SolveStatus, chan struct{}) {
v, ok := <-ch
if !ok {
close(done)
w.ClearLogSource(done)
return
}

if len(v.Logs) > 0 {
logs := make([]*client.VertexLog, 0, len(v.Logs))
for _, l := range v.Logs {
if w.ValidateLogSource(l.Vertex, done) {
logs = append(logs, l)
}
}
v.Logs = logs
}

w.Write(v)
}
}()
Expand Down

0 comments on commit 9fcea76

Please sign in to comment.