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