-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathgen.go
236 lines (208 loc) · 5.28 KB
/
gen.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
package gen
import (
"fmt"
"os"
"sync"
"time"
"github.com/cosmos/iavl-bench/bench"
"github.com/cosmos/iavl/v2"
"github.com/cosmos/iavl/v2/testutil"
"github.com/dustin/go-humanize"
"github.com/kocubinski/costor-api/compact"
"github.com/kocubinski/costor-api/core"
"github.com/rs/zerolog"
zlog "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var log = zlog.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.Stamp,
})
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "gen",
Short: "generate changesets",
}
cmd.AddCommand(emitCommand(), treeCommand())
return cmd
}
func getChangesetIterator(typ string) (bench.ChangesetIterator, error) {
switch typ {
case "osmo-like":
return testutil.OsmoLike().Iterator, nil
case "osmo-like-many":
return testutil.OsmoLikeManyTrees().Iterator, nil
case "height-zero":
return testutil.NewTreeBuildOptions().Iterator, nil
default:
return nil, fmt.Errorf("unknown generator type %s", typ)
}
}
func emitCommand() *cobra.Command {
var (
typ string
out string
start int
limit int
)
cmd := &cobra.Command{
Use: "emit",
Short: "emit generated changesets to disk",
RunE: func(cmd *cobra.Command, _ []string) error {
itr, err := getChangesetIterator(typ)
if err != nil {
return err
}
ctx := core.Context{Context: cmd.Context()}
stream := compact.StreamingContext{
In: make(chan compact.Sequenced),
Context: ctx,
OutDir: out,
MaxFileSize: 100 * 1024 * 1024,
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
stats, err := stream.Compact()
if err != nil {
log.Fatal().Err(err).Msg("failed to compact")
}
log.Info().Msg(stats.Report())
wg.Done()
}()
var cnt int64
for ; itr.Valid(); err = itr.Next() {
if err != nil {
return err
}
if limit > 0 && itr.Version() > int64(limit) {
break
}
nodes := itr.Nodes()
for ; nodes.Valid(); err = nodes.Next() {
cnt++
if itr.Version() < int64(start) {
if cnt%5_000_000 == 0 {
log.Info().Msgf("fast forward version=%d nodes=%s", itr.Version(), humanize.Comma(cnt))
}
continue
}
if cnt%500_000 == 0 {
log.Info().Msgf("version=%d nodes=%s", itr.Version(), humanize.Comma(cnt))
}
select {
case <-cmd.Context().Done():
close(stream.In)
wg.Wait()
return nil
default:
}
if err != nil {
return err
}
stream.In <- nodes.GetNode()
}
}
close(stream.In)
wg.Wait()
return nil
},
}
cmd.Flags().StringVar(&typ, "type", "", "the type of changeset to generate")
if err := cmd.MarkFlagRequired("type"); err != nil {
panic(err)
}
cmd.Flags().StringVar(&out, "out", "", "the directory to write changesets to")
if err := cmd.MarkFlagRequired("out"); err != nil {
panic(err)
}
cmd.Flags().IntVar(&limit, "limit", -1, "the version (inclusive) to halt generation at. -1 means no limit")
cmd.Flags().IntVar(&start, "start", 1, "the version (inclusive) to start generation at")
return cmd
}
func treeCommand() *cobra.Command {
var (
dbPath string
genType string
limit int64
)
cmd := &cobra.Command{
Use: "tree",
Short: "build and save a Tree to disk, taking generated changesets as input",
RunE: func(_ *cobra.Command, _ []string) error {
multiTree := iavl.NewMultiTree(dbPath, iavl.TreeOptions{StateStorage: true})
defer func(mt *iavl.MultiTree) {
err := mt.Close()
if err != nil {
log.Error().Err(err).Msg("failed to close db")
}
}(multiTree)
itr, err := getChangesetIterator(genType)
if err != nil {
return err
}
var i int64
var lastHash []byte
var lastVersion int64
start := time.Now()
for ; itr.Valid(); err = itr.Next() {
if err != nil {
return err
}
if limit > -1 && itr.Version() > limit {
break
}
changeset := itr.Nodes()
for ; changeset.Valid(); err = changeset.Next() {
if err != nil {
return err
}
node := changeset.GetNode()
key := node.Key
tree, ok := multiTree.Trees[node.StoreKey]
if !ok {
if err = multiTree.MountTree(node.StoreKey); err != nil {
return err
}
tree = multiTree.Trees[node.StoreKey]
}
if node.Delete {
_, _, err = tree.Remove(key)
if err != nil {
return err
}
} else {
_, err = tree.Set(key, node.Value)
if err != nil {
return err
}
}
i++
if i%100_000 == 0 {
log.Info().Msgf("leaves=%s dur=%s rate=%s version=%d",
humanize.Comma(i),
time.Since(start),
humanize.Comma(int64(100_000/time.Since(start).Seconds())),
itr.Version(),
)
start = time.Now()
}
}
lastHash, lastVersion, err = multiTree.SaveVersionConcurrently()
if err != nil {
return err
}
}
log.Info().Msgf("last version=%d hash=%x", lastVersion, lastHash)
return nil
},
}
cmd.Flags().StringVar(&genType, "type", "", "the type of changeset to generate")
if err := cmd.MarkFlagRequired("type"); err != nil {
panic(err)
}
cmd.Flags().StringVar(&dbPath, "db", "/tmp", "the path to the database")
cmd.Flags().Int64Var(&limit, "limit", -1, "the version (inclusive) to halt generation at. -1 means no limit")
return cmd
}
// pre-requisites this command