Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mocks/origin/blobclient/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions mocks/origin/blobclient/clusterclient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions origin/blobclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Client interface {
DuplicateUploadBlob(namespace string, d core.Digest, blob io.Reader, delay time.Duration) error

DownloadBlob(ctx context.Context, namespace string, d core.Digest, dst io.Writer) error
PrefetchBlob(namespace string, d core.Digest) error
PrefetchBlob(ctx context.Context, namespace string, d core.Digest) error

ReplicateToRemote(namespace string, d core.Digest, remoteDNS string) error

Expand Down Expand Up @@ -270,15 +270,30 @@ func (c *HTTPClient) DownloadBlob(ctx context.Context, namespace string, d core.

// PrefetchBlob is an asynchronous, idempotent operation that preheats the origin's cache with the given blob.
// If the blob is not present, it is downloaded asynchronously. If the blob is present, this is a no-op.
func (c *HTTPClient) PrefetchBlob(namespace string, d core.Digest) error {
func (c *HTTPClient) PrefetchBlob(ctx context.Context, namespace string, d core.Digest) error {
ctx, span := c.tracer.Start(ctx, "blobclient.prefetch_blob",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("component", "origin-client"),
attribute.String("operation", "prefetch_blob"),
attribute.String("namespace", namespace),
attribute.String("blob.digest", d.Hex()),
),
)
defer span.End()

r, err := httputil.Post(
fmt.Sprintf("http://%s/namespace/%s/blobs/%s/prefetch", c.addr, url.PathEscape(namespace), d),
httputil.SendAcceptedCodes(http.StatusOK, http.StatusAccepted),
httputil.SendTLS(c.tls))
httputil.SendTLS(c.tls),
httputil.SendTracingContext(ctx))
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "prefetch failed")
return err
}
defer closers.Close(r.Body)
span.SetStatus(codes.Ok, "prefetch triggered")
return nil
}

Expand Down
32 changes: 27 additions & 5 deletions origin/blobclient/cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type ClusterClient interface {
CheckReadiness() error
UploadBlob(ctx context.Context, namespace string, d core.Digest, blob io.ReadSeeker) error
DownloadBlob(ctx context.Context, namespace string, d core.Digest, dst io.Writer) error
PrefetchBlob(namespace string, d core.Digest) error
PrefetchBlob(ctx context.Context, namespace string, d core.Digest) error
GetMetaInfo(namespace string, d core.Digest) (*core.MetaInfo, error)
Stat(namespace string, d core.Digest) (*core.BlobInfo, error)
OverwriteMetaInfo(d core.Digest, pieceLength int64) error
Expand Down Expand Up @@ -278,27 +278,49 @@ func (c *clusterClient) DownloadBlob(ctx context.Context, namespace string, d co

// PrefetchBlob preheats a blob in the origin cluster for downloading.
// Check [Client].PrefetchBlob's comment for more info.
func (c *clusterClient) PrefetchBlob(namespace string, d core.Digest) error {
func (c *clusterClient) PrefetchBlob(ctx context.Context, namespace string, d core.Digest) error {
ctx, span := otel.Tracer("kraken-origin-cluster").Start(ctx, "cluster.prefetch_blob",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("component", "origin-cluster-client"),
attribute.String("namespace", namespace),
attribute.String("blob.digest", d.Hex()),
),
)
Comment on lines +282 to +289
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The span attributes are missing the "operation" attribute that is present in other similar methods (UploadBlob and DownloadBlob) in this file. For consistency with the established tracing pattern in this codebase, consider adding attribute.String("operation", "prefetch_blob") to the attributes list.

Copilot uses AI. Check for mistakes.
defer span.End()

logger := log.WithTraceContext(ctx).With("namespace", namespace, "digest", d.Hex())
logger.Debug("Starting blob prefetch from origin cluster")

clients, err := c.resolver.Resolve(d)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "resolve clients failed")
return fmt.Errorf("resolve clients: %w", err)
}

var errs []error
for _, client := range clients {
err = client.PrefetchBlob(namespace, d)
err = client.PrefetchBlob(ctx, namespace, d)
if err == nil {
span.SetStatus(codes.Ok, "prefetch completed")
return nil
}

if httputil.IsNotFound(err) {
// no need to iterate over other origins
return ErrBlobNotFound
err = ErrBlobNotFound
span.RecordError(err)
span.SetStatus(codes.Error, "blob not found")
return err
}
errs = append(errs, err)
}

return fmt.Errorf("all origins unavailable: %w", errors.Join(errs...))
err = fmt.Errorf("all origins unavailable: %w", errors.Join(errs...))
span.RecordError(err)
span.SetStatus(codes.Error, "prefetch failed")
return err
}

