-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
686 lines (620 loc) · 16.9 KB
/
context.go
File metadata and controls
686 lines (620 loc) · 16.9 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* Copyright 2018 Amient Ltd, London
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 goconnect
import (
"fmt"
"github.com/amient/goconnect/util"
"log"
"math"
"os"
"reflect"
"runtime/pprof"
"sync/atomic"
"time"
)
type PC struct {
Uniq uint64
UpstreamNodeId uint16
Checkpoint *Checkpoint
}
type Receiver interface {
Elements() <-chan *Element
Ack(upstreamNodeId uint16, uniq uint64) error
}
type Sender interface {
Acks() <-chan uint64
Send(element *Element)
Eos()
Close() error
}
type Connector interface {
GetNodeID() uint16
MakeReceiver(stage uint16) Receiver
GetNumPeers() uint16
NewSender(nodeId uint16, stage uint16) Sender
}
func NewContext(connector Connector, stageId uint16, def *Def) *Context {
context := Context{
stage: stageId,
connector: connector,
def: def,
limitEnabled: def.limit > 0,
termination: make(chan bool, 2), //RootFn implementations must consume context.Termination() channel
output: make(chan *Element, def.maxVerticalParallelism*4), //TODO ouptut buffer may need to be bigger than factor of for, e.g. large FlatMaps
completed: make(chan bool),
data: make(map[int]interface{}),
}
return &context
}
type Context struct {
Emit func(element *Element)
termination chan bool
ack func(uniq uint64)
stop func(uniq uint64)
Close func()
data map[int]interface{}
up *Context
stage uint16
output chan *Element
connector Connector
receiver Receiver
def *Def
limitEnabled bool
limitCounter uint64
autoi uint64
closed bool
completed chan bool
}
func (c *Context) GetNodeID() uint16 {
if c.connector == nil {
return 1
} else {
return c.connector.GetNodeID()
}
}
func (c *Context) GetStage() uint16 {
return c.stage
}
func (c *Context) GetNumPeers() uint16 {
c.checkConnector()
return c.connector.GetNumPeers()
}
func (c *Context) GetReceiver() Receiver {
c.checkConnector()
if c.receiver == nil {
c.receiver = c.connector.MakeReceiver(c.stage)
}
return c.receiver
}
func (c *Context) MakeSender(targetNodeId uint16) Sender {
c.checkConnector()
sender := c.connector.NewSender(targetNodeId, c.stage)
go func() {
for stamp := range sender.Acks() {
c.up.ack(stamp)
}
}()
return sender
}
func (c *Context) MakeSenders() []Sender {
c.checkConnector()
senders := make([]Sender, c.GetNumPeers())
for peer := uint16(1); peer <= c.GetNumPeers(); peer++ {
senders[peer-1] = c.MakeSender(peer)
}
return senders
}
func (c *Context) checkConnector() {
if c.connector == nil {
panic("the pipeline requires a network runner - use network.Runner(pipeline,...) instead of Pipeline.Run()")
}
}
//FIXME instead of Put and Get on Context migrate all transforms that needs to materialized forms
func (c *Context) Put(index int, data interface{}) {
c.data[index] = data
}
func (c *Context) Get(index int) interface{} {
return c.data[index]
}
func (c *Context) Termination() <-chan bool {
return c.termination
}
func (c *Context) Start() {
switch fn := c.def.Fn.(type) {
case Root:
pending := make(chan PC, c.def.bufferCap)
c.initializeCheckpointer(c.def.bufferCap, pending, fn)
go func() {
fn.Run(c)
close(c.output)
c.Close()
}()
case Processor:
NewWorkerGroup(c, fn).Start(c.up.output)
case Mapper:
c.ack = c.up.ack
c.stop = c.up.stop
NewWorkerGroup(c, &MapProcessor{fn}).Start(c.up.output)
case Filter:
c.ack = c.up.ack
c.stop = c.up.stop
NewWorkerGroup(c, &FilterProcessor{fn}).Start(c.up.output)
case NetTransform:
pending := make(chan PC, c.def.bufferCap)
//TODO implement limit/stop for network buffer
acks := make(chan uint64, c.def.bufferCap)
c.ack = func(uniq uint64) {
acks <- uniq
}
terminate := make(chan bool)
terminating := false
c.Close = func() {
terminate <- true
}
pendingSuspendable := pending
pendingCheckpoints := make(map[uint64]*PC, c.def.bufferCap)
acked := make(map[uint64]bool, c.def.bufferCap)
var highestPending uint64 //FIXME these two vars are volatile
var highestAcked uint64
maybeTerminate := func() {
if terminating {
if len(pendingCheckpoints) == 0 && highestPending == highestAcked {
//c.log("Completed - closing")
close(pending)
close(acks)
acks = nil
c.close()
} else {
//c.log("Awaiting Completion highestPending: %d, highestAcked: %d", c.highestPending, c.highestAcked)
}
}
}
resolveAcks := func(uniq uint64) {
if p, exists := pendingCheckpoints[uniq]; exists {
if _, alsoExists := acked[uniq]; alsoExists {
delete(pendingCheckpoints, uniq)
delete(acked, uniq)
c.receiver.Ack(p.UpstreamNodeId, p.Uniq)
//c.log(action+"(%v) RESOLVED PART %d DATA %v PENDING %d pendingCommitReuqest = %v\n", uniq, p.Checkpoint.Part, p.Checkpoint.Data, len(pendingCheckpoints), pendingCommitReuqest)
}
}
if len(pendingCheckpoints) < c.def.bufferCap && pendingSuspendable == nil {
//release the backpressure after capacity is freed
pendingSuspendable = pending
}
maybeTerminate()
}
go func() {
for !c.closed {
select {
case <-terminate:
terminating = true
maybeTerminate()
case uniq := <-acks:
if uniq > highestAcked {
highestAcked = uniq
}
acked[uniq] = true
resolveAcks(uniq)
case p := <-pendingSuspendable:
pendingCheckpoints[p.Uniq] = &p
resolveAcks(p.Uniq)
if len(pendingCheckpoints) == c.def.bufferCap {
//in order to apply backpressure this channel needs to be nilld but right now it hangs after second pendingSuspendable
pendingSuspendable = nil
//c.log("STAGE[%d] Applying backpressure, pending acks: %d\n", c.stage, len(pendingCheckpoints))
} else if len(pendingCheckpoints) > c.def.bufferCap {
panic(fmt.Errorf("illegal accumulator state, buffer size higher than %d", c.def.bufferCap))
}
}
}
}()
c.Emit = func(element *Element) {
//TODO network stage need implementing .stop for limit conditions to propagate through them
if element.Stamp.Uniq > highestPending {
highestPending = element.Stamp.Uniq
}
pending <- PC{
Uniq: element.Stamp.Uniq,
Checkpoint: &element.Checkpoint,
UpstreamNodeId: element.FromNodeId,
}
c.output <- element
}
go func() {
fn.Run(c.up.output, c)
close(c.output)
c.Close()
}()
case Sink:
c.Close = c.close
c.stop = c.up.stop
go func() {
unflushed := false
n := 0
trigger := func() {
if unflushed {
if err := fn.Flush(c); err != nil {
panic(err)
}
unflushed = false
}
}
var ticker <-chan time.Time
if c.def.triggerEvery > 0 {
ticker = time.NewTicker(c.def.triggerEvery).C
}
for {
select {
case <-ticker:
trigger()
case e, ok := <-c.up.output:
if !ok {
trigger()
close(c.output)
c.Close()
return
} else {
e.ack = c.up.ack
if !c.limitReached(e.Stamp.Uniq) {
fn.Process(e, c)
unflushed = true
if c.def.triggerEach > 0 {
n++
if n%c.def.triggerEach == 0 {
if err := fn.Flush(c); err != nil {
panic(err)
}
}
}
}
}
}
}
}()
case FoldFn:
c.Close = c.close
stops := make(chan uint64, 1)
c.stop = func(uniq uint64) {
stops <- uniq
}
acks := make(chan uint64, c.def.bufferCap)
c.ack = func(uniq uint64) {
acks <- uniq
}
go func() {
pending := make(map[uint64][]uint64, c.def.bufferCap)
buffer := make([]uint64, 0, c.def.triggerEach)
isClean := true
highestAcked := uint64(0)
trigger := func() {
if !isClean {
e := fn.Collect()
e.Stamp.Uniq = atomic.AddUint64(&c.autoi, 1)
if !c.limitReached(e.Stamp.Uniq) {
if e.Stamp.Unix == 0 {
e.Stamp.Unix = time.Now().Unix()
}
swap := buffer
buffer = make([]uint64, 0, c.def.triggerEach)
pending[e.Stamp.Uniq] = swap
c.output <- &e
}
isClean = true
}
}
var ticker <-chan time.Time
if c.def.triggerEvery > 0 {
ticker = time.NewTicker(c.def.triggerEvery).C
}
resolveAck := func(uniq uint64) {
if uniq > highestAcked {
highestAcked = uniq
}
buffer := pending[uniq]
delete(pending, uniq)
for _, u := range buffer {
//log.Println("FOLD UPSTREAM ACK ", u)
c.up.ack(u)
}
}
var stopStamp = uint64(math.MaxUint64)
var terminated = false
var doStop = func(uniq uint64) {
stopStamp = uniq
maxInputStopStamp := uint64(0)
for _, x := range pending[uniq] {
if x > maxInputStopStamp {
maxInputStopStamp = x
}
}
//log.Println("FOLD UPSTREAM STOP ", maxInputStopStamp)
c.up.stop(maxInputStopStamp)
}
defer c.Close()
upstream := c.up.output
checkTerminated := func() {
if highestAcked >= stopStamp && upstream == nil {
terminated = true
}
}
for !terminated {
select {
case uniq := <-stops:
doStop(uniq)
if highestAcked >= stopStamp && upstream == nil {
terminated = true
}
default:
//select is split into 2 sections to guarantee that the stop signal is processed before any ack
//because the pseudo-random selection may trigger ack before stop and the pending buffer which
//must be available for the stop id upstream resolution would be be deleted
select {
case uniq := <-stops:
doStop(uniq)
checkTerminated()
case uniq := <-acks:
resolveAck(uniq)
if highestAcked >= stopStamp && upstream == nil {
terminated = true
}
case <-ticker:
trigger()
case e, ok := <-upstream:
if !ok {
trigger()
upstream = nil
close(c.output)
if c.autoi < stopStamp {
stopStamp = c.autoi
}
if highestAcked >= stopStamp && upstream == nil {
terminated = true
}
} else {
fn.Process(e.Value)
isClean = false
buffer = append(buffer, e.Stamp.Uniq)
if c.def.triggerEach > 0 {
if len(buffer) >= c.def.triggerEach {
trigger()
}
}
}
}
}
}
}()
default:
panic(fmt.Errorf("unknown Fn Type: %v", reflect.TypeOf(fn)))
}
}
func (c *Context) initializeCheckpointer(cap int, pending chan PC, fn Root) {
var highestPending uint64 //FIXME volatile
var nextAllowedEmit time.Time
c.Emit = func(element *Element) {
if c.def.throttle > 0 {
nextAllowedEmit = util.Thorttle(c.def.throttle, nextAllowedEmit)
}
element.Stamp.Uniq = atomic.AddUint64(&c.autoi, 1)
highestPending = element.Stamp.Uniq
element.ack = c.ack
if element.Stamp.Unix == 0 {
element.Stamp.Unix = time.Now().Unix()
}
if ! c.limitReached(element.Stamp.Uniq) {
pending <- PC{
Uniq: element.Stamp.Uniq,
Checkpoint: &element.Checkpoint,
UpstreamNodeId: element.FromNodeId,
}
t := time.NewTimer(30 * time.Second).C //TODO configurable pipeline-level timeout
for {
select {
case c.output <- element:
return
case <-t:
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
panic(fmt.Errorf("timeout while trying to push into the root output buffer"))
}
}
}
}
acks := make(chan uint64, cap)
c.ack = func(uniq uint64) {
if uniq == 0 {
panic(fmt.Errorf("ack stamp cannot be zero"))
}
//acks get nulled when closing but if this was caused by Limit there may still follow some calls to c.ack
acks <- uniq
}
commits := make(chan Watermark, c.def.maxVerticalParallelism*100)
go func() {
defer close(commits)
//commits consumer must run in a separate routine so that the output emitters continue working even if the commit takes a long time, in other words this is one aspect of the backpressure feature
t := time.NewTicker(250 * time.Millisecond).C
var checkpoint Watermark
var final = false
for {
select {
case w := <-commits:
if len(w) == 0 {
final = true
} else if checkpoint == nil {
checkpoint = w
} else {
for x := range w {
checkpoint[x] = w[x]
}
}
case <-t:
if checkpoint != nil {
//c.log("Commit Checkpoint: %v", checkpoint)
fn.Commit(checkpoint, c)
checkpoint = nil
}
if final {
//terminating checkpoint
//c.log("Completed - closing")
close(pending)
close(acks)
acks = nil
c.close()
return
}
}
}
}()
terminate := make(chan uint64, 1)
c.Close = func() {
terminate <- 0
}
pendingSuspendable := pending
pendingCheckpoints := make([]*PC, 0, cap)
acked := make(map[uint64]bool, cap)
watermark := make(Watermark)
var stopStamp uint64 = math.MaxUint64
c.stop = func(uniq uint64) {
c.termination <- true
terminate <- uniq
}
go func() {
terminating := false
var highestAcked uint64
resolveAcks := func() {
for len(pendingCheckpoints) > 0 && acked[pendingCheckpoints[0].Uniq] {
p := pendingCheckpoints[0]
watermark[p.Checkpoint.Part] = p.Checkpoint.Data
pendingCheckpoints = pendingCheckpoints[1:]
delete(acked, p.Uniq)
if p.UpstreamNodeId > 0 {
c.receiver.Ack(p.UpstreamNodeId, p.Uniq)
} else if c.up != nil {
c.up.ack(p.Uniq)
}
}
if len(pendingCheckpoints) < cap && pendingSuspendable == nil {
//release the backpressure after capacity is freed
pendingSuspendable = pending
}
if len(watermark) > 0 {
commits <- watermark
watermark = make(map[int]interface{})
}
if terminating {
if highestPending <= highestAcked || stopStamp <= highestAcked {
if len(watermark) == 0 && len(pendingCheckpoints) == 0 && len(acked) == 0 {
commits <- Watermark{}
} else {
//c.log("Watermark: %v", watermark)
//c.log("Pending: %v", pendingCheckpoints)
//c.log("acked: %v", acked)
}
}
}
}
for !c.closed {
select {
case s := <-terminate:
terminating = true
if s > 0 {
//c.log("ROOT STOP STAMP %v", s)
stopStamp = s
//delete all pending checkpoints above stopStamp
for p := len(pendingCheckpoints) - 1; p >= 0; p-- {
if pendingCheckpoints[p].Uniq > stopStamp {
//c.log("DROPPING CHECKPOINT= %v", pendingCheckpoints[p].Uniq)
pendingCheckpoints = append(pendingCheckpoints[:p], pendingCheckpoints[p+1:]...)
}
}
//TODO delete acked higher than stopStamp
for a := range acked {
if a > stopStamp {
delete(acked, a)
}
}
}
resolveAcks()
case uniq, ok := <-acks:
if ok && uniq <= stopStamp {
//c.log("ACK %d", uniq)
if uniq > highestAcked {
highestAcked = uniq
}
acked[uniq] = true
//c.log("BEFORE RESOLVE ROOT ACK %v -> %v", uniq, acked)
resolveAcks()
//c.log("AFTER RESOLVE ROOT ACK %v -> %v", uniq, acked)
//c.log("AFTER ROOT ACK WATERMARK: %v", watermark)
}
case p, ok := <-pendingSuspendable:
if ok && p.Uniq <= stopStamp {
pendingCheckpoints = append(pendingCheckpoints, &p)
resolveAcks()
if len(pendingCheckpoints) == cap {
//in order to apply backpressure this channel needs to be set to nil so that the select doesn't enter into a busy loop until resolved
pendingSuspendable = nil
//c.log("STAGE[%d] Applying backpressure, pending acks: %d\n", c.stage, len(pendingCheckpoints))
} else if len(pendingCheckpoints) > cap {
panic(fmt.Errorf("illegal accumulator state, buffer size higher than %d", cap))
}
}
}
}
}()
}
func (c *Context) close() {
if ! c.closed {
//c.log("CLOSED")
c.completed <- true
c.closed = true
if fn, ok := c.def.Fn.(Closeable); ok {
if err := fn.Close(c); err != nil {
panic(err)
}
}
}
}
func (c *Context) log(f string, args ... interface{}) {
args2 := make([]interface{}, len(args)+2)
args2[0] = c.GetNodeID()
args2[1] = c.stage
for i := range args {
args2[i+2] = args[i]
}
if c.stage > 0 {
log.Printf("NODE[%d] STAGE[%d] "+f, args2...)
}
}
func (c *Context) limitReached(stopStamp uint64) bool {
if c.limitEnabled {
after := atomic.AddUint64(&c.limitCounter, 1)
if after == c.def.limit {
if c.stop == nil {
panic(fmt.Errorf("stage[%d] stop function is nil", c.stage))
} else {
//c.log("Limit Reached @ %d", stopStamp)
c.stop(stopStamp)
}
} else if after > c.def.limit {
return true
}
}
return false
}