-
Notifications
You must be signed in to change notification settings - Fork 214
[Storehouse] 015 - add util to extract payloadless #8608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhangchiqing
wants to merge
1
commit into
leo/payloadless-util-create-v6-checkpoint-from-v7
Choose a base branch
from
leo/payloadless-util-extract-payloadless
base: leo/payloadless-util-create-v6-checkpoint-from-v7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
cmd/util/cmd/execution-state-extract-payloadless/cmd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package extractpayloadless | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "fmt" | ||
| "os" | ||
| "path" | ||
|
|
||
| "github.com/rs/zerolog/log" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/onflow/flow-go/cmd/util/ledger/util" | ||
| "github.com/onflow/flow-go/ledger" | ||
| "github.com/onflow/flow-go/ledger/complete/payloadless" | ||
| "github.com/onflow/flow-go/ledger/complete/wal" | ||
| "github.com/onflow/flow-go/model/bootstrap" | ||
| "github.com/onflow/flow-go/model/flow" | ||
| ) | ||
|
|
||
| var ( | ||
| flagExecutionStateDir string | ||
| flagOutputDir string | ||
| flagStateCommitment string | ||
| flagNWorker uint | ||
| flagMTrieCacheSize uint32 | ||
| ) | ||
|
|
||
| // Cmd extracts the payloadless (V7) trie at a given state commitment from a WAL directory and writes | ||
| // it as a single-trie V7 root checkpoint. It is the payloadless counterpart of | ||
| // execution-state-extract: no migration is performed and no payloads are read, because a payloadless | ||
| // trie stores only leaf hashes. | ||
| var Cmd = &cobra.Command{ | ||
| Use: "execution-state-extract-payloadless", | ||
| Short: "Extract a payloadless (V7) trie at a state commitment into a V7 root checkpoint", | ||
| Long: `Extract the payloadless (V7) trie at a given state commitment and write it as a V7 root checkpoint. | ||
|
|
||
| The trie is loaded from the WAL directory (--execution-state-dir), recovering in-memory state from the | ||
| latest V7 checkpoint plus any newer WAL segments, exactly like the node does at startup. The trie whose | ||
| root hash matches --state-commitment is written to --output-dir as a single-trie V7 root checkpoint | ||
| ("` + bootstrap.FilenameWALRootCheckpoint + wal.V7FileSuffix + `"). | ||
|
|
||
| Because a payloadless trie carries only leaf hashes and no payloads, no migration is possible or needed; | ||
| this command only re-checkpoints the selected trie. It acquires an exclusive lock on the WAL directory, | ||
| so it must be run against a stopped node's data directory.`, | ||
| RunE: runE, | ||
| } | ||
|
|
||
| func init() { | ||
| Cmd.Flags().StringVar(&flagExecutionStateDir, "execution-state-dir", "", | ||
| "Execution Node state dir (where the V7 checkpoint and WAL logs are written)") | ||
| _ = Cmd.MarkFlagRequired("execution-state-dir") | ||
|
|
||
| Cmd.Flags().StringVar(&flagOutputDir, "output-dir", "", | ||
| "Directory to write the V7 root checkpoint to") | ||
| _ = Cmd.MarkFlagRequired("output-dir") | ||
|
|
||
| Cmd.Flags().StringVar(&flagStateCommitment, "state-commitment", "", | ||
| "state commitment of the trie to extract (hex-encoded, 64 characters)") | ||
| _ = Cmd.MarkFlagRequired("state-commitment") | ||
|
|
||
| Cmd.Flags().UintVar(&flagNWorker, "nworker", 16, | ||
| "number of subtrie files to encode in parallel (valid range [1, 16])") | ||
|
|
||
| Cmd.Flags().Uint32Var(&flagMTrieCacheSize, "mtrie-cache-size", ledger.DefaultMTrieCacheSize, | ||
| "number of tries retained in the forest during WAL replay; match the node's --mtrie-cache-size. "+ | ||
| "This is the main driver of peak memory; lower it to reduce memory (at the risk of failing to "+ | ||
| "resolve tries across WAL forks)") | ||
| } | ||
|
|
||
| func runE(*cobra.Command, []string) error { | ||
| stateCommitmentBytes, err := hex.DecodeString(flagStateCommitment) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot decode state commitment: %w", err) | ||
| } | ||
| stateCommitment, err := flow.ToStateCommitment(stateCommitmentBytes) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid state commitment length: %w", err) | ||
| } | ||
|
|
||
| outputFile := bootstrap.FilenameWALRootCheckpoint + wal.V7FileSuffix | ||
|
|
||
| log.Info(). | ||
| Str("execution-state-dir", flagExecutionStateDir). | ||
| Str("output-dir", flagOutputDir). | ||
| Str("state-commitment", stateCommitment.String()). | ||
| Str("output", path.Join(flagOutputDir, outputFile)). | ||
| Msg("extracting payloadless (V7) trie at state commitment") | ||
|
|
||
| if err := os.MkdirAll(flagOutputDir, 0755); err != nil { | ||
| return fmt.Errorf("cannot create output directory %s: %w", flagOutputDir, err) | ||
| } | ||
|
|
||
| trie, err := util.ReadPayloadlessTrie(flagExecutionStateDir, stateCommitment, int(flagMTrieCacheSize)) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot read payloadless trie for state commitment %s: %w", stateCommitment, err) | ||
| } | ||
|
|
||
| log.Info(). | ||
| Str("root_hash", trie.RootHash().String()). | ||
| Uint64("allocated_reg_count", trie.AllocatedRegCount()). | ||
| Msg("loaded payloadless trie, storing V7 root checkpoint") | ||
|
|
||
| err = wal.StoreCheckpointV7( | ||
| []*payloadless.MTrie{trie}, | ||
| flagOutputDir, | ||
| outputFile, | ||
| log.Logger, | ||
| flagNWorker, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot store V7 root checkpoint: %w", err) | ||
| } | ||
|
|
||
| log.Info(). | ||
| Str("state-commitment", ledger.State(trie.RootHash()).String()). | ||
| Str("output", path.Join(flagOutputDir, outputFile)). | ||
| Msg("✅ payloadless (V7) state extraction completed successfully") | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use a different replay function because the original replay would always replay ALL wal files, even if we have found a trie root hash.
For instance, given we have a
root.checkpoint.v7file and 3 wal files: 000, 001, 002. And wal file 001 contains a trie update of root hashA. If we need to extract state for root hash A, the original replay would initialize the DiskWAL by replaying all 3 wal files, if 002 has 1000 trie updates, then the final forest will not include A. However, we could stop wal replaying as soon as replaying 001 and found root hash A, so that the final forest has A as last trie's root hash, and can be used for extracting state.