review comments

This commit is contained in:
Travis Person 2023-03-07 19:35:40 +00:00
parent 814c146626
commit b4e589a0f7
2 changed files with 22 additions and 25 deletions

View File

@ -176,9 +176,9 @@ func (sm *StateManager) HandleStateForks(ctx context.Context, root cid.Cid, heig
retCid := root retCid := root
u := sm.stateMigrations[height] u := sm.stateMigrations[height]
if u != nil && u.upgrade != nil { if u != nil && u.upgrade != nil {
migCid, ok, err := u.resultCache.Result(ctx, root) migCid, ok, err := u.migrationResultCache.Get(ctx, root)
if err == nil && ok { if err == nil && ok {
log.Warnw("CACHED migration", "height", height, "from", root, "to", migCid) log.Infow("CACHED migration", "height", height, "from", root, "to", migCid)
return migCid, nil return migCid, nil
} else if err != nil { } else if err != nil {
log.Errorw("failed to lookup previous migration result", "err", err) log.Errorw("failed to lookup previous migration result", "err", err)
@ -206,7 +206,7 @@ func (sm *StateManager) HandleStateForks(ctx context.Context, root cid.Cid, heig
) )
// Only set if migration ran, we do not want a root => root mapping // Only set if migration ran, we do not want a root => root mapping
if err := u.resultCache.Store(ctx, root, retCid); err != nil { if err := u.migrationResultCache.Store(ctx, root, retCid); err != nil {
log.Errorw("failed to store migration result", "err", err) log.Errorw("failed to store migration result", "err", err)
} }
} }

View File

@ -8,6 +8,7 @@ import (
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
dstore "github.com/ipfs/go-datastore" dstore "github.com/ipfs/go-datastore"
cbor "github.com/ipfs/go-ipld-cbor" cbor "github.com/ipfs/go-ipld-cbor"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2" logging "github.com/ipfs/go-log/v2"
"golang.org/x/xerrors" "golang.org/x/xerrors"
@ -56,29 +57,26 @@ type migration struct {
upgrade MigrationFunc upgrade MigrationFunc
preMigrations []PreMigration preMigrations []PreMigration
cache *nv16.MemMigrationCache cache *nv16.MemMigrationCache
resultCache *resultCache migrationResultCache *migrationResultCache
} }
type resultCache struct { type migrationResultCache struct {
ds dstore.Batching ds dstore.Batching
keyPrefix string keyPrefix string
} }
func (m *resultCache) Result(ctx context.Context, root cid.Cid) (cid.Cid, bool, error) { func (m *migrationResultCache) keyForMigration(root cid.Cid) dstore.Key {
kStr := fmt.Sprintf("%s-%s", m.keyPrefix, root) kStr := fmt.Sprintf("%s/%s", m.keyPrefix, root)
k := dstore.NewKey(kStr) return dstore.NewKey(kStr)
found, err := m.ds.Has(ctx, k)
if err != nil {
return cid.Undef, false, xerrors.Errorf("error looking up migration result: %w", err)
} }
if !found { func (m *migrationResultCache) Get(ctx context.Context, root cid.Cid) (cid.Cid, bool, error) {
return cid.Undef, false, nil k := m.keyForMigration(root)
}
bs, err := m.ds.Get(ctx, k) bs, err := m.ds.Get(ctx, k)
if err != nil { if ipld.IsNotFound(err) {
return cid.Undef, false, nil
} else if err != nil {
return cid.Undef, false, xerrors.Errorf("error loading migration result: %w", err) return cid.Undef, false, xerrors.Errorf("error loading migration result: %w", err)
} }
@ -90,9 +88,8 @@ func (m *resultCache) Result(ctx context.Context, root cid.Cid) (cid.Cid, bool,
return c, true, nil return c, true, nil
} }
func (m *resultCache) Store(ctx context.Context, root cid.Cid, resultCid cid.Cid) error { func (m *migrationResultCache) Store(ctx context.Context, root cid.Cid, resultCid cid.Cid) error {
kStr := fmt.Sprintf("%s-%s", m.keyPrefix, root) k := m.keyForMigration(root)
k := dstore.NewKey(kStr)
if err := m.ds.Put(ctx, k, resultCid.Bytes()); err != nil { if err := m.ds.Put(ctx, k, resultCid.Bytes()); err != nil {
return err return err
} }
@ -166,8 +163,8 @@ func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder,
upgrade: upgrade.Migration, upgrade: upgrade.Migration,
preMigrations: upgrade.PreMigrations, preMigrations: upgrade.PreMigrations,
cache: nv16.NewMemMigrationCache(), cache: nv16.NewMemMigrationCache(),
resultCache: &resultCache{ migrationResultCache: &migrationResultCache{
keyPrefix: fmt.Sprintf("nv%d-%d", upgrade.Network, upgrade.Height), keyPrefix: fmt.Sprintf("/migration-cache/nv%d", upgrade.Network),
ds: metadataDs, ds: metadataDs,
}, },
} }