2023-05-31 12:26:23 -05:00
|
|
|
//go:build sync
|
|
|
|
// +build sync
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-31 12:26:23 -05:00
|
|
|
"errors"
|
2022-01-10 11:06:12 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
2021-06-08 15:11:18 -05:00
|
|
|
|
|
|
|
"github.com/containers/common/pkg/retry"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2022-12-22 13:19:42 -05:00
|
|
|
"zotregistry.io/zot/pkg/common"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
2022-11-15 01:21:49 -05:00
|
|
|
)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
type request struct {
|
|
|
|
repo string
|
|
|
|
reference string
|
|
|
|
// used for background retries, at most one background retry per service
|
|
|
|
serviceID int
|
|
|
|
isBackground bool
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
/*
|
|
|
|
a request can be an image/signature/sbom
|
|
|
|
|
|
|
|
keep track of all parallel requests, if two requests of same image/signature/sbom comes at the same time,
|
|
|
|
process just the first one, also keep track of all background retrying routines.
|
|
|
|
*/
|
|
|
|
type BaseOnDemand struct {
|
|
|
|
services []Service
|
|
|
|
// map[request]chan err
|
|
|
|
requestStore *sync.Map
|
|
|
|
log log.Logger
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func NewOnDemand(log log.Logger) *BaseOnDemand {
|
|
|
|
return &BaseOnDemand{log: log, requestStore: &sync.Map{}}
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func (onDemand *BaseOnDemand) Add(service Service) {
|
|
|
|
onDemand.services = append(onDemand.services, service)
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func (onDemand *BaseOnDemand) SyncImage(repo, reference string) error {
|
|
|
|
req := request{
|
|
|
|
repo: repo,
|
|
|
|
reference: reference,
|
|
|
|
}
|
2022-01-10 11:06:12 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
val, found := onDemand.requestStore.Load(req)
|
2022-01-10 11:06:12 -05:00
|
|
|
if found {
|
2023-05-31 12:26:23 -05:00
|
|
|
onDemand.log.Info().Str("repo", repo).Str("reference", reference).
|
|
|
|
Msg("image already demanded, waiting on channel")
|
|
|
|
|
|
|
|
syncResult, _ := val.(chan error)
|
2022-01-10 11:06:12 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err, ok := <-syncResult
|
2022-01-10 11:06:12 -05:00
|
|
|
// if channel closed exit
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
syncResult := make(chan error)
|
|
|
|
onDemand.requestStore.Store(req, syncResult)
|
|
|
|
|
|
|
|
defer onDemand.requestStore.Delete(req)
|
|
|
|
defer close(syncResult)
|
2022-01-10 11:06:12 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
go onDemand.syncImage(repo, reference, syncResult)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err, ok := <-syncResult
|
2022-01-10 11:06:12 -05:00
|
|
|
if !ok {
|
2021-12-07 13:26:26 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func (onDemand *BaseOnDemand) SyncReference(repo string, subjectDigestStr string, referenceType string) error {
|
|
|
|
var err error
|
2022-01-10 11:06:12 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
for _, service := range onDemand.services {
|
|
|
|
err = service.SetNextAvailableURL()
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
2023-05-31 12:26:23 -05:00
|
|
|
return err
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = service.SyncReference(repo, subjectDigestStr, referenceType)
|
|
|
|
if err != nil {
|
2021-06-08 15:11:18 -05:00
|
|
|
continue
|
2023-05-31 12:26:23 -05:00
|
|
|
} else {
|
|
|
|
return nil
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
2023-05-31 12:26:23 -05:00
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-10-25 07:05:03 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
func (onDemand *BaseOnDemand) syncImage(repo, reference string, syncResult chan error) {
|
|
|
|
var err error
|
|
|
|
for serviceID, service := range onDemand.services {
|
|
|
|
err = service.SetNextAvailableURL()
|
|
|
|
if err != nil {
|
|
|
|
syncResult <- err
|
2022-01-10 11:06:12 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
return
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
err = service.SyncImage(repo, reference)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrManifestNotFound) ||
|
|
|
|
errors.Is(err, zerr.ErrSyncImageFilteredOut) ||
|
|
|
|
errors.Is(err, zerr.ErrSyncImageNotSigned) {
|
|
|
|
continue
|
2023-03-07 12:58:42 -05:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
req := request{
|
|
|
|
repo: repo,
|
|
|
|
reference: reference,
|
|
|
|
serviceID: serviceID,
|
|
|
|
isBackground: true,
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
2023-03-07 12:58:42 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
// if there is already a background routine, skip
|
|
|
|
if _, requested := onDemand.requestStore.LoadOrStore(req, struct{}{}); requested {
|
2022-03-07 03:45:10 -05:00
|
|
|
continue
|
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
retryOptions := service.GetRetryOptions()
|
|
|
|
|
|
|
|
if retryOptions.MaxRetry > 0 {
|
|
|
|
// retry in background
|
|
|
|
go func(service Service) {
|
2022-01-10 11:06:12 -05:00
|
|
|
// remove image after syncing
|
|
|
|
defer func() {
|
2023-05-31 12:26:23 -05:00
|
|
|
onDemand.requestStore.Delete(req)
|
|
|
|
onDemand.log.Info().Str("repo", repo).Str("reference", reference).
|
|
|
|
Msg("sync routine for image exited")
|
2022-01-10 11:06:12 -05:00
|
|
|
}()
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
onDemand.log.Info().Str("repo", repo).Str(reference, "reference").Str("err", err.Error()).
|
|
|
|
Msg("sync routine: starting routine to copy image, because of error")
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
time.Sleep(retryOptions.Delay)
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
if err = retry.RetryIfNecessary(context.Background(), func() error {
|
|
|
|
err := service.SyncImage(repo, reference)
|
2022-01-10 11:06:12 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}, retryOptions); err != nil {
|
2023-05-31 12:26:23 -05:00
|
|
|
onDemand.log.Error().Str("errorType", common.TypeOf(err)).Str("repo", repo).Str("reference", reference).
|
|
|
|
Err(err).Msg("sync routine: error while copying image")
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
2023-05-31 12:26:23 -05:00
|
|
|
}(service)
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
2023-05-31 12:26:23 -05:00
|
|
|
} else {
|
|
|
|
break
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 12:26:23 -05:00
|
|
|
syncResult <- err
|
2022-11-15 01:21:49 -05:00
|
|
|
}
|