// Owners returns the origin peers which own d.
Expand Down
2 changes: 1 addition & 1 deletion origin/blobserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func TestPrefetchHandler(t *testing.T) {

ensureHasBlob(t, cp.Provide(s.host), namespace, blob)

err := cp.Provide(master1).PrefetchBlob(namespace, blob.Digest)
err := cp.Provide(master1).PrefetchBlob(context.Background(), namespace, blob.Digest)
require.NoError(err)
}

Expand Down
4 changes: 1 addition & 3 deletions proxy/proxyserver/prefetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,6 @@ func (ph *PrefetchHandler) triggerPrefetchBlobs(input *prefetchInput) error {
)
defer span.End()

_ = ctx // PrefetchBlob doesn't accept context yet

var wg sync.WaitGroup
var mu sync.Mutex
var errList []error
Expand All @@ -490,7 +488,7 @@ func (ph *PrefetchHandler) triggerPrefetchBlobs(input *prefetchInput) error {
wg.Add(1)
go func(digest core.Digest) {
defer wg.Done()
err := ph.clusterClient.PrefetchBlob(input.namespace, digest)
err := ph.clusterClient.PrefetchBlob(ctx, input.namespace, digest)
if err != nil {
mu.Lock()
errList = append(errList, fmt.Errorf("digest %q, namespace %q, blob prefetch failure: %w", digest, input.namespace, err))
Expand Down
8 changes: 4 additions & 4 deletions proxy/proxyserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ func TestPrefetchV2(t *testing.T) {
mocks.tagClient.EXPECT().Get(tagRequest).Return(manifest, nil)
mocks.originClient.EXPECT().DownloadBlob(gomock.Any(), namespace, manifest, mockutil.MatchWriter(bs)).Return(nil)

mocks.originClient.EXPECT().PrefetchBlob(namespace, layers[1]).Return(nil)
mocks.originClient.EXPECT().PrefetchBlob(namespace, layers[2]).Return(nil)
mocks.originClient.EXPECT().PrefetchBlob(gomock.Any(), namespace, layers[1]).Return(nil)
mocks.originClient.EXPECT().PrefetchBlob(gomock.Any(), namespace, layers[2]).Return(nil)
res, err := httputil.Post(
fmt.Sprintf("http://%s/proxy/v2/registry/prefetch", addr),
httputil.SendBody(bytes.NewReader(b)))
Expand Down Expand Up @@ -278,8 +278,8 @@ func TestPrefetchV2OriginError(t *testing.T) {
mocks.tagClient.EXPECT().Get(tagRequest).Return(manifest, nil)
mocks.originClient.EXPECT().DownloadBlob(gomock.Any(), namespace, manifest, mockutil.MatchWriter(bs)).Return(nil)

mocks.originClient.EXPECT().PrefetchBlob(namespace, layers[1]).Return(errors.New("foo err"))
mocks.originClient.EXPECT().PrefetchBlob(namespace, layers[2]).Return(nil)
mocks.originClient.EXPECT().PrefetchBlob(gomock.Any(), namespace, layers[1]).Return(errors.New("foo err"))
mocks.originClient.EXPECT().PrefetchBlob(gomock.Any(), namespace, layers[2]).Return(nil)
_, err := httputil.Post(
fmt.Sprintf("http://%s/proxy/v2/registry/prefetch", addr),
httputil.SendBody(bytes.NewReader(b)))
Expand Down
Loading