2020-10-29 17:39:53 +01:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lbryio/reflector.go/internal/metrics"
|
2021-01-09 05:08:20 +01:00
|
|
|
"github.com/lbryio/reflector.go/shared"
|
2020-10-29 17:39:53 +01:00
|
|
|
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
|
|
|
|
|
|
|
"golang.org/x/sync/singleflight"
|
|
|
|
)
|
|
|
|
|
|
|
|
func WithSingleFlight(component string, origin BlobStore) BlobStore {
|
|
|
|
return &singleflightStore{
|
|
|
|
BlobStore: origin,
|
|
|
|
component: component,
|
|
|
|
sf: new(singleflight.Group),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type singleflightStore struct {
|
|
|
|
BlobStore
|
|
|
|
|
|
|
|
component string
|
|
|
|
sf *singleflight.Group
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singleflightStore) Name() string {
|
|
|
|
return "sf_" + s.BlobStore.Name()
|
|
|
|
}
|
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
type getterResponse struct {
|
|
|
|
blob stream.Blob
|
|
|
|
stack shared.BlobTrace
|
|
|
|
}
|
|
|
|
|
2020-10-29 17:39:53 +01:00
|
|
|
// Get ensures that only one request per hash is sent to the origin at a time,
|
|
|
|
// thereby protecting against https://en.wikipedia.org/wiki/Thundering_herd_problem
|
2021-01-09 05:08:20 +01:00
|
|
|
func (s *singleflightStore) Get(hash string) (stream.Blob, shared.BlobTrace, error) {
|
|
|
|
start := time.Now()
|
2020-11-20 21:01:33 +01:00
|
|
|
metrics.CacheWaitingRequestsCount.With(metrics.CacheLabels(s.Name(), s.component)).Inc()
|
|
|
|
defer metrics.CacheWaitingRequestsCount.With(metrics.CacheLabels(s.Name(), s.component)).Dec()
|
2020-10-29 17:39:53 +01:00
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
gr, err, _ := s.sf.Do(hash, s.getter(hash))
|
2020-10-29 17:39:53 +01:00
|
|
|
if err != nil {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, shared.NewBlobTrace(time.Since(start), s.Name()), err
|
2020-10-29 17:39:53 +01:00
|
|
|
}
|
2021-01-09 05:08:20 +01:00
|
|
|
rsp := gr.(getterResponse)
|
|
|
|
return rsp.blob, rsp.stack, nil
|
2020-10-29 17:39:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getter returns a function that gets a blob from the origin
|
|
|
|
// only one getter per hash will be executing at a time
|
|
|
|
func (s *singleflightStore) getter(hash string) func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
2020-11-20 21:01:33 +01:00
|
|
|
metrics.CacheOriginRequestsCount.With(metrics.CacheLabels(s.Name(), s.component)).Inc()
|
|
|
|
defer metrics.CacheOriginRequestsCount.With(metrics.CacheLabels(s.Name(), s.component)).Dec()
|
2020-10-29 17:39:53 +01:00
|
|
|
|
|
|
|
start := time.Now()
|
2021-01-09 05:08:20 +01:00
|
|
|
blob, stack, err := s.BlobStore.Get(hash)
|
2020-10-29 17:39:53 +01:00
|
|
|
if err != nil {
|
2021-01-09 05:08:20 +01:00
|
|
|
return getterResponse{
|
|
|
|
blob: nil,
|
|
|
|
stack: stack.Stack(time.Since(start), s.Name()),
|
|
|
|
}, err
|
2020-10-29 17:39:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rate := float64(len(blob)) / 1024 / 1024 / time.Since(start).Seconds()
|
|
|
|
metrics.CacheRetrievalSpeed.With(map[string]string{
|
2020-11-20 21:01:33 +01:00
|
|
|
metrics.LabelCacheType: s.Name(),
|
2020-10-29 17:39:53 +01:00
|
|
|
metrics.LabelComponent: s.component,
|
|
|
|
metrics.LabelSource: "origin",
|
|
|
|
}).Set(rate)
|
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
return getterResponse{
|
|
|
|
blob: blob,
|
|
|
|
stack: stack.Stack(time.Since(start), s.Name()),
|
|
|
|
}, nil
|
2020-10-29 17:39:53 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-23 06:04:42 +01:00
|
|
|
|
|
|
|
// Shutdown shuts down the store gracefully
|
|
|
|
func (s *singleflightStore) Shutdown() {
|
|
|
|
s.BlobStore.Shutdown()
|
|
|
|
return
|
|
|
|
}
|