2022-01-11 16:31:59 +00:00
|
|
|
package blockstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2022-01-12 01:29:26 +00:00
|
|
|
"time"
|
2022-01-11 16:31:59 +00:00
|
|
|
|
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 {
|
2022-01-12 01:29:26 +00:00
|
|
|
// TODO: drop if memory consumption is too high
|
|
|
|
addedCids map[cid.Cid]struct{}
|
|
|
|
|
|
|
|
bufferedLk sync.Mutex
|
|
|
|
bufferedBlksOrdered []block.Block
|
|
|
|
bufferedBlksMap map[cid.Cid]block.Block
|
|
|
|
|
|
|
|
flushingLk sync.Mutex
|
|
|
|
flushingBlksMap map[cid.Cid]block.Block
|
|
|
|
|
|
|
|
flushCh chan struct{}
|
|
|
|
flushErr error
|
|
|
|
flushRetryDelay time.Duration
|
|
|
|
flushCtx context.Context
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
|
|
|
|
backingBs Blockstore
|
|
|
|
|
2022-01-11 16:31:59 +00:00
|
|
|
bufferCapacity int
|
|
|
|
bufferSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAutobatch(ctx context.Context, backingBs Blockstore, bufferCapacity int) *AutobatchBlockstore {
|
|
|
|
bs := &AutobatchBlockstore{
|
2022-01-12 01:29:26 +00:00
|
|
|
addedCids: make(map[cid.Cid]struct{}),
|
|
|
|
backingBs: backingBs,
|
|
|
|
bufferCapacity: bufferCapacity,
|
|
|
|
bufferedBlksMap: make(map[cid.Cid]block.Block),
|
|
|
|
flushingBlksMap: make(map[cid.Cid]block.Block),
|
|
|
|
flushCtx: ctx,
|
|
|
|
flushCh: make(chan struct{}, 1),
|
|
|
|
// could be made configable
|
|
|
|
flushRetryDelay: time.Second * 5,
|
2022-01-11 16:31:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 00:44:56 +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 {
|
2022-01-12 01:29:26 +00:00
|
|
|
bs.bufferedLk.Lock()
|
2022-01-11 22:17:34 +00:00
|
|
|
_, ok := bs.addedCids[blk.Cid()]
|
|
|
|
if !ok {
|
|
|
|
bs.addedCids[blk.Cid()] = struct{}{}
|
2022-01-12 01:29:26 +00:00
|
|
|
bs.bufferedBlksOrdered = append(bs.bufferedBlksOrdered, blk)
|
|
|
|
bs.bufferedBlksMap[blk.Cid()] = blk
|
2022-01-11 22:17:34 +00:00
|
|
|
bs.bufferSize += len(blk.RawData())
|
|
|
|
if bs.bufferSize >= bs.bufferCapacity {
|
2022-01-12 00:44:56 +00:00
|
|
|
// 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
|
|
|
}
|
2022-01-12 01:29:26 +00:00
|
|
|
bs.bufferedLk.Unlock()
|
2022-01-11 16:31:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 00:44:56 +00:00
|
|
|
func (bs *AutobatchBlockstore) flushWorker() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-bs.flushCh:
|
|
|
|
putErr := bs.doFlush(bs.flushCtx)
|
2022-01-12 01:29:26 +00:00
|
|
|
for putErr != nil {
|
|
|
|
select {
|
|
|
|
case <-bs.shutdownCh:
|
|
|
|
bs.flushErr = putErr
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
autolog.Errorf("FLUSH ERRORED: %w, retrying in %v", putErr, bs.flushRetryDelay)
|
|
|
|
time.Sleep(bs.flushRetryDelay)
|
|
|
|
putErr = bs.doFlush(bs.flushCtx)
|
|
|
|
}
|
2022-01-12 00:44:56 +00:00
|
|
|
}
|
|
|
|
case <-bs.shutdownCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *AutobatchBlockstore) doFlush(ctx context.Context) error {
|
2022-01-12 01:29:26 +00:00
|
|
|
bs.bufferedLk.Lock()
|
|
|
|
bs.flushingLk.Lock()
|
2022-01-11 22:44:45 +00:00
|
|
|
// We do NOT clear addedCids here, because its purpose is to expedite Puts
|
2022-01-12 01:29:26 +00:00
|
|
|
flushingBlksOrdered := bs.bufferedBlksOrdered
|
|
|
|
bs.flushingBlksMap = bs.bufferedBlksMap
|
|
|
|
bs.bufferedBlksOrdered = []block.Block{}
|
|
|
|
bs.bufferedBlksMap = make(map[cid.Cid]block.Block)
|
|
|
|
bs.bufferedLk.Unlock()
|
|
|
|
bs.flushingLk.Unlock()
|
|
|
|
return bs.backingBs.PutMany(ctx, flushingBlksOrdered)
|
2022-01-12 00:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *AutobatchBlockstore) Shutdown(ctx context.Context) error {
|
2022-01-12 01:29:26 +00:00
|
|
|
// request one last flush of the worker
|
|
|
|
bs.flushCh <- struct{}{}
|
|
|
|
// shutdown the flush worker
|
2022-01-12 00:44:56 +00:00
|
|
|
bs.shutdownCh <- struct{}{}
|
2022-01-12 01:29:26 +00:00
|
|
|
// if it ever errored, this method fails
|
2022-01-12 00:44:56 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *AutobatchBlockstore) Get(ctx context.Context, c cid.Cid) (block.Block, error) {
|
2022-01-12 01:29:26 +00:00
|
|
|
// may seem backward to check the backingBs first, but that is the likeliest case
|
2022-01-11 22:44:45 +00:00
|
|
|
blk, err := bs.backingBs.Get(ctx, c)
|
|
|
|
if err == nil {
|
|
|
|
return blk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != ErrNotFound {
|
|
|
|
return blk, err
|
|
|
|
}
|
|
|
|
|
2022-01-12 01:29:26 +00:00
|
|
|
bs.flushingLk.Lock()
|
|
|
|
v, ok := bs.flushingBlksMap[c]
|
|
|
|
bs.flushingLk.Unlock()
|
|
|
|
if ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bs.bufferedLk.Lock()
|
|
|
|
v, ok = bs.bufferedBlksMap[c]
|
|
|
|
bs.bufferedLk.Unlock()
|
|
|
|
if ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotFound
|
2022-01-11 22:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *AutobatchBlockstore) DeleteBlock(context.Context, cid.Cid) error {
|
|
|
|
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) {
|
2022-01-12 01:29:26 +00:00
|
|
|
return nil, xerrors.New("unsupported")
|
|
|
|
|
2022-01-11 22:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-01-12 01:29:26 +00:00
|
|
|
return xerrors.New("unsupported")
|
2022-01-11 16:31:59 +00:00
|
|
|
}
|