2019-07-08 20:29:01 +00:00
|
|
|
package bufbstore
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-01 20:37:34 +00:00
|
|
|
"os"
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
block "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
2020-04-01 20:37:34 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-07-23 02:05:11 +00:00
|
|
|
|
|
|
|
bstore "github.com/filecoin-project/lotus/lib/blockstore"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
2020-04-01 20:37:34 +00:00
|
|
|
var log = logging.Logger("bufbs")
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
type BufferedBS struct {
|
|
|
|
read bstore.Blockstore
|
|
|
|
write bstore.Blockstore
|
2020-11-10 12:40:32 +00:00
|
|
|
|
|
|
|
readviewer bstore.Viewer
|
|
|
|
writeviewer bstore.Viewer
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBufferedBstore(base bstore.Blockstore) *BufferedBS {
|
2020-10-12 22:52:38 +00:00
|
|
|
var buf bstore.Blockstore
|
2020-04-01 20:37:34 +00:00
|
|
|
if os.Getenv("LOTUS_DISABLE_VM_BUF") == "iknowitsabadidea" {
|
|
|
|
log.Warn("VM BLOCKSTORE BUFFERING IS DISABLED")
|
|
|
|
buf = base
|
2020-10-12 22:52:38 +00:00
|
|
|
} else {
|
|
|
|
buf = bstore.NewTemporary()
|
2020-04-01 20:37:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 12:40:32 +00:00
|
|
|
bs := &BufferedBS{
|
2019-07-05 14:29:17 +00:00
|
|
|
read: base,
|
|
|
|
write: buf,
|
|
|
|
}
|
2020-11-10 12:40:32 +00:00
|
|
|
if v, ok := base.(bstore.Viewer); ok {
|
|
|
|
bs.readviewer = v
|
|
|
|
}
|
|
|
|
if v, ok := buf.(bstore.Viewer); ok {
|
|
|
|
bs.writeviewer = v
|
|
|
|
}
|
|
|
|
if (bs.writeviewer == nil) != (bs.readviewer == nil) {
|
|
|
|
log.Warnf("one of the stores is not viewable; running less efficiently")
|
|
|
|
}
|
|
|
|
return bs
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 23:51:55 +00:00
|
|
|
func NewTieredBstore(r bstore.Blockstore, w bstore.Blockstore) *BufferedBS {
|
|
|
|
return &BufferedBS{
|
|
|
|
read: r,
|
|
|
|
write: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:40:32 +00:00
|
|
|
var _ bstore.Blockstore = (*BufferedBS)(nil)
|
|
|
|
var _ bstore.Viewer = (*BufferedBS)(nil)
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
func (bs *BufferedBS) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
|
|
|
a, err := bs.read.AllKeysChan(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := bs.write.AllKeysChan(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make(chan cid.Cid)
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
for a != nil || b != nil {
|
|
|
|
select {
|
|
|
|
case val, ok := <-a:
|
|
|
|
if !ok {
|
|
|
|
a = nil
|
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case out <- val:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case val, ok := <-b:
|
|
|
|
if !ok {
|
|
|
|
b = nil
|
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case out <- val:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) DeleteBlock(c cid.Cid) error {
|
|
|
|
if err := bs.read.DeleteBlock(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs.write.DeleteBlock(c)
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:40:32 +00:00
|
|
|
func (bs *BufferedBS) View(c cid.Cid, callback func([]byte) error) error {
|
|
|
|
if bs.writeviewer == nil || bs.readviewer == nil {
|
2020-11-10 17:51:11 +00:00
|
|
|
// one of the stores isn't Viewer; fall back to pure Get behaviour.
|
2020-11-10 12:40:32 +00:00
|
|
|
blk, err := bs.Get(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return callback(blk.RawData())
|
|
|
|
}
|
|
|
|
|
|
|
|
// both stores are viewable.
|
|
|
|
if err := bs.writeviewer.View(c, callback); err == bstore.ErrNotFound {
|
|
|
|
// not found in write blockstore; fall through.
|
|
|
|
} else {
|
|
|
|
return err // propagate errors, or nil, i.e. found.
|
|
|
|
}
|
|
|
|
return bs.readviewer.View(c, callback)
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
func (bs *BufferedBS) Get(c cid.Cid) (block.Block, error) {
|
2020-11-03 22:02:01 +00:00
|
|
|
if out, err := bs.write.Get(c); err != nil {
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != bstore.ErrNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 22:02:01 +00:00
|
|
|
return bs.read.Get(c)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) GetSize(c cid.Cid) (int, error) {
|
2020-07-22 19:26:57 +00:00
|
|
|
s, err := bs.read.GetSize(c)
|
|
|
|
if err == bstore.ErrNotFound || s == 0 {
|
|
|
|
return bs.write.GetSize(c)
|
|
|
|
}
|
|
|
|
|
2020-07-23 08:23:44 +00:00
|
|
|
return s, err
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) Put(blk block.Block) error {
|
2020-11-03 22:02:01 +00:00
|
|
|
has, err := bs.read.Has(blk.Cid()) // TODO: consider dropping this check
|
2020-07-02 23:48:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if has {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
return bs.write.Put(blk)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) Has(c cid.Cid) (bool, error) {
|
2020-11-03 22:02:01 +00:00
|
|
|
has, err := bs.write.Has(c)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 22:02:01 +00:00
|
|
|
return bs.read.Has(c)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) HashOnRead(hor bool) {
|
|
|
|
bs.read.HashOnRead(hor)
|
|
|
|
bs.write.HashOnRead(hor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) PutMany(blks []block.Block) error {
|
|
|
|
return bs.write.PutMany(blks)
|
2019-07-05 14:36:08 +00:00
|
|
|
}
|
2019-07-08 20:29:01 +00:00
|
|
|
|
|
|
|
func (bs *BufferedBS) Read() bstore.Blockstore {
|
|
|
|
return bs.read
|
|
|
|
}
|