-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
496 lines (440 loc) · 13.2 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package main
import (
"context"
_ "embed"
"fmt"
"os"
"strings"
"time"
metrics "github.com/ipfs/go-metrics-interface"
"github.com/urfave/cli/v2"
)
//go:embed VERSION
var rawVersion string
var version string
const appName = "archiver"
const epochInSeconds = 30
func init() {
version = rawVersion
if idx := strings.Index(version, "\n"); idx > -1 {
version = version[:idx]
}
}
func main() {
ctx := context.Background()
if err := app.RunContext(ctx, os.Args); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
var app = &cli.App{
Name: appName,
Usage: "produces regular archives of on-chain state for the Filecoin network.",
Version: version,
Commands: []*cli.Command{
{
Name: "run",
Usage: "Produce daily archives of data.",
Before: configure,
Flags: flagSet(
loggingFlags,
networkFlags,
lilyFlags,
storageFlags,
diagnosticsFlags,
[]cli.Flag{
&cli.StringFlag{
Name: "ship-path",
EnvVars: []string{"ARCHIVER_SHIP_PATH"},
Usage: "Path used to write verified exports from lily.",
Required: true,
},
&cli.Int64Flag{
Name: "min-height",
EnvVars: []string{"ARCHIVER_MIN_HEIGHT"},
Usage: "Minimum height that should be exported. This may be used for nodes that do not have full state history.",
Value: 1005360, // TODO: remove default
},
&cli.Int64Flag{
Name: "max-height",
EnvVars: []string{"ARCHIVER_MAX_HEIGHT"},
Usage: "Maximum height that should be exported. If not specified the archiver will continue forever, waiting for the chain to advance.",
},
&cli.StringFlag{
Name: "tasks",
EnvVars: []string{"ARCHIVER_TASKS"},
Usage: "Comma separated list of tasks that are allowed to be processed. Default is all tasks.",
Value: "",
},
&cli.StringFlag{
Name: "compression",
EnvVars: []string{"ARCHIVER_COMPRESSION"},
Usage: "Type of compression to use.",
Value: "gz",
Hidden: true,
},
},
),
Action: func(cc *cli.Context) error {
ctx := metrics.CtxScope(cc.Context, appName)
setupMetrics(ctx)
tasks := cc.String("tasks")
shipPath := cc.String("ship-path")
minHeight := cc.Int64("min-height")
maxHeight := cc.Int64("max-height")
// Build list of allowed tables. Could be all tables.
var allowedTables []Table
if tasks == "" || tasks == "all" {
allowedTables = append(allowedTables, TableList...)
} else {
taskList, err := parseTaskList(cc.String("tasks"))
if err != nil {
return fmt.Errorf("invalid tasks specified: %v", err)
}
if len(taskList) == 0 {
return fmt.Errorf("invalid tasks specified")
}
for _, task := range taskList {
tables := TablesByTask(task, storageConfig.schemaVersion)
allowedTables = append(allowedTables, tables...)
}
}
c, ok := CompressionByName[cc.String("compression")]
if !ok {
return fmt.Errorf("unknown compression %q", cc.String("compression"))
}
if err := verifyShipDependencies(shipPath, c); err != nil {
return fmt.Errorf("unable to ship files: %w", err)
}
if err := ensureAncillaryFiles(shipPath, allowedTables); err != nil {
return fmt.Errorf("unable to ensure ancillary files exist: %w", err)
}
p := firstExportPeriodAfter(minHeight, networkConfig.genesisTs)
for {
// Retry this export until it works
if err := WaitUntil(ctx, exportIsProcessed(p, allowedTables, c, shipPath), 0, time.Minute*15); err != nil {
return fmt.Errorf("fatal error processing export: %w", err)
}
exportLastCompletedHeightGauge.Set(float64(p.EndHeight))
p = p.Next()
if maxHeight > 0 && maxHeight < p.EndHeight {
logger.Infof("reached configured maximum height")
return nil
}
}
},
},
{
Name: "stat",
Usage: "Report the status of exports.",
Before: configure,
Flags: flagSet(
loggingFlags,
networkFlags,
storageFlags,
[]cli.Flag{
&cli.StringFlag{
Name: "ship-path",
EnvVars: []string{"ARCHIVER_SHIP_PATH"},
Usage: "Path used to write verified exports from lily.",
Required: true,
},
&cli.BoolFlag{
Name: "shipped",
EnvVars: []string{"ARCHIVER_SHIPPED"},
Usage: "Include files that have been shipped.",
Value: false,
},
&cli.StringFlag{
Name: "from-date",
EnvVars: []string{"ARCHIVER_FROM_DATE"},
Usage: "Include only files that are exported on or after this date.",
},
&cli.StringFlag{
Name: "to-date",
EnvVars: []string{"ARCHIVER_TO_DATE"},
Usage: "Include only files that are exported on or before this date.",
},
&cli.StringFlag{
Name: "compression",
EnvVars: []string{"ARCHIVER_COMPRESSION"},
Usage: "Type of compression to use.",
Value: "gz",
Hidden: true,
},
},
),
Action: func(cc *cli.Context) error {
ctx := cc.Context
var fromDate Date
if cc.IsSet("from-date") {
var err error
fromDate, err = DateFromString(cc.String("from-date"))
if err != nil {
return fmt.Errorf("invalid from date: %w", err)
}
}
var toDate Date
if cc.IsSet("to-date") {
var err error
toDate, err = DateFromString(cc.String("to-date"))
if err != nil {
return fmt.Errorf("invalid to date: %w", err)
}
}
c, ok := CompressionByName[cc.String("compression")]
if !ok {
return fmt.Errorf("unknown compression %q", cc.String("compression"))
}
shipPath := cc.String("ship-path")
includeShipped := cc.Bool("shipped")
current := CurrentHeight(networkConfig.genesisTs)
for p := firstExportPeriod(networkConfig.genesisTs); p.EndHeight+Finality < current; p = p.Next() {
if !fromDate.IsZero() && fromDate.After(p.Date) {
continue
}
if !toDate.IsZero() && p.Date.After(toDate) {
continue
}
em, err := manifestForPeriod(ctx, p, networkConfig.name, networkConfig.genesisTs, shipPath, storageConfig.schemaVersion, TableList, c)
if err != nil {
return fmt.Errorf("build manifest for period: %w", err)
}
for _, ef := range em.Files {
shipped := "x"
if ef.Shipped {
if !includeShipped {
continue
}
shipped = "S"
}
fmt.Printf("%s %s %d-%d %s\n", shipped, p.Date.String(), p.StartHeight, p.EndHeight, ef.TableName)
}
}
return nil
},
},
{
Name: "verify",
Usage: "Verify raw export files.",
Before: configure,
Flags: flagSet(
loggingFlags,
networkFlags,
storageFlags,
[]cli.Flag{
&cli.StringFlag{
Name: "tables",
EnvVars: []string{"ARCHIVER_TABLES"},
Usage: "Tables to verify, comma separated.",
Required: true,
},
&cli.StringFlag{
Name: "name",
EnvVars: []string{"ARCHIVER_EXPORT_NAME"},
Usage: "Name of the export to be verified.",
Required: true,
},
},
),
Action: func(cc *cli.Context) error {
tables, err := parseTableList(cc.String("tables"))
if err != nil {
return fmt.Errorf("invalid tables: %w", err)
}
tasks := make(map[string]struct{}, 0)
for _, table := range tables {
t, ok := TablesByName[table]
if !ok {
return fmt.Errorf("unknown table %q", table)
}
tasks[t.Task] = struct{}{}
}
tasklist := make([]string, 0, len(tasks))
for task := range tasks {
tasklist = append(tasklist, task)
}
wi := WalkInfo{
Name: cc.String("name"),
Path: storageConfig.path,
Format: "csv",
}
rep, err := verifyTasks(cc.Context, wi, tasklist)
if err != nil {
return fmt.Errorf("verify task: %w", err)
}
reportFailed := false
for _, table := range tables {
t, ok := TablesByName[table]
if !ok {
fmt.Printf("%s: unknown table\n", table)
reportFailed = true
continue
}
status, ok := rep.TaskStatus[t.Task]
if !ok {
fmt.Printf("%s: verification failed, no further information\n", table)
reportFailed = true
continue
}
if len(status.Missing) == 0 && len(status.Error) == 0 && len(status.Unexpected) == 0 {
fmt.Printf("%s: ok\n", table)
continue
}
rs := ranges(status.Missing)
for _, r := range rs {
fmt.Printf("%s: found gap from %d to %d\n", table, r.Lower, r.Upper)
reportFailed = true
}
if len(status.Error) > 0 {
fmt.Printf("%s: found %d errors\n", table, len(status.Error))
reportFailed = true
}
if len(status.Unexpected) > 0 {
fmt.Printf("%s: found %d unexpected processing reports\n", table, len(status.Unexpected))
reportFailed = true
}
}
if reportFailed {
return fmt.Errorf("one or more verification failures found")
}
return nil
},
},
{
Name: "export",
Usage: "Produce epoch-bound archives of Lily data.",
Before: configure,
Flags: flagSet(
loggingFlags,
networkFlags,
lilyFlags,
storageFlags,
diagnosticsFlags,
[]cli.Flag{
&cli.StringFlag{
Name: "ship-path",
EnvVars: []string{"ARCHIVER_SHIP_PATH"},
Usage: "Path used to write verified exports from lily.",
Required: true,
},
&cli.Int64Flag{
Name: "min-height",
EnvVars: []string{"ARCHIVER_MIN_HEIGHT"},
Usage: "Minimum height that should be exported. This may be used for nodes that do not have full state history.",
Value: 1005360, // TODO: remove default
},
&cli.Int64Flag{
Name: "max-height",
EnvVars: []string{"ARCHIVER_MAX_HEIGHT"},
Usage: "Maximum height that should be exported. If not specified the archiver will continue forever, waiting for the chain to advance.",
},
&cli.StringFlag{
Name: "tasks",
EnvVars: []string{"ARCHIVER_TASKS"},
Usage: "Comma separated list of tasks that are allowed to be processed. Default is all tasks.",
Value: "",
},
&cli.StringFlag{
Name: "compression",
EnvVars: []string{"ARCHIVER_COMPRESSION"},
Usage: "Type of compression to use.",
Value: "gz",
Hidden: true,
},
},
),
Action: func(cc *cli.Context) error {
ctx := metrics.CtxScope(cc.Context, appName)
setupMetrics(ctx)
tasks := cc.String("tasks")
shipPath := cc.String("ship-path")
minHeight := cc.Int64("min-height")
maxHeight := cc.Int64("max-height")
// Build list of allowed tables. Could be all tables.
var allowedTables []Table
if tasks == "" || tasks == "all" {
allowedTables = append(allowedTables, TableList...)
} else {
taskList, err := parseTaskList(cc.String("tasks"))
if err != nil {
return fmt.Errorf("invalid tasks specified: %v", err)
}
if len(taskList) == 0 {
return fmt.Errorf("invalid tasks specified")
}
for _, task := range taskList {
tables := TablesByTask(task, storageConfig.schemaVersion)
allowedTables = append(allowedTables, tables...)
}
}
c, ok := CompressionByName[cc.String("compression")]
if !ok {
return fmt.Errorf("unknown compression %q", cc.String("compression"))
}
if err := verifyShipDependencies(shipPath, c); err != nil {
return fmt.Errorf("unable to ship files: %w", err)
}
date := time.Unix(networkConfig.genesisTs+minHeight*epochInSeconds, 0).UTC()
midnight := midnightEpochForTs(date.AddDate(0, 0, 1).Unix(), networkConfig.genesisTs)
// the first day of our range
p := ExportPeriod{
Date: Date{
Year: date.Year(),
Month: int(date.Month()),
Day: date.Day(),
},
StartHeight: minHeight,
EndHeight: midnight - 1,
}
for {
// This forces a ranged export since the original p.Next()
// behavior is partitioned by day
if maxHeight < p.EndHeight {
p.EndHeight = maxHeight
}
em, err := manifestForPeriod(ctx, p, networkConfig.name, networkConfig.genesisTs, shipPath, storageConfig.schemaVersion, allowedTables, c)
if err != nil {
return fmt.Errorf("failed to create manifest, error: %s, date: %s", err, p.Date.String())
}
if err := processRangedExport(ctx, em, shipPath); err != nil {
processExportErrorsCounter.Inc()
logger.With("date", em.Period.Date.String(), "from", em.Period.StartHeight, "to", em.Period.EndHeight)
return fmt.Errorf("failed to process export: %s", err)
}
exportLastCompletedHeightGauge.Set(float64(p.EndHeight))
if maxHeight == p.EndHeight {
logger.Infof("reached configured maximum height")
return nil
}
p = p.Next()
}
},
},
},
}
func flagSet(fs ...[]cli.Flag) []cli.Flag {
var flags []cli.Flag
for _, f := range fs {
flags = append(flags, f...)
}
return flags
}
func parseTableList(str string) ([]string, error) {
tables := strings.Split(str, ",")
for _, table := range tables {
if _, ok := TablesByName[table]; !ok {
return nil, fmt.Errorf("unknown table: %q", table)
}
}
return tables, nil
}
func parseTaskList(str string) ([]string, error) {
tasks := strings.Split(str, ",")
for _, task := range tasks {
if _, ok := KnownTasks[task]; !ok {
return nil, fmt.Errorf("unknown task: %q", task)
}
}
return tasks, nil
}