2015-05-15 22:54:04 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-05-15 22:54:04 +00:00
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
dcontext "github.com/distribution/distribution/v3/context"
|
|
|
|
prometheus "github.com/distribution/distribution/v3/metrics"
|
2017-08-11 22:31:16 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2015-05-15 22:54:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type cachedBlobStatter struct {
|
|
|
|
cache distribution.BlobDescriptorService
|
2015-05-27 17:52:22 +00:00
|
|
|
backend distribution.BlobDescriptorService
|
2015-05-15 22:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 11:43:55 +00:00
|
|
|
var (
|
|
|
|
// cacheRequestCount is the number of total cache requests received.
|
|
|
|
cacheRequestCount = prometheus.StorageNamespace.NewCounter("cache_requests", "The number of cache request received")
|
|
|
|
// cacheRequestCount is the number of total cache requests received.
|
|
|
|
cacheHitCount = prometheus.StorageNamespace.NewCounter("cache_hits", "The number of cache request received")
|
|
|
|
// cacheErrorCount is the number of cache request errors.
|
|
|
|
cacheErrorCount = prometheus.StorageNamespace.NewCounter("cache_errors", "The number of cache request errors")
|
|
|
|
)
|
2017-11-17 00:43:38 +00:00
|
|
|
|
2015-05-15 22:54:04 +00:00
|
|
|
// NewCachedBlobStatter creates a new statter which prefers a cache and
|
|
|
|
// falls back to a backend.
|
2015-05-27 17:52:22 +00:00
|
|
|
func NewCachedBlobStatter(cache distribution.BlobDescriptorService, backend distribution.BlobDescriptorService) distribution.BlobDescriptorService {
|
2015-05-15 22:54:04 +00:00
|
|
|
return &cachedBlobStatter{
|
|
|
|
cache: cache,
|
|
|
|
backend: backend,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cbds *cachedBlobStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
2019-04-09 11:43:55 +00:00
|
|
|
cacheRequestCount.Inc(1)
|
2015-05-15 22:54:04 +00:00
|
|
|
|
2019-11-19 23:31:25 +00:00
|
|
|
// try getting from cache
|
|
|
|
desc, cacheErr := cbds.cache.Stat(ctx, dgst)
|
|
|
|
if cacheErr == nil {
|
2019-04-09 11:43:55 +00:00
|
|
|
cacheHitCount.Inc(1)
|
2019-11-19 23:31:25 +00:00
|
|
|
return desc, nil
|
2015-05-15 22:54:04 +00:00
|
|
|
}
|
2019-11-19 23:31:25 +00:00
|
|
|
|
|
|
|
// couldn't get from cache; get from backend
|
|
|
|
desc, err := cbds.backend.Stat(ctx, dgst)
|
2015-05-15 22:54:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return desc, err
|
|
|
|
}
|
|
|
|
|
2019-11-19 23:31:25 +00:00
|
|
|
if cacheErr == distribution.ErrBlobUnknown {
|
|
|
|
if err := cbds.cache.SetDescriptor(ctx, dgst, desc); err != nil {
|
|
|
|
dcontext.GetLoggerWithField(ctx, "blob", dgst).WithError(err).Error("error from cache setting desc")
|
|
|
|
}
|
|
|
|
// we don't need to return cache error upstream if any. continue returning value from backend
|
2020-03-04 22:37:31 +00:00
|
|
|
} else {
|
|
|
|
// unknown error from cache. just log and error. do not store cache as it may be trigger many set calls
|
|
|
|
dcontext.GetLoggerWithField(ctx, "blob", dgst).WithError(cacheErr).Error("error from cache stat(ing) blob")
|
2019-04-09 11:43:55 +00:00
|
|
|
cacheErrorCount.Inc(1)
|
2015-05-15 22:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 23:31:25 +00:00
|
|
|
return desc, nil
|
2015-05-27 17:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cbds *cachedBlobStatter) Clear(ctx context.Context, dgst digest.Digest) error {
|
|
|
|
err := cbds.cache.Clear(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cbds.backend.Clear(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cbds *cachedBlobStatter) SetDescriptor(ctx context.Context, dgst digest.Digest, desc distribution.Descriptor) error {
|
|
|
|
if err := cbds.cache.SetDescriptor(ctx, dgst, desc); err != nil {
|
2019-11-19 23:31:25 +00:00
|
|
|
dcontext.GetLoggerWithField(ctx, "blob", dgst).WithError(err).Error("error from cache setting desc")
|
2015-05-27 17:52:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-05-15 22:54:04 +00:00
|
|
|
}
|