Files
zot/pkg/extensions/sync/on_demand.go
T
Andrei Aaron 1fb2b67419 fix: minor fixes based on intermittent test failures (#3465)
1. preload busybox image to fix: https://github.com/project-zot/zot/actions/runs/18614431126/job/53077015870?pr=3465
2. stabilize test coverage in by using different error type: https://app.codecov.io/gh/project-zot/zot/pull/3444/indirect-changes
3. attempt to fx an intermitent sync test failure:
Failures:

  * /home/andaaron/zot/pkg/extensions/sync/sync_test.go
  Line 4857:
  Expected: digest.Digest("sha256:dc1377539a9db8bf077100bfa3118052feb6b5c67509ca09bdd841e4ac14c4cc")
  Actual:   digest.Digest("sha256:3a3fb31a422846a680f0a07b8b666bdcb1122d912d1adca79523c7bf2715996e")
  (Should equal)!

4. fix a race condition in sync by, I don't have a link, but this is the failure:

  * zotregistry.dev/zot/pkg/extensions/sync/sync_test.go
  Line 5963:
  Expected: 1
  Actual:   2
  (Should equal)!

1426 total assertions

--- FAIL: TestOnDemandPullsOnce (0.42s)
    sync_test.go:5921: Goroutine 0: Sending request to http://127.0.0.1:36421/v2/zot-test/manifests/0.0.1
    sync_test.go:5921: Goroutine 1: Sending request to http://127.0.0.1:36421/v2/zot-test/manifests/0.0.1
    sync_test.go:5921: Goroutine 4: Sending request to http://127.0.0.1:36421/v2/zot-test/manifests/0.0.1
    sync_test.go:5921: Goroutine 3: Sending request to http://127.0.0.1:36421/v2/zot-test/manifests/0.0.1
    sync_test.go:5921: Goroutine 2: Sending request to http://127.0.0.1:36421/v2/zot-test/manifests/0.0.1
FAIL
coverage: 21.4% of statements in ./...
FAIL	zotregistry.dev/zot/pkg/extensions/sync	255.189s

5. Fix flaky coverage in https://app.codecov.io/gh/project-zot/zot/pull/3465/indirect-changes

6. Stability fix for https://github.com/project-zot/zot/actions/runs/18632536285/job/53119244557?pr=3465

Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
2025-10-19 17:59:32 -07:00

219 lines
5.6 KiB
Go

//go:build sync
// +build sync
package sync
import (
"context"
"errors"
"sync"
zerr "zotregistry.dev/zot/v2/errors"
"zotregistry.dev/zot/v2/pkg/common"
"zotregistry.dev/zot/v2/pkg/log"
)
type request struct {
repo string
reference string
// used for background retries, at most one background retry per service
serviceID int
isBackground bool
}
/*
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
}
func NewOnDemand(log log.Logger) *BaseOnDemand {
return &BaseOnDemand{log: log, requestStore: &sync.Map{}}
}
func (onDemand *BaseOnDemand) Add(service Service) {
onDemand.services = append(onDemand.services, service)
}
func (onDemand *BaseOnDemand) SyncImage(ctx context.Context, repo, reference string) error {
req := request{
repo: repo,
reference: reference,
}
syncResult := make(chan error)
val, loaded := onDemand.requestStore.LoadOrStore(req, syncResult)
if loaded {
onDemand.log.Info().Str("repo", repo).Str("reference", reference).
Msg("image already demanded, waiting on channel")
syncResult, _ := val.(chan error)
err := <-syncResult
return err
}
defer onDemand.requestStore.Delete(req)
go onDemand.syncImage(ctx, repo, reference, syncResult)
err := <-syncResult
return err
}
func (onDemand *BaseOnDemand) SyncReferrers(ctx context.Context, repo string,
subjectDigestStr string, referenceTypes []string,
) error {
req := request{
repo: repo,
reference: subjectDigestStr,
}
syncResult := make(chan error)
val, loaded := onDemand.requestStore.LoadOrStore(req, syncResult)
if loaded {
onDemand.log.Info().Str("repo", repo).Str("reference", subjectDigestStr).
Msg("referrers for image already demanded, waiting on channel")
syncResult, _ := val.(chan error)
err := <-syncResult
return err
}
defer onDemand.requestStore.Delete(req)
go onDemand.syncReferrers(ctx, repo, subjectDigestStr, referenceTypes, syncResult)
err := <-syncResult
return err
}
func (onDemand *BaseOnDemand) syncReferrers(ctx context.Context, repo, subjectDigestStr string,
referenceTypes []string, syncResult chan error,
) {
defer close(syncResult)
var err error
for serviceID, service := range onDemand.services {
err = service.SyncReferrers(ctx, repo, subjectDigestStr, referenceTypes)
if err != nil {
if errors.Is(err, zerr.ErrManifestNotFound) ||
errors.Is(err, zerr.ErrSyncImageFilteredOut) ||
errors.Is(err, zerr.ErrSyncImageNotSigned) ||
// some public registries may return 401 for not found.
errors.Is(err, zerr.ErrUnauthorizedAccess) {
continue
}
req := request{
repo: repo,
reference: subjectDigestStr,
serviceID: serviceID,
isBackground: true,
}
// if there is already a background routine, skip
if _, requested := onDemand.requestStore.LoadOrStore(req, struct{}{}); requested {
continue
}
if service.CanRetryOnError() {
// retry in background
go func(service Service) {
// remove image after syncing
defer func() {
onDemand.requestStore.Delete(req)
onDemand.log.Info().Str("repo", repo).Str("reference", subjectDigestStr).
Msg("sync routine for image exited")
}()
onDemand.log.Info().Str("repo", repo).Str("reference", subjectDigestStr).Str("err", err.Error()).
Msg("sync routine: starting routine to copy image, because of error")
err := service.SyncReferrers(context.Background(), repo, subjectDigestStr, referenceTypes)
if err != nil {
onDemand.log.Error().Str("errorType", common.TypeOf(err)).Str("repo", repo).Str("reference", subjectDigestStr).
Err(err).Msg("sync routine: starting routine to retry copy image due to error")
}
}(service)
}
} else {
break
}
}
syncResult <- err
}
func (onDemand *BaseOnDemand) syncImage(ctx context.Context, repo, reference string, syncResult chan error) {
defer close(syncResult)
var err error
for serviceID, service := range onDemand.services {
err = service.SyncImage(ctx, repo, reference)
if err != nil {
if errors.Is(err, zerr.ErrManifestNotFound) ||
errors.Is(err, zerr.ErrSyncImageFilteredOut) ||
errors.Is(err, zerr.ErrSyncImageNotSigned) ||
// some public registries may return 401 for not found.
errors.Is(err, zerr.ErrUnauthorizedAccess) {
continue
}
req := request{
repo: repo,
reference: reference,
serviceID: serviceID,
isBackground: true,
}
// if there is already a background routine, skip
if _, requested := onDemand.requestStore.LoadOrStore(req, struct{}{}); requested {
continue
}
if service.CanRetryOnError() {
retryErr := err
// retry in background
go func(service Service) {
// remove image after syncing
defer func() {
onDemand.requestStore.Delete(req)
onDemand.log.Info().Str("repo", repo).Str("reference", reference).
Msg("sync routine for image exited")
}()
onDemand.log.Info().Str("repo", repo).Str("reference", reference).Str("err", retryErr.Error()).
Msg("sync routine: starting routine to retry copy image due to error")
err := service.SyncImage(context.Background(), repo, reference)
if err != nil {
onDemand.log.Error().Str("errorType", common.TypeOf(err)).Str("repo", repo).Str("reference", reference).
Err(err).Msg("sync routine: error while copying image")
}
}(service)
}
} else {
break
}
}
syncResult <- err
}