/
parallel_step.go
263 lines (228 loc) · 9.23 KB
/
parallel_step.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
package cli
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
manifestlib "github.com/streamingfast/sparkle/manifest"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/streamingfast/derr"
"github.com/streamingfast/dstore"
"github.com/streamingfast/eth-go/rpc"
"github.com/streamingfast/sparkle/blocks"
"github.com/streamingfast/sparkle/entity"
"github.com/streamingfast/sparkle/indexer"
"github.com/streamingfast/sparkle/metrics"
"github.com/streamingfast/sparkle/storage"
"github.com/streamingfast/sparkle/storage/squashable"
"github.com/streamingfast/sparkle/subgraph"
"go.uber.org/zap"
)
var parallelStepCmd = &cobra.Command{
Use: "step",
Short: "Run a step",
Args: cobra.NoArgs,
RunE: runParallelStep,
}
func init() {
parallelStepCmd.Flags().StringP("rpc-endpoint", "e", "http://localhost:8545", "ETH JSON-RPC Endpoint")
parallelStepCmd.Flags().String("firehose-api-key", "", "your API key (can also be passed by env var STREAMINGFAST_API_KEY)")
//parallelStepCmd.Flags().String("blocks-store-url", "gs://dfuseio-global-blocks-us/eth-bsc-mainnet/v1", "dfuse Blocks Store URL")
parallelStepCmd.Flags().String("firehose-endpoint", "https://api.streamingfast.io", "dfuse endpoint")
parallelStepCmd.Flags().IntP("step", "s", 1, "First step in parallel loader")
parallelStepCmd.Flags().BoolP("flush-entities", "b", false, "Flush entities to 'output-path'")
parallelStepCmd.Flags().Bool("store-snapshot", true, "Enables snapshot storage in 'output_path' at the end of the batch")
parallelStepCmd.Flags().Bool("debug-cache", false, "Enables a cache dump after the preload, and before the batch is run in 'tmp/content.json'")
parallelStepCmd.Flags().Bool("enable-poi", false, "Enable POI injection")
parallelStepCmd.Flags().Bool("enable-aggregate-snapshot-save", false, "Enable the save of the aggregate snapshot")
parallelStepCmd.Flags().Bool("non-archive-node", false, "Remove the requirement for an archive node. RPC Calls will be called on LATEST (breaks consistency and POI)")
parallelStepCmd.Flags().String("rpc-cache-load-path", "", "If non-empty, will load and use rpc request/response cache from this path")
parallelStepCmd.Flags().String("rpc-cache-save-path", "", "If non-empty, will save rpc request/response cache to this path (can be the same as load-path to append data)")
parallelCmd.AddCommand(parallelStepCmd)
}
func runParallelStep(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
firehoseEndpoint := viper.GetString("parallel-cmd-firehose-endpoint")
apiKey := viper.GetString("parallel-cmd-firehose-api-key")
startBlock := viper.GetUint64("parallel-cmd-start-block")
stopBlock := viper.GetUint64("parallel-cmd-stop-block")
rpcEndpoint := viper.GetString("parallel-step-cmd-rpc-endpoint")
step := viper.GetInt("parallel-step-cmd-step")
blocksStoreURL := viper.GetString("parallel-step-cmd-blocks-store-url")
flushEntities := viper.GetBool("parallel-step-cmd-flush-entities")
outputPath := viper.GetString("parallel-cmd-output-path")
inputPath := viper.GetString("parallel-cmd-input-path")
rpcCacheLoadPath := viper.GetString("parallel-step-cmd-rpc-cache-load-path")
rpcCacheSavePath := viper.GetString("parallel-step-cmd-rpc-cache-save-path")
entitiesPath := viper.GetString("parallel-step-cmd-entities-path")
storeSnapshot := viper.GetBool("parallel-step-cmd-store-snapshot")
debugCache := viper.GetBool("parallel-step-cmd-debug-cache")
enablePOI := viper.GetBool("parallel-step-cmd-enable-poi")
enableAggregateSnapshotSave := viper.GetBool("parallel-step-cmd-enable-aggregate-snapshot-save")
nonArchiveNode := viper.GetBool("parallel-step-cmd-non-archive-node")
zlog.Info("fetching transactions for network",
zap.String("rpc_endpoint", rpcEndpoint),
zap.Uint64("start_block", startBlock),
zap.Uint64("stop_block", stopBlock),
zap.String("output_path", outputPath),
zap.String("input_path", inputPath),
zap.String("rpc_cache_load_path", rpcCacheLoadPath),
zap.String("rpc_cache_save_path", rpcCacheSavePath),
zap.String("entities_path", entitiesPath),
zap.String("blocks_store_url", blocksStoreURL),
zap.Int("step", step),
zap.Bool("flush_entities", flushEntities),
zap.Bool("store_snapshots", storeSnapshot),
zap.Bool("debug_cache", debugCache),
zap.Bool("enable_poi", enablePOI),
zap.Bool("enable_aggregate_snapshot_save", enableAggregateSnapshotSave),
zap.Bool("non_archive_node", nonArchiveNode),
)
zlog.Info("creating rpc client")
rpcClient := rpc.NewClient(rpcEndpoint, rpc.WithHttpClient(&http.Client{
Timeout: 10 * time.Second,
}))
var inputStore dstore.Store
var err error
if inputPath != "" {
inputStore, err = dstore.NewStore(inputPath, "", "", true)
if err != nil {
return fmt.Errorf("unable to create input store: %w", err)
}
}
outputStore, err := dstore.NewStore(outputPath, "", "", true)
if err != nil {
return fmt.Errorf("unable to create output store: %w", err)
}
// blocksStore, err := dstore.NewDBinStore(blocksStoreURL)
// if err != nil {
// return fmt.Errorf("unable to create blocks store: %w", err)
// }
subgraphDef := subgraph.MainSubgraphDef
// TODO: rename to `mergeableStore`. for consistency.
squashableStore := squashable.New(ctx, zlog, subgraphDef, step, startBlock, stopBlock)
if flushEntities {
zlog.Info("defaulting entities store to output store, since 'entities_path' is not defined", zap.String("entities_store_path", outputPath))
squashableStore.FlushEntities(outputStore)
}
if inputStore != nil {
startBlockNum, stopBlockNum, err := squashableStore.Preload(ctx, inputStore)
if err != nil {
return fmt.Errorf("unable to load snapshots: %w", err)
}
if enableAggregateSnapshotSave && startBlockNum != 0 && stopBlockNum != 0 {
aggregateFilename := fmt.Sprintf("%010d-%010d.jsonl", startBlockNum, stopBlockNum)
zlog.Info("flushing aggregate snapshot to input path",
zap.String("input_path", inputPath),
zap.Int("step", step),
zap.String("snapshot_filename", aggregateFilename),
)
_, err = squashableStore.WriteSnapshot(inputStore, aggregateFilename)
if err != nil {
return fmt.Errorf("unable store aggregate snapshot: %w", err)
}
}
}
if debugCache {
zlog.Info("dumping Dumping squashed cache content into /tmp/content.json")
cnt, _ := json.MarshalIndent(squashableStore.GetCache(), "", " ")
ioutil.WriteFile("/tmp/content.json", cnt, 0644)
}
if apiKey == "" {
apiKey = os.Getenv("STREAMINGFAST_API_KEY")
if apiKey == "" {
return fmt.Errorf("pass an API key as `STREAMINGFAST_API_KEY` env var, or with --api-key")
}
}
firehoseFactory, err := blocks.NewStreamingFastFirehoseFactory(apiKey, firehoseEndpoint)
if err != nil {
return err
}
sf := func(_ context.Context, _ *zap.Logger, _ *metrics.BlockMetrics, _ *entity.Registry) (storage.Store, error) {
return squashableStore, nil
}
// TODO: this can be stored in the generated subgraph
manifest, err := manifestlib.DecodeYamlManifest(subgraphDef.Manifest)
if err != nil {
return fmt.Errorf("unable to decode manifest: %w", err)
}
var indexerOpts []indexer.Option
if enablePOI {
indexerOpts = append(indexerOpts, indexer.WithPOI(manifest.Network()))
}
if nonArchiveNode {
indexerOpts = append(indexerOpts, indexer.WithNonArchiveNode())
}
var rpcCache *indexer.RPCCache
if rpcCacheLoadPath != "" || rpcCacheSavePath != "" {
var loadStore, saveStore dstore.Store
if rpcCacheSavePath != "" {
saveStore, err = dstore.NewStore(rpcCacheSavePath, "json.zst", "zstd", true)
if err != nil {
return fmt.Errorf("cannot open RPC Cache save store:%w", err)
}
}
if rpcCacheLoadPath != "" {
loadStore, err = dstore.NewStore(rpcCacheLoadPath, "json.zst", "zstd", false)
if err != nil {
return fmt.Errorf("cannot open RPC Cache load store:%w", err)
}
}
rpcCache = indexer.NewCache(loadStore, saveStore, startBlock, stopBlock)
rpcCache.Load(ctx)
indexerOpts = append(indexerOpts, indexer.WithRPCCache(rpcCache))
}
indexer := indexer.NewBatch(
step,
startBlock,
stopBlock,
rpcClient,
firehoseFactory,
subgraphDef,
indexerOpts...,
)
err = indexer.Start(sf)
if err != nil {
return fmt.Errorf("start indexer returned error: %w", err)
}
signalHandler := derr.SetupSignalHandler(viper.GetDuration("common-system-shutdown-signal-delay"))
select {
case <-signalHandler:
zlog.Info("Received termination signal, quitting")
indexer.Shutdown(nil)
case <-indexer.Terminating():
}
zlog.Info("wait for complete shutdown")
<-indexer.Terminated()
zlog.Info("terminated")
if indexer.Err() != nil {
return indexer.Err()
}
if flushEntities {
zlog.Info("closing entities flushing pipeline")
if err := squashableStore.CloseEntities(); err != nil {
return fmt.Errorf("unable to close squasahble store entities writer: %w", err)
}
}
if storeSnapshot {
snapshotFilename := fmt.Sprintf("%010d-%010d.jsonl", squashableStore.StartBlock, squashableStore.EndBlock)
zlog.Info("flushing snapshot to output store",
zap.Int("step", step),
zap.String("output_path", outputPath),
zap.String("snapshot_filename", snapshotFilename),
)
_, err := squashableStore.WriteSnapshot(outputStore, snapshotFilename)
if err != nil {
return err
}
}
if rpcCache != nil {
zlog.Info("saving rpc cache", zap.String("rpc_cache_save_path", rpcCacheSavePath))
rpcCache.Save(ctx)
}
return nil
}