retry several times when failed to copy blob during the replication

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2020-08-23 21:31:17 +08:00
parent afab4169f6
commit be8cbf8944

View File

@ -17,7 +17,10 @@ package image
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"strconv"
"strings" "strings"
"time"
"github.com/docker/distribution/manifest/manifestlist" "github.com/docker/distribution/manifest/manifestlist"
@ -30,7 +33,15 @@ import (
trans "github.com/goharbor/harbor/src/replication/transfer" trans "github.com/goharbor/harbor/src/replication/transfer"
) )
var (
retry int
)
func init() { func init() {
retry, _ = strconv.Atoi(os.Getenv("COPY_BLOB_RETRY_COUNT"))
if retry <= 0 {
retry = 5
}
if err := trans.RegisterFactory(model.ResourceTypeImage, factory); err != nil { if err := trans.RegisterFactory(model.ResourceTypeImage, factory); err != nil {
log.Errorf("failed to register transfer factory: %v", err) log.Errorf("failed to register transfer factory: %v", err)
} }
@ -219,16 +230,33 @@ func (t *transfer) copyContent(content distribution.Descriptor, srcRepo, dstRepo
// the media type of the layer or config can be "application/octet-stream", // the media type of the layer or config can be "application/octet-stream",
// schema1.MediaTypeManifestLayer, schema2.MediaTypeLayer, schema2.MediaTypeImageConfig // schema1.MediaTypeManifestLayer, schema2.MediaTypeLayer, schema2.MediaTypeImageConfig
default: default:
return t.copyBlob(srcRepo, dstRepo, digest) return t.copyBlobWithRetry(srcRepo, dstRepo, digest)
} }
} }
func (t *transfer) copyBlobWithRetry(srcRepo, dstRepo, digest string) error {
var err error
for i, backoff := 1, 2*time.Second; i <= retry; i, backoff = i+1, backoff*2 {
t.logger.Infof("copying the blob %s(the %dth running)...", digest, i)
if err = t.copyBlob(srcRepo, dstRepo, digest); err == nil {
t.logger.Infof("copy the blob %s completed", digest)
return nil
}
t.logger.Errorf("failed to copy the blob %s: %v", digest, err)
if i == retry {
break
}
t.logger.Infof("will retry %v later", backoff)
time.Sleep(backoff)
}
return err
}
// copy the layer or image config from the source registry to destination // copy the layer or image config from the source registry to destination
func (t *transfer) copyBlob(srcRepo, dstRepo, digest string) error { func (t *transfer) copyBlob(srcRepo, dstRepo, digest string) error {
if t.shouldStop() { if t.shouldStop() {
return nil return nil
} }
t.logger.Infof("copying the blob %s...", digest)
exist, err := t.dst.BlobExist(dstRepo, digest) exist, err := t.dst.BlobExist(dstRepo, digest)
if err != nil { if err != nil {
t.logger.Errorf("failed to check the existence of blob %s on the destination registry: %v", digest, err) t.logger.Errorf("failed to check the existence of blob %s on the destination registry: %v", digest, err)
@ -245,11 +273,11 @@ func (t *transfer) copyBlob(srcRepo, dstRepo, digest string) error {
return err return err
} }
defer data.Close() defer data.Close()
if err = t.dst.PushBlob(dstRepo, digest, size, data); err != nil { if err = t.dst.PushBlob(dstRepo, digest, size, data); err != nil {
t.logger.Errorf("failed to pushing the blob %s: %v", digest, err) t.logger.Errorf("failed to pushing the blob %s, size %d: %v", digest, size, err)
return err return err
} }
t.logger.Infof("copy the blob %s completed", digest)
return nil return nil
} }