-
Notifications
You must be signed in to change notification settings - Fork 153
/
dataset.go
332 lines (277 loc) · 8.03 KB
/
dataset.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
package execute
import (
"context"
"strconv"
"github.com/apache/arrow/go/v7/arrow/memory"
uuid "github.com/gofrs/uuid"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute/table"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/plan"
)
// Dataset represents the set of data produced by a transformation.
type Dataset interface {
Node
RetractTable(key flux.GroupKey) error
UpdateProcessingTime(t Time) error
UpdateWatermark(mark Time) error
Finish(error)
SetTriggerSpec(t plan.TriggerSpec)
}
// DatasetContext represents a Dataset with a context.Context attached.
type DatasetContext interface {
Dataset
WithContext(ctx context.Context)
}
// DataCache holds all working data for a transformation.
type DataCache interface {
Table(flux.GroupKey) (flux.Table, error)
ForEach(func(flux.GroupKey) error) error
ForEachWithContext(func(flux.GroupKey, Trigger, TableContext) error) error
DiscardTable(flux.GroupKey)
ExpireTable(flux.GroupKey)
SetTriggerSpec(t plan.TriggerSpec)
}
type AccumulationMode int
const (
// DiscardingMode will discard the data associated with a group key
// after it has been processed.
DiscardingMode AccumulationMode = iota
// AccumulatingMode will retain the data associated with a group key
// after it has been processed. If it has already sent a table with
// that group key to a downstream transformation, it will signal
// to that transformation that the previous table should be retracted.
//
// This is not implemented at the moment.
AccumulatingMode
)
type DatasetID uuid.UUID
func (id DatasetID) String() string {
return uuid.UUID(id).String()
}
var ZeroDatasetID DatasetID
func (id DatasetID) IsZero() bool {
return id == ZeroDatasetID
}
func datasetIDFromNodeID(id plan.NodeID, instance int) DatasetID {
return DatasetID(uuid.NewV5(uuid.UUID{}, string(id)+"-"+strconv.Itoa(instance)))
}
type dataset struct {
ctx context.Context
id DatasetID
ts TransformationSet
accMode AccumulationMode
watermark Time
processingTime Time
cache DataCache
}
func NewDataset(id DatasetID, accMode AccumulationMode, cache DataCache) *dataset {
return &dataset{
ctx: context.Background(),
id: id,
accMode: accMode,
cache: cache,
}
}
func (d *dataset) WithContext(ctx context.Context) {
d.ctx = ctx
}
func (d *dataset) AddTransformation(t Transformation) {
d.ts = append(d.ts, t)
}
func (d *dataset) SetTriggerSpec(spec plan.TriggerSpec) {
d.cache.SetTriggerSpec(spec)
}
func (d *dataset) UpdateWatermark(mark Time) error {
d.watermark = mark
if err := d.evalTriggers(); err != nil {
return err
}
return d.ts.UpdateWatermark(d.id, mark)
}
func (d *dataset) UpdateProcessingTime(time Time) error {
d.processingTime = time
if err := d.evalTriggers(); err != nil {
return err
}
return d.ts.UpdateProcessingTime(d.id, time)
}
func (d *dataset) evalTriggers() error {
return d.cache.ForEachWithContext(func(key flux.GroupKey, trigger Trigger, bc TableContext) error {
err := d.ctx.Err()
if err != nil {
return err
}
c := TriggerContext{
Table: bc,
Watermark: d.watermark,
CurrentProcessingTime: d.processingTime,
}
if trigger.Triggered(c) {
err = d.triggerTable(key)
}
if trigger.Finished() {
d.expireTable(key)
}
return err
})
}
func (d *dataset) triggerTable(key flux.GroupKey) error {
b, err := d.cache.Table(key)
if err != nil {
return err
}
switch d.accMode {
case DiscardingMode:
if err := d.ts.Process(d.id, b); err != nil {
return err
}
d.cache.DiscardTable(key)
case AccumulatingMode:
return errors.New(codes.Unimplemented)
}
return nil
}
func (d *dataset) expireTable(key flux.GroupKey) {
d.cache.ExpireTable(key)
}
func (d *dataset) RetractTable(key flux.GroupKey) error {
d.cache.DiscardTable(key)
return d.ts.RetractTable(d.id, key)
}
func (d *dataset) Finish(err error) {
// Only trigger tables if we are not finishing because of an error.
if err == nil {
err = d.cache.ForEach(func(bk flux.GroupKey) error {
if err = d.ctx.Err(); err != nil {
return err
}
err = d.triggerTable(bk)
d.cache.ExpireTable(bk)
return err
})
}
d.ts.Finish(d.id, err)
}
// PassthroughDataset is a Dataset that will passthrough
// the processed data to the next Transformation.
type PassthroughDataset struct {
id DatasetID
ts TransformationSet
}
// NewPassthroughDataset constructs a new PassthroughDataset.
func NewPassthroughDataset(id DatasetID) *PassthroughDataset {
return &PassthroughDataset{id: id}
}
func (d *PassthroughDataset) AddTransformation(t Transformation) {
d.ts = append(d.ts, t)
}
func (d *PassthroughDataset) Process(tbl flux.Table) error {
return d.ts.Process(d.id, tbl)
}
func (d *PassthroughDataset) RetractTable(key flux.GroupKey) error {
return d.ts.RetractTable(d.id, key)
}
func (d *PassthroughDataset) UpdateProcessingTime(t Time) error {
return d.ts.UpdateProcessingTime(d.id, t)
}
func (d *PassthroughDataset) UpdateWatermark(mark Time) error {
return d.ts.UpdateWatermark(d.id, mark)
}
func (d *PassthroughDataset) Finish(err error) {
d.ts.Finish(d.id, err)
}
func (d *PassthroughDataset) SetTriggerSpec(t plan.TriggerSpec) {
}
// TransportDataset holds data for a specific node and sends
// messages to downstream nodes using the Transport.
//
// This Dataset also implements a shim for execute.Dataset
// so it can be integrated with the existing execution engine.
// These methods are stubs and do not do anything.
type TransportDataset struct {
id DatasetID
transports []Transport
cache *RandomAccessGroupLookup
mem memory.Allocator
}
// NewTransportDataset constructs a TransportDataset.
func NewTransportDataset(id DatasetID, mem memory.Allocator) *TransportDataset {
return &TransportDataset{
id: id,
cache: NewRandomAccessGroupLookup(),
mem: mem,
}
}
// AddTransformation is used to add downstream Transformation nodes
// to this Transport.
func (d *TransportDataset) AddTransformation(t Transformation) {
if t, ok := t.(Transport); ok {
d.transports = append(d.transports, t)
return
}
d.transports = append(d.transports, WrapTransformationInTransport(t, d.mem))
}
func (d *TransportDataset) sendMessage(m Message) error {
if len(d.transports) == 1 {
return d.transports[0].ProcessMessage(m)
}
defer m.Ack()
for _, t := range d.transports {
if err := t.ProcessMessage(m.Dup()); err != nil {
return err
}
}
return nil
}
// Process sends the given Chunk to be processed by the downstream transports.
func (d *TransportDataset) Process(chunk table.Chunk) error {
m := &processChunkMsg{
srcMessage: srcMessage(d.id),
chunk: chunk,
}
return d.sendMessage(m)
}
// FlushKey sends the flush key message to the downstream transports.
func (d *TransportDataset) FlushKey(key flux.GroupKey) error {
m := &flushKeyMsg{
srcMessage: srcMessage(d.id),
key: key,
}
return d.sendMessage(m)
}
func (d *TransportDataset) Lookup(key flux.GroupKey) (interface{}, bool) {
return d.cache.Lookup(key)
}
func (d *TransportDataset) LookupOrCreate(key flux.GroupKey, fn func() interface{}) interface{} {
if fn == nil {
fn = func() interface{} {
return nil
}
}
return d.cache.LookupOrCreate(key, fn)
}
func (d *TransportDataset) Set(key flux.GroupKey, value interface{}) {
d.cache.Set(key, value)
}
func (d *TransportDataset) Delete(key flux.GroupKey) (v interface{}, found bool) {
return d.cache.Delete(key)
}
func (d *TransportDataset) Range(f func(key flux.GroupKey, value interface{}) error) error {
return d.cache.Range(func(key flux.GroupKey, value interface{}) error {
return f(key, value)
})
}
func (d *TransportDataset) RetractTable(key flux.GroupKey) error { return nil }
func (d *TransportDataset) UpdateProcessingTime(t Time) error { return nil }
func (d *TransportDataset) UpdateWatermark(mark Time) error { return nil }
func (d *TransportDataset) Finish(err error) {
m := &finishMsg{
srcMessage: srcMessage(d.id),
err: err,
}
_ = d.sendMessage(m)
d.cache.Clear()
}
func (d *TransportDataset) SetTriggerSpec(t plan.TriggerSpec) {}