lotus/blockstore/autobatch.go

174 lines
4.2 KiB
Go
Raw Normal View History

2022-01-11 16:31:59 +00:00
package blockstore
import (
"context"
"sync"
2022-01-11 22:44:45 +00:00
"golang.org/x/xerrors"
2022-01-11 16:31:59 +00:00
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
)
// autolog is a logger for the autobatching blockstore. It is subscoped from the
// blockstore logger.
var autolog = log.Named("auto")
type AutobatchBlockstore struct {
bufferedBlks []block.Block
2022-01-11 22:17:34 +00:00
addedCids map[cid.Cid]struct{}
2022-01-11 16:31:59 +00:00
bufferedBlksLk sync.Mutex
flushCh chan struct{}
flushErr error
shutdownCh chan struct{}
flushCtx context.Context
2022-01-11 16:31:59 +00:00
backingBs Blockstore
bufferCapacity int
bufferSize int
}
func NewAutobatch(ctx context.Context, backingBs Blockstore, bufferCapacity int) *AutobatchBlockstore {
bs := &AutobatchBlockstore{
backingBs: backingBs,
bufferCapacity: bufferCapacity,
2022-01-11 22:17:34 +00:00
addedCids: make(map[cid.Cid]struct{}),
flushCtx: ctx,
2022-01-11 16:31:59 +00:00
}
go bs.flushWorker()
2022-01-11 16:31:59 +00:00
return bs
}
func (bs *AutobatchBlockstore) Put(ctx context.Context, blk block.Block) error {
bs.bufferedBlksLk.Lock()
2022-01-11 22:17:34 +00:00
_, ok := bs.addedCids[blk.Cid()]
if !ok {
bs.bufferedBlks = append(bs.bufferedBlks, blk)
bs.addedCids[blk.Cid()] = struct{}{}
bs.bufferSize += len(blk.RawData())
if bs.bufferSize >= bs.bufferCapacity {
// signal that a flush is appropriate, may be ignored
select {
case bs.flushCh <- struct{}{}:
default:
// do nothing
}
2022-01-11 22:17:34 +00:00
}
2022-01-11 16:31:59 +00:00
}
bs.bufferedBlksLk.Unlock()
return nil
}
func (bs *AutobatchBlockstore) flushWorker() {
for {
select {
case <-bs.flushCh:
putErr := bs.doFlush(bs.flushCtx)
if putErr != nil {
autolog.Errorf("FLUSH ERRORED: %w", putErr)
bs.flushErr = xerrors.Errorf("%w, put error: %w", bs.flushErr, putErr)
}
case <-bs.shutdownCh:
return
}
}
}
func (bs *AutobatchBlockstore) doFlush(ctx context.Context) error {
2022-01-11 16:31:59 +00:00
bs.bufferedBlksLk.Lock()
2022-01-11 22:44:45 +00:00
// We do NOT clear addedCids here, because its purpose is to expedite Puts
2022-01-11 16:31:59 +00:00
toFlush := bs.bufferedBlks
bs.bufferedBlks = []block.Block{}
bs.bufferedBlksLk.Unlock()
return bs.backingBs.PutMany(ctx, toFlush)
}
func (bs *AutobatchBlockstore) Flush(ctx context.Context) error {
return bs.doFlush(ctx)
}
func (bs *AutobatchBlockstore) Shutdown(ctx context.Context) error {
bs.shutdownCh <- struct{}{}
if bs.flushErr != nil {
return xerrors.Errorf("flushWorker errored: %w", bs.flushErr)
}
// one last flush in case it's needed
return bs.doFlush(ctx)
2022-01-11 22:44:45 +00:00
}
// May be very slow if the cid queried wasn't in the backingBs at the time of creation of this AutobatchBlockstore
func (bs *AutobatchBlockstore) Get(ctx context.Context, c cid.Cid) (block.Block, error) {
blk, err := bs.backingBs.Get(ctx, c)
if err == nil {
return blk, nil
}
if err != ErrNotFound {
return blk, err
}
bs.Flush(ctx)
return bs.backingBs.Get(ctx, c)
}
func (bs *AutobatchBlockstore) DeleteBlock(context.Context, cid.Cid) error {
// if we wanted to support this, we would have to:
// - flush
// - delete from the backingBs (if present)
// - remove from addedCids (if present)
// - if present in addedCids, also walk bufferedBlks and remove if present
return xerrors.New("deletion is unsupported")
}
func (bs *AutobatchBlockstore) DeleteMany(ctx context.Context, cids []cid.Cid) error {
// see note in DeleteBlock()
return xerrors.New("deletion is unsupported")
}
func (bs *AutobatchBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
_, err := bs.Get(ctx, c)
if err == nil {
return true, nil
}
if err == ErrNotFound {
return false, nil
}
return false, err
}
func (bs *AutobatchBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
blk, err := bs.Get(ctx, c)
if err != nil {
return 0, err
}
return len(blk.RawData()), nil
}
func (bs *AutobatchBlockstore) PutMany(ctx context.Context, blks []block.Block) error {
for _, blk := range blks {
if err := bs.Put(ctx, blk); err != nil {
return err
}
}
return nil
}
func (bs *AutobatchBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
bs.Flush(ctx)
return bs.backingBs.AllKeysChan(ctx)
}
func (bs *AutobatchBlockstore) HashOnRead(enabled bool) {
bs.backingBs.HashOnRead(enabled)
}
func (bs *AutobatchBlockstore) View(ctx context.Context, cid cid.Cid, callback func([]byte) error) error {
bs.Flush(ctx)
return bs.backingBs.View(ctx, cid, callback)
2022-01-11 16:31:59 +00:00
}