forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoiningsource.go
More file actions
460 lines (390 loc) · 13.3 KB
/
joiningsource.go
File metadata and controls
460 lines (390 loc) · 13.3 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2019 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bstream
import (
"context"
"fmt"
"sync"
"time"
"github.com/streamingfast/shutter"
"go.uber.org/zap"
)
// JoiningSource needs a buffer, in which to put the blocks from the liveSource until joined.
// as soon as the fileSource reads a block that is in the buffer, we:
// 1) shutdown the filesource and set livePassThru to true
// 2) process all the blocks from the buffer that are >= that joining block (with a lock...)
// 3) "delete" the buffer
// 4) configure the "incomingFromLive" handler to directly process the next blocks
type JoiningSource struct {
*shutter.Shutter
fileSourceFactory SourceFactory
liveSourceFactory SourceFactory
liveSourceWrapper Handler
sourcesLock sync.Mutex
handlerLock sync.Mutex
liveSource Source
livePassThru bool
fileSource Source
targetBlockID string
targetBlockNum uint64
startLiveImmediately *bool
tracker *Tracker
trackerTimeout time.Duration
handler Handler
lastFileProcessedBlock *Block
highestFileProcessedBlockNum uint64
liveBuffer *Buffer
liveBufferSize int
state joinSourceState
rateLimit func(counter int)
rateLimiterCounter int
logger *zap.Logger
}
type JoiningSourceOption = func(s *JoiningSource)
func NewJoiningSource(fileSourceFactory, liveSourceFactory SourceFactory, h Handler, options ...JoiningSourceOption) *JoiningSource {
s := &JoiningSource{
fileSourceFactory: fileSourceFactory,
liveSourceFactory: liveSourceFactory,
handler: h,
liveBufferSize: 300,
trackerTimeout: 6 * time.Second,
logger: zlog,
}
for _, option := range options {
option(s)
}
// Created after option so logger is set when called
s.liveBuffer = NewBuffer("joiningSource", s.logger.Named("buffer"))
s.logger.Info("creating new joining source")
s.Shutter = shutter.New()
s.Shutter.OnTerminating(func(err error) {
s.sourcesLock.Lock()
defer s.sourcesLock.Unlock()
if s.fileSource != nil {
s.logger.Debug("shutting down file source")
s.fileSource.Shutdown(err)
}
if s.liveSource != nil {
s.logger.Debug("shutting down live source")
s.liveSource.Shutdown(err)
}
})
return s
}
// JoiningSourceTargetBlockID is an option for when we know right away
// the ID of the block where we want to start. In this case we'll accept that block coming
// from live stream right away (if we have not started processing blocks from file)
// it prevents waiting for a file to be merged when the live stream is serving us our startBlockID
// This is not recommended from a block number because we could have missed a "version" of that block
// number from the live source, but in a filesource, we always start from the first occurence of a blocknum.
func JoiningSourceTargetBlockID(id string) JoiningSourceOption {
return func(s *JoiningSource) {
s.targetBlockID = id
}
}
// JoiningSourceTargetBlockNum is like JoiningSourceTargetBlockID
// but allows starting immediately from block num == 2 on EOS or similar
// You better be DAMN SURE that you won't get a forked block at this number, beware
func JoiningSourceTargetBlockNum(num uint64) JoiningSourceOption {
return func(s *JoiningSource) {
s.targetBlockNum = num
}
}
func JoiningSourceLiveTracker(nearBlocksCount uint64, liveHeadGetter BlockRefGetter) JoiningSourceOption {
// most of the time, use `bstream.HeadBlockRefGetter(headinfoAddr)` as `liveHeadGetter`.
return func(s *JoiningSource) {
s.tracker = NewTracker(nearBlocksCount)
s.tracker.AddGetter(FileSourceHeadTarget, s.LastFileBlockRefGetter)
s.tracker.AddGetter(LiveSourceHeadTarget, liveHeadGetter)
}
}
func JoiningLiveSourceWrapper(h Handler) JoiningSourceOption {
return func(s *JoiningSource) {
if s.liveSourceFactory == nil {
panic("using JoiningLiveSourceWrapper option when live source factory not set.")
}
s.liveSourceWrapper = h
}
}
func JoiningSourceLogger(logger *zap.Logger) JoiningSourceOption {
return func(s *JoiningSource) {
s.logger = logger.Named("js")
}
}
func JoiningSourceRateLimit(rampLength int, sleepBetweenBlocks time.Duration) JoiningSourceOption {
return func(s *JoiningSource) {
s.rateLimit = func(counter int) {
if counter >= rampLength {
time.Sleep(sleepBetweenBlocks)
}
sleepTime := sleepBetweenBlocks * time.Duration((counter*100/rampLength)/100)
if sleepTime > 0 {
time.Sleep(sleepTime)
}
}
}
}
// JoiningSourceStartLiveImmediately sets the behavior to override waiting for the tracker before starting the live source
func JoiningSourceStartLiveImmediately(b bool) JoiningSourceOption {
return func(s *JoiningSource) {
s.startLiveImmediately = &b
}
}
func (s *JoiningSource) BufferRef() *Buffer {
return s.liveBuffer
}
func (s *JoiningSource) SetLogger(logger *zap.Logger) {
s.logger = logger
}
func (s *JoiningSource) Run() {
s.Shutdown(s.run())
}
func (s *JoiningSource) run() error {
s.logger.Info("joining Source is now running")
s.sourcesLock.Lock()
s.state = joinSourceState{logger: s.logger}
s.state.logd(s)
if s.fileSourceFactory != nil {
s.fileSource = s.fileSourceFactory(HandlerFunc(s.incomingFromFile))
s.fileSource.SetLogger(s.logger.Named("file"))
}
if s.liveSourceFactory != nil {
liveFactory := s.liveSourceFactory
if s.liveSourceWrapper != nil {
liveFactory = func(h Handler) Source {
return s.liveSourceFactory(HandlerFunc(s.incomingFromLive))
}
}
s.liveSource = liveFactory(HandlerFunc(s.incomingFromLive))
s.liveSource.SetLogger(s.logger.Named("live"))
}
_ = s.LockedInit(func() error {
if s.fileSource != nil {
s.fileSource.OnTerminating(func(err error) {
if err := s.fileSource.Err(); err != nil {
s.Shutdown(fmt.Errorf("file source failed: %w", err))
}
if !s.livePassThru {
s.Shutdown(fmt.Errorf("file source was shut down and we're not live"))
}
})
go s.fileSource.Run()
}
if s.liveSource != nil {
s.liveSource.OnTerminating(func(err error) {
if err != nil {
s.Shutdown(fmt.Errorf("live source failed: %w", err))
} else {
s.Shutdown(nil)
}
})
if s.startLiveImmediately == nil {
targetBlockIDSet := s.targetBlockID != "" // allows quick reconnection from eternal source: default behavior
s.startLiveImmediately = &targetBlockIDSet
}
go func() {
for s.tracker != nil && !*s.startLiveImmediately {
if s.IsTerminating() { // no more need to start live if joiningSource is shut down
return
}
ctx, cancel := context.WithTimeout(context.Background(), s.trackerTimeout)
fileBlock, liveBlock, near, err := s.tracker.IsNearWithResults(ctx, FileSourceHeadTarget, LiveSourceHeadTarget)
if err == nil && near {
zlog.Debug("tracker near, starting live source")
cancel()
break
}
// manually checking nearness to targetBlockNum if not zero
if liveBlock != nil {
s.handlerLock.Lock()
s.state.lastLiveBlock = liveBlock.Num()
s.handlerLock.Unlock()
if s.targetBlockNum != 0 && s.tracker.IsNearManualCheck(s.targetBlockNum, liveBlock.Num()) {
s.logger.Debug("tracker near 'targetBlockNum', starting live source")
cancel()
break
}
}
zlog.Debug("tracker returned not ready", zap.Error(err))
if fileBlock == nil || fileBlock.Num() == 0 {
time.Sleep(200 * time.Millisecond)
cancel()
}
<-ctx.Done()
cancel()
continue
}
s.logger.Debug("calling run on live source")
s.liveSource.Run()
}()
}
return nil
})
s.sourcesLock.Unlock()
<-s.Terminating()
return s.Err()
}
func (s *JoiningSource) incomingFromFile(blk *Block, obj interface{}) error {
s.handlerLock.Lock()
defer s.handlerLock.Unlock()
if s.IsTerminating() {
return fmt.Errorf("not processing blocks when down")
}
if s.livePassThru {
return fmt.Errorf("fileSource should be shut down, incomingFromFile should not be called")
}
s.state.lastFileBlock = blk.Num()
if s.liveBuffer.Exists(blk.ID()) {
s.livePassThru = true
err := s.processLiveBuffer(blk)
if err != nil {
return err
}
s.logger.Info("shutting file source, switching to live (from a file block matching)")
s.fileSource.Shutdown(nil)
s.liveBuffer = nil
return nil
}
if s.rateLimit != nil {
s.rateLimit(s.rateLimiterCounter)
s.rateLimiterCounter++
}
if blk.Num() > s.highestFileProcessedBlockNum {
s.highestFileProcessedBlockNum = blk.Num()
}
s.lastFileProcessedBlock = blk
s.logger.Debug("processing from file", zap.Stringer("block_num", blk))
return s.handler.ProcessBlock(blk, obj)
}
func (s *JoiningSource) incomingFromLive(blk *Block, obj interface{}) error {
s.handlerLock.Lock()
defer s.handlerLock.Unlock()
if s.IsTerminating() {
return fmt.Errorf("not processing blocks when down")
}
s.logger.Info("incoming live block", zap.Stringer("block", blk), zap.Bool("live_pass_through", s.livePassThru))
s.state.lastLiveBlock = blk.Num()
if s.livePassThru {
if tracer.Enabled() {
s.logger.Debug("processing from live", zap.Stringer("block", blk))
} else if blk.Number%600 == 0 {
s.logger.Debug("processing from live (1/600 sampling)", zap.Stringer("block", blk))
}
return s.handler.ProcessBlock(blk, obj)
}
if s.lastFileProcessedBlock.ID() == blk.ID() {
s.livePassThru = true
s.logger.Info("shutting file source, switching to live (from a live block matching)", zap.Stringer("block", blk))
s.fileSource.Shutdown(nil)
s.liveBuffer = nil
return nil
}
if s.targetBlockNum != 0 && blk.Num() == s.targetBlockNum && s.lastFileProcessedBlock == nil {
s.livePassThru = true
s.logger.Info("shutting file source, starting from live at requested block ID", zap.Stringer("block", blk))
s.fileSource.Shutdown(nil)
s.liveBuffer = nil
return s.handler.ProcessBlock(blk, obj)
}
if s.targetBlockID != "" && blk.ID() == s.targetBlockID && s.lastFileProcessedBlock == nil {
s.livePassThru = true
s.logger.Info("shutting file source, starting from live at requested block ID", zap.Stringer("block", blk))
s.fileSource.Shutdown(nil)
s.liveBuffer = nil
return s.handler.ProcessBlock(blk, obj)
}
if s.liveBuffer.Len() >= s.liveBufferSize {
s.liveBuffer.Delete(s.liveBuffer.Tail())
}
s.liveBuffer.AppendHead(&PreprocessedBlock{Block: blk, Obj: obj})
s.logger.Info("added live block to buffer", zap.Stringer("block", blk), zap.Int("buffer_size", s.liveBuffer.Len()))
return nil
}
func (s *JoiningSource) LastFileBlockRefGetter(_ context.Context) (BlockRef, error) {
// TODO: lock if needed
if s.lastFileProcessedBlock != nil {
return s.lastFileProcessedBlock, nil
}
return nil, ErrTrackerBlockNotFound
}
func (s *JoiningSource) processLiveBuffer(liveBlock *Block) (err error) {
liveID := liveBlock.ID()
s.logger.Debug("looking for ID", zap.String("live_id", liveID), zap.Uint64("live_num", liveBlock.Num()))
gatePassed := false
count := 0
for _, blk := range s.liveBuffer.AllBlocks() {
blk, ok := blk.(*PreprocessedBlock)
if !ok {
panic("buffer contained non-block object")
}
if blk.ID() == liveID {
gatePassed = true
}
if gatePassed {
count += 1
s.logger.Debug("processing from live buffer", zap.Stringer("block", blk))
if err = s.handler.ProcessBlock(blk.Block, blk.Obj); err != nil {
return err
}
}
}
s.logger.Debug("finished processing liveBuffer", zap.Bool("gatePassed", gatePassed), zap.Int("count", count), zap.Int("len_livebuffer", len(s.liveBuffer.AllBlocks())), zap.Bool("exists_in_livebuffer", s.liveBuffer.Exists(liveID)))
return nil
}
type joinSourceState struct {
lastFileBlock uint64
lastLiveBlock uint64
logger *zap.Logger
seenLive bool
}
func (s *joinSourceState) logd(joiningSource *JoiningSource) {
go func() {
sleepDuration := 2 * time.Second
time.Sleep(sleepDuration) // start logging after we've had a chance to connect
for {
if joiningSource.IsTerminating() {
return
}
sleepDuration = s.log(joiningSource)
time.Sleep(sleepDuration)
}
}()
}
func (s *joinSourceState) log(joiningSource *JoiningSource) time.Duration {
if joiningSource.livePassThru {
if s.seenLive {
s.logger.Debug("joining state LIVE", zap.Uint64("last_live_block", s.lastLiveBlock))
} else {
s.logger.Info("joining state LIVE", zap.Uint64("last_live_block", s.lastLiveBlock))
s.seenLive = true
}
return 30 * time.Second
} else {
var tailNum, headNum uint64
if tail := joiningSource.liveBuffer.Tail(); tail != nil {
tailNum = tail.Num()
}
if head := joiningSource.liveBuffer.Head(); head != nil {
headNum = head.Num()
}
s.logger.Info("joining state JOINING",
zap.Int64("block_behind_live", int64(s.lastLiveBlock)-int64(s.lastFileBlock)),
zap.Uint64("last_file_block", s.lastFileBlock),
zap.Uint64("last_live_block", s.lastLiveBlock),
zap.Uint64("buffer_lower_block", tailNum),
zap.Uint64("buffer_higher_block", headNum),
)
return 5 * time.Second
}
}