2019-10-03 19:36:35 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2020-07-09 04:28:34 +02:00
|
|
|
"time"
|
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2020-07-09 04:28:34 +02:00
|
|
|
|
|
|
|
"github.com/lbryio/reflector.go/internal/metrics"
|
2020-10-14 22:08:48 +02:00
|
|
|
|
|
|
|
"golang.org/x/sync/singleflight"
|
2019-10-03 19:36:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// CachingBlobStore combines two stores, typically a local and a remote store, to improve performance.
|
|
|
|
// Accessed blobs are stored in and retrieved from the cache. If they are not in the cache, they
|
|
|
|
// are retrieved from the origin and cached. Puts are cached and also forwarded to the origin.
|
|
|
|
type CachingBlobStore struct {
|
|
|
|
origin, cache BlobStore
|
2020-10-14 22:08:48 +02:00
|
|
|
|
|
|
|
sf *singleflight.Group
|
2019-10-03 19:36:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCachingBlobStore makes a new caching disk store and returns a pointer to it.
|
|
|
|
func NewCachingBlobStore(origin, cache BlobStore) *CachingBlobStore {
|
2020-10-14 22:08:48 +02:00
|
|
|
return &CachingBlobStore{origin: origin, cache: cache, sf: new(singleflight.Group)}
|
2019-10-03 19:36:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Has checks the cache and then the origin for a hash. It returns true if either store has it.
|
|
|
|
func (c *CachingBlobStore) Has(hash string) (bool, error) {
|
|
|
|
has, err := c.cache.Has(hash)
|
|
|
|
if has || err != nil {
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
return c.origin.Has(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get tries to get the blob from the cache first, falling back to the origin. If the blob comes
|
|
|
|
// from the origin, it is also stored in the cache.
|
2019-10-03 22:34:57 +02:00
|
|
|
func (c *CachingBlobStore) Get(hash string) (stream.Blob, error) {
|
2020-07-09 04:28:34 +02:00
|
|
|
start := time.Now()
|
2019-10-03 19:36:35 +02:00
|
|
|
blob, err := c.cache.Get(hash)
|
|
|
|
if err == nil || !errors.Is(err, ErrBlobNotFound) {
|
2020-07-09 04:28:34 +02:00
|
|
|
metrics.CacheHitCount.Inc()
|
2020-10-14 22:08:48 +02:00
|
|
|
rate := float64(len(blob)) / 1024 / 1024 / time.Since(start).Seconds()
|
2020-07-09 04:28:34 +02:00
|
|
|
metrics.RetrieverSpeed.With(map[string]string{metrics.MtrLabelSource: "cache"}).Set(rate)
|
2019-10-03 19:36:35 +02:00
|
|
|
return blob, err
|
|
|
|
}
|
|
|
|
|
2020-10-14 22:08:48 +02:00
|
|
|
metrics.CacheMissCount.Inc()
|
|
|
|
return c.getFromOrigin(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFromOrigin ensures that only one Get per hash is sent to the origin at a time,
|
|
|
|
// thereby protecting against https://en.wikipedia.org/wiki/Thundering_herd_problem
|
|
|
|
func (c *CachingBlobStore) getFromOrigin(hash string) (stream.Blob, error) {
|
|
|
|
metrics.CacheWaitingRequestsCount.Inc()
|
|
|
|
defer metrics.CacheWaitingRequestsCount.Dec()
|
|
|
|
originBlob, err, _ := c.sf.Do(hash, func() (interface{}, error) {
|
|
|
|
metrics.CacheOriginRequestsCount.Inc()
|
|
|
|
defer metrics.CacheOriginRequestsCount.Dec()
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
blob, err := c.origin.Get(hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rate := float64(len(blob)) / 1024 / 1024 / time.Since(start).Seconds()
|
|
|
|
metrics.RetrieverSpeed.With(map[string]string{metrics.MtrLabelSource: "origin"}).Set(rate)
|
|
|
|
|
|
|
|
err = c.cache.Put(hash, blob)
|
|
|
|
return blob, err
|
|
|
|
})
|
2019-10-03 19:36:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-14 22:08:48 +02:00
|
|
|
return originBlob.(stream.Blob), nil
|
2019-10-03 19:36:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put stores the blob in the origin and the cache
|
2019-10-03 22:34:57 +02:00
|
|
|
func (c *CachingBlobStore) Put(hash string, blob stream.Blob) error {
|
2019-10-03 19:36:35 +02:00
|
|
|
err := c.origin.Put(hash, blob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.cache.Put(hash, blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutSD stores the sd blob in the origin and the cache
|
2019-10-03 22:34:57 +02:00
|
|
|
func (c *CachingBlobStore) PutSD(hash string, blob stream.Blob) error {
|
2019-10-03 19:36:35 +02:00
|
|
|
err := c.origin.PutSD(hash, blob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.cache.PutSD(hash, blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes the blob from the origin and the cache
|
|
|
|
func (c *CachingBlobStore) Delete(hash string) error {
|
|
|
|
err := c.origin.Delete(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.cache.Delete(hash)
|
|
|
|
}
|