forked from brimdata/super
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
182 lines (168 loc) · 4.02 KB
/
router.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
package op
import (
"context"
"sync"
"github.com/brimdata/zed/zbuf"
)
type Selector interface {
Forward(*Router, zbuf.Batch) bool
}
type Router struct {
ctx context.Context
parent zbuf.Puller
selector Selector
routes []*route
once sync.Once
}
func NewRouter(ctx context.Context, parent zbuf.Puller) *Router {
return &Router{
ctx: ctx,
parent: NewCatcher(parent),
}
}
func (r *Router) Link(s Selector) {
r.selector = s
}
func (r *Router) AddRoute() zbuf.Puller {
child := &route{
router: r,
resultCh: make(chan Result),
doneCh: make(chan struct{}),
id: len(r.routes),
}
r.routes = append(r.routes, child)
return child
}
func (r *Router) run() {
for {
if r.blocked() {
// If everything is blocked, send a done upstream
// and unblock everything to resume the next platoon.
batch, err := r.parent.Pull(true)
if batch != nil {
panic("non-nil done batch")
}
if err != nil {
if ok := r.sendEOS(err); !ok {
return
}
continue
}
r.unblock()
}
batch, err := r.parent.Pull(false)
if err != nil || batch == nil {
if ok := r.sendEOS(err); !ok {
return
}
r.unblock()
continue
}
// The selectors decides what if any of the batch it
// wants to send to which down stream procs by calling
// back the Router.Send() method. We Unref() the batch here
// after calling Forward(), so it is up to the selector to Ref()
// the batch before sending to Send() if it wants to hold on to it.
if ok := r.selector.Forward(r, batch); !ok {
return
}
batch.Unref()
}
}
func (r *Router) blocked() bool {
for _, p := range r.routes {
if !p.blocked {
return false
}
}
return true
}
// Send an EOS to each unblocked route. On return evertyhing is unblocked
// and everything downstream has been sent an EOS. If a channel is sending
// done concurrently with the EOS being sent to it, we resolve the done
// with its matching EOS. Thus, the invariant holds that everything downstream
// has EOS either via Done or batch EOS. If the Route receives a Pull(done)
// after receiving the EOS, it's done will be captured as soon as we unblock
// all channels.
func (r *Router) sendEOS(err error) bool {
// First, we need to send EOS to all non-blocked legs and
// catch any dones in progress. This result in all routes
// being blocked.
for _, p := range r.routes {
// XXX If we get done while trying to send an EOS, we need to
// ack the done and then send the EOS. We treat it as if it
// happened before the EOS being sent.
select {
case p.resultCh <- Result{}:
// This case sends EOS. If a done arrives first,
// the rooute won't read from its resultCh and the
// done case will get captured below.
p.blocked = true
case <-p.doneCh:
// This path was about to be blocked with a done so
// just mark it blocked now.
p.blocked = true
case <-r.ctx.Done():
return false
}
}
for _, p := range r.routes {
p.blocked = false
}
return true
}
func (r *Router) unblock() {
for _, p := range r.routes {
p.blocked = false
}
}
func (r *Router) Send(p zbuf.Puller, b zbuf.Batch, err error) bool {
if b == nil {
panic("EOS sent through router send API")
}
to := p.(*route)
if to.blocked {
b.Unref()
return true
}
select {
case to.resultCh <- Result{Batch: b, Err: err}:
return true
case <-to.doneCh:
// If we get a done while trying to write,
// mark this route blocked and drop the
// batch being sent.
b.Unref()
to.blocked = true
return true
case <-r.ctx.Done():
return false
}
}
type route struct {
router *Router
resultCh chan Result
doneCh chan struct{}
id int
// Used only by Router
blocked bool
}
func (r *route) Pull(done bool) (zbuf.Batch, error) {
r.router.once.Do(func() {
go r.router.run()
})
if done {
select {
case r.doneCh <- struct{}{}:
return nil, nil
case <-r.router.ctx.Done():
return nil, r.router.ctx.Err()
}
}
select {
case result := <-r.resultCh:
return result.Batch, result.Err
case <-r.router.ctx.Done():
return nil, r.router.ctx.Err()
}
}