2018-01-31 02:15:21 +01:00
|
|
|
package store
|
|
|
|
|
2018-02-02 22:49:20 +01:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-09-20 17:24:36 +02:00
|
|
|
"sync"
|
2021-01-09 05:08:20 +01:00
|
|
|
"time"
|
2018-02-02 22:49:20 +01:00
|
|
|
|
2020-12-23 23:08:13 +01:00
|
|
|
"github.com/lbryio/reflector.go/db"
|
2021-01-09 05:08:20 +01:00
|
|
|
"github.com/lbryio/reflector.go/shared"
|
2019-10-03 22:34:57 +02:00
|
|
|
|
2021-07-20 02:09:14 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
|
|
|
|
2018-09-20 17:24:36 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-02-02 22:49:20 +01:00
|
|
|
)
|
2018-01-31 02:15:21 +01:00
|
|
|
|
2019-10-03 22:58:17 +02:00
|
|
|
// DBBackedStore is a store that's backed by a DB. The DB contains data about what's in the store.
|
|
|
|
type DBBackedStore struct {
|
2020-12-22 20:53:48 +01:00
|
|
|
blobs BlobStore
|
|
|
|
db *db.SQL
|
|
|
|
blockedMu sync.RWMutex
|
|
|
|
blocked map[string]bool
|
|
|
|
deleteOnMiss bool
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:58:17 +02:00
|
|
|
// NewDBBackedStore returns an initialized store pointer.
|
2020-12-22 20:53:48 +01:00
|
|
|
func NewDBBackedStore(blobs BlobStore, db *db.SQL, deleteOnMiss bool) *DBBackedStore {
|
|
|
|
return &DBBackedStore{blobs: blobs, db: db, deleteOnMiss: deleteOnMiss}
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2020-10-22 19:49:02 +02:00
|
|
|
const nameDBBacked = "db-backed"
|
|
|
|
|
|
|
|
// Name is the cache type name
|
|
|
|
func (d *DBBackedStore) Name() string { return nameDBBacked }
|
|
|
|
|
2018-08-20 23:50:39 +02:00
|
|
|
// Has returns true if the blob is in the store
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) Has(hash string) (bool, error) {
|
2021-01-05 17:36:33 +01:00
|
|
|
return d.db.HasBlob(hash, false)
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2018-08-20 23:50:39 +02:00
|
|
|
// Get gets the blob
|
2021-01-09 05:08:20 +01:00
|
|
|
func (d *DBBackedStore) Get(hash string) (stream.Blob, shared.BlobTrace, error) {
|
|
|
|
start := time.Now()
|
2021-01-05 17:36:33 +01:00
|
|
|
has, err := d.db.HasBlob(hash, true)
|
2020-10-05 20:05:00 +02:00
|
|
|
if err != nil {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, shared.NewBlobTrace(time.Since(start), d.Name()), err
|
2020-10-05 20:05:00 +02:00
|
|
|
}
|
|
|
|
if !has {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, shared.NewBlobTrace(time.Since(start), d.Name()), ErrBlobNotFound
|
2020-10-05 20:05:00 +02:00
|
|
|
}
|
2020-12-30 04:24:11 +01:00
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
b, stack, err := d.blobs.Get(hash)
|
2021-01-05 17:36:33 +01:00
|
|
|
if d.deleteOnMiss && errors.Is(err, ErrBlobNotFound) {
|
|
|
|
e2 := d.Delete(hash)
|
|
|
|
if e2 != nil {
|
|
|
|
log.Errorf("error while deleting blob from db: %s", errors.FullTrace(err))
|
2020-12-22 20:53:48 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-05 20:05:00 +02:00
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
return b, stack.Stack(time.Since(start), d.Name()), err
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Put stores the blob in the S3 store and stores the blob information in the DB.
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) Put(hash string, blob stream.Blob) error {
|
|
|
|
err := d.blobs.Put(hash, blob)
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-02 22:49:20 +01:00
|
|
|
return d.db.AddBlob(hash, len(blob), true)
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// PutSD stores the SDBlob in the S3 store. It will return an error if the sd blob is missing the stream hash or if
|
|
|
|
// there is an error storing the blob information in the DB.
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) PutSD(hash string, blob stream.Blob) error {
|
2018-08-07 22:51:02 +02:00
|
|
|
var blobContents db.SdBlob
|
2018-02-02 22:49:20 +01:00
|
|
|
err := json.Unmarshal(blob, &blobContents)
|
|
|
|
if err != nil {
|
2020-02-25 21:49:51 +01:00
|
|
|
return errors.Err(err)
|
2018-02-02 22:49:20 +01:00
|
|
|
}
|
|
|
|
if blobContents.StreamHash == "" {
|
2018-03-01 22:28:06 +01:00
|
|
|
return errors.Err("sd blob is missing stream hash")
|
2018-02-02 22:49:20 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:58:17 +02:00
|
|
|
err = d.blobs.PutSD(hash, blob)
|
2018-02-02 22:49:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.db.AddSDBlob(hash, len(blob), blobContents)
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
2018-07-26 16:25:47 +02:00
|
|
|
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) Delete(hash string) error {
|
|
|
|
err := d.blobs.Delete(hash)
|
2018-09-11 13:41:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.db.Delete(hash)
|
|
|
|
}
|
|
|
|
|
2018-09-20 17:24:36 +02:00
|
|
|
// Block deletes the blob and prevents it from being uploaded in the future
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) Block(hash string) error {
|
2018-09-20 17:24:36 +02:00
|
|
|
if blocked, err := d.isBlocked(hash); blocked || err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("blocking %s", hash)
|
|
|
|
|
|
|
|
err := d.db.Block(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-05 17:36:33 +01:00
|
|
|
has, err := d.db.HasBlob(hash, false)
|
2018-09-20 17:24:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if has {
|
2019-10-03 22:58:17 +02:00
|
|
|
err = d.blobs.Delete(hash)
|
2018-09-20 17:24:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.db.Delete(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.markBlocked(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wants returns false if the hash exists or is blocked, true otherwise
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) Wants(hash string) (bool, error) {
|
2019-07-02 15:21:26 +02:00
|
|
|
blocked, err := d.isBlocked(hash)
|
|
|
|
if blocked || err != nil {
|
2018-09-20 17:24:36 +02:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2019-07-02 15:21:26 +02:00
|
|
|
has, err := d.Has(hash)
|
|
|
|
return !has, err
|
2018-09-20 17:24:36 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 02:17:02 +02:00
|
|
|
// MissingBlobsForKnownStream returns missing blobs for an existing stream
|
|
|
|
// WARNING: if the stream does NOT exist, no blob hashes will be returned, which looks
|
|
|
|
// like no blobs are missing
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) MissingBlobsForKnownStream(sdHash string) ([]string, error) {
|
2018-08-16 02:17:02 +02:00
|
|
|
return d.db.MissingBlobsForKnownStream(sdHash)
|
2018-07-26 16:25:47 +02:00
|
|
|
}
|
2018-09-20 17:24:36 +02:00
|
|
|
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) markBlocked(hash string) error {
|
2018-09-20 17:24:36 +02:00
|
|
|
err := d.initBlocked()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.blockedMu.Lock()
|
|
|
|
defer d.blockedMu.Unlock()
|
|
|
|
|
|
|
|
d.blocked[hash] = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) isBlocked(hash string) (bool, error) {
|
2018-09-20 17:24:36 +02:00
|
|
|
err := d.initBlocked()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.blockedMu.RLock()
|
|
|
|
defer d.blockedMu.RUnlock()
|
|
|
|
|
|
|
|
return d.blocked[hash], nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:58:17 +02:00
|
|
|
func (d *DBBackedStore) initBlocked() error {
|
2018-09-20 17:24:36 +02:00
|
|
|
// first check without blocking since this is the most likely scenario
|
|
|
|
if d.blocked != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.blockedMu.Lock()
|
|
|
|
defer d.blockedMu.Unlock()
|
|
|
|
|
|
|
|
// check again in case of race condition
|
|
|
|
if d.blocked != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
d.blocked, err = d.db.GetBlocked()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2020-12-23 06:04:42 +01:00
|
|
|
|
|
|
|
// Shutdown shuts down the store gracefully
|
|
|
|
func (d *DBBackedStore) Shutdown() {
|
|
|
|
d.blobs.Shutdown()
|
|
|
|
}
|