2020-11-03 22:04:44 +00:00
|
|
|
package blockstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
2022-06-28 11:09:59 +00:00
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
2022-06-14 15:00:51 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-11-03 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
// UnwrapFallbackStore takes a blockstore, and returns the underlying blockstore
|
|
|
|
// if it was a FallbackStore. Otherwise, it just returns the supplied store
|
|
|
|
// unmodified.
|
|
|
|
func UnwrapFallbackStore(bs Blockstore) (Blockstore, bool) {
|
|
|
|
if fbs, ok := bs.(*FallbackStore); ok {
|
|
|
|
return fbs.Blockstore, true
|
|
|
|
}
|
|
|
|
return bs, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// FallbackStore is a read-through store that queries another (potentially
|
|
|
|
// remote) source if the block is not found locally. If the block is found
|
|
|
|
// during the fallback, it stores it in the local store.
|
2020-11-03 22:04:44 +00:00
|
|
|
type FallbackStore struct {
|
2021-01-29 20:01:00 +00:00
|
|
|
Blockstore
|
2020-11-03 22:04:44 +00:00
|
|
|
|
2021-01-29 23:24:44 +00:00
|
|
|
lk sync.RWMutex
|
|
|
|
// missFn is the function that will be invoked on a local miss to pull the
|
|
|
|
// block from elsewhere.
|
|
|
|
missFn func(context.Context, cid.Cid) (blocks.Block, error)
|
2020-11-03 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:01:00 +00:00
|
|
|
var _ Blockstore = (*FallbackStore)(nil)
|
|
|
|
|
2021-01-29 23:24:44 +00:00
|
|
|
func (fbs *FallbackStore) SetFallback(missFn func(context.Context, cid.Cid) (blocks.Block, error)) {
|
2020-11-03 22:04:44 +00:00
|
|
|
fbs.lk.Lock()
|
|
|
|
defer fbs.lk.Unlock()
|
|
|
|
|
2021-01-29 23:24:44 +00:00
|
|
|
fbs.missFn = missFn
|
2020-11-03 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fbs *FallbackStore) getFallback(c cid.Cid) (blocks.Block, error) {
|
2021-02-28 22:48:36 +00:00
|
|
|
log.Warnf("fallbackstore: block not found locally, fetching from the network; cid: %s", c)
|
2020-11-03 22:04:44 +00:00
|
|
|
fbs.lk.RLock()
|
|
|
|
defer fbs.lk.RUnlock()
|
|
|
|
|
2021-01-29 23:24:44 +00:00
|
|
|
if fbs.missFn == nil {
|
2020-11-03 22:04:44 +00:00
|
|
|
// FallbackStore wasn't configured yet (chainstore/bitswap aren't up yet)
|
|
|
|
// Wait for a bit and retry
|
|
|
|
fbs.lk.RUnlock()
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
fbs.lk.RLock()
|
|
|
|
|
2021-01-29 23:24:44 +00:00
|
|
|
if fbs.missFn == nil {
|
|
|
|
log.Errorw("fallbackstore: missFn not configured yet")
|
2022-06-28 11:09:59 +00:00
|
|
|
return nil, ipld.ErrNotFound{Cid: c}
|
2020-11-03 22:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 120*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-01-29 23:24:44 +00:00
|
|
|
b, err := fbs.missFn(ctx, c)
|
2020-11-03 22:04:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// chain bitswap puts blocks in temp blockstore which is cleaned up
|
|
|
|
// every few min (to drop any messages we fetched but don't want)
|
|
|
|
// in this case we want to keep this block around
|
2021-11-19 01:50:25 +00:00
|
|
|
if err := fbs.Put(ctx, b); err != nil {
|
2020-11-03 22:04:44 +00:00
|
|
|
return nil, xerrors.Errorf("persisting fallback-fetched block: %w", err)
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (fbs *FallbackStore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
|
|
|
|
b, err := fbs.Blockstore.Get(ctx, c)
|
2022-06-28 11:09:59 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
2020-11-03 22:04:44 +00:00
|
|
|
return b, nil
|
2022-06-28 11:09:59 +00:00
|
|
|
case ipld.IsNotFound(err):
|
2020-11-03 22:04:44 +00:00
|
|
|
return fbs.getFallback(c)
|
|
|
|
default:
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (fbs *FallbackStore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
|
|
|
|
sz, err := fbs.Blockstore.GetSize(ctx, c)
|
2022-06-28 11:09:59 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
2020-11-03 22:04:44 +00:00
|
|
|
return sz, nil
|
2022-06-28 11:09:59 +00:00
|
|
|
case ipld.IsNotFound(err):
|
2020-11-03 22:04:44 +00:00
|
|
|
b, err := fbs.getFallback(c)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(b.RawData()), nil
|
|
|
|
default:
|
|
|
|
return sz, err
|
|
|
|
}
|
|
|
|
}
|