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
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2017-11-17 00:43:38 +00:00
|
|
|
prometheus "github.com/docker/distribution/metrics"
|
2017-08-11 22:31:16 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2015-05-15 22:54:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Metrics is used to hold metric counters
|
|
|
|
// related to the number of times a cache was
|
|
|
|
// hit or missed.
|
|
|
|
type Metrics struct {
|
|
|
|
Requests uint64
|
|
|
|
Hits uint64
|
|
|
|
Misses uint64
|
|
|
|
}
|
|
|
|
|
2017-08-15 01:30:46 +00:00
|
|
|
// Logger can be provided on the MetricsTracker to log errors.
|
|
|
|
//
|
|
|
|
// Usually, this is just a proxy to dcontext.GetLogger.
|
|
|
|
type Logger interface {
|
|
|
|
Errorf(format string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
2015-05-15 22:54:04 +00:00
|
|
|
// MetricsTracker represents a metric tracker
|
|
|
|
// which simply counts the number of hits and misses.
|
|
|
|
type MetricsTracker interface {
|
|
|
|
Hit()
|
|
|
|
Miss()
|
|
|
|
Metrics() Metrics
|
2017-08-15 01:30:46 +00:00
|
|
|
Logger(context.Context) Logger
|
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
|
|
|
tracker MetricsTracker
|
|
|
|
}
|
|
|
|
|
2017-11-17 00:43:38 +00:00
|
|
|
var (
|
|
|
|
// cacheCount is the number of total cache request received/hits/misses
|
|
|
|
cacheCount = prometheus.StorageNamespace.NewLabeledCounter("cache", "The number of cache request received", "type")
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCachedBlobStatterWithMetrics creates a new statter which prefers a cache and
|
|
|
|
// falls back to a backend. Hits and misses will send to the tracker.
|
2015-05-27 17:52:22 +00:00
|
|
|
func NewCachedBlobStatterWithMetrics(cache distribution.BlobDescriptorService, backend distribution.BlobDescriptorService, tracker MetricsTracker) distribution.BlobStatter {
|
2015-05-15 22:54:04 +00:00
|
|
|
return &cachedBlobStatter{
|
|
|
|
cache: cache,
|
|
|
|
backend: backend,
|
|
|
|
tracker: tracker,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cbds *cachedBlobStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
2017-11-17 00:43:38 +00:00
|
|
|
cacheCount.WithValues("Request").Inc(1)
|
2015-05-15 22:54:04 +00:00
|
|
|
desc, err := cbds.cache.Stat(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
if err != distribution.ErrBlobUnknown {
|
2017-08-15 01:30:46 +00:00
|
|
|
logErrorf(ctx, cbds.tracker, "error retrieving descriptor from cache: %v", err)
|
2015-05-15 22:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goto fallback
|
|
|
|
}
|
2017-11-17 00:43:38 +00:00
|
|
|
cacheCount.WithValues("Hit").Inc(1)
|
2015-05-15 22:54:04 +00:00
|
|
|
if cbds.tracker != nil {
|
|
|
|
cbds.tracker.Hit()
|
|
|
|
}
|
|
|
|
return desc, nil
|
|
|
|
fallback:
|
2017-11-17 00:43:38 +00:00
|
|
|
cacheCount.WithValues("Miss").Inc(1)
|
2015-05-15 22:54:04 +00:00
|
|
|
if cbds.tracker != nil {
|
|
|
|
cbds.tracker.Miss()
|
|
|
|
}
|
|
|
|
desc, err = cbds.backend.Stat(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return desc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cbds.cache.SetDescriptor(ctx, dgst, desc); err != nil {
|
2017-08-15 01:30:46 +00:00
|
|
|
logErrorf(ctx, cbds.tracker, "error adding descriptor %v to cache: %v", desc.Digest, err)
|
2015-05-15 22:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return desc, err
|
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 {
|
2017-08-15 01:30:46 +00:00
|
|
|
logErrorf(ctx, cbds.tracker, "error adding descriptor %v to cache: %v", desc.Digest, err)
|
2015-05-27 17:52:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-05-15 22:54:04 +00:00
|
|
|
}
|
2017-08-15 01:30:46 +00:00
|
|
|
|
|
|
|
func logErrorf(ctx context.Context, tracker MetricsTracker, format string, args ...interface{}) {
|
|
|
|
if tracker == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := tracker.Logger(ctx)
|
|
|
|
if logger == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Errorf(format, args...)
|
|
|
|
}
|