Skip to content

Commit e4fd80a

Browse files
update: compact dbs after pruning. (#13)
* update: compact dbs after pruning. * update: support any db.
1 parent fbbddd5 commit e4fd80a

File tree

3 files changed

+169
-52
lines changed

3 files changed

+169
-52
lines changed

store/helpers.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package store
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"cosmossdk.io/log"
9+
10+
"github.com/KYVENetwork/supervysor/cmd/supervysor/commands/helpers"
11+
"github.com/syndtr/goleveldb/leveldb/opt"
12+
13+
"github.com/tendermint/tendermint/state"
14+
tmStore "github.com/tendermint/tendermint/store"
15+
db "github.com/tendermint/tm-db"
16+
)
17+
18+
func PruneGoLevelDB(home string, untilHeight int64, statePruning bool, logger log.Logger) error {
19+
o := opt.Options{
20+
DisableSeeksCompaction: true,
21+
}
22+
23+
// Get BlockStore
24+
blockStoreDB, err := db.NewGoLevelDBWithOpts("blockstore", filepath.Join(home, "data"), &o)
25+
if err != nil {
26+
return fmt.Errorf("failed to create blockStoreDB: %w", err)
27+
}
28+
blockStore := tmStore.NewBlockStore(blockStoreDB)
29+
30+
// Get StateStore
31+
stateDB, err := db.NewGoLevelDBWithOpts("state", filepath.Join(home, "data"), &o)
32+
if err != nil {
33+
return fmt.Errorf("failed to create stateStoreDB: %w", err)
34+
}
35+
stateStore := state.NewStore(stateDB)
36+
37+
base := blockStore.Base()
38+
39+
logger.Info("blockstore base", "base", base)
40+
41+
if untilHeight < base {
42+
logger.Error("Error: base height is higher than prune height", "base height", base, "until height", untilHeight)
43+
os.Exit(0)
44+
}
45+
46+
blocks, err := blockStore.PruneBlocks(untilHeight)
47+
if err != nil {
48+
logger.Error(err.Error())
49+
os.Exit(0)
50+
}
51+
52+
if err = blockStoreDB.ForceCompact(nil, nil); err != nil {
53+
logger.Error(err.Error())
54+
os.Exit(0)
55+
}
56+
57+
if statePruning {
58+
err = stateStore.PruneStates(base, untilHeight)
59+
if err != nil {
60+
logger.Error(err.Error())
61+
os.Exit(0)
62+
}
63+
64+
if err = stateDB.ForceCompact(nil, nil); err != nil {
65+
logger.Error(err.Error())
66+
os.Exit(0)
67+
}
68+
69+
logger.Info(fmt.Sprintf("Pruned %d blocks and the state until %d, new base height is %d", blocks, untilHeight, blockStore.Base()))
70+
} else {
71+
logger.Info(fmt.Sprintf("Pruned %d blocks until %d, new base height is %d", blocks, untilHeight, blockStore.Base()))
72+
}
73+
74+
return nil
75+
}
76+
77+
func PruneAnyDB(home string, untilHeight int64, statePruning bool, logger log.Logger) error {
78+
config, err := helpers.LoadConfig(home)
79+
if err != nil {
80+
return fmt.Errorf("failed to load config: %w", err)
81+
}
82+
83+
blockStoreDB, blockStore, err := GetBlockstoreDBs(config)
84+
defer func(blockStoreDB db.DB) {
85+
err = blockStoreDB.Close()
86+
if err != nil {
87+
logger.Error(err.Error())
88+
os.Exit(0)
89+
}
90+
}(blockStoreDB)
91+
92+
if err != nil {
93+
panic(fmt.Errorf("failed to load blockstore db: %w", err))
94+
}
95+
96+
base := blockStore.Base()
97+
98+
logger.Info("blockstore base", "base", base)
99+
100+
if untilHeight < base {
101+
logger.Error("Error: base height is higher than prune height", "base height", base, "until height", untilHeight)
102+
os.Exit(0)
103+
}
104+
105+
blocks, err := blockStore.PruneBlocks(untilHeight)
106+
if err != nil {
107+
logger.Error(err.Error())
108+
os.Exit(0)
109+
}
110+
111+
if statePruning {
112+
stateStoreDB, stateStore, err := GetStateDBs(config)
113+
defer func(stateStoreDB db.DB) {
114+
err = stateStoreDB.Close()
115+
if err != nil {
116+
logger.Error(err.Error())
117+
os.Exit(0)
118+
}
119+
}(stateStoreDB)
120+
121+
if err = stateStore.PruneStates(base, untilHeight); err != nil {
122+
logger.Error(err.Error())
123+
os.Exit(0)
124+
}
125+
126+
logger.Info(fmt.Sprintf("Pruned %d blocks and the state until %d, new base height is %d", blocks, untilHeight, blockStore.Base()))
127+
} else {
128+
logger.Info(fmt.Sprintf("Pruned %d blocks until %d, new base height is %d", blocks, untilHeight, blockStore.Base()))
129+
}
130+
131+
return nil
132+
}

