Skip to content

Commit

Permalink
stmgr: only persist the migration cache on success
Browse files Browse the repository at this point in the history
Otherwise, we may end up referencing blocks we never wrote to disk.
  • Loading branch information
Stebalien committed Jan 27, 2021
1 parent 55c244f commit a93a186
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
18 changes: 17 additions & 1 deletion chain/stmgr/forks.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,17 @@ func (sm *StateManager) handleStateForks(ctx context.Context, root cid.Cid, heig
var err error
u := sm.stateMigrations[height]
if u != nil && u.upgrade != nil {
retCid, err = u.upgrade(ctx, sm, u.cache, cb, root, height, ts)
// Yes, we clone the cache, even for the final upgrade epoch. Why? Reverts. We may
// have to migrate multiple times.
tmpCache := u.cache.Clone()
retCid, err = u.upgrade(ctx, sm, tmpCache, cb, root, height, ts)
if err != nil {
return cid.Undef, err
}
// Yes, we update the cache, even for the final upgrade epoch. Why? Reverts. This
// can save us a _lot_ of time because very few actors will have changed if we
// do a small revert then need to re-run the migration.
u.cache.Update(tmpCache)
}

return retCid, nil
Expand Down Expand Up @@ -279,11 +286,20 @@ func (sm *StateManager) preMigrationWorker(ctx context.Context) {
wg.Add(1)
go func() {
defer wg.Done()

// Clone the cache so we don't actually _update_ it
// till we're done. Otherwise, if we fail, the next
// migration to use the cache may assume that
// certain blocks exist, even if they don't.
tmpCache := cache.Clone()
err := migrationFunc(preCtx, sm, cache, ts.ParentState(), ts.Height(), ts)
if err != nil {
log.Errorw("failed to run pre-migration",
"error", err)
return
}
// Finally, if everything worked, update the cache.
cache.Update(tmpCache)
}()
},
})
Expand Down
2 changes: 1 addition & 1 deletion chain/stmgr/stmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type versionSpec struct {
type migration struct {
upgrade MigrationFunc
preMigrations []PreMigration
cache MigrationCache
cache *nv10.MemMigrationCache
}

type StateManager struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
github.com/filecoin-project/go-storedcounter v0.0.0-20200421200003-1c99c62e8a5b
github.com/filecoin-project/specs-actors v0.9.13
github.com/filecoin-project/specs-actors/v2 v2.3.4
github.com/filecoin-project/specs-actors/v3 v3.0.1-0.20210126204401-0e4fdfa27dea
github.com/filecoin-project/specs-actors/v3 v3.0.1-0.20210127005641-3320300e4add
github.com/filecoin-project/specs-storage v0.1.1-0.20201105051918-5188d9774506
github.com/filecoin-project/test-vectors/schema v0.0.5
github.com/gbrlsnchs/jwt/v3 v3.0.0-beta.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ github.com/filecoin-project/specs-actors/v2 v2.3.2 h1:2Vcf4CGa29kRh4JJ02m+FbvD/p
github.com/filecoin-project/specs-actors/v2 v2.3.2/go.mod h1:UuJQLoTx/HPvvWeqlIFmC/ywlOLHNe8SNQ3OunFbu2Y=
github.com/filecoin-project/specs-actors/v2 v2.3.4 h1:NZK2oMCcA71wNsUzDBmLQyRMzcCnX9tDGvwZ53G67j8=
github.com/filecoin-project/specs-actors/v2 v2.3.4/go.mod h1:UuJQLoTx/HPvvWeqlIFmC/ywlOLHNe8SNQ3OunFbu2Y=
github.com/filecoin-project/specs-actors/v3 v3.0.1-0.20210126204401-0e4fdfa27dea h1:TPMvEAUP4bUTDu7LaKkk0zKhtQHEPuOI5C4cEZls2ho=
github.com/filecoin-project/specs-actors/v3 v3.0.1-0.20210126204401-0e4fdfa27dea/go.mod h1:aVf248CfjfyCmVel4UuFAA3u+9UQjqtqHpgfYv+M+9U=
github.com/filecoin-project/specs-actors/v3 v3.0.1-0.20210127005641-3320300e4add h1:LvS5fTSY5HgibaXbFEscox2SfiuBlS8RrVgBdGbozOE=
github.com/filecoin-project/specs-actors/v3 v3.0.1-0.20210127005641-3320300e4add/go.mod h1:aVf248CfjfyCmVel4UuFAA3u+9UQjqtqHpgfYv+M+9U=
github.com/filecoin-project/specs-storage v0.1.1-0.20201105051918-5188d9774506 h1:Ur/l2+6qN+lQiqjozWWc5p9UDaAMDZKTlDS98oRnlIw=
github.com/filecoin-project/specs-storage v0.1.1-0.20201105051918-5188d9774506/go.mod h1:nJRRM7Aa9XVvygr3W9k6xGF46RWzr2zxF/iGoAIfA/g=
github.com/filecoin-project/test-vectors/schema v0.0.5 h1:w3zHQhzM4pYxJDl21avXjOKBLF8egrvwUwjpT8TquDg=
Expand Down

0 comments on commit a93a186

Please sign in to comment.