-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtier1.go
336 lines (285 loc) · 10 KB
/
tier1.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
package app
import (
"context"
"fmt"
"net/url"
"time"
"connectrpc.com/connect"
"github.com/streamingfast/bstream"
"github.com/streamingfast/bstream/blockstream"
"github.com/streamingfast/bstream/hub"
pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1"
dauth "github.com/streamingfast/dauth"
"github.com/streamingfast/dmetrics"
"github.com/streamingfast/dstore"
pbfirehose "github.com/streamingfast/pbgo/sf/firehose/v2"
"github.com/streamingfast/shutter"
"github.com/streamingfast/substreams/client"
"github.com/streamingfast/substreams/metrics"
"github.com/streamingfast/substreams/orchestrator/work"
"github.com/streamingfast/substreams/pb/sf/substreams/rpc/v2/pbsubstreamsrpcconnect"
ssconnect "github.com/streamingfast/substreams/pb/sf/substreams/rpc/v2/pbsubstreamsrpcconnect"
"github.com/streamingfast/substreams/reqctx"
"github.com/streamingfast/substreams/service"
"github.com/streamingfast/substreams/wasm"
"github.com/streamingfast/substreams/wasm/wazero"
_ "github.com/streamingfast/substreams/wasm/wasmtime"
"go.uber.org/atomic"
"go.uber.org/zap"
)
type Tier1Modules struct {
// Required dependencies
Authenticator dauth.Authenticator
HeadTimeDriftMetric *dmetrics.HeadTimeDrift
HeadBlockNumberMetric *dmetrics.HeadBlockNum
CheckPendingShutDown func() bool
InfoServer InfoServer
WorkerPoolFactory work.WorkerPoolFactory
GlobalRequestPool *service.GlobalRequestPool
}
type InfoServer interface {
Init(ctx context.Context, fhub *hub.ForkableHub, mergedBlocksStore dstore.Store, oneBlockStore dstore.Store, logger *zap.Logger) error
Info(ctx context.Context, request *pbfirehose.InfoRequest) (*pbfirehose.InfoResponse, error)
}
// returns config with default sane values
func NewDefaultTier1Config() *Tier1Config {
return &Tier1Config{
SharedCacheSize: 15,
MaxSubrequests: 10,
StateBundleSize: 1000,
BlockExecutionTimeout: 1 * time.Minute,
}
}
type Tier1Config struct {
MeteringConfig string
MergedBlocksStoreURL string
OneBlocksStoreURL string
ForkedBlocksStoreURL string
BlockStreamAddr string // gRPC endpoint to get real-time blocks, can be "" in which live streams is disabled
GRPCListenAddr string // gRPC address where this app will listen to
GRPCShutdownGracePeriod time.Duration // The duration we allow for gRPC connections to terminate gracefully prior forcing shutdown
ServiceDiscoveryURL *url.URL
BlockExecutionTimeout time.Duration
TmpDir string
StateStoreURL string
QuickSaveStoreURL string
StateStoreDefaultTag string
BlockType string
StateBundleSize uint64
EnforceCompression bool // refuse incoming requests that do not accept gzip compression (ConnectRPC or GRPC)
ActiveRequestsSoftLimit int // maximum number of active requests a tier1 app can have with external clients before starting to advertise itself as unready in the health check
ActiveRequestsHardLimit int // maximum number of active requests a tier1 app can have with external clients, refuse with CodeUnavailable if reached
MaxSubrequests uint64
SubrequestsEndpoint string
SubrequestsInsecure bool
SubrequestsPlaintext bool
SharedCacheSize uint64
WASMExtensions wasm.WASMExtensioner
Tracing bool
}
type Tier1App struct {
*shutter.Shutter
config *Tier1Config
modules *Tier1Modules
logger *zap.Logger
isReady *atomic.Bool
}
func NewTier1(logger *zap.Logger, config *Tier1Config, modules *Tier1Modules) *Tier1App {
if modules.CheckPendingShutDown == nil {
modules.CheckPendingShutDown = func() bool { return false }
}
return &Tier1App{
Shutter: shutter.New(),
config: config,
modules: modules,
logger: logger,
isReady: atomic.NewBool(false),
}
}
func (a *Tier1App) Run() error {
dmetrics.Register(metrics.MetricSet)
a.logger.Info("running substreams-tier1", zap.Reflect("config", a.config))
if err := a.config.Validate(); err != nil {
return fmt.Errorf("invalid app config: %w", err)
}
mergedBlocksStore, err := dstore.NewDBinStore(a.config.MergedBlocksStoreURL)
if err != nil {
return fmt.Errorf("failed setting up block store from url %q: %w", a.config.MergedBlocksStoreURL, err)
}
oneBlocksStore, err := dstore.NewDBinStore(a.config.OneBlocksStoreURL)
if err != nil {
return fmt.Errorf("failed setting up one-block store from url %q: %w", a.config.OneBlocksStoreURL, err)
}
stateStore, err := dstore.NewStore(a.config.StateStoreURL, "zst", "zstd", true)
if err != nil {
return fmt.Errorf("failed setting up state store from url %q: %w", a.config.StateStoreURL, err)
}
var quickSaveStore dstore.Store
if a.config.QuickSaveStoreURL != "" {
quickSaveStore, err = dstore.NewStore(a.config.QuickSaveStoreURL, "zst", "zstd", true)
if err != nil {
return fmt.Errorf("failed setting up quickSave store from url %q: %w", a.config.QuickSaveStoreURL, err)
}
}
// set to empty store interface if URL is ""
var forkedBlocksStore dstore.Store
if a.config.ForkedBlocksStoreURL != "" {
forkedBlocksStore, err = dstore.NewDBinStore(a.config.ForkedBlocksStoreURL)
if err != nil {
return fmt.Errorf("failed setting up block store from url %q: %w", a.config.ForkedBlocksStoreURL, err)
}
}
withLive := a.config.BlockStreamAddr != ""
var forkableHub *hub.ForkableHub
if withLive {
liveSourceFactory := bstream.SourceFactory(func(h bstream.Handler) bstream.Source {
return blockstream.NewSource(
context.Background(),
a.config.BlockStreamAddr,
2,
bstream.HandlerFunc(func(blk *pbbstream.Block, obj interface{}) error {
a.modules.HeadBlockNumberMetric.SetUint64(blk.Number)
a.modules.HeadTimeDriftMetric.SetBlockTime(blk.Time())
return h.ProcessBlock(blk, obj)
}),
blockstream.WithRequester("substreams-tier1"),
)
})
forkableHub = hub.NewForkableHub(liveSourceFactory, 200, oneBlocksStore)
forkableHub.OnTerminated(a.Shutdown)
go forkableHub.Run()
}
subRequestsClientConfig := client.NewSubstreamsClientConfig(
a.config.SubrequestsEndpoint,
"",
client.None,
a.config.SubrequestsInsecure,
a.config.SubrequestsPlaintext,
"substreams_tier1",
)
var opts []service.Option
if a.config.WASMExtensions != nil {
opts = append(opts, service.WithWASMExtensioner(a.config.WASMExtensions))
}
if a.config.Tracing {
opts = append(opts, service.WithModuleExecutionTracing())
}
if a.config.BlockExecutionTimeout != 0 {
opts = append(opts, service.WithBlockExecutionTimeout(a.config.BlockExecutionTimeout))
}
if a.config.TmpDir != "" {
wazero.SetTempDir(a.config.TmpDir)
}
var wasmModules map[string]string
if a.config.WASMExtensions != nil {
wasmModules = a.config.WASMExtensions.Params()
}
tier2RequestParameters := reqctx.Tier2RequestParameters{
MeteringConfig: a.config.MeteringConfig,
FirstStreamableBlock: bstream.GetProtocolFirstStreamableBlock,
MergedBlockStoreURL: a.config.MergedBlocksStoreURL,
StateStoreURL: a.config.StateStoreURL,
StateBundleSize: a.config.StateBundleSize,
StateStoreDefaultTag: a.config.StateStoreDefaultTag,
WASMModules: wasmModules,
}
svc, err := service.NewTier1(
a.logger,
mergedBlocksStore,
forkedBlocksStore,
forkableHub,
stateStore,
quickSaveStore,
a.config.StateStoreDefaultTag,
a.config.MaxSubrequests,
a.config.StateBundleSize,
a.config.BlockType,
a.setIsReady,
subRequestsClientConfig,
tier2RequestParameters,
a.modules.WorkerPoolFactory,
a.config.EnforceCompression,
a.config.ActiveRequestsSoftLimit,
a.config.ActiveRequestsHardLimit,
a.config.SharedCacheSize,
a.modules.GlobalRequestPool,
a.modules.CheckPendingShutDown,
opts...,
)
if err != nil {
return err
}
a.OnTerminating(func(err error) {
metrics.AppReadinessTier1.SetNotReady()
svc.Shutdown(err)
time.Sleep(2 * time.Second) // enough time to send termination grpc responses
})
go func() {
var infoServer ssconnect.EndpointInfoHandler
if a.modules.InfoServer != nil {
a.logger.Info("waiting until info server is ready")
infoServer = &InfoServerWrapper{a.modules.InfoServer}
if err := a.modules.InfoServer.Init(context.Background(), forkableHub, mergedBlocksStore, oneBlocksStore, a.logger); err != nil {
a.Shutdown(fmt.Errorf("cannot initialize info server: %w", err))
return
}
}
if withLive {
a.logger.Info("waiting until hub is real-time synced")
select {
case <-forkableHub.Ready:
// Wait until the hub is ready
case <-a.Terminating():
return
}
}
a.logger.Info("launching gRPC server", zap.Bool("live_support", withLive))
a.setIsReady(true)
err := service.ListenTier1(a.config.GRPCListenAddr, svc, infoServer, a.modules.Authenticator, a.logger, a.HealthCheck)
a.Shutdown(err)
}()
return nil
}
func (a *Tier1App) HealthCheck(ctx context.Context) (bool, interface{}, error) {
return a.IsReady(ctx), nil, nil
}
// IsReady return `true` if the apps is ready to accept requests, `false` is returned
// otherwise.
func (a *Tier1App) IsReady(ctx context.Context) bool {
if a.IsTerminating() {
return false
}
if !a.modules.Authenticator.Ready(ctx) {
return false
}
if a.modules.CheckPendingShutDown != nil && a.modules.CheckPendingShutDown() {
return false
}
return a.isReady.Load()
}
func (a *Tier1App) setIsReady(ready bool) {
if ready {
a.isReady.Store(true)
metrics.AppReadinessTier1.SetReady()
} else {
a.isReady.Store(false)
metrics.AppReadinessTier1.SetNotReady()
}
}
// Validate inspects itself to determine if the current config is valid according to
// substreams rules.
func (config *Tier1Config) Validate() error {
return nil
}
var _ pbsubstreamsrpcconnect.EndpointInfoHandler = (*InfoServerWrapper)(nil)
type InfoServerWrapper struct {
rpcInfoServer InfoServer
}
// Info implements pbsubstreamsrpcconnect.EndpointInfoHandler.
func (i *InfoServerWrapper) Info(ctx context.Context, req *connect.Request[pbfirehose.InfoRequest]) (*connect.Response[pbfirehose.InfoResponse], error) {
resp, err := i.rpcInfoServer.Info(ctx, req.Msg)
if err != nil {
return nil, err
}
return connect.NewResponse(resp), nil
}