github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/cmd/tendermint/commands/compact.go (about) 1 package commands 2 3 import ( 4 "errors" 5 "path/filepath" 6 "sync" 7 8 "github.com/spf13/cobra" 9 "github.com/syndtr/goleveldb/leveldb" 10 "github.com/syndtr/goleveldb/leveldb/opt" 11 "github.com/syndtr/goleveldb/leveldb/util" 12 13 "github.com/ari-anchor/sei-tendermint/config" 14 "github.com/ari-anchor/sei-tendermint/libs/log" 15 ) 16 17 func MakeCompactDBCommand(cfg *config.Config, logger log.Logger) *cobra.Command { 18 cmd := &cobra.Command{ 19 Use: "experimental-compact-goleveldb", 20 Short: "force compacts the tendermint storage engine (only GoLevelDB supported)", 21 Long: ` 22 This is a temporary utility command that performs a force compaction on the state 23 and blockstores to reduce disk space for a pruning node. This should only be run 24 once the node has stopped. This command will likely be omitted in the future after 25 the planned refactor to the storage engine. 26 27 Currently, only GoLevelDB is supported. 28 `, 29 RunE: func(cmd *cobra.Command, args []string) error { 30 if cfg.DBBackend != "goleveldb" { 31 return errors.New("compaction is currently only supported with goleveldb") 32 } 33 34 compactGoLevelDBs(cfg.RootDir, logger) 35 return nil 36 }, 37 } 38 39 return cmd 40 } 41 42 func compactGoLevelDBs(rootDir string, logger log.Logger) { 43 dbNames := []string{"state", "blockstore"} 44 o := &opt.Options{ 45 DisableSeeksCompaction: true, 46 } 47 wg := sync.WaitGroup{} 48 49 for _, dbName := range dbNames { 50 dbName := dbName 51 wg.Add(1) 52 go func() { 53 defer wg.Done() 54 dbPath := filepath.Join(rootDir, "data", dbName+".db") 55 store, err := leveldb.OpenFile(dbPath, o) 56 if err != nil { 57 logger.Error("failed to initialize tendermint db", "path", dbPath, "err", err) 58 return 59 } 60 defer store.Close() 61 62 logger.Info("starting compaction...", "db", dbPath) 63 64 err = store.CompactRange(util.Range{Start: nil, Limit: nil}) 65 if err != nil { 66 logger.Error("failed to compact tendermint db", "path", dbPath, "err", err) 67 } 68 }() 69 } 70 wg.Wait() 71 }