-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
242 lines (197 loc) · 6.23 KB
/
main.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
package main
import (
"fmt"
"github.com/ardanlabs/conf"
"github.com/cockroachdb/pebble"
"github.com/pkg/errors"
"github.com/qubic/archiver-db-migrator/migration"
"github.com/qubic/archiver-db-migrator/store"
"log"
"os"
)
const prefix = "QUBIC_ARCHIVER_DB_MIGRATOR"
func main() {
if err := run(); err != nil {
log.Fatalf("main: exited with error: %s", err.Error())
}
}
func run() error {
var config struct {
Migration struct {
TickData bool `conf:"default:true"`
QuorumData bool `conf:"default:true"`
ComputorList bool `conf:"default:true"`
Transactions bool `conf:"default:true"`
TransactionStatus bool `conf:"default:true"`
LastProcessedTick bool `conf:"default:true"`
LastProcessedTickPerEpoch bool `conf:"default:true"`
SkippedTicksInterval bool `conf:"default:true"`
IdentityTransferTransactions bool `conf:"default:true"`
ChainDigest bool `conf:"default:true"`
ProcessedTickIntervals bool `conf:"default:true"`
TickTransactionStatus bool `conf:"default:true"`
StoreDigest bool `conf:"default:true"`
EmptyTicksPerEpoch bool `conf:"default:true"`
}
Options struct {
MigrateQuorumDataToV2 bool `conf:"default:true"`
}
Database struct {
OldPath string `conf:"default:./storage/old"`
OldCompression string `conf:"default:Snappy"`
NewPath string `conf:"default:./storage/new/zstd"`
NewCompression string `conf:"default:Zstd"`
}
}
if err := conf.Parse(os.Args[1:], prefix, &config); err != nil {
switch err {
case conf.ErrHelpWanted:
usage, err := conf.Usage(prefix, &config)
if err != nil {
return errors.Wrap(err, "generating config usage")
}
fmt.Println(usage)
return nil
case conf.ErrVersionWanted:
version, err := conf.VersionString(prefix, &config)
if err != nil {
return errors.Wrap(err, "generating config version")
}
fmt.Println(version)
return nil
}
return errors.Wrap(err, "parsing config")
}
out, err := conf.String(&config)
if err != nil {
return errors.Wrap(err, "generating output for config")
}
log.Printf("main: Config :\n%v\n", out)
println("Migrator started")
oldDB, err := createDBFromConfig(config.Database.OldPath, config.Database.OldCompression)
if err != nil {
return errors.Wrap(err, "creating old db")
}
defer oldDB.Close()
newDB, err := createDBFromConfig(config.Database.NewPath, config.Database.NewCompression)
if err != nil {
return errors.Wrap(err, "creating new db")
}
defer newDB.Close()
if config.Migration.TickData {
println("Migrating tick data...")
err = migration.MigrateTickData(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating tick data")
}
}
if config.Migration.QuorumData {
println("Migrating quorum data...")
if !config.Options.MigrateQuorumDataToV2 {
err = migration.MigrateQuorumData(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating quorum data")
}
}
err = migration.MigrateQuorumDataV2(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating quorum data to v2")
}
}
if config.Migration.ComputorList {
println("Migrating computor list...")
err = migration.MigrateComputorList(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating computor list")
}
}
if config.Migration.Transactions {
println("Migrating transactions...")
err = migration.MigrateTransactions(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating transactions")
}
}
if config.Migration.TransactionStatus {
println("Migrating transaction status...")
err = migration.MigrateTransactionStatus(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating transaction status")
}
}
if config.Migration.LastProcessedTick {
println("Migrating last processed tick...")
err = migration.MigrateLastProcessedTick(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating last processed tick")
}
}
if config.Migration.LastProcessedTickPerEpoch {
println("Migrating last processed tick per epoch...")
err = migration.MigrateLastProcessedTicksPerEpoch(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating last processed tick per epoch")
}
}
if config.Migration.SkippedTicksInterval {
println("Migrating skipped ticks interval...")
err = migration.MigrateSkippedTicksIntervals(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating skipped ticks interval")
}
}
if config.Migration.IdentityTransferTransactions {
println("Migrating identity transfer transactions...")
err = migration.MigrateIdentityTransferTransactions(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating identity transfer transactions")
}
}
if config.Migration.ChainDigest {
println("Migrating chain digest...")
err = migration.MigrateChainDigest(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating chain digest")
}
}
if config.Migration.ProcessedTickIntervals {
println("Migrating processed tick intervals...")
err = migration.MigrateProcessedTickIntervals(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating processed tick intervals")
}
}
if config.Migration.TickTransactionStatus {
println("Migrating transaction status...")
err = migration.MigrateTickTransactionsStatus(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating transaction status")
}
}
if config.Migration.StoreDigest {
println("Migrating store digest...")
err = migration.MigrateStoreDigest(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating store digest")
}
}
if config.Migration.EmptyTicksPerEpoch {
println("Migrating empty ticks per epoch...")
err = migration.MigrateEmptyTicksPerEpoch(oldDB, newDB)
if err != nil {
return errors.Wrap(err, "migrating empty ticks per epoch")
}
}
println("Migration done.")
return nil
}
func createDBFromConfig(path, compressionType string) (*pebble.DB, error) {
switch compressionType {
case "Snappy":
return store.CreateDBWithDefaultOptions(path)
case "Zstd":
return store.CreateDBWithZstdCompression(path)
default:
return nil, errors.New("unknown compression type")
}
}