-
Notifications
You must be signed in to change notification settings - Fork 16
/
indexer.go
350 lines (305 loc) · 8.73 KB
/
indexer.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
package indexer
import (
"context"
"errors"
"fmt"
"sync/atomic"
"time"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/service"
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/core"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/oasisprotocol/emerald-web3-gateway/conf"
"github.com/oasisprotocol/emerald-web3-gateway/storage"
)
const (
indexerLoopDelay = 1 * time.Second
pruningCheckInterval = 60 * time.Second
healthCheckInterval = 10 * time.Second
healthCheckIndexerDriftThreshold = 10
blockIndexTimeout = 60 * time.Second
pruneIterationTimeout = 60 * time.Second
healthIterationTimeout = 60 * time.Second
)
var (
metricBlockIndexed = promauto.NewGauge(prometheus.GaugeOpts{Name: "oasis_emerald_web3_gateway_block_indexed", Help: "Indexed block heights."})
metricBlockPruned = promauto.NewGauge(prometheus.GaugeOpts{Name: "oasis_emerald_web3_gateway_block_pruned", Help: "Pruned block heights."})
metricHealthy = promauto.NewGauge(prometheus.GaugeOpts{Name: "oasis_emerald_web3_gateway_health", Help: "1 if gateway healthcheck is reporting as healthy, 0 otherwise."})
)
// ErrNotHealthy is the error returned if the gateway is unhealthy.
var ErrNotHealthy = errors.New("not healthy")
// Service is an indexer service.
type Service struct {
service.BaseBackgroundService
runtimeID common.Namespace
enablePruning bool
pruningStep uint64
indexingStart uint64
backend Backend
client client.RuntimeClient
core core.V1
queryBlockGasLimit bool
blockGasLimit uint64
ctx context.Context
cancelCtx context.CancelFunc
health uint32
}
// Implements server.HealthCheck.
func (s *Service) Health() error {
if atomic.LoadUint32(&s.health) == 0 {
return ErrNotHealthy
}
return nil
}
// indexBlock indexes given block number.
func (s *Service) indexBlock(ctx context.Context, round uint64) error {
blk, err := s.client.GetBlock(ctx, round)
if err != nil {
return fmt.Errorf("querying block: %w", err)
}
if s.blockGasLimit == 0 || s.queryBlockGasLimit {
// Query parameters for block gas limit.
var params *core.Parameters
params, err = s.core.Parameters(ctx, round)
if err != nil {
return fmt.Errorf("querying block parameters: %w", err)
}
s.blockGasLimit = params.MaxBatchGas
}
txs, err := s.client.GetTransactionsWithResults(ctx, blk.Header.Round)
if err != nil {
return fmt.Errorf("querying transactions with results: %w", err)
}
err = s.backend.Index(ctx, blk, txs, s.blockGasLimit)
if err != nil {
return fmt.Errorf("indexing block: %w", err)
}
metricBlockIndexed.Set(float64(blk.Header.Round))
switch {
case blk.Header.HeaderType == block.EpochTransition:
// Epoch transition block, ensure block gas is queried on next normal round.
s.queryBlockGasLimit = true
case s.queryBlockGasLimit && blk.Header.HeaderType == block.Normal:
// Block gas was queried, no need to query it until next epoch.
s.queryBlockGasLimit = false
default:
}
return nil
}
// getRoundLatest returns the latest round.
func (s *Service) getRoundLatest() (uint64, error) {
blk, err := s.client.GetBlock(s.ctx, client.RoundLatest)
if err != nil {
return 0, err
}
return blk.Header.Round, nil
}
func (s *Service) updateHealth(healthy bool) {
if healthy {
atomic.StoreUint32(&s.health, 1)
metricHealthy.Set(float64(1))
} else {
atomic.StoreUint32(&s.health, 0)
metricHealthy.Set(float64(0))
}
}
// healthWorker is the health check worker.
func (s *Service) healthWorker() {
s.Logger.Debug("starting health check worker")
for {
select {
case <-s.ctx.Done():
return
case <-time.After(healthCheckInterval):
func() {
ctx, cancel := context.WithTimeout(s.ctx, healthIterationTimeout)
defer cancel()
// Query last indexed round.
lastIndexed, err := s.backend.QueryLastIndexedRound(ctx)
if err != nil {
s.Logger.Error("failed to query last indexed round",
"err", err,
)
s.updateHealth(false)
return
}
// Query latest round on the node.
latestBlk, err := s.client.GetBlock(ctx, client.RoundLatest)
if err != nil {
s.Logger.Error("failed to query latest block",
"err", err,
)
s.updateHealth(false)
return
}
latestRound := latestBlk.Header.Round
s.Logger.Debug("checking health", "latest_round", latestRound, "latest_indexed", lastIndexed)
switch {
case latestRound >= lastIndexed:
s.updateHealth(latestRound-lastIndexed <= healthCheckIndexerDriftThreshold)
default:
// Indexer in front of node - not healthy.
s.updateHealth(false)
}
}()
}
}
}
// pruningWorker handles data pruning.
func (s *Service) pruningWorker() {
s.Logger.Debug("starting periodic pruning worker")
for {
select {
case <-s.ctx.Done():
return
case <-time.After(pruningCheckInterval):
func() {
ctx, cancel := context.WithTimeout(s.ctx, pruneIterationTimeout)
defer cancel()
lastIndexed, err := s.backend.QueryLastIndexedRound(ctx)
if err != nil {
s.Logger.Error("failed to query last indexed round",
"err", err,
)
return
}
if lastIndexed > s.pruningStep {
round := lastIndexed - s.pruningStep
if err := s.backend.Prune(ctx, round); err != nil {
s.Logger.Error("failed to prune round",
"err", err,
"round", round,
)
return
}
metricBlockPruned.Set(float64(round))
}
}()
}
}
}
// indexingWorker is a worker for indexing.
func (s *Service) indexingWorker() {
for {
select {
case <-s.ctx.Done():
return
case <-time.After(indexerLoopDelay):
}
func() {
ctx, cancel := context.WithTimeout(s.ctx, blockIndexTimeout)
defer cancel()
// Query latest round available at the node.
latest, err := s.getRoundLatest()
if err != nil {
s.Logger.Info("failed to query latest round",
"err", err,
)
return
}
var startAt uint64
// Get last indexed round.
lastIndexed, err := s.backend.QueryLastIndexedRound(ctx)
switch {
case errors.Is(err, storage.ErrNoRoundsIndexed):
// No rounds indexed, start at 0.
startAt = 0
case err != nil:
s.Logger.Error("failed to query last indexed round",
"err", err,
)
return
default:
if latest < lastIndexed {
panic("This is a new chain, please clear the db first!")
}
// Latest round already indexed.
if latest == lastIndexed {
return
}
startAt = lastIndexed + 1
}
// If a special indexing_start is set, skip ahead to that.
if s.indexingStart > startAt {
startAt = s.indexingStart
}
// Get last retained round on the node.
lastRetainedBlock, err := s.client.GetLastRetainedBlock(ctx)
if err != nil {
s.Logger.Error("failed to retrieve last retained round",
"err", err,
)
return
}
// Adjust startAt round in case node pruned missing rounds.
if lastRetainedBlock.Header.Round > startAt {
startAt = lastRetainedBlock.Header.Round
}
// Following code uses a new context with new timeout.
cancel()
for round := startAt; round <= latest; round++ {
select {
case <-s.ctx.Done():
return
default:
}
indexCtx, cancel := context.WithTimeout(s.ctx, blockIndexTimeout)
// Try to index block.
if err = s.indexBlock(indexCtx, round); err != nil {
s.Logger.Warn("failed to index block",
"err", err,
"round", round,
)
cancel()
break
}
cancel()
}
}()
}
}
// Start starts service.
func (s *Service) Start() {
go s.indexingWorker()
go s.healthWorker()
if s.enablePruning {
go s.pruningWorker()
}
}
// Stop stops service.
func (s *Service) Stop() {
s.cancelCtx()
}
// New creates a new indexer service.
func New(
ctxBackend context.Context,
backend Backend,
client client.RuntimeClient,
runtimeID common.Namespace,
storage storage.Storage,
cfg *conf.Config,
) (*Service, Backend, error) {
ctx, cancelCtx := context.WithCancel(ctxBackend)
cachingBackend, err := newCachingBackend(ctx, backend, cfg.Cache)
if err != nil {
cancelCtx()
return nil, nil, err
}
s := &Service{
BaseBackgroundService: *service.NewBaseBackgroundService("gateway/indexer"),
runtimeID: runtimeID,
backend: cachingBackend,
client: client,
core: core.NewV1(client),
ctx: ctx,
cancelCtx: cancelCtx,
enablePruning: cfg.EnablePruning,
pruningStep: cfg.PruningStep,
indexingStart: cfg.IndexingStart,
}
s.Logger = s.Logger.With("runtime_id", s.runtimeID.String())
return s, backend, nil
}