/
data.go
322 lines (262 loc) · 8.27 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package cli
import (
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/cosmos/cosmos-sdk/server"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/syndtr/goleveldb/leveldb/util"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/node"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/store"
dbm "github.com/tendermint/tm-db"
)
const (
flagHeight = "height"
flagPruning = "enable_pruning"
flagDBBackend = "db_backend"
blockDBName = "blockstore"
stateDBName = "state"
appDBName = "application"
)
func DataCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "data",
Short: "modify data or query data in database",
}
cmd.AddCommand(
dataQueryCmd(),
dataPruningCmd(),
)
return cmd
}
func dataQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Short: "Query blocks and states in database",
}
queryBlockState := &cobra.Command{
Use: "block",
Short: "Query blocks heights in db",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
config := ctx.Config
blockStoreDB := initDB(config, blockDBName)
blockStore := store.NewBlockStore(blockStoreDB)
fmt.Printf("[%d ~ %d]\n", blockStore.Base(), blockStore.Height())
return nil
},
}
cmd.AddCommand(queryBlockState)
cmd.PersistentFlags().String(flagDBBackend, "goleveldb", "Database backend: goleveldb")
return cmd
}
func dataPruningCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "prune-compact",
Short: "Prune and Compact blocks and application states",
}
cmd.AddCommand(
pruneAllCmd(),
pruneAppCmd(),
pruneBlockCmd(),
)
cmd.PersistentFlags().Int64P(flagHeight, "r", 0, "Removes block or state up to (but not including) a height")
cmd.PersistentFlags().BoolP(flagPruning, "p", false, "Enable pruning")
cmd.PersistentFlags().String(flagDBBackend, "goleveldb", "Database backend: goleveldb")
return cmd
}
func pruneAllCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "all",
Short: "Compact both application states and blocks",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
config := ctx.Config
if err := checkBackend(dbm.BackendType(ctx.Config.DBBackend)); err != nil {
return err
}
blockStoreDB := initDB(config, blockDBName)
stateDB := initDB(config, stateDBName)
appDB := initDB(config, appDBName)
if viper.GetBool(flagPruning) {
baseHeight, retainHeight := getPruneBlockParams(blockStoreDB)
var wg sync.WaitGroup
log.Println("--------- pruning start... ---------")
wg.Add(2)
go pruneBlocks(blockStoreDB, baseHeight, retainHeight, &wg)
go pruneStates(stateDB, baseHeight, retainHeight, &wg)
wg.Wait()
log.Println("--------- pruning end!!! ---------")
}
log.Println("--------- compact start... ---------")
var wg sync.WaitGroup
wg.Add(3)
go compactDB(blockStoreDB, blockDBName, dbm.BackendType(ctx.Config.DBBackend), &wg)
go compactDB(stateDB, stateDBName, dbm.BackendType(ctx.Config.DBBackend), &wg)
go compactDB(appDB, appDBName, dbm.BackendType(ctx.Config.DBBackend), &wg)
wg.Wait()
log.Println("--------- compact end!!! ---------")
return nil
},
}
return cmd
}
func pruneAppCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "state",
Short: "Compact application state",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
config := ctx.Config
if err := checkBackend(dbm.BackendType(ctx.Config.DBBackend)); err != nil {
return err
}
appDB := initDB(config, appDBName)
log.Println("--------- compact start ---------")
var wg sync.WaitGroup
wg.Add(1)
compactDB(appDB, appDBName, dbm.BackendType(ctx.Config.DBBackend), &wg)
log.Println("--------- compact end ---------")
return nil
},
}
return cmd
}
func pruneBlockCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "block",
Short: "Compact while pruning blocks and states",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)
config := ctx.Config
if err := checkBackend(dbm.BackendType(ctx.Config.DBBackend)); err != nil {
return err
}
blockStoreDB := initDB(config, blockDBName)
stateDB := initDB(config, stateDBName)
if viper.GetBool(flagPruning) {
baseHeight, retainHeight := getPruneBlockParams(blockStoreDB)
//
log.Println("--------- pruning start... ---------")
var wg sync.WaitGroup
wg.Add(2)
go pruneBlocks(blockStoreDB, baseHeight, retainHeight, &wg)
go pruneStates(stateDB, baseHeight, retainHeight, &wg)
wg.Wait()
log.Println("--------- pruning end!!! ---------")
}
log.Println("--------- compact start... ---------")
var wg sync.WaitGroup
wg.Add(2)
go compactDB(blockStoreDB, blockDBName, dbm.BackendType(ctx.Config.DBBackend), &wg)
go compactDB(stateDB, stateDBName, dbm.BackendType(ctx.Config.DBBackend), &wg)
wg.Wait()
log.Println("--------- compact end!!! ---------")
return nil
},
}
return cmd
}
func getPruneBlockParams(blockStoreDB dbm.DB) (baseHeight, retainHeight int64) {
baseHeight, size := getBlockInfo(blockStoreDB)
retainHeight = viper.GetInt64(flagHeight)
if retainHeight >= baseHeight+size-1 || retainHeight <= baseHeight {
retainHeight = baseHeight + size - 2
}
return
}
func initDB(config *cfg.Config, dbName string) dbm.DB {
if dbName != blockDBName && dbName != stateDBName && dbName != appDBName {
panic(fmt.Sprintf("unknow db name:%s", dbName))
}
db, err := node.DefaultDBProvider(&node.DBContext{ID: dbName, Config: config})
if err != nil {
panic(err)
}
return db
}
// pruneBlocks deletes blocks between the given heights (including from, excluding to).
func pruneBlocks(blockStoreDB dbm.DB, baseHeight, retainHeight int64, wg *sync.WaitGroup) {
defer wg.Done()
log.Printf("Prune blocks [%d,%d)...", baseHeight, retainHeight)
if retainHeight <= baseHeight {
return
}
baseHeightBefore, sizeBefore := getBlockInfo(blockStoreDB)
start := time.Now()
_, err := store.NewBlockStore(blockStoreDB).PruneBlocks(retainHeight)
if err != nil {
panic(fmt.Errorf("failed to prune block store: %w", err))
}
baseHeightAfter, sizeAfter := getBlockInfo(blockStoreDB)
log.Printf("Block db info [baseHeight,size]: [%d,%d] --> [%d,%d]\n", baseHeightBefore, sizeBefore, baseHeightAfter, sizeAfter)
log.Printf("Prune blocks done in %v \n", time.Since(start))
}
// pruneStates deletes states between the given heights (including from, excluding to).
func pruneStates(stateDB dbm.DB, from, to int64, wg *sync.WaitGroup) {
defer wg.Done()
log.Printf("Prune states [%d,%d)...", from, to)
if to <= from {
return
}
start := time.Now()
stateStore := sm.NewStore(stateDB, sm.StoreOptions{
DiscardABCIResponses: false,
})
if err := stateStore.PruneStates(from, to); err != nil {
panic(fmt.Errorf("failed to prune state database: %w", err))
}
log.Printf("Prune states done in %v \n", time.Since(start))
}
func compactDB(db dbm.DB, name string, dbType dbm.BackendType, wg *sync.WaitGroup) {
defer wg.Done()
log.Printf("Compact %s... \n", name)
start := time.Now()
if dbCompactor, ok := backends[dbType]; !ok {
panic(fmt.Sprintf("Unknown db_backend %s, ", dbType))
} else {
dbCompactor(db)
}
log.Printf("Compact %s done in %v \n", name, time.Since(start))
}
func getBlockInfo(blockStoreDB dbm.DB) (baseHeight, size int64) {
blockStore := store.NewBlockStore(blockStoreDB)
baseHeight = blockStore.Base()
size = blockStore.Size()
return
}
type dbCompactor func(dbm.DB)
var backends = map[dbm.BackendType]dbCompactor{}
func init() {
dbCompactor := func(db dbm.DB) {
if ldb, ok := db.(*dbm.GoLevelDB); ok {
if err := ldb.DB().CompactRange(util.Range{}); err != nil {
panic(err)
}
}
}
registerDBCompactor(dbm.GoLevelDBBackend, dbCompactor)
}
func registerDBCompactor(dbType dbm.BackendType, compactor dbCompactor) {
if _, ok := backends[dbType]; ok {
return
}
backends[dbType] = compactor
}
func checkBackend(dbType dbm.BackendType) error {
if _, ok := backends[dbType]; !ok {
keys := make([]string, len(backends))
i := 0
for k := range backends {
keys[i] = string(k)
i++
}
return fmt.Errorf("unknown db_backend %s, expected <%s>", dbType, strings.Join(keys, " , "))
}
return nil
}