2015-11-13 19:59:01 -05:00
|
|
|
package xfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-03-01 13:56:05 -05:00
|
|
|
"github.com/docker/distribution"
|
2015-11-13 19:59:01 -05:00
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
"github.com/docker/docker/pkg/progress"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
const maxUploadAttempts = 5
|
|
|
|
|
|
|
|
// LayerUploadManager provides task management and progress reporting for
|
|
|
|
// uploads.
|
|
|
|
type LayerUploadManager struct {
|
|
|
|
tm TransferManager
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLayerUploadManager returns a new LayerUploadManager.
|
|
|
|
func NewLayerUploadManager(concurrencyLimit int) *LayerUploadManager {
|
|
|
|
return &LayerUploadManager{
|
|
|
|
tm: NewTransferManager(concurrencyLimit),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type uploadTransfer struct {
|
|
|
|
Transfer
|
|
|
|
|
2016-03-01 13:56:05 -05:00
|
|
|
remoteDescriptor distribution.Descriptor
|
|
|
|
err error
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// An UploadDescriptor references a layer that may need to be uploaded.
|
|
|
|
type UploadDescriptor interface {
|
|
|
|
// Key returns the key used to deduplicate uploads.
|
|
|
|
Key() string
|
|
|
|
// ID returns the ID for display purposes.
|
|
|
|
ID() string
|
|
|
|
// DiffID should return the DiffID for this layer.
|
|
|
|
DiffID() layer.DiffID
|
|
|
|
// Upload is called to perform the Upload.
|
2016-03-01 13:56:05 -05:00
|
|
|
Upload(ctx context.Context, progressOutput progress.Output) (distribution.Descriptor, error)
|
|
|
|
// SetRemoteDescriptor provides the distribution.Descriptor that was
|
|
|
|
// returned by Upload. This descriptor is not to be confused with
|
|
|
|
// the UploadDescriptor interface, which is used for internally
|
|
|
|
// identifying layers that are being uploaded.
|
|
|
|
SetRemoteDescriptor(descriptor distribution.Descriptor)
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upload is a blocking function which ensures the listed layers are present on
|
|
|
|
// the remote registry. It uses the string returned by the Key method to
|
|
|
|
// deduplicate uploads.
|
2015-12-10 19:27:58 -05:00
|
|
|
func (lum *LayerUploadManager) Upload(ctx context.Context, layers []UploadDescriptor, progressOutput progress.Output) error {
|
2015-11-13 19:59:01 -05:00
|
|
|
var (
|
|
|
|
uploads []*uploadTransfer
|
2016-03-01 13:56:05 -05:00
|
|
|
dedupDescriptors = make(map[string]*uploadTransfer)
|
2015-11-13 19:59:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, descriptor := range layers {
|
|
|
|
progress.Update(progressOutput, descriptor.ID(), "Preparing")
|
|
|
|
|
|
|
|
key := descriptor.Key()
|
|
|
|
if _, present := dedupDescriptors[key]; present {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
xferFunc := lum.makeUploadFunc(descriptor)
|
|
|
|
upload, watcher := lum.tm.Transfer(descriptor.Key(), xferFunc, progressOutput)
|
|
|
|
defer upload.Release(watcher)
|
|
|
|
uploads = append(uploads, upload.(*uploadTransfer))
|
2016-03-01 13:56:05 -05:00
|
|
|
dedupDescriptors[key] = upload.(*uploadTransfer)
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, upload := range uploads {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2015-12-10 19:27:58 -05:00
|
|
|
return ctx.Err()
|
2015-11-13 19:59:01 -05:00
|
|
|
case <-upload.Transfer.Done():
|
|
|
|
if upload.err != nil {
|
2015-12-10 19:27:58 -05:00
|
|
|
return upload.err
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 13:56:05 -05:00
|
|
|
for _, l := range layers {
|
|
|
|
l.SetRemoteDescriptor(dedupDescriptors[l.Key()].remoteDescriptor)
|
|
|
|
}
|
2015-11-13 19:59:01 -05:00
|
|
|
|
2015-12-10 19:27:58 -05:00
|
|
|
return nil
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lum *LayerUploadManager) makeUploadFunc(descriptor UploadDescriptor) DoFunc {
|
|
|
|
return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
|
|
|
|
u := &uploadTransfer{
|
|
|
|
Transfer: NewTransfer(),
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
close(progressChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
progressOutput := progress.ChanOutput(progressChan)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-start:
|
|
|
|
default:
|
|
|
|
progress.Update(progressOutput, descriptor.ID(), "Waiting")
|
|
|
|
<-start
|
|
|
|
}
|
|
|
|
|
|
|
|
retries := 0
|
|
|
|
for {
|
2016-03-01 13:56:05 -05:00
|
|
|
remoteDescriptor, err := descriptor.Upload(u.Transfer.Context(), progressOutput)
|
2015-11-13 19:59:01 -05:00
|
|
|
if err == nil {
|
2016-03-01 13:56:05 -05:00
|
|
|
u.remoteDescriptor = remoteDescriptor
|
2015-11-13 19:59:01 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an error was returned because the context
|
|
|
|
// was cancelled, we shouldn't retry.
|
|
|
|
select {
|
|
|
|
case <-u.Transfer.Context().Done():
|
|
|
|
u.err = err
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
retries++
|
|
|
|
if _, isDNR := err.(DoNotRetry); isDNR || retries == maxUploadAttempts {
|
|
|
|
logrus.Errorf("Upload failed: %v", err)
|
|
|
|
u.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Errorf("Upload failed, retrying: %v", err)
|
|
|
|
delay := retries * 5
|
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
|
|
|
|
|
selectLoop:
|
|
|
|
for {
|
2016-03-04 10:15:44 -05:00
|
|
|
progress.Updatef(progressOutput, descriptor.ID(), "Retrying in %d second%s", delay, (map[bool]string{true: "s"})[delay != 1])
|
2015-11-13 19:59:01 -05:00
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
delay--
|
|
|
|
if delay == 0 {
|
|
|
|
ticker.Stop()
|
|
|
|
break selectLoop
|
|
|
|
}
|
|
|
|
case <-u.Transfer.Context().Done():
|
|
|
|
ticker.Stop()
|
|
|
|
u.err = errors.New("upload cancelled during retry delay")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
}
|