store/prune.go

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,24 @@ package store
22

33
import (
44
"fmt"
5-
"os"
5+
"strings"
66

7-
"github.com/KYVENetwork/supervysor/cmd/supervysor/commands/helpers"
7+
"github.com/KYVENetwork/supervysor/utils"
88

99
"cosmossdk.io/log"
10-
11-
dbm "github.com/tendermint/tm-db"
1210
)
1311

1412
func Prune(home string, untilHeight int64, statePruning bool, logger log.Logger) error {
15-
config, err := helpers.LoadConfig(home)
13+
config, err := utils.LoadConfig(home)
1614
if err != nil {
1715
return fmt.Errorf("failed to load config: %w", err)
1816
}
1917

20-
blockStoreDB, blockStore, err := GetBlockstoreDBs(config)
21-
defer func(blockStoreDB dbm.DB) {
22-
err = blockStoreDB.Close()
23-
if err != nil {
24-
logger.Error(err.Error())
25-
os.Exit(0)
26-
}
27-
}(blockStoreDB)
28-
29-
if err != nil {
30-
panic(fmt.Errorf("failed to load blockstore db: %w", err))
31-
}
32-
33-
base := blockStore.Base()
34-
35-
logger.Info("blockstore base", "base", base)
36-
37-
if untilHeight < base {
38-
logger.Error("Error: base height is higher than prune height", "base height", base, "until height", untilHeight)
39-
os.Exit(0)
40-
}
41-
42-
blocks, err := blockStore.PruneBlocks(untilHeight)
43-
if err != nil {
44-
logger.Error(err.Error())
45-
os.Exit(0)
46-
}
47-
48-
if statePruning {
49-
stateStoreDB, stateStore, err := GetStateDBs(config)
50-
defer func(stateStoreDB dbm.DB) {
51-
err = stateStoreDB.Close()
52-
if err != nil {
53-
logger.Error(err.Error())
54-
os.Exit(0)
55-
}
56-
}(stateStoreDB)
57-
58-
if err = stateStore.PruneStates(base, untilHeight); err != nil {
59-
logger.Error(err.Error())
60-
os.Exit(0)
61-
}
62-
63-
logger.Info(fmt.Sprintf("Pruned %d blocks and the state until %d, new base height is %d", blocks, untilHeight, blockStore.Base()))
18+
if strings.ToLower(config.DBBackend) == "goleveldb" {
19+
logger.Info("GoLevelDB detected, using ForceCompact")
20+
return PruneGoLevelDB(home, untilHeight, statePruning, logger)
6421
} else {
65-
logger.Info(fmt.Sprintf("Pruned %d blocks until %d, new base height is %d", blocks, untilHeight, blockStore.Base()))
22+
logger.Info("another DB backend than GoLevelDB detected, ForceCompact not supported")
23+
return PruneAnyDB(home, untilHeight, statePruning, logger)
6624
}
67-
68-
return nil
6925
}

utils/config.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package utils
2+
3+
import (
4+
"path/filepath"
5+
6+
"github.com/spf13/viper"
7+
cfg "github.com/tendermint/tendermint/config"
8+
)
9+
10+
func LoadConfig(homePath string) (*cfg.Config, error) {
11+
config := cfg.DefaultConfig()
12+
13+
viper.SetConfigName("config")
14+
viper.SetConfigType("toml")
15+
viper.AddConfigPath(homePath)
16+
viper.AddConfigPath(filepath.Join(homePath, "config"))
17+
18+
if err := viper.ReadInConfig(); err != nil {
19+
return nil, err
20+
}
21+
22+
if err := viper.Unmarshal(config); err != nil {
23+
return nil, err
24+
}
25+
26+
config.SetRoot(homePath)
27+
28+
return config, nil
29+
}

0 commit comments

Comments
 (0)