lotus/blockstore/splitstore/splitstore.go

1392 lines
32 KiB
Go
Raw Normal View History

package splitstore
import (
2021-03-13 10:00:28 +00:00
"bytes"
"context"
2020-11-24 17:41:07 +00:00
"encoding/binary"
"errors"
"sync"
2020-11-29 13:10:30 +00:00
"sync/atomic"
2020-11-25 08:11:42 +00:00
"time"
2021-03-03 08:56:41 +00:00
"go.uber.org/multierr"
"golang.org/x/xerrors"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
2020-11-24 17:41:07 +00:00
dstore "github.com/ipfs/go-datastore"
logging "github.com/ipfs/go-log/v2"
2021-03-13 10:00:28 +00:00
cbg "github.com/whyrusleeping/cbor-gen"
"github.com/filecoin-project/go-state-types/abi"
2021-03-01 07:25:52 +00:00
bstore "github.com/filecoin-project/lotus/blockstore"
2020-11-24 17:15:38 +00:00
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
2021-03-05 09:54:06 +00:00
"github.com/filecoin-project/lotus/metrics"
"github.com/filecoin-project/specs-actors/v2/actors/builtin"
2021-03-05 09:54:06 +00:00
"go.opencensus.io/stats"
)
2021-02-28 11:51:42 +00:00
var (
// CompactionThreshold is the number of epochs that need to have elapsed
// from the previously compacted epoch to trigger a new compaction.
//
// |················· CompactionThreshold ··················|
// | |
2021-06-17 17:27:37 +00:00
// =======‖≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡‖----------|------------------------»
// | | | chain --> ↑__ current epoch
// | archived epochs ___↑ |
// | ↑________ CompactionBoundary
// ↑__ CompactionSlack
//
// === :: cold (already archived)
// ≡≡≡ :: to be archived in this compaction
// --- :: hot
2021-06-21 11:50:03 +00:00
CompactionThreshold = 7 * build.Finality
// CompactionBoundary is the number of epochs from the current epoch at which
2021-03-13 10:00:28 +00:00
// we will walk the chain for live objects.
2021-06-21 11:50:03 +00:00
CompactionBoundary = 4 * build.Finality
2021-06-17 17:27:37 +00:00
// CompactionSlack is the number of epochs from the compaction boundary to the beginning
// of the cold epoch.
2021-06-21 11:50:03 +00:00
CompactionSlack = 2 * build.Finality
2021-06-17 17:27:37 +00:00
// SyncGapTime is the time delay from a tipset's min timestamp before we decide
// there is a sync gap
SyncGapTime = time.Minute
)
2020-11-24 17:26:28 +00:00
var (
// baseEpochKey stores the base epoch (last compaction epoch) in the
// metadata store.
baseEpochKey = dstore.NewKey("/splitstore/baseEpoch")
// warmupEpochKey stores whether a hot store warmup has been performed.
// On first start, the splitstore will walk the state tree and will copy
// all active blocks into the hotstore.
warmupEpochKey = dstore.NewKey("/splitstore/warmupEpoch")
2021-03-02 08:04:02 +00:00
2021-03-05 08:00:17 +00:00
// markSetSizeKey stores the current estimate for the mark set size.
// this is first computed at warmup and updated in every compaction
markSetSizeKey = dstore.NewKey("/splitstore/markSetSize")
2021-03-02 08:04:02 +00:00
log = logging.Logger("splitstore")
2021-06-22 07:10:24 +00:00
// set this to true if you are debugging the splitstore to enable debug logging
enableDebugLog = false
// set this to true if you want to track origin stack traces in the write log
enableDebugLogWriteTraces = false
)
2020-11-24 17:41:07 +00:00
2021-03-02 16:59:00 +00:00
const (
batchSize = 16384
defaultColdPurgeSize = 7_000_000
)
2021-03-01 18:30:15 +00:00
type Config struct {
// TrackingStore is the type of tracking store to use.
//
2021-03-05 17:55:32 +00:00
// Supported values are: "bolt" (default if omitted), "mem" (for tests and readonly access).
2021-02-28 07:59:11 +00:00
TrackingStoreType string
// MarkSetType is the type of mark set to use.
//
2021-03-02 08:04:02 +00:00
// Supported values are: "bloom" (default if omitted), "bolt".
MarkSetType string
// HotHeaders indicates whether to keep chain block headers in hotstore or not.
// This is necessary, and automatically set by DI in lotus node construction, if
// you are running with a noop coldstore.
HotHeaders bool
// SkipMoveColdBlocks indicates whether to skip moving cold blocks to the coldstore.
// If the splitstore is running with a noop coldstore then this option is set to true
// which skips moving (as it is a noop, but still takes time to read all the cold objects)
// and directly purges cold blocks.
SkipMoveColdBlocks bool
}
// ChainAccessor allows the Splitstore to access the chain. It will most likely
// be a ChainStore at runtime.
type ChainAccessor interface {
2021-03-19 10:17:32 +00:00
GetGenesis() (*types.BlockHeader, error)
GetTipsetByHeight(context.Context, abi.ChainEpoch, *types.TipSet, bool) (*types.TipSet, error)
GetHeaviestTipSet() *types.TipSet
SubscribeHeadChanges(change func(revert []*types.TipSet, apply []*types.TipSet) error)
}
type SplitStore struct {
compacting int32 // compaction (or warmp up) in progress
critsection int32 // compaction critical section
closing int32 // the split store is closing
2020-11-29 13:10:30 +00:00
cfg *Config
baseEpoch abi.ChainEpoch
warmupEpoch abi.ChainEpoch
writeEpoch abi.ChainEpoch
2020-11-29 13:10:30 +00:00
2021-03-02 16:59:00 +00:00
coldPurgeSize int
2020-11-29 13:10:30 +00:00
mx sync.Mutex
curTs *types.TipSet
chain ChainAccessor
ds dstore.Datastore
hot bstore.Blockstore
cold bstore.Blockstore
tracker TrackingStore
2020-11-24 17:26:28 +00:00
env MarkSetEnv
2021-02-28 19:35:18 +00:00
markSetSize int64
ctx context.Context
cancel func()
2021-06-21 12:17:00 +00:00
debug *debugLog
// protection for concurrent read/writes during compaction
txnLk sync.RWMutex
txnEnv MarkSetEnv
txnProtect MarkSet
// pending write set
pendingWrites map[cid.Cid]struct{}
pendingWritesImplicit map[cid.Cid]struct{}
}
var _ bstore.Blockstore = (*SplitStore)(nil)
2021-03-01 17:39:00 +00:00
// Open opens an existing splistore, or creates a new splitstore. The splitstore
// is backed by the provided hot and cold stores. The returned SplitStore MUST be
// attached to the ChainStore with Start in order to trigger compaction.
func Open(path string, ds dstore.Datastore, hot, cold bstore.Blockstore, cfg *Config) (*SplitStore, error) {
2020-12-01 15:17:34 +00:00
// the tracking store
tracker, err := OpenTrackingStore(path, cfg.TrackingStoreType)
2020-12-01 15:17:34 +00:00
if err != nil {
return nil, err
}
// the markset env
env, err := OpenMarkSetEnv(path, cfg.MarkSetType)
if err != nil {
_ = tracker.Close()
return nil, err
2020-12-01 15:17:34 +00:00
}
// the txn markset env
2021-06-25 16:41:31 +00:00
txnEnv, err := OpenMarkSetEnv(path, "mapts")
if err != nil {
_ = tracker.Close()
_ = env.Close()
return nil, err
}
2020-12-01 15:17:34 +00:00
// and now we can make a SplitStore
ss := &SplitStore{
cfg: cfg,
ds: ds,
hot: hot,
cold: cold,
tracker: tracker,
env: env,
txnEnv: txnEnv,
2021-03-02 16:59:00 +00:00
coldPurgeSize: defaultColdPurgeSize,
pendingWrites: make(map[cid.Cid]struct{}),
2021-03-02 16:59:00 +00:00
}
ss.ctx, ss.cancel = context.WithCancel(context.Background())
2021-06-22 07:10:24 +00:00
if enableDebugLog {
ss.debug, err = openDebugLog(path)
if err != nil {
return nil, err
}
}
2020-12-01 15:17:34 +00:00
return ss, nil
}
// Blockstore interface
2021-03-01 17:39:00 +00:00
func (s *SplitStore) DeleteBlock(_ cid.Cid) error {
// afaict we don't seem to be using this method, so it's not implemented
2020-11-24 22:01:10 +00:00
return errors.New("DeleteBlock not implemented on SplitStore; don't do this Luke!") //nolint
}
2021-03-02 14:45:45 +00:00
func (s *SplitStore) DeleteMany(_ []cid.Cid) error {
// afaict we don't seem to be using this method, so it's not implemented
return errors.New("DeleteMany not implemented on SplitStore; don't do this Luke!") //nolint
}
func (s *SplitStore) Has(c cid.Cid) (bool, error) {
s.txnLk.RLock()
defer s.txnLk.RUnlock()
has, err := s.hot.Has(c)
if err != nil {
return has, err
}
if has {
// treat it as an implicit Write, absence options -- the vm uses this check to avoid duplicate
// writes on Flush. When we have options in the API, the vm can explicitly signal that this is
// an implicit Write.
s.trackWrite(c, true)
// also make sure the object is considered live during compaction in case we have already
// flushed pending writes and started compaction
trackErr := s.trackTxnRef(c, true)
2021-07-02 07:09:12 +00:00
// if we failed to track the object and all its dependencies, then return false so as
// to cause the vm to recompute
return trackErr == nil, nil
}
return s.cold.Has(c)
}
func (s *SplitStore) Get(cid cid.Cid) (blocks.Block, error) {
s.txnLk.RLock()
defer s.txnLk.RUnlock()
blk, err := s.hot.Get(cid)
switch err {
case nil:
err = s.trackTxnRef(cid, false)
return blk, err
case bstore.ErrNotFound:
2021-06-22 07:10:24 +00:00
s.mx.Lock()
2021-06-22 07:56:35 +00:00
warmup := s.warmupEpoch > 0
2021-06-22 07:10:24 +00:00
curTs := s.curTs
s.mx.Unlock()
2021-06-22 07:56:35 +00:00
if warmup {
s.debug.LogReadMiss(curTs, cid)
}
2021-06-21 12:17:00 +00:00
2021-03-05 09:54:06 +00:00
blk, err = s.cold.Get(cid)
if err == nil {
2021-03-05 09:54:06 +00:00
stats.Record(context.Background(), metrics.SplitstoreMiss.M(1))
2021-06-21 12:17:00 +00:00
2021-03-05 09:54:06 +00:00
}
return blk, err
default:
return nil, err
}
}
func (s *SplitStore) GetSize(cid cid.Cid) (int, error) {
s.txnLk.RLock()
defer s.txnLk.RUnlock()
size, err := s.hot.GetSize(cid)
switch err {
case nil:
err = s.trackTxnRef(cid, false)
return size, err
case bstore.ErrNotFound:
2021-06-22 07:10:24 +00:00
s.mx.Lock()
2021-06-22 07:56:35 +00:00
warmup := s.warmupEpoch > 0
2021-06-22 07:10:24 +00:00
curTs := s.curTs
s.mx.Unlock()
2021-06-22 07:56:35 +00:00
if warmup {
s.debug.LogReadMiss(curTs, cid)
}
2021-06-21 12:17:00 +00:00
2021-03-05 09:54:06 +00:00
size, err = s.cold.GetSize(cid)
if err == nil {
2021-03-05 09:54:06 +00:00
stats.Record(context.Background(), metrics.SplitstoreMiss.M(1))
}
return size, err
default:
return 0, err
}
}
func (s *SplitStore) Put(blk blocks.Block) error {
s.txnLk.RLock()
defer s.txnLk.RUnlock()
err := s.hot.Put(blk)
if err == nil {
s.trackWrite(blk.Cid(), false)
err = s.trackTxnRef(blk.Cid(), false)
2021-06-28 12:21:51 +00:00
}
return err
}
func (s *SplitStore) PutMany(blks []blocks.Block) error {
batch := make([]cid.Cid, 0, len(blks))
for _, blk := range blks {
batch = append(batch, blk.Cid())
}
s.txnLk.RLock()
defer s.txnLk.RUnlock()
err := s.hot.PutMany(blks)
if err == nil {
2021-07-02 11:25:02 +00:00
s.trackWriteMany(batch)
err = s.trackTxnRefMany(batch)
2021-06-28 12:21:51 +00:00
}
return err
}
func (s *SplitStore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
ctx, cancel := context.WithCancel(ctx)
chHot, err := s.hot.AllKeysChan(ctx)
if err != nil {
2020-11-24 22:01:10 +00:00
cancel()
return nil, err
}
chCold, err := s.cold.AllKeysChan(ctx)
if err != nil {
cancel()
return nil, err
}
ch := make(chan cid.Cid)
go func() {
defer cancel()
defer close(ch)
for _, in := range []<-chan cid.Cid{chHot, chCold} {
for cid := range in {
select {
case ch <- cid:
case <-ctx.Done():
return
}
}
}
}()
return ch, nil
}
func (s *SplitStore) HashOnRead(enabled bool) {
s.hot.HashOnRead(enabled)
s.cold.HashOnRead(enabled)
}
func (s *SplitStore) View(cid cid.Cid, cb func([]byte) error) error {
s.txnLk.RLock()
defer s.txnLk.RUnlock()
err := s.hot.View(cid, cb)
switch err {
case nil:
err = s.trackTxnRef(cid, false)
return err
case bstore.ErrNotFound:
2021-06-22 07:10:24 +00:00
s.mx.Lock()
2021-06-22 07:56:35 +00:00
warmup := s.warmupEpoch > 0
2021-06-22 07:10:24 +00:00
curTs := s.curTs
s.mx.Unlock()
2021-06-22 07:56:35 +00:00
if warmup {
s.debug.LogReadMiss(curTs, cid)
}
2021-06-21 12:17:00 +00:00
2021-03-16 20:05:22 +00:00
err = s.cold.View(cid, cb)
if err == nil {
stats.Record(context.Background(), metrics.SplitstoreMiss.M(1))
}
return err
default:
return err
}
}
2020-11-24 17:15:38 +00:00
2020-11-24 17:26:28 +00:00
// State tracking
func (s *SplitStore) Start(chain ChainAccessor) error {
s.chain = chain
s.curTs = chain.GetHeaviestTipSet()
2020-11-24 17:41:07 +00:00
// load base epoch from metadata ds
// if none, then use current epoch because it's a fresh start
bs, err := s.ds.Get(baseEpochKey)
switch err {
case nil:
2020-11-26 18:37:02 +00:00
s.baseEpoch = bytesToEpoch(bs)
2020-11-24 17:41:07 +00:00
case dstore.ErrNotFound:
if s.curTs == nil {
// this can happen in some tests
break
}
2020-11-24 17:41:07 +00:00
err = s.setBaseEpoch(s.curTs.Height())
if err != nil {
return xerrors.Errorf("error saving base epoch: %w", err)
2020-11-24 17:41:07 +00:00
}
default:
return xerrors.Errorf("error loading base epoch: %w", err)
}
// load warmup epoch from metadata ds
// if none, then the splitstore will warm up the hotstore at first head change notif
// by walking the current tipset
bs, err = s.ds.Get(warmupEpochKey)
switch err {
case nil:
s.warmupEpoch = bytesToEpoch(bs)
case dstore.ErrNotFound:
// the hotstore hasn't warmed up, load the genesis into the hotstore
err = s.warmup(s.curTs)
if err != nil {
return xerrors.Errorf("error warming up: %w", err)
}
default:
return xerrors.Errorf("error loading warmup epoch: %w", err)
2020-11-24 17:41:07 +00:00
}
2021-03-05 08:00:17 +00:00
// load markSetSize from metadata ds
// if none, the splitstore will compute it during warmup and update in every compaction
bs, err = s.ds.Get(markSetSizeKey)
switch err {
case nil:
s.markSetSize = bytesToInt64(bs)
case dstore.ErrNotFound:
default:
return xerrors.Errorf("error loading mark set size: %w", err)
}
s.updateWriteEpoch()
log.Infow("starting splitstore", "baseEpoch", s.baseEpoch, "warmupEpoch", s.warmupEpoch, "writeEpoch", s.writeEpoch)
go s.background()
2021-03-02 17:07:54 +00:00
2020-11-24 17:41:07 +00:00
// watch the chain
chain.SubscribeHeadChanges(s.HeadChange)
2020-11-24 17:41:07 +00:00
return nil
2020-11-24 17:26:28 +00:00
}
2020-11-26 15:49:47 +00:00
func (s *SplitStore) Close() error {
atomic.StoreInt32(&s.closing, 1)
if atomic.LoadInt32(&s.critsection) == 1 {
log.Warn("ongoing compaction in critical section; waiting for it to finish...")
for atomic.LoadInt32(&s.critsection) == 1 {
2020-11-26 15:49:47 +00:00
time.Sleep(time.Second)
}
}
s.flushPendingWrites(false)
s.cancel()
2021-06-22 07:14:58 +00:00
return multierr.Combine(s.tracker.Close(), s.env.Close(), s.debug.Close())
2020-11-26 15:49:47 +00:00
}
func (s *SplitStore) HeadChange(_, apply []*types.TipSet) error {
// Revert only.
if len(apply) == 0 {
return nil
}
s.mx.Lock()
curTs := apply[len(apply)-1]
epoch := curTs.Height()
s.curTs = curTs
s.mx.Unlock()
s.updateWriteEpoch()
timestamp := time.Unix(int64(curTs.MinTimestamp()), 0)
if time.Since(timestamp) > SyncGapTime {
// don't attempt compaction before we have caught up syncing
return nil
}
2020-11-29 13:10:30 +00:00
if !atomic.CompareAndSwapInt32(&s.compacting, 0, 1) {
// we are currently compacting, do nothing and wait for the next head change
return nil
}
if epoch-s.baseEpoch > CompactionThreshold {
// it's time to compact
2020-11-24 17:26:28 +00:00
go func() {
2020-11-29 13:10:30 +00:00
defer atomic.StoreInt32(&s.compacting, 0)
2020-11-25 08:11:42 +00:00
log.Info("compacting splitstore")
start := time.Now()
s.compact(curTs)
2020-11-25 08:11:42 +00:00
log.Infow("compaction done", "took", time.Since(start))
2020-11-24 17:26:28 +00:00
}()
2020-11-29 13:10:30 +00:00
} else {
// no compaction necessary
atomic.StoreInt32(&s.compacting, 0)
2020-11-24 17:26:28 +00:00
}
return nil
}
func (s *SplitStore) updateWriteEpoch() {
s.mx.Lock()
defer s.mx.Unlock()
curTs := s.curTs
timestamp := time.Unix(int64(curTs.MinTimestamp()), 0)
dt := time.Since(timestamp)
if dt < 0 {
writeEpoch := curTs.Height() + 1
if writeEpoch > s.writeEpoch {
s.flushPendingWrites(true)
s.writeEpoch = writeEpoch
}
return
}
writeEpoch := curTs.Height() + abi.ChainEpoch(dt.Seconds())/builtin.EpochDurationSeconds + 1
if writeEpoch > s.writeEpoch {
s.flushPendingWrites(true)
s.writeEpoch = writeEpoch
}
}
// Unfortunately we can't just directly tracker.Put one by one, as it is ridiculously slow with
// bbolt because of syncing (order of 10ms), so we batch them.
func (s *SplitStore) trackWrite(c cid.Cid, implicit bool) {
s.mx.Lock()
defer s.mx.Unlock()
s.pendingWrites[c] = struct{}{}
}
// and also combine batch writes into them
func (s *SplitStore) trackWriteMany(cids []cid.Cid) {
s.mx.Lock()
defer s.mx.Unlock()
for _, c := range cids {
s.pendingWrites[c] = struct{}{}
}
}
func (s *SplitStore) flushPendingWrites(locked bool) {
if !locked {
s.mx.Lock()
defer s.mx.Unlock()
}
if len(s.pendingWrites) == 0 {
return
}
cids := make([]cid.Cid, 0, len(s.pendingWrites))
seen := make(map[cid.Cid]struct{})
walked := cid.NewSet()
for c := range s.pendingWrites {
_, ok := seen[c]
if ok {
continue
}
cids = append(cids, c)
seen[c] = struct{}{}
_, implicit := s.pendingWritesImplicit[c]
if !implicit {
continue
}
// recursively walk dags to propagate dependent references
if c.Prefix().Codec != cid.DagCBOR {
continue
}
err := s.walkLinks(c, walked, func(c cid.Cid) error {
_, ok := seen[c]
if !ok {
cids = append(cids, c)
seen[c] = struct{}{}
}
return nil
})
if err != nil {
2021-07-02 11:31:31 +00:00
log.Warnf("error tracking dependent writes for cid %s: %s", c, err)
}
}
s.pendingWrites = make(map[cid.Cid]struct{})
if len(s.pendingWritesImplicit) > 0 {
s.pendingWritesImplicit = make(map[cid.Cid]struct{})
}
epoch := s.writeEpoch
err := s.tracker.PutBatch(cids, epoch)
if err != nil {
log.Errorf("error putting implicit write batch to tracker: %s", err)
}
s.debug.LogWriteMany(s.curTs, cids, epoch)
}
func (s *SplitStore) trackTxnRef(c cid.Cid, implicit bool) error {
if s.txnProtect == nil {
// not compacting
return nil
}
// NOTE: this occurs check assumes a markset without false positives, which is currently the case
// with the map
has, err := s.txnProtect.Has(c)
if err != nil {
log.Errorf("error occur checking object (cid: %s) for compaction transaction: %s", c, err)
return err
}
if has {
return nil
}
if c.Prefix().Codec != cid.DagCBOR || !implicit {
err = s.txnProtect.Mark(c)
} else {
err = s.walkLinks(c, cid.NewSet(), func(c cid.Cid) error {
return s.txnProtect.Mark(c)
})
}
if err != nil {
2021-07-02 11:31:31 +00:00
log.Warnf("error protecting object (cid: %s) from compaction transaction: %s", c, err)
}
return err
}
func (s *SplitStore) trackTxnRefMany(cids []cid.Cid) error {
if s.txnProtect == nil {
// not compacting
return nil
}
var err error
for _, c := range cids {
err2 := s.trackTxnRef(c, false)
if err2 != nil {
err = multierr.Combine(err, err2)
}
}
return err
}
func (s *SplitStore) background() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-s.ctx.Done():
return
case <-ticker.C:
s.updateWriteEpoch()
}
}
}
func (s *SplitStore) warmup(curTs *types.TipSet) error {
err := s.loadGenesisState()
if err != nil {
2021-06-17 09:35:56 +00:00
return xerrors.Errorf("error loading genesis state: %w", err)
}
if !atomic.CompareAndSwapInt32(&s.compacting, 0, 1) {
return xerrors.Errorf("error locking compaction")
}
go func() {
defer atomic.StoreInt32(&s.compacting, 0)
log.Info("warming up hotstore")
start := time.Now()
err = s.doWarmup(curTs)
if err != nil {
log.Errorf("error warming up hotstore: %s", err)
return
}
log.Infow("warm up done", "took", time.Since(start))
}()
return nil
}
func (s *SplitStore) loadGenesisState() error {
// makes sure the genesis and its state root are hot
gb, err := s.chain.GetGenesis()
if err != nil {
return xerrors.Errorf("error getting genesis: %w", err)
}
genesis := gb.Cid()
genesisStateRoot := gb.ParentStateRoot
has, err := s.hot.Has(genesis)
if err != nil {
return xerrors.Errorf("error checking hotstore for genesis: %w", err)
}
if !has {
blk, err := gb.ToStorageBlock()
if err != nil {
return xerrors.Errorf("error converting genesis block to storage block: %w", err)
}
err = s.hot.Put(blk)
if err != nil {
return xerrors.Errorf("error putting genesis block to hotstore: %w", err)
}
}
err = s.walkLinks(genesisStateRoot, cid.NewSet(), func(c cid.Cid) error {
has, err = s.hot.Has(c)
if err != nil {
return xerrors.Errorf("error checking hotstore for genesis state root: %w", err)
}
if !has {
blk, err := s.cold.Get(c)
if err != nil {
if err == bstore.ErrNotFound {
return nil
}
return xerrors.Errorf("error retrieving genesis state linked object from coldstore: %w", err)
}
err = s.hot.Put(blk)
if err != nil {
return xerrors.Errorf("error putting genesis state linked object to hotstore: %w", err)
}
}
return nil
})
if err != nil {
return xerrors.Errorf("error walking genesis state root links: %w", err)
}
return nil
}
func (s *SplitStore) doWarmup(curTs *types.TipSet) error {
epoch := curTs.Height()
2021-03-01 18:11:35 +00:00
batchHot := make([]blocks.Block, 0, batchSize)
batchSnoop := make([]cid.Cid, 0, batchSize)
count := int64(0)
2021-03-13 10:00:28 +00:00
xcount := int64(0)
missing := int64(0)
2021-07-01 11:06:28 +00:00
err := s.walk(curTs, epoch, false, s.cfg.HotHeaders,
func(cid cid.Cid) error {
count++
has, err := s.hot.Has(cid)
if err != nil {
return err
}
if has {
return nil
}
blk, err := s.cold.Get(cid)
if err != nil {
2021-03-13 10:00:28 +00:00
if err == bstore.ErrNotFound {
missing++
return nil
}
return err
}
2021-03-13 10:00:28 +00:00
xcount++
2021-03-01 18:11:35 +00:00
batchHot = append(batchHot, blk)
batchSnoop = append(batchSnoop, cid)
if len(batchHot) == batchSize {
err = s.tracker.PutBatch(batchSnoop, epoch)
2021-03-01 18:11:35 +00:00
if err != nil {
return err
}
batchSnoop = batchSnoop[:0]
err = s.hot.PutMany(batchHot)
if err != nil {
return err
}
batchHot = batchHot[:0]
}
2021-03-01 18:11:35 +00:00
return nil
})
if err != nil {
2021-03-13 10:00:28 +00:00
return err
}
2021-03-01 18:11:35 +00:00
if len(batchHot) > 0 {
err = s.tracker.PutBatch(batchSnoop, epoch)
2021-03-01 18:11:35 +00:00
if err != nil {
2021-03-13 10:00:28 +00:00
return err
2021-03-01 18:11:35 +00:00
}
err = s.hot.PutMany(batchHot)
if err != nil {
2021-03-13 10:00:28 +00:00
return err
2021-03-01 18:11:35 +00:00
}
}
log.Infow("warmup stats", "visited", count, "warm", xcount, "missing", missing)
2021-03-13 10:00:28 +00:00
if count > s.markSetSize {
2021-03-05 08:00:17 +00:00
s.markSetSize = count + count>>2 // overestimate a bit
}
2021-03-05 08:00:17 +00:00
err = s.ds.Put(markSetSizeKey, int64ToBytes(s.markSetSize))
if err != nil {
2021-03-13 10:00:28 +00:00
log.Warnf("error saving mark set size: %s", err)
2021-03-05 08:00:17 +00:00
}
2021-03-13 10:00:28 +00:00
// save the warmup epoch
err = s.ds.Put(warmupEpochKey, epochToBytes(epoch))
if err != nil {
return xerrors.Errorf("error saving warm up epoch: %w", err)
}
s.warmupEpoch = epoch
2021-03-13 10:00:28 +00:00
return nil
}
2020-11-24 17:15:38 +00:00
// Compaction/GC Algorithm
func (s *SplitStore) compact(curTs *types.TipSet) {
2021-03-05 08:29:49 +00:00
var err error
if s.markSetSize == 0 {
2021-02-28 19:35:18 +00:00
start := time.Now()
log.Info("estimating mark set size")
2021-03-05 08:29:49 +00:00
err = s.estimateMarkSetSize(curTs)
if err != nil {
log.Errorf("error estimating mark set size: %s; aborting compaction", err)
return
}
log.Infow("estimating mark set size done", "took", time.Since(start), "size", s.markSetSize)
2021-02-28 19:35:18 +00:00
} else {
log.Infow("current mark set size estimate", "size", s.markSetSize)
2021-02-28 19:35:18 +00:00
}
2021-03-05 09:54:06 +00:00
start := time.Now()
err = s.doCompact(curTs)
2021-03-05 09:54:06 +00:00
took := time.Since(start).Milliseconds()
stats.Record(context.Background(), metrics.SplitstoreCompactionTimeSeconds.M(float64(took)/1e3))
2021-03-05 08:29:49 +00:00
if err != nil {
log.Errorf("COMPACTION ERROR: %s", err)
}
}
2021-03-05 08:29:49 +00:00
func (s *SplitStore) estimateMarkSetSize(curTs *types.TipSet) error {
2021-03-13 10:00:28 +00:00
epoch := curTs.Height()
2021-03-03 07:46:12 +00:00
var count int64
2021-07-01 11:06:28 +00:00
err := s.walk(curTs, epoch, false, s.cfg.HotHeaders,
2021-02-28 19:35:18 +00:00
func(cid cid.Cid) error {
2021-03-03 07:46:12 +00:00
count++
2021-02-28 19:35:18 +00:00
return nil
})
if err != nil {
2021-03-05 08:29:49 +00:00
return err
2021-02-28 19:35:18 +00:00
}
2021-03-03 07:46:12 +00:00
2021-03-05 08:00:17 +00:00
s.markSetSize = count + count>>2 // overestimate a bit
2021-03-05 08:29:49 +00:00
return nil
2021-02-28 19:35:18 +00:00
}
func (s *SplitStore) doCompact(curTs *types.TipSet) error {
currentEpoch := curTs.Height()
boundaryEpoch := currentEpoch - CompactionBoundary
2021-06-17 17:27:37 +00:00
coldEpoch := boundaryEpoch - CompactionSlack
log.Infow("running compaction", "currentEpoch", currentEpoch, "baseEpoch", s.baseEpoch, "coldEpoch", coldEpoch, "boundaryEpoch", boundaryEpoch)
markSet, err := s.env.Create("live", s.markSetSize)
if err != nil {
2021-03-05 08:29:49 +00:00
return xerrors.Errorf("error creating mark set: %w", err)
}
defer markSet.Close() //nolint:errcheck
// create the pruge protect filter
s.txnLk.Lock()
s.txnProtect, err = s.txnEnv.Create("protected", s.markSetSize)
if err != nil {
s.txnLk.Unlock()
return xerrors.Errorf("error creating transactional mark set: %w", err)
}
s.txnLk.Unlock()
defer func() {
s.txnLk.Lock()
_ = s.txnProtect.Close()
s.txnProtect = nil
s.txnLk.Unlock()
}()
defer s.debug.Flush()
// flush pending writes to update the tracker
s.flushPendingWrites(false)
// 1. mark reachable objects by walking the chain from the current epoch to the boundary epoch
log.Infow("marking reachable blocks", "currentEpoch", currentEpoch, "boundaryEpoch", boundaryEpoch)
startMark := time.Now()
var count int64
2021-07-01 11:06:28 +00:00
err = s.walk(curTs, boundaryEpoch, true, s.cfg.HotHeaders,
func(cid cid.Cid) error {
2021-02-28 19:35:18 +00:00
count++
return markSet.Mark(cid)
})
if err != nil {
2021-03-05 08:29:49 +00:00
return xerrors.Errorf("error marking cold blocks: %w", err)
}
if count > s.markSetSize {
2021-03-05 08:00:17 +00:00
s.markSetSize = count + count>>2 // overestimate a bit
2021-02-28 19:35:18 +00:00
}
2021-03-13 10:00:28 +00:00
log.Infow("marking done", "took", time.Since(startMark), "marked", count)
// 2. move cold unreachable objects to the coldstore
log.Info("collecting cold objects")
startCollect := time.Now()
2021-03-02 16:59:00 +00:00
cold := make([]cid.Cid, 0, s.coldPurgeSize)
// some stats for logging
var hotCnt, coldCnt, liveCnt int
// 2.1 iterate through the tracking store and collect unreachable cold objects
err = s.tracker.ForEach(func(cid cid.Cid, writeEpoch abi.ChainEpoch) error {
// is the object still hot?
if writeEpoch > coldEpoch {
// yes, stay in the hotstore
hotCnt++
return nil
}
// check whether it is reachable in the cold boundary
mark, err := markSet.Has(cid)
if err != nil {
return xerrors.Errorf("error checkiing mark set for %s: %w", cid, err)
}
if mark {
hotCnt++
return nil
}
live, err := s.txnProtect.Has(cid)
if err != nil {
return xerrors.Errorf("error checking liveness for %s: %w", cid, err)
}
if live {
liveCnt++
return nil
}
// it's cold, mark it for move
2021-03-02 16:59:00 +00:00
cold = append(cold, cid)
coldCnt++
2021-06-21 12:17:00 +00:00
return nil
})
if err != nil {
2021-03-05 08:29:49 +00:00
return xerrors.Errorf("error collecting cold objects: %w", err)
}
if coldCnt > 0 {
s.coldPurgeSize = coldCnt + coldCnt>>2 // overestimate a bit
}
2021-03-02 16:59:00 +00:00
log.Infow("collection done", "took", time.Since(startCollect))
log.Infow("compaction stats", "hot", hotCnt, "cold", coldCnt, "live", liveCnt)
2021-03-05 09:54:06 +00:00
stats.Record(context.Background(), metrics.SplitstoreCompactionHot.M(int64(hotCnt)))
stats.Record(context.Background(), metrics.SplitstoreCompactionCold.M(int64(coldCnt)))
// Enter critical section
atomic.StoreInt32(&s.critsection, 1)
defer atomic.StoreInt32(&s.critsection, 0)
// check to see if we are closing first; if that's the case just return
if atomic.LoadInt32(&s.closing) == 1 {
log.Info("splitstore is closing; aborting compaction")
2021-03-05 08:29:49 +00:00
return xerrors.Errorf("compaction aborted")
}
// 2.2 copy the cold objects to the coldstore -- if we have one
if !s.cfg.SkipMoveColdBlocks {
log.Info("moving cold blocks to the coldstore")
startMove := time.Now()
err = s.moveColdBlocks(cold)
if err != nil {
return xerrors.Errorf("error moving cold blocks: %w", err)
}
log.Infow("moving done", "took", time.Since(startMove))
2021-03-02 09:20:39 +00:00
}
// 2.3 purge cold objects from the hotstore
2021-03-02 09:20:39 +00:00
log.Info("purging cold objects from the hotstore")
startPurge := time.Now()
err = s.purge(curTs, cold)
2021-03-02 09:20:39 +00:00
if err != nil {
2021-03-05 08:29:49 +00:00
return xerrors.Errorf("error purging cold blocks: %w", err)
2021-03-02 09:20:39 +00:00
}
log.Infow("purging cold from hotstore done", "took", time.Since(startPurge))
// we are done; do some housekeeping
err = s.tracker.Sync()
if err != nil {
2021-03-05 08:29:49 +00:00
return xerrors.Errorf("error syncing tracker: %w", err)
2021-03-02 09:20:39 +00:00
}
2021-03-08 17:46:21 +00:00
s.gcHotstore()
2021-03-02 09:20:39 +00:00
err = s.setBaseEpoch(coldEpoch)
if err != nil {
2021-03-05 08:29:49 +00:00
return xerrors.Errorf("error saving base epoch: %w", err)
2021-03-02 09:20:39 +00:00
}
2021-03-05 08:00:17 +00:00
err = s.ds.Put(markSetSizeKey, int64ToBytes(s.markSetSize))
if err != nil {
2021-03-05 08:29:49 +00:00
return xerrors.Errorf("error saving mark set size: %w", err)
2021-03-05 08:00:17 +00:00
}
2021-03-05 08:29:49 +00:00
return nil
2021-03-02 09:20:39 +00:00
}
2021-07-01 11:06:28 +00:00
func (s *SplitStore) walk(ts *types.TipSet, boundary abi.ChainEpoch, inclMsgs, fullChain bool,
f func(cid.Cid) error) error {
visited := cid.NewSet()
2021-03-13 10:00:28 +00:00
walked := cid.NewSet()
toWalk := ts.Cids()
2021-07-01 11:10:57 +00:00
walkCnt := 0
scanCnt := 0
2021-03-13 10:00:28 +00:00
walkBlock := func(c cid.Cid) error {
if !visited.Visit(c) {
2021-03-13 10:00:28 +00:00
return nil
}
2021-07-01 11:10:57 +00:00
walkCnt++
2021-07-01 11:06:28 +00:00
if err := f(c); err != nil {
return err
}
blk, err := s.get(c)
2021-03-13 10:00:28 +00:00
if err != nil {
return xerrors.Errorf("error retrieving block (cid: %s): %w", c, err)
}
var hdr types.BlockHeader
if err := hdr.UnmarshalCBOR(bytes.NewBuffer(blk.RawData())); err != nil {
return xerrors.Errorf("error unmarshaling block header (cid: %s): %w", c, err)
}
2021-07-01 11:06:28 +00:00
// don't walk under the boundary, unless we are walking the full chain
if hdr.Height < boundary && !fullChain {
2021-03-13 10:00:28 +00:00
return nil
}
2021-07-01 11:06:28 +00:00
// we only scan the block if it is above the boundary
if hdr.Height >= boundary {
2021-07-01 11:10:57 +00:00
scanCnt++
if inclMsgs {
if err := s.walkLinks(hdr.Messages, walked, f); err != nil {
return xerrors.Errorf("error walking messages (cid: %s): %w", hdr.Messages, err)
}
if err := s.walkLinks(hdr.ParentMessageReceipts, walked, f); err != nil {
return xerrors.Errorf("error walking message receipts (cid: %s): %w", hdr.ParentMessageReceipts, err)
}
}
2021-03-13 10:00:28 +00:00
if err := s.walkLinks(hdr.ParentStateRoot, walked, f); err != nil {
return xerrors.Errorf("error walking state root (cid: %s): %w", hdr.ParentStateRoot, err)
}
2021-03-13 10:00:28 +00:00
}
if hdr.Height > 0 {
toWalk = append(toWalk, hdr.Parents...)
}
2021-07-01 11:06:28 +00:00
2021-03-13 10:00:28 +00:00
return nil
}
for len(toWalk) > 0 {
walking := toWalk
toWalk = nil
for _, c := range walking {
if err := walkBlock(c); err != nil {
return xerrors.Errorf("error walking block (cid: %s): %w", c, err)
}
}
}
2021-07-01 11:10:57 +00:00
log.Infow("chain walk done", "walked", walkCnt, "scanned", scanCnt)
2021-07-01 11:06:28 +00:00
2021-03-13 10:00:28 +00:00
return nil
}
func (s *SplitStore) walkLinks(c cid.Cid, walked *cid.Set, f func(cid.Cid) error) error {
if !walked.Visit(c) {
return nil
}
if err := f(c); err != nil {
return err
}
if c.Prefix().Codec != cid.DagCBOR {
return nil
}
blk, err := s.get(c)
2021-03-13 10:00:28 +00:00
if err != nil {
return xerrors.Errorf("error retrieving linked block (cid: %s): %w", c, err)
}
var rerr error
err = cbg.ScanForLinks(bytes.NewReader(blk.RawData()), func(c cid.Cid) {
if rerr != nil {
return
}
2021-07-01 11:06:28 +00:00
err := s.walkLinks(c, walked, f)
if err != nil {
rerr = err
}
2021-03-13 10:00:28 +00:00
})
if err != nil {
return xerrors.Errorf("error scanning links (cid: %s): %w", c, err)
}
return rerr
}
// internal version used by walk so that we don't blow the txn
func (s *SplitStore) get(cid cid.Cid) (blocks.Block, error) {
blk, err := s.hot.Get(cid)
switch err {
case bstore.ErrNotFound:
return s.cold.Get(cid)
default:
return blk, err
}
}
2021-03-02 16:59:00 +00:00
func (s *SplitStore) moveColdBlocks(cold []cid.Cid) error {
batch := make([]blocks.Block, 0, batchSize)
2021-03-02 16:59:00 +00:00
for _, cid := range cold {
blk, err := s.hot.Get(cid)
if err != nil {
2021-06-28 10:35:06 +00:00
if err == bstore.ErrNotFound {
// this can happen if the node is killed after we have deleted the block from the hotstore
// but before we have deleted it from the tracker; just delete the tracker.
err = s.tracker.Delete(cid)
if err != nil {
2021-03-02 09:20:39 +00:00
return xerrors.Errorf("error deleting unreachable cid %s from tracker: %w", cid, err)
}
} else {
2021-03-02 09:20:39 +00:00
return xerrors.Errorf("error retrieving tracked block %s from hotstore: %w", cid, err)
}
continue
}
batch = append(batch, blk)
if len(batch) == batchSize {
err = s.cold.PutMany(batch)
if err != nil {
2021-03-02 09:20:39 +00:00
return xerrors.Errorf("error putting batch to coldstore: %w", err)
}
batch = batch[:0]
}
}
if len(batch) > 0 {
2021-03-02 09:20:39 +00:00
err := s.cold.PutMany(batch)
if err != nil {
2021-03-02 09:20:39 +00:00
return xerrors.Errorf("error putting cold to coldstore: %w", err)
}
}
2021-03-02 09:20:39 +00:00
return nil
}
2021-03-03 18:04:29 +00:00
func (s *SplitStore) purgeBatch(cids []cid.Cid, deleteBatch func([]cid.Cid) error) error {
if len(cids) == 0 {
return nil
}
// don't delete one giant batch of 7M objects, but rather do smaller batches
done := false
2021-03-03 17:36:13 +00:00
for i := 0; !done; i++ {
start := i * batchSize
end := start + batchSize
if end >= len(cids) {
end = len(cids)
done = true
}
2021-03-03 18:04:29 +00:00
err := deleteBatch(cids[start:end])
if err != nil {
2021-03-03 18:04:29 +00:00
return xerrors.Errorf("error deleting batch: %w", err)
}
}
2021-03-02 09:20:39 +00:00
return nil
}
func (s *SplitStore) purge(curTs *types.TipSet, cids []cid.Cid) error {
deadCids := make([]cid.Cid, 0, batchSize)
var purgeCnt, liveCnt int
2021-06-30 17:35:00 +00:00
defer func() {
log.Infow("purged objects", "purged", purgeCnt, "live", liveCnt)
2021-06-30 17:35:00 +00:00
}()
return s.purgeBatch(cids,
func(cids []cid.Cid) error {
deadCids := deadCids[:0]
s.txnLk.Lock()
defer s.txnLk.Unlock()
for _, c := range cids {
live, err := s.txnProtect.Has(c)
if err != nil {
return xerrors.Errorf("error checking for liveness: %w", err)
}
if live {
liveCnt++
continue
}
deadCids = append(deadCids, c)
s.debug.LogMove(curTs, c)
}
err := s.tracker.DeleteBatch(deadCids)
if err != nil {
return xerrors.Errorf("error purging tracking: %w", err)
}
err = s.hot.DeleteMany(deadCids)
if err != nil {
return xerrors.Errorf("error purging cold objects: %w", err)
}
purgeCnt += len(deadCids)
return nil
})
}
2021-03-08 17:46:21 +00:00
func (s *SplitStore) gcHotstore() {
if compact, ok := s.hot.(interface{ Compact() error }); ok {
log.Infof("compacting hotstore")
startCompact := time.Now()
err := compact.Compact()
if err != nil {
log.Warnf("error compacting hotstore: %s", err)
return
}
2021-03-11 11:10:44 +00:00
log.Infow("hotstore compaction done", "took", time.Since(startCompact))
}
2021-03-08 17:46:21 +00:00
if gc, ok := s.hot.(interface{ CollectGarbage() error }); ok {
log.Infof("garbage collecting hotstore")
startGC := time.Now()
err := gc.CollectGarbage()
if err != nil {
log.Warnf("error garbage collecting hotstore: %s", err)
2021-03-11 11:10:44 +00:00
return
2021-03-08 17:46:21 +00:00
}
2021-03-11 11:10:44 +00:00
log.Infow("hotstore garbage collection done", "took", time.Since(startGC))
2021-03-08 17:46:21 +00:00
}
}
2020-11-24 17:41:07 +00:00
func (s *SplitStore) setBaseEpoch(epoch abi.ChainEpoch) error {
s.baseEpoch = epoch
2020-11-26 18:37:02 +00:00
return s.ds.Put(baseEpochKey, epochToBytes(epoch))
}
func epochToBytes(epoch abi.ChainEpoch) []byte {
2021-03-05 08:00:17 +00:00
return uint64ToBytes(uint64(epoch))
}
func bytesToEpoch(buf []byte) abi.ChainEpoch {
return abi.ChainEpoch(bytesToUint64(buf))
}
func int64ToBytes(i int64) []byte {
return uint64ToBytes(uint64(i))
}
func bytesToInt64(buf []byte) int64 {
return int64(bytesToUint64(buf))
}
func uint64ToBytes(i uint64) []byte {
2020-11-26 18:37:02 +00:00
buf := make([]byte, 16)
2021-03-05 08:00:17 +00:00
n := binary.PutUvarint(buf, i)
2020-11-26 18:37:02 +00:00
return buf[:n]
}
2021-03-05 08:00:17 +00:00
func bytesToUint64(buf []byte) uint64 {
i, _ := binary.Uvarint(buf)
return i
2020-11-24 17:15:38 +00:00
}