-
Notifications
You must be signed in to change notification settings - Fork 151
Release oasis storage prune and compact subcomands #6521
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ed218bd
go/oasis-test-runner: Add wait methods to controller
martintomazic be68cda
go/oasis-test-runner/scenario/e2e: Add offline pruning scenario
martintomazic b4033cb
go/oasis-node/cmd/storage: Factor pruning command to separate file
martintomazic c39461d
go/oasis-node/cmd/storage: Add new newPruneCmd function
martintomazic dd1593b
go/oasis-node/cmd/storage: Generalize pruneConsensusNodeDB
martintomazic 2276a97
go/oasis-node/cmd/storage: Prune block and state store in batches
martintomazic 25f7da1
go/runtime/history: Add new Prune method
martintomazic f8dc052
go/oasis-node/cmd/storage: Release offline pruning
martintomazic 99f5d97
go/oasis-node/cmd/storage: Factor compact comand to separate file
martintomazic c7a6b4e
go/oasis-node/cmd/storage: Add new newCompactCmd function
martintomazic a1eafc0
go/oasis-node/cmd/storage: Defer badger DB close
martintomazic d3f07d4
go/runtime/history: Add new Compact command
martintomazic 0ce7d80
go/oasis-node/cmd/storage: Release offline compaction command
martintomazic d3189ce
go/oasis-node/cmd/storage: Improve compact commands helpers
martintomazic 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
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,5 @@ | ||
| go/oasis-node/cmd/storage: Release offline pruning command | ||
|
|
||
| Command `storage prune-experimental` has been released as | ||
| `storage prune` and now in addition to consensus databases | ||
| also prunes runtime databases. |
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,5 @@ | ||
| go/oasis-node/cmd/storage: Release offline compaction command | ||
|
|
||
| Command `storage compact-experimental` has been released as | ||
| `storage compact` and now in addition to consensus databases | ||
| also compacts runtime databases. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/fs" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/oasisprotocol/oasis-core/go/common" | ||
| cmtDBProvider "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/db/badger" | ||
| cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common" | ||
| "github.com/oasisprotocol/oasis-core/go/runtime/registry" | ||
| ) | ||
|
|
||
| func newCompactCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "compact", | ||
| Args: cobra.NoArgs, | ||
| Short: "trigger compaction for all databases", | ||
| Long: `Optimize the storage for all databases by manually compacting the underlying storage engines. | ||
|
|
||
| WARNING: Ensure you have at least as much of a free disk as your largest database. | ||
| `, | ||
| PreRunE: func(_ *cobra.Command, args []string) error { | ||
| if err := cmdCommon.Init(); err != nil { | ||
| cmdCommon.EarlyLogAndExit(err) | ||
| } | ||
|
|
||
| running, err := cmdCommon.IsNodeRunning() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to ensure the node is not running: %w", err) | ||
| } | ||
|
|
||
| if running { | ||
| return fmt.Errorf("compaction can only be done when the node is not running") | ||
| } | ||
| return nil | ||
| }, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| dataDir := cmdCommon.DataDir() | ||
|
|
||
| logger.Info("Starting database compactions. This may take a while...") | ||
|
|
||
| if err := compactConsensusDBs(dataDir); err != nil { | ||
| return fmt.Errorf("failed to compact consensus databases: %w", err) | ||
| } | ||
|
|
||
| runtimes, err := registry.GetConfiguredRuntimeIDs() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get configured runtimes: %w", err) | ||
| } | ||
| for _, rt := range runtimes { | ||
| if err := compactRuntimeDBs(dataDir, rt); err != nil { | ||
| return fmt.Errorf("failed to compact runtime dbs (runtime ID: %s): %w", rt, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func compactConsensusDBs(dataDir string) error { | ||
| // Compact CometBFT managed databases: block store, evidence and state (NOT application state). | ||
| if err := compactCometDBs(dataDir); err != nil { | ||
| return fmt.Errorf("failed to compact CometBFT managed databases: %w", err) | ||
| } | ||
|
|
||
| // Compact consensus NodeDB (application state). | ||
| ndb, close, err := openConsensusNodeDB(dataDir) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open consensus NodeDB: %w", err) | ||
| } | ||
| defer close() | ||
|
|
||
| if err := ndb.Compact(); err != nil { | ||
| return fmt.Errorf("failed to compact consensus node DB: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func compactCometDBs(dataDir string) error { | ||
| dir := fmt.Sprintf("%s/consensus/data", dataDir) | ||
|
|
||
| var dbDirs []string | ||
| err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if d.IsDir() && strings.HasSuffix(d.Name(), ".db") { | ||
| dbDirs = append(dbDirs, path) | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to walk dir %s: %w", dir, err) | ||
| } | ||
|
|
||
| if len(dbDirs) == 0 { | ||
| return fmt.Errorf("zero database instances found") | ||
| } | ||
|
|
||
| for _, dbDir := range dbDirs { | ||
| if err := compactCometDB(dbDir); err != nil { | ||
| return fmt.Errorf("failed to compact %s: %w", dbDir, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func compactCometDB(path string) error { | ||
| logger := logger.With("path", path) | ||
| db, err := cmtDBProvider.OpenBadger(path, logger) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open BadgerDB: %w", err) | ||
| } | ||
| defer db.Close() | ||
|
|
||
| logger.Info("compacting") | ||
|
|
||
| if err := db.Flatten(1); err != nil { | ||
| return fmt.Errorf("failed to flatten db: %w", err) | ||
| } | ||
|
|
||
| logger.Info("compaction completed") | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func compactRuntimeDBs(dataDir string, rt common.Namespace) error { | ||
| history, err := openRuntimeLightHistory(dataDir, rt) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open runtime history: %w", err) | ||
| } | ||
| defer history.Close() | ||
| if err := history.Compact(); err != nil { | ||
| return fmt.Errorf("failed to compact runtime history: %w", err) | ||
| } | ||
| ndb, err := openRuntimeStateDB(dataDir, rt) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open runtime state DB: %w", err) | ||
| } | ||
| defer ndb.Close() | ||
| if err := ndb.Compact(); err != nil { | ||
| return fmt.Errorf("failed to compact runtime state DB: %w", err) | ||
| } | ||
| return nil | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.