Use channels to trigger flushes in a dedicated goroutine

This commit is contained in:
Aayush Rajasekaran 2022-01-11 19:44:56 -05:00 committed by Jennifer Wang
parent b628958cf6
commit 2ba131e802
2 changed files with 51 additions and 9 deletions

View File

@ -18,7 +18,10 @@ type AutobatchBlockstore struct {
bufferedBlks []block.Block bufferedBlks []block.Block
addedCids map[cid.Cid]struct{} addedCids map[cid.Cid]struct{}
bufferedBlksLk sync.Mutex bufferedBlksLk sync.Mutex
flushLk sync.Mutex flushCh chan struct{}
flushErr error
shutdownCh chan struct{}
flushCtx context.Context
backingBs Blockstore backingBs Blockstore
bufferCapacity int bufferCapacity int
bufferSize int bufferSize int
@ -29,8 +32,11 @@ func NewAutobatch(ctx context.Context, backingBs Blockstore, bufferCapacity int)
backingBs: backingBs, backingBs: backingBs,
bufferCapacity: bufferCapacity, bufferCapacity: bufferCapacity,
addedCids: make(map[cid.Cid]struct{}), addedCids: make(map[cid.Cid]struct{}),
flushCtx: ctx,
} }
go bs.flushWorker()
return bs return bs
} }
@ -42,24 +48,54 @@ func (bs *AutobatchBlockstore) Put(ctx context.Context, blk block.Block) error {
bs.addedCids[blk.Cid()] = struct{}{} bs.addedCids[blk.Cid()] = struct{}{}
bs.bufferSize += len(blk.RawData()) bs.bufferSize += len(blk.RawData())
if bs.bufferSize >= bs.bufferCapacity { if bs.bufferSize >= bs.bufferCapacity {
// time to flush // signal that a flush is appropriate, may be ignored
go bs.Flush(ctx) select {
case bs.flushCh <- struct{}{}:
default:
// do nothing
}
} }
} }
bs.bufferedBlksLk.Unlock() bs.bufferedBlksLk.Unlock()
return nil return nil
} }
func (bs *AutobatchBlockstore) Flush(ctx context.Context) { func (bs *AutobatchBlockstore) flushWorker() {
bs.flushLk.Lock() for {
defer bs.flushLk.Unlock() 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 {
bs.bufferedBlksLk.Lock() bs.bufferedBlksLk.Lock()
// We do NOT clear addedCids here, because its purpose is to expedite Puts // We do NOT clear addedCids here, because its purpose is to expedite Puts
toFlush := bs.bufferedBlks toFlush := bs.bufferedBlks
bs.bufferedBlks = []block.Block{} bs.bufferedBlks = []block.Block{}
bs.bufferedBlksLk.Unlock() bs.bufferedBlksLk.Unlock()
err := bs.backingBs.PutMany(ctx, toFlush) return bs.backingBs.PutMany(ctx, toFlush)
autolog.Errorf("FLUSH ERRORED, maybe async: %w", err) }
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)
} }
// May be very slow if the cid queried wasn't in the backingBs at the time of creation of this AutobatchBlockstore // May be very slow if the cid queried wasn't in the backingBs at the time of creation of this AutobatchBlockstore

View File

@ -1298,7 +1298,13 @@ func upgradeActorsV7Common(
} }
// Persist the new tree. Blocks until the entire writeStore is in the state blockstore. // Persist the new tree. Blocks until the entire writeStore is in the state blockstore.
writeStore.Flush(ctx) if err := writeStore.Flush(ctx); err != nil {
return cid.Undef, xerrors.Errorf("failed to flush writestore: %w", err)
}
if err := writeStore.Shutdown(ctx); err != nil {
return cid.Undef, xerrors.Errorf("writeStore failed: %w", err)
}
return newRoot, nil return newRoot, nil
} }