|
| 1 | +package migration |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/cockroachdb/pebble" |
| 8 | + "github.com/dgraph-io/badger/v2" |
| 9 | + "github.com/rs/zerolog" |
| 10 | + |
| 11 | + "github.com/onflow/flow-go/engine/execution/state/bootstrap" |
| 12 | + "github.com/onflow/flow-go/model/flow" |
| 13 | + "github.com/onflow/flow-go/module/block_iterator/latest" |
| 14 | + "github.com/onflow/flow-go/module/metrics" |
| 15 | + "github.com/onflow/flow-go/state/protocol" |
| 16 | + "github.com/onflow/flow-go/storage" |
| 17 | + "github.com/onflow/flow-go/storage/operation" |
| 18 | + "github.com/onflow/flow-go/storage/operation/badgerimpl" |
| 19 | + "github.com/onflow/flow-go/storage/operation/pebbleimpl" |
| 20 | + "github.com/onflow/flow-go/storage/store" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + mainnet26SporkID flow.Identifier |
| 25 | + testnet52SporkID flow.Identifier |
| 26 | +) |
| 27 | + |
| 28 | +func init() { |
| 29 | + var err error |
| 30 | + |
| 31 | + mainnet26SporkID, err = flow.HexStringToIdentifier("45894bde3f45dbfd89cab12be84e9172385da5079d40bf63979ca8a6a7ede741") |
| 32 | + if err != nil { |
| 33 | + panic(fmt.Sprintf("failed to parse Mainnet26SporkID: %v", err)) |
| 34 | + } |
| 35 | + |
| 36 | + testnet52SporkID, err = flow.HexStringToIdentifier("5b88b81cfce2619305213489c2137f98e6efa0ec333dab2c31042b743388a3ce") |
| 37 | + if err != nil { |
| 38 | + panic(fmt.Sprintf("failed to parse Testnet52SporkID: %v", err)) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// MigrateLastSealedExecutedResultToPebble run the migration to the pebble database, so that |
| 43 | +// it has necessary data to be able execute the next block. |
| 44 | +// the migration includes the following operations: |
| 45 | +// 1. bootstrap the pebble database |
| 46 | +// 2. copy execution data of the last sealed and executed block from badger to pebble. |
| 47 | +// the execution data includes the execution result and statecommitment, which is the minimum data needed from the database |
| 48 | +// to be able to continue executing the next block |
| 49 | +func MigrateLastSealedExecutedResultToPebble(logger zerolog.Logger, badgerDB *badger.DB, pebbleDB *pebble.DB, state protocol.State, rootSeal *flow.Seal) error { |
| 50 | + // only run the migration for mainnet26 and testnet52 |
| 51 | + sporkID := state.Params().SporkID() |
| 52 | + chainID := state.Params().ChainID() |
| 53 | + if chainID == flow.Mainnet || chainID == flow.Testnet { |
| 54 | + if sporkID != mainnet26SporkID && sporkID != testnet52SporkID { |
| 55 | + logger.Warn().Msgf("spork ID %v is not Mainnet26SporkID %v or Testnet52SporkID %v, skip migration", |
| 56 | + sporkID, mainnet26SporkID, testnet52SporkID) |
| 57 | + return nil |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + bdb := badgerimpl.ToDB(badgerDB) |
| 62 | + pdb := pebbleimpl.ToDB(pebbleDB) |
| 63 | + lg := logger.With().Str("module", "badger-pebble-migration").Logger() |
| 64 | + |
| 65 | + // bootstrap pebble database |
| 66 | + bootstrapper := bootstrap.NewBootstrapper(logger) |
| 67 | + commit, bootstrapped, err := bootstrapper.IsBootstrapped(pdb) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("could not query database to know whether database has been bootstrapped: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + if !bootstrapped { |
| 73 | + err = bootstrapper.BootstrapExecutionDatabase(pdb, rootSeal) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("could not bootstrap pebble execution database: %w", err) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // get last sealed and executed block in badger |
| 80 | + lastExecutedSealedHeightInBadger, err := latest.LatestSealedAndExecutedHeight(state, bdb) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("failed to get last executed sealed block: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + // read all the data and save to pebble |
| 86 | + header, err := state.AtHeight(lastExecutedSealedHeightInBadger).Head() |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to get block at height %d: %w", lastExecutedSealedHeightInBadger, err) |
| 89 | + } |
| 90 | + |
| 91 | + blockID := header.ID() |
| 92 | + |
| 93 | + lg.Info().Msgf( |
| 94 | + "migrating last executed and sealed block %v (%v) from badger to pebble", |
| 95 | + header.Height, blockID) |
| 96 | + |
| 97 | + // create badger storage modules |
| 98 | + badgerResults, badgerCommits := createStores(bdb) |
| 99 | + // read data from badger |
| 100 | + result, commit, err := readResultsForBlock(blockID, badgerResults, badgerCommits) |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("failed to read data from badger: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + // create pebble storage modules |
| 107 | + pebbleResults, pebbleCommits := createStores(pdb) |
| 108 | + |
| 109 | + var existingExecuted flow.Identifier |
| 110 | + err = operation.RetrieveExecutedBlock(pdb.Reader(), &existingExecuted) |
| 111 | + if err == nil { |
| 112 | + // there is an executed block in pebble, compare if it's newer than the badger one, |
| 113 | + // if newer, it means EN is storing new results in pebble, in this case, we don't |
| 114 | + // want to update the executed block with the badger one. |
| 115 | + |
| 116 | + header, err := state.AtBlockID(existingExecuted).Head() |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("failed to get block at height %d from badger: %w", lastExecutedSealedHeightInBadger, err) |
| 119 | + } |
| 120 | + |
| 121 | + if header.Height > lastExecutedSealedHeightInBadger { |
| 122 | + // existing executed in pebble is higher than badger, no need to store anything |
| 123 | + // why? |
| 124 | + // because the migration only copy the last sealed and executed block from badger to pebble, |
| 125 | + // if EN is still storing new results in badger, then the existingExecuted in pebble will be the same as |
| 126 | + // badger not higher. |
| 127 | + // if EN is storing new results in pebble, then the existingExecuted in pebble will be higher than badger, |
| 128 | + // in this case, we don't need to update the executed block in pebble. |
| 129 | + lg.Info().Msgf("existing executed block %v in pebble is newer than %v in badger, skip update", |
| 130 | + header.Height, lastExecutedSealedHeightInBadger) |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + // otherwise continue to update last executed block in pebble |
| 135 | + lg.Info().Msgf("existing executed block %v in pebble is older than %v in badger, update executed block", |
| 136 | + header.Height, lastExecutedSealedHeightInBadger, |
| 137 | + ) |
| 138 | + } else if !errors.Is(err, storage.ErrNotFound) { |
| 139 | + // exception |
| 140 | + return fmt.Errorf("failed to retrieve executed block from pebble: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + // store data to pebble in a batch update |
| 144 | + err = pdb.WithReaderBatchWriter(func(batch storage.ReaderBatchWriter) error { |
| 145 | + if err := pebbleResults.BatchStore(result, batch); err != nil { |
| 146 | + return fmt.Errorf("failed to store receipt for block %s: %w", blockID, err) |
| 147 | + } |
| 148 | + |
| 149 | + if err := pebbleResults.BatchIndex(blockID, result.ID(), batch); err != nil { |
| 150 | + return fmt.Errorf("failed to index result for block %s: %w", blockID, err) |
| 151 | + } |
| 152 | + |
| 153 | + if err := pebbleCommits.BatchStore(blockID, commit, batch); err != nil { |
| 154 | + return fmt.Errorf("failed to store commit for block %s: %w", blockID, err) |
| 155 | + } |
| 156 | + |
| 157 | + // two cases here: |
| 158 | + // 1. no executed block in pebble |
| 159 | + // in this case: set this block as last executed block |
| 160 | + // 2. badger has newer executed block than pebble |
| 161 | + // in this case: set this block as last executed block |
| 162 | + if err := operation.UpdateExecutedBlock(batch.Writer(), blockID); err != nil { |
| 163 | + return fmt.Errorf("failed to update executed block in pebble: %w", err) |
| 164 | + } |
| 165 | + |
| 166 | + return nil |
| 167 | + }) |
| 168 | + |
| 169 | + if err != nil { |
| 170 | + return fmt.Errorf("failed to write data to pebble: %w", err) |
| 171 | + } |
| 172 | + |
| 173 | + lg.Info().Msgf("migrated last executed and sealed block %v (%v) from badger to pebble", |
| 174 | + header.Height, blockID) |
| 175 | + |
| 176 | + return nil |
| 177 | +} |
| 178 | + |
| 179 | +func readResultsForBlock( |
| 180 | + blockID flow.Identifier, |
| 181 | + resultsStore storage.ExecutionResults, |
| 182 | + commitsStore storage.Commits, |
| 183 | +) (*flow.ExecutionResult, flow.StateCommitment, error) { |
| 184 | + result, err := resultsStore.ByBlockID(blockID) |
| 185 | + if err != nil { |
| 186 | + return nil, flow.DummyStateCommitment, fmt.Errorf("failed to get receipt for block %s: %w", blockID, err) |
| 187 | + } |
| 188 | + |
| 189 | + commit, err := commitsStore.ByBlockID(blockID) |
| 190 | + if err != nil { |
| 191 | + return nil, flow.DummyStateCommitment, fmt.Errorf("failed to get commit for block %s: %w", blockID, err) |
| 192 | + } |
| 193 | + return result, commit, nil |
| 194 | +} |
| 195 | + |
| 196 | +func createStores(db storage.DB) (storage.ExecutionResults, storage.Commits) { |
| 197 | + noop := metrics.NewNoopCollector() |
| 198 | + results := store.NewExecutionResults(noop, db) |
| 199 | + commits := store.NewCommits(noop, db) |
| 200 | + return results, commits |
| 201 | +} |
0 commit comments