/
backfill.go
134 lines (117 loc) · 4.09 KB
/
backfill.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
package main
import (
"context"
"fmt"
"github.com/fatih/color"
"github.com/spf13/cobra"
"go.uber.org/fx"
"go.uber.org/zap"
"golang.org/x/xerrors"
"github.com/coinbase/chainstorage/internal/aws"
"github.com/coinbase/chainstorage/internal/blockchain/client"
"github.com/coinbase/chainstorage/internal/blockchain/endpoints"
"github.com/coinbase/chainstorage/internal/blockchain/jsonrpc"
"github.com/coinbase/chainstorage/internal/blockchain/parser"
"github.com/coinbase/chainstorage/internal/blockchain/restapi"
"github.com/coinbase/chainstorage/internal/config"
"github.com/coinbase/chainstorage/internal/s3"
"github.com/coinbase/chainstorage/internal/storage"
"github.com/coinbase/chainstorage/internal/storage/blobstorage"
"github.com/coinbase/chainstorage/internal/storage/metastorage"
api "github.com/coinbase/chainstorage/protos/coinbase/chainstorage"
)
var (
backfillFlags struct {
tag uint32
startHeight uint64
endHeight uint64
updateWatermark bool
dataCompression string
}
backfillCmd = &cobra.Command{
Use: "backfill",
Short: "Backfill a block",
RunE: func(cmd *cobra.Command, args []string) error {
var deps struct {
fx.In
Config *config.Config
Client client.Client `name:"slave"`
BlobStorage blobstorage.BlobStorage
MetaStorage metastorage.MetaStorage
}
app := startApp(
client.Module,
jsonrpc.Module,
restapi.Module,
endpoints.Module,
aws.Module,
s3.Module,
storage.Module,
parser.Module,
fx.Populate(&deps),
)
defer app.Close()
ctx := context.Background()
tag := deps.Config.GetEffectiveBlockTag(backfillFlags.tag)
updateWatermark := backfillFlags.updateWatermark
compression := deps.Config.AWS.Storage.DataCompression
startHeight := backfillFlags.startHeight
endHeight := backfillFlags.endHeight
if endHeight == 0 {
endHeight = startHeight + 1
}
if startHeight >= endHeight {
return xerrors.Errorf("invalid block range: [%v, %v)", startHeight, endHeight)
}
chainInfo := fmt.Sprintf("%v-%v", commonFlags.blockchain, commonFlags.network)
if commonFlags.sidechain != "" {
chainInfo = fmt.Sprintf("%v-%v", chainInfo, commonFlags.sidechain)
}
prompt := color.CyanString(fmt.Sprintf(
"Are you sure you want backfill [%v, %v) in %v::%v? (y/N) ",
startHeight, endHeight, env, chainInfo,
))
if !confirm(prompt) {
return nil
}
blockMetadatas := make([]*api.BlockMetadata, 0, endHeight-startHeight)
for height := startHeight; height < endHeight; height++ {
block, err := deps.Client.GetBlockByHeight(ctx, tag, height)
if err != nil {
return xerrors.Errorf("failed to get block: %w", err)
}
objectKey, err := deps.BlobStorage.Upload(ctx, block, compression)
if err != nil {
return xerrors.Errorf("failed to upload block in blob storage: %w", err)
}
block.Metadata.ObjectKeyMain = objectKey
blockMetadatas = append(blockMetadatas, block.Metadata)
logger.Info(
"block is ingested successfully",
zap.Uint32("tag", tag),
zap.Uint64("height", height),
zap.String("compression", compression.String()),
zap.Reflect("metadata", block.Metadata),
)
}
if err := deps.MetaStorage.PersistBlockMetas(ctx, updateWatermark, blockMetadatas, nil); err != nil {
return xerrors.Errorf("failed to persist block in meta storage: %w", err)
}
logger.Info(
"blocks are persisted successfully",
zap.Uint32("tag", tag),
zap.Uint64("startHeight", startHeight),
zap.Uint64("endHeight", endHeight),
zap.Bool("updateWatermark", updateWatermark),
)
return nil
},
}
)
func init() {
backfillCmd.Flags().Uint32Var(&backfillFlags.tag, "tag", 0, "tag")
backfillCmd.Flags().Uint64Var(&backfillFlags.startHeight, "start-height", 0, "start height (inclusive)")
backfillCmd.Flags().Uint64Var(&backfillFlags.endHeight, "end-height", 0, "end height (exclusive). if omitted, it defaults to start height + 1.")
backfillCmd.Flags().BoolVar(&backfillFlags.updateWatermark, "update-watermark", false, "whether to update latest watermark")
rootCmd.AddCommand(backfillCmd)
}