Skip to content
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

Add cmd for db flattening #160

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"log"
"os"
"runtime"
"strconv"

"github.com/aurora-is-near/relayer2-base/db/badger/core"
Expand Down Expand Up @@ -109,6 +111,42 @@ func GetBlockCmd() *cobra.Command {
return getLastBlockCmd
}

func FlattenDB() *cobra.Command {
return &cobra.Command{
Use: "flatten-db <dbPath>",
Short: "Command to flatten db (merge all LSM levels) to improve performance",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dbPath := args[0]

log.Printf("Opening database at %s...", dbPath)
config := core.Config{
BadgerConfig: badger.DefaultOptions(dbPath),
GcIntervalSeconds: 60 * 60 * 24 * 365, // We don't want GC to be running during the flattening
}
db, err := core.NewDB(config, codec.NewTinypackCodec())
if err != nil {
return fmt.Errorf("unable to open database: %w", err)
}

defer func() {
log.Printf("Closing database")
if err := db.Close(); err != nil {
log.Printf("Error: unable to close database normally: %v", err)
}
}()

log.Printf("Flattening database, that might take some time...")
threads := runtime.GOMAXPROCS(0)
if err := db.BadgerDB().Flatten(threads); err != nil {
return fmt.Errorf("can't flatten database: %w", err)
}

return nil
},
}
}

// dbView opens db in read-only mode and calls fn with ViewTxn
func dbView(dbPath string, fn func(txn *core.ViewTxn) error) error {
config := core.Config{
Expand Down