/
main.go
410 lines (330 loc) · 9.5 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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"runtime"
"strings"
"sync"
"syscall"
"time"
"github.com/cristalhq/aconfig"
"github.com/go-pkgz/lgr"
"github.com/gomodule/redigo/redis"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/sgraph-protocol/sgraph/indexer/cli"
"github.com/sgraph-protocol/sgraph/indexer/repo"
"github.com/sgraph-protocol/sgraph/indexer/srv"
graph "github.com/sgraph-protocol/sgraph/sdk/go"
"net/http"
_ "net/http/pprof"
)
// todo graceful exit
// todo metrics
//go:generate go run github.com/mailru/easyjson/... -all ./cli/cli.go
//go:generate go run github.com/matryer/moq@v0.2.7 -pkg mocks -fmt goimports -rm -skip-ensure -out ./mocks/rpc.go . RPC:RpcMock
//go:generate go run github.com/matryer/moq@v0.2.7 -pkg mocks -fmt goimports -rm -skip-ensure -out ./mocks/redis.go . Redis:RedisMock
type config struct {
LogLevel string
RpcEndpoint string
BlockProcessorConcurrency int
RedisHost string
RedisPort int
MongoHost string
}
func main() {
if err := run(); err != nil {
panic(err)
}
}
type redisCfg struct {
redisAddr string
}
func newRedisPool(cfg redisCfg, l lgr.L) (*redis.Pool, func(), error) {
handleErr := func(err error) (*redis.Pool, func(), error) {
return nil, nil, fmt.Errorf("new redis pool: %w", err)
}
const (
maxIdle = 10
idleTimeout = 5 * time.Minute
)
dialFunc := func(ctx context.Context) (redis.Conn, error) {
return redis.DialContext(ctx, "tcp", cfg.redisAddr)
}
p := redis.Pool{
MaxIdle: maxIdle,
IdleTimeout: idleTimeout,
DialContext: func(ctx context.Context) (redis.Conn, error) {
c, err := dialFunc(ctx)
if err != nil {
return nil, fmt.Errorf("connect to redis: %v", err)
}
if _, err = c.Do("PING"); err != nil {
return nil, fmt.Errorf("redis ping: %w", err)
}
return c, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if time.Since(t) < time.Minute {
return nil
}
if _, err := c.Do("PING"); err != nil {
return fmt.Errorf("redis ping: %w", err)
}
return nil
},
}
conn, err := p.DialContext(context.Background())
if err != nil {
return handleErr(fmt.Errorf("make test redis connection on initialization: %w", err))
}
conn.Close()
cleanup := func() {
if err := p.Close(); err != nil {
l.Logf("%v", err)
}
}
return &p, cleanup, nil
}
func prepare(ctx context.Context, l lgr.L, cfg config) (*redis.Pool, repo.Redis, func(), error) {
handleErr := func(err error) (*redis.Pool, repo.Redis, func(), error) {
return nil, repo.Redis{}, nil, err
}
fmt.Println(cfg)
addr := fmt.Sprintf("%s:%d", cfg.RedisHost, cfg.RedisPort)
fmt.Println(addr)
pool, cleanup2, err := newRedisPool(redisCfg{redisAddr: fmt.Sprintf("%s:%d", cfg.RedisHost, cfg.RedisPort)}, lgr.New())
if err != nil {
return handleErr(fmt.Errorf("error connecting to redis: %w", err))
}
redis := repo.NewRedis(l, pool)
if err := redis.InitializeRedis(ctx); err != nil {
return handleErr(fmt.Errorf("error initializing redis: %v", err))
}
return pool, redis, func() {
cleanup2()
}, nil
}
const (
mongoPort = 27017
mongoDBName = "graph"
)
func MakeMongo(ctx context.Context, host string, l lgr.L) (repo.Mongo, func(), error) {
handleErr := func(err error) (repo.Mongo, func(), error) {
return repo.Mongo{}, nil, fmt.Errorf("new mongo client: %w", err)
}
mongoURI := fmt.Sprintf("mongodb://%s:%d/%s", host, mongoPort, mongoDBName)
opts := options.Client().ApplyURI(mongoURI)
client, err := mongo.Connect(ctx, opts)
if err != nil {
return handleErr(err)
}
// TODO figure out readPreference
if err := client.Ping(ctx, nil); err != nil {
return handleErr(err)
}
cleanup := func() {
if err := client.Disconnect(ctx); err != nil {
l.Logf("disconnect from mongodb: %v", err, ctx)
}
}
return repo.NewMongo(client, l), cleanup, nil
}
func run() error {
var cfg config
loader := aconfig.LoaderFor(&cfg, aconfig.Config{AllFieldRequired: true})
if err := loader.Load(); err != nil {
return fmt.Errorf("load config: %w", err)
}
ctx, cancelCtx := context.WithCancel(context.Background())
defer cancelCtx()
logLevel := lgr.Debug
if strings.ToLower(cfg.LogLevel) == "trace" {
logLevel = lgr.Trace
}
l := lgr.New(logLevel, lgr.CallerFile)
_, redis, cleanup, err := prepare(ctx, l, cfg)
if err != nil {
return err
}
defer cleanup()
mongo, cleanup2, err := MakeMongo(ctx, cfg.MongoHost, l)
if err != nil {
return fmt.Errorf("init mongo: %w", err)
}
defer cleanup2()
rpc := cli.NewRpc(l, cfg.RpcEndpoint)
h, err := NewBlockHarvester(l, rpc, redis)
if err != nil {
return fmt.Errorf("fail to initialize harvester instance: %w", err)
}
p, err := NewProcesor(l, rpc, redis, mongo)
if err != nil {
return fmt.Errorf("fail to initialize processor instance: %w", err)
}
api := NewAPI(mongo)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := h.HarvestBlocks(ctx); !errors.Is(err, context.Canceled) {
lgr.Fatalf("error starting block harvester: %v", err)
}
}()
const consumerID = 0 // todo specify when we scale
processors := cfg.BlockProcessorConcurrency
if processors < 1 {
processors = runtime.GOMAXPROCS(-1)
}
l.Logf("using %d threads for processing blocks (gomaxprocs = %d)", processors, runtime.GOMAXPROCS(-1))
for i := 0; i < processors; i++ {
i := i
wg.Add(1)
go func() {
defer wg.Done()
l.Logf("starting processor #%d", i)
if err := p.StartProcessingBlocks(ctx, consumerID, uint(i)); !errors.Is(err, context.Canceled) {
panic(fmt.Sprintf("error starting processor: %v", err))
}
}()
}
const reportInterval = time.Second * 30
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(reportInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
p.ReportProgress()
}
}
}()
// pprof
go func() {
l.Logf("[ERROR] bind pprof server: %v", http.ListenAndServe("0.0.0.0:4444", nil))
}()
wg.Add(1)
go func() {
defer wg.Done()
runServer(ctx, api)
}()
WaitForShutdownSignal(
func() { l.Logf("[WARN] received exit signal. waiting for all processes to finish") },
cancelCtx,
wg.Wait,
)
l.Logf("goodbye")
return nil
}
func runServer(ctx context.Context, a API) {
s := srv.NewServer()
s.Register("sg_findRelations", srv.WrapH(a.FindRelations))
if err := s.Run(ctx); err != http.ErrServerClosed {
panic(err)
}
}
type Redis interface {
// initialize exists only on implementation
SaveLastSeenBlock(ctx context.Context, block uint64) error
GetLastSeenBlock(ctx context.Context) (uint64, error)
AddBlocks(ctx context.Context, blocks []uint64) error
FetchStreamEvents(ctx context.Context, consumerID string, batchSize uint) (map[repo.EventID]uint64, error)
FindStaleBlocks(ctx context.Context, consumerID string, staleTimeout time.Duration, batchSize uint) (map[repo.EventID]uint64, error)
AcknowledgeBlocks(ctx context.Context, events []repo.EventID) error
}
type Mongo interface {
FetchRelations(ctx context.Context, from, to string, providers []string, after string, limit uint) ([]graph.Relation, error)
SaveRelations(ctx context.Context, relations []graph.Relation) error
}
type RPC interface {
GetBlocks(ctx context.Context, retries uint, blocksIds ...uint64) ([]cli.Block, []int, error)
GetBlocksWithLimit(ctx context.Context, from, limit uint64) ([]uint64, error)
GetLatestBlock(ctx context.Context) (uint64, error)
}
type BlockHarvester struct {
l lgr.L
rpc RPC
redis Redis
}
func NewBlockHarvester(l lgr.L, rpc RPC, redis repo.Redis) (BlockHarvester, error) {
return BlockHarvester{l, rpc, redis}, nil
}
const (
blockHarvestInterval = 400 * time.Millisecond // average block time
)
// called once
func (h *BlockHarvester) HarvestBlocks(ctx context.Context) error {
handleErr := func(err error) error {
return fmt.Errorf("harvest blocks: %w", err)
}
for {
select {
case <-ctx.Done():
return handleErr(ctx.Err())
default:
}
// get latest processed block, if not present start with latest
startBlock, err := h.redis.GetLastSeenBlock(ctx)
if err != nil {
return handleErr(err)
}
if startBlock == 0 {
startBlock, err = h.rpc.GetLatestBlock(ctx)
if err != nil {
return handleErr(err)
}
h.l.Logf("[WARN] No saved block in Redis. Starting with latest block %d", startBlock)
}
// fetch all block that came after this one, insert them into stream
const blockLimit = 1000
blocks, err := h.rpc.GetBlocksWithLimit(ctx, startBlock, blockLimit)
if err != nil {
return handleErr(err)
}
h.l.Logf("[TRACE] adding blocks %d", blocks)
if err := h.redis.AddBlocks(ctx, blocks); err != nil {
return handleErr(fmt.Errorf("error adding blocks to the queue: %w", err))
}
h.l.Logf("[TRACE] added %d blocks\n", len(blocks))
lastSeen := startBlock
if len(blocks) > 0 {
lastSeen = blocks[len(blocks)-1] + 1 // start from next one
}
if err := h.redis.SaveLastSeenBlock(ctx, lastSeen); err != nil {
return handleErr(err)
}
// sleep some more
time.Sleep(blockHarvestInterval)
}
}
func keys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for key := range m {
keys = append(keys, key)
}
return keys
}
func values[K comparable, V any](m map[K]V) []V {
values := make([]V, 0, len(m))
for _, value := range m {
values = append(values, value)
}
return values
}
func WaitForShutdownSignal(cf ...func()) {
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGTERM)
<-done
signal.Stop(done)
lgr.Printf("shutting things down")
for _, f := range cf {
f()
}
}