-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.go
288 lines (245 loc) · 5.52 KB
/
join.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
package streams
import (
"context"
"io"
"time"
"golang.org/x/sync/errgroup"
)
type tableJoinReader[K comparable, V, VJoin, VOut any] struct {
Reader Reader[K, V]
Table TableReader[K, VJoin]
Joiner func(Record[K, V], VJoin) VOut
}
func TableJoinReader[K comparable, V, VJoin, VOut any](
reader Reader[K, V],
table TableReader[K, VJoin],
joiner func(Record[K, V], VJoin) VOut,
) Reader[K, VOut] {
return &tableJoinReader[K, V, VJoin, VOut]{
Reader: reader,
Table: table,
Joiner: joiner,
}
}
func (j *tableJoinReader[K, V, VJoin, VOut]) Read(ctx context.Context) (Record[K, VOut], CommitFunc, error) {
msg, done, err := j.Reader.Read(ctx)
if err != nil {
return Record[K, VOut]{}, done, err
}
join, err := j.Table.Get(msg.Key)
if err != nil {
return Record[K, VOut]{}, done, err
}
out := j.Joiner(msg, join)
return Record[K, VOut]{
Key: msg.Key,
Value: out,
Time: msg.Time,
}, done, nil
}
type JoinWindows struct {
Before time.Duration
After time.Duration
}
// This is buggy and probably doesn't work
type streamJoinReader[K comparable, V, VJoin, VOut any] struct {
Left Reader[K, V]
Right Reader[K, VJoin]
LeftState WindowState[K, V]
RightState WindowState[K, VJoin]
Joiner func(Record[K, V], Record[K, VJoin]) VOut
Cfg JoinWindows
batchNo int
batch []Record[K, VOut]
batchRemaining map[int]int
batchCommits map[int]CommitFunc
}
func StreamJoinReader[K comparable, V, VJoin, VOut any](
left Reader[K, V],
right Reader[K, VJoin],
leftState WindowState[K, V],
rightState WindowState[K, VJoin],
joiner func(Record[K, V], Record[K, VJoin]) VOut,
cfg JoinWindows,
) Reader[K, VOut] {
return &streamJoinReader[K, V, VJoin, VOut]{
Left: left,
Right: right,
LeftState: leftState,
RightState: rightState,
Joiner: joiner,
Cfg: cfg,
batchRemaining: make(map[int]int),
batchCommits: make(map[int]CommitFunc),
}
}
func min(a, b time.Time) time.Time {
if a.Before(b) {
return a
}
return b
}
func max(a, b time.Time) time.Time {
if a.Before(b) {
return a
}
return b
}
func readWindow[K comparable, V any](
ctx context.Context,
stream Reader[K, V],
state WindowState[K, V],
minTime, maxTime time.Time,
) (CommitFunc, error) {
var dones []CommitFunc
commit := func() error {
for _, done := range dones {
if err := done(); err != nil {
return err
}
}
return nil
}
for {
msg, done, err := stream.Read(ctx)
if err != nil {
if err == io.EOF {
return commit, nil
}
return commit, err
}
dones = append(dones, done)
// Skip records that are too old and catch up to the
// current min
if msg.Time.Before(minTime) {
continue
}
state.Put(
// Each record creates a new window
WindowKey[K]{
K: msg.Key,
Start: msg.Time,
},
msg.Value,
)
// If we've read all records within the window, we can
// stop
if msg.Time.After(maxTime) {
return commit, nil
}
}
}
func (j *streamJoinReader[K, V, VJoin, VOut]) prepareBatch(ctx context.Context) (CommitFunc, error) {
var dones []CommitFunc
commit := func() error {
for _, done := range dones {
if err := done(); err != nil {
return err
}
}
return nil
}
left, done, err := j.Left.Read(ctx)
if err != nil {
return commit, err
}
dones = append(dones, done)
j.LeftState.Put(
WindowKey[K]{
K: left.Key,
Start: left.Time,
},
left.Value,
)
right, done, err := j.Right.Read(ctx)
if err != nil {
return commit, err
}
j.RightState.Put(
WindowKey[K]{
K: right.Key,
Start: right.Time,
},
right.Value,
)
dones = append(dones, done)
minTime := min(left.Time.Add(-j.Cfg.Before), right.Time.Add(-j.Cfg.Before))
maxTime := max(left.Time.Add(j.Cfg.After), right.Time.Add(j.Cfg.After))
var g errgroup.Group
// Read all records from the left and right streams that fall within the
// window
g.Go(func() error {
done, err := readWindow(
ctx,
j.Left,
j.LeftState,
minTime,
maxTime,
)
if err != nil {
return err
}
dones = append(dones, done)
return nil
})
g.Go(func() error {
done, err := readWindow(
ctx,
j.Right,
j.RightState,
minTime,
maxTime,
)
if err != nil {
return err
}
dones = append(dones, done)
return nil
})
if err := g.Wait(); err != nil {
return commit, err
}
j.LeftState.Every(minTime, maxTime, func(key WindowKey[K], value V) error {
joinStart := key.Start.Add(-j.Cfg.Before)
joinEnd := key.End.Add(j.Cfg.After)
j.RightState.Each(key.K, joinStart, joinEnd, func(joinKey WindowKey[K], joinValue VJoin) error {
out := j.Joiner(
Record[K, V]{Key: key.K, Value: value},
Record[K, VJoin]{Key: joinKey.K, Value: joinValue},
)
j.batch = append(j.batch, Record[K, VOut]{
Key: key.K,
Value: out,
Time: key.Start,
})
return nil
})
return nil
})
return commit, nil
}
func (j *streamJoinReader[K, V, VJoin, VOut]) Read(ctx context.Context) (Record[K, VOut], CommitFunc, error) {
if len(j.batch) == 0 {
done, err := j.prepareBatch(ctx)
if err != nil {
return Record[K, VOut]{}, done, err
}
j.batchCommits[j.batchNo] = done
j.batchRemaining[j.batchNo] = len(j.batch)
j.batchNo++
}
// If we have records in the batch, return the first one
msg := j.batch[0]
j.batch = j.batch[1:]
commit := func() error {
batchNo := j.batchNo - 1
j.batchRemaining[batchNo]--
if j.batchRemaining[batchNo] == 0 {
commit := j.batchCommits[batchNo]
delete(j.batchRemaining, batchNo)
return commit()
}
return nil
}
return msg, commit, nil
}