forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.go
662 lines (583 loc) · 19.3 KB
/
sequence.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
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
package input
import (
"errors"
"fmt"
"sync"
"time"
"github.com/Jeffail/gabs/v2"
"github.com/OneOfOne/xxhash"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/internal/interop"
"github.com/dafanshu/benthos/v3/internal/shutdown"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/message"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/response"
"github.com/dafanshu/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeSequence] = TypeSpec{
constructor: fromSimpleConstructor(NewSequence),
Summary: `
Reads messages from a sequence of child inputs, starting with the first and once
that input gracefully terminates starts consuming from the next, and so on.`,
Description: `
This input is useful for consuming from inputs that have an explicit end but
must not be consumed in parallel.`,
Examples: []docs.AnnotatedExample{
{
Title: "End of Stream Message",
Summary: "A common use case for sequence might be to generate a message at the end of our main input. With the following config once the records within `./dataset.csv` are exhausted our final payload `{\"status\":\"finished\"}` will be routed through the pipeline.",
Config: `
input:
sequence:
inputs:
- csv:
paths: [ ./dataset.csv ]
- generate:
count: 1
mapping: 'root = {"status":"finished"}'
`,
},
{
Title: "Joining Data (Simple)",
Summary: `Benthos can be used to join unordered data from fragmented datasets in memory by specifying a common identifier field and a number of sharded iterations. For example, given two CSV files, the first called "main.csv", which contains rows of user data:
` + "```csv" + `
uuid,name,age
AAA,Melanie,34
BBB,Emma,28
CCC,Geri,45
` + "```" + `
And the second called "hobbies.csv" that, for each user, contains zero or more rows of hobbies:
` + "```csv" + `
uuid,hobby
CCC,pokemon go
AAA,rowing
AAA,golf
` + "```" + `
We can parse and join this data into a single dataset:
` + "```json" + `
{"uuid":"AAA","name":"Melanie","age":34,"hobbies":["rowing","golf"]}
{"uuid":"BBB","name":"Emma","age":28}
{"uuid":"CCC","name":"Geri","age":45,"hobbies":["pokemon go"]}
` + "```" + `
With the following config:`,
Config: `
input:
sequence:
sharded_join:
type: full-outter
id_path: uuid
merge_strategy: array
inputs:
- csv:
paths:
- ./hobbies.csv
- ./main.csv
`,
},
{
Title: "Joining Data (Advanced)",
Summary: `In this example we are able to join unordered and fragmented data from a combination of CSV files and newline-delimited JSON documents by specifying multiple sequence inputs with their own processors for extracting the structured data.
The first file "main.csv" contains straight forward CSV data:
` + "```csv" + `
uuid,name,age
AAA,Melanie,34
BBB,Emma,28
CCC,Geri,45
` + "```" + `
And the second file called "hobbies.ndjson" contains JSON documents, one per line, that associate an identifer with an array of hobbies. However, these data objects are in a nested format:
` + "```json" + `
{"document":{"uuid":"CCC","hobbies":[{"type":"pokemon go"}]}}
{"document":{"uuid":"AAA","hobbies":[{"type":"rowing"},{"type":"golf"}]}}
` + "```" + `
And so we will want to map these into a flattened structure before the join, and then we will end up with a single dataset that looks like this:
` + "```json" + `
{"uuid":"AAA","name":"Melanie","age":34,"hobbies":["rowing","golf"]}
{"uuid":"BBB","name":"Emma","age":28}
{"uuid":"CCC","name":"Geri","age":45,"hobbies":["pokemon go"]}
` + "```" + `
With the following config:`,
Config: `
input:
sequence:
sharded_join:
type: full-outter
id_path: uuid
iterations: 10
merge_strategy: array
inputs:
- csv:
paths: [ ./main.csv ]
- file:
codec: lines
paths: [ ./hobbies.ndjson ]
processors:
- bloblang: |
root.uuid = this.document.uuid
root.hobbies = this.document.hobbies.map_each(this.type)
`,
},
},
FieldSpecs: docs.FieldSpecs{
docs.FieldAdvanced(
"sharded_join",
`EXPERIMENTAL: Provides a way to perform outter joins of arbitrarily structured and unordered data resulting from the input sequence, even when the overall size of the data surpasses the memory available on the machine.
When configured the sequence of inputs will be consumed one or more times according to the number of iterations, and when more than one iteration is specified each iteration will process an entirely different set of messages by sharding them by the ID field. Increasing the number of iterations reduces the memory consumption at the cost of needing to fully parse the data each time.
Each message must be structured (JSON or otherwise processed into a structured form) and the fields will be aggregated with those of other messages sharing the ID. At the end of each iteration the joined messages are flushed downstream before the next iteration begins, hence keeping memory usage limited.`,
).WithChildren(
docs.FieldCommon("type", "The type of join to perform. A `full-outter` ensures that all identifiers seen in any of the input sequences are sent, and is performed by consuming all input sequences before flushing the joined results. An `outter` join consumes all input sequences but only writes data joined from the last input in the sequence, similar to a left or right outter join. With an `outter` join if an identifier appears multiple times within the final sequence input it will be flushed each time it appears.").HasOptions("none", "full-outter", "outter"),
docs.FieldCommon("id_path", "A [dot path](/docs/configuration/field_paths) that points to a common field within messages of each fragmented data set and can be used to join them. Messages that are not structured or are missing this field will be dropped. This field must be set in order to enable joins."),
docs.FieldCommon("iterations", "The total number of iterations (shards), increasing this number will increase the overall time taken to process the data, but reduces the memory used in the process. The real memory usage required is significantly higher than the real size of the data and therefore the number of iterations should be at least an order of magnitude higher than the available memory divided by the overall size of the dataset."),
docs.FieldCommon(
"merge_strategy",
"The chosen strategy to use when a data join would otherwise result in a collision of field values. The strategy `array` means non-array colliding values are placed into an array and colliding arrays are merged. The strategy `replace` replaces old values with new values. The strategy `keep` keeps the old value.",
).HasOptions("array", "replace", "keep"),
).AtVersion("3.40.0"),
docs.FieldCommon("inputs", "An array of inputs to read from sequentially.").Array().HasType(docs.FieldTypeInput),
},
Categories: []Category{
CategoryUtility,
},
}
}
//------------------------------------------------------------------------------
// SequenceShardedJoinConfig describes an optional mechanism for performing
// sharded joins of structured data resulting from the input sequence. This is a
// way to merge the structured fields of fragmented datasets within memory even
// when the overall size of the data surpasses the memory available on the
// machine.
//
// When configured the sequence of inputs will be consumed multiple times
// according to the number of iterations, and each iteration will process an
// entirely different set of messages by sharding them by the ID field.
//
// Each message must be structured (JSON or otherwise processed into a
// structured form) and the fields will be aggregated with those of other
// messages sharing the ID. At the end of each iteration the joined messages are
// flushed downstream before the next iteration begins.
type SequenceShardedJoinConfig struct {
Type string `json:"type" yaml:"type"`
IDPath string `json:"id_path" yaml:"id_path"`
Iterations int `json:"iterations" yaml:"iterations"`
MergeStrategy string `json:"merge_strategy" yaml:"merge_strategy"`
}
// NewSequenceShardedJoinConfig creates a new sequence sharding configuration
// with default values.
func NewSequenceShardedJoinConfig() SequenceShardedJoinConfig {
return SequenceShardedJoinConfig{
Type: "none",
IDPath: "",
Iterations: 1,
MergeStrategy: "array",
}
}
func (s SequenceShardedJoinConfig) validate() (*messageJoiner, error) {
var flushOnLast bool
switch s.Type {
case "none":
return nil, nil
case "full-outter":
flushOnLast = false
case "outter":
flushOnLast = true
default:
return nil, fmt.Errorf("join type '%v' was not recognized", s.Type)
}
if s.IDPath == "" {
return nil, errors.New("the id path must not be empty")
}
if s.Iterations <= 0 {
return nil, fmt.Errorf("invalid number of iterations: %v", s.Iterations)
}
collisionFn, err := getMessageJoinerCollisionFn(s.MergeStrategy)
if err != nil {
return nil, err
}
return &messageJoiner{
totalIterations: s.Iterations,
idPath: s.IDPath,
messages: map[string]*joinedMessage{},
collisionFn: collisionFn,
flushOnLast: flushOnLast,
}, nil
}
// SequenceConfig contains configuration values for the Sequence input type.
type SequenceConfig struct {
ShardedJoin SequenceShardedJoinConfig `json:"sharded_join" yaml:"sharded_join"`
Inputs []Config `json:"inputs" yaml:"inputs"`
}
// NewSequenceConfig creates a new SequenceConfig with default values.
func NewSequenceConfig() SequenceConfig {
return SequenceConfig{
ShardedJoin: NewSequenceShardedJoinConfig(),
Inputs: []Config{},
}
}
//------------------------------------------------------------------------------
type joinedMessage struct {
metadata types.Metadata
fields *gabs.Container
}
func (j *joinedMessage) ToMsg() types.Message {
part := message.NewPart(nil)
part.SetJSON(j.fields)
part.SetMetadata(j.metadata)
msg := message.New(nil)
msg.Append(part)
return msg
}
type messageJoinerCollisionFn func(dest, source interface{}) interface{}
func getMessageJoinerCollisionFn(name string) (messageJoinerCollisionFn, error) {
switch name {
case "array":
return func(dest, source interface{}) interface{} {
destArr, destIsArray := dest.([]interface{})
sourceArr, sourceIsArray := source.([]interface{})
if destIsArray {
if sourceIsArray {
return append(destArr, sourceArr...)
}
return append(destArr, source)
}
if sourceIsArray {
return append(append([]interface{}{}, dest), sourceArr...)
}
return []interface{}{dest, source}
}, nil
case "replace":
return func(dest, source interface{}) interface{} {
return source
}, nil
case "keep":
return func(dest, source interface{}) interface{} {
return dest
}, nil
}
return nil, fmt.Errorf("merge strategy '%v' was not recognised", name)
}
type messageJoiner struct {
currentIteration int
totalIterations int
idPath string
messages map[string]*joinedMessage
collisionFn messageJoinerCollisionFn
flushOnLast bool
}
func (m *messageJoiner) Add(msg types.Message, lastInSequence bool, fn func(msg types.Message)) {
if m.messages == nil {
m.messages = map[string]*joinedMessage{}
}
msg.Iter(func(i int, p types.Part) error {
var incomingObj map[string]interface{}
if jData, err := p.JSON(); err == nil {
incomingObj, _ = jData.(map[string]interface{})
}
if incomingObj == nil {
// Messages that aren't structured objects are dropped.
// TODO: Propagate errors?
return nil
}
gIncoming := gabs.Wrap(incomingObj)
id, _ := gIncoming.Path(m.idPath).Data().(string)
if id == "" {
// TODO: Propagate errors?
return nil
}
// Drop all messages that aren't within our current shard.
if int(xxhash.ChecksumString64(id)%uint64(m.totalIterations)) != m.currentIteration {
return nil
}
jObj := m.messages[id]
if jObj == nil {
jObj = &joinedMessage{
fields: gIncoming,
metadata: p.Metadata().Copy(),
}
m.messages[id] = jObj
if m.flushOnLast && lastInSequence {
fn(jObj.ToMsg().DeepCopy())
}
return nil
}
_ = gIncoming.Delete(m.idPath)
_ = jObj.fields.MergeFn(gIncoming, m.collisionFn)
p.Metadata().Iter(func(k, v string) error {
jObj.metadata.Set(k, v)
return nil
})
if m.flushOnLast && lastInSequence {
fn(jObj.ToMsg().DeepCopy())
}
return nil
})
}
func (m *messageJoiner) GetIteration() (int, bool) {
return m.currentIteration, m.currentIteration == (m.totalIterations - 1)
}
func (m *messageJoiner) Empty(fn func(types.Message)) bool {
for k, v := range m.messages {
if !m.flushOnLast {
msg := v.ToMsg()
fn(msg)
}
delete(m.messages, k)
}
m.currentIteration++
return m.currentIteration >= m.totalIterations
}
//------------------------------------------------------------------------------
// Sequence is an input type that reads from a sequence of inputs, starting with
// the first, and when it ends gracefully it moves onto the next, and so on.
type Sequence struct {
conf SequenceConfig
targetMut sync.Mutex
target Type
remaining []sequenceTarget
spent []sequenceTarget
joiner *messageJoiner
wrapperMgr types.Manager
wrapperLog log.Modular
wrapperStats metrics.Type
stats metrics.Type
log log.Modular
transactions chan types.Transaction
shutSig *shutdown.Signaller
}
type sequenceTarget struct {
index int
config Config
}
// NewSequence creates a new Sequence input type.
func NewSequence(
conf Config,
mgr types.Manager,
log log.Modular,
stats metrics.Type,
) (Type, error) {
if len(conf.Sequence.Inputs) == 0 {
return nil, errors.New("requires at least one child input")
}
targets := make([]sequenceTarget, 0, len(conf.Sequence.Inputs))
for i, c := range conf.Sequence.Inputs {
targets = append(targets, sequenceTarget{
index: i,
config: c,
})
}
_, rLog, rStats := interop.LabelChild("sequence", mgr, log, stats)
rdr := &Sequence{
conf: conf.Sequence,
remaining: targets,
wrapperLog: log,
wrapperStats: stats,
wrapperMgr: mgr,
log: rLog,
stats: rStats,
transactions: make(chan types.Transaction),
shutSig: shutdown.NewSignaller(),
}
var err error
if rdr.joiner, err = rdr.conf.ShardedJoin.validate(); err != nil {
return nil, fmt.Errorf("invalid sharded join config: %w", err)
}
if target, _, err := rdr.createNextTarget(); err != nil {
return nil, err
} else if target == nil {
return nil, errors.New("failed to initialize first input")
}
go rdr.loop()
return rdr, nil
}
//------------------------------------------------------------------------------
func (r *Sequence) getTarget() (Type, bool) {
r.targetMut.Lock()
target := r.target
final := len(r.remaining) == 0
r.targetMut.Unlock()
return target, final
}
func (r *Sequence) createNextTarget() (Type, bool, error) {
var target Type
var err error
r.targetMut.Lock()
r.target = nil
if len(r.remaining) > 0 {
if target, err = New(
r.remaining[0].config,
r.wrapperMgr,
r.wrapperLog,
r.wrapperStats,
); err == nil {
r.spent = append(r.spent, r.remaining[0])
r.remaining = r.remaining[1:]
} else {
err = fmt.Errorf("failed to initialize input index %v: %w", r.remaining[0].index, err)
}
}
if target != nil {
r.log.Debugf("Initialized sequence input %v.", len(r.spent)-1)
r.target = target
}
final := len(r.remaining) == 0
r.targetMut.Unlock()
return target, final, err
}
func (r *Sequence) resetTargets() {
r.targetMut.Lock()
r.remaining = r.spent
r.spent = nil
r.targetMut.Unlock()
}
func (r *Sequence) dispatchJoinedMessage(wg *sync.WaitGroup, msg types.Message) {
resChan := make(chan types.Response)
tran := types.NewTransaction(msg, resChan)
select {
case r.transactions <- tran:
case <-r.shutSig.CloseNowChan():
return
}
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case res := <-resChan:
if res.Error() == nil {
return
}
r.log.Errorf("Failed to send joined message: %v\n", res.Error())
case <-r.shutSig.CloseNowChan():
return
}
select {
case <-time.After(time.Second):
case <-r.shutSig.CloseNowChan():
return
}
select {
case r.transactions <- tran:
case <-r.shutSig.CloseNowChan():
return
}
}
}()
}
func (r *Sequence) loop() {
var shardJoinWG sync.WaitGroup
defer func() {
shardJoinWG.Wait()
if t, _ := r.getTarget(); t != nil {
t.CloseAsync()
go func() {
select {
case <-r.shutSig.CloseNowChan():
_ = t.WaitForClose(0)
case <-r.shutSig.HasClosedChan():
}
}()
_ = t.WaitForClose(shutdown.MaximumShutdownWait())
}
close(r.transactions)
r.shutSig.ShutdownComplete()
}()
target, finalInSequence := r.getTarget()
runLoop:
for {
if target == nil {
var err error
if target, finalInSequence, err = r.createNextTarget(); err != nil {
r.log.Errorf("Unable to start next sequence: %v\n", err)
select {
case <-time.After(time.Second):
case <-r.shutSig.CloseAtLeisureChan():
return
}
continue runLoop
}
}
if target == nil {
if r.joiner != nil {
iteration, _ := r.joiner.GetIteration()
r.log.Debugf("Finished sharded iteration %v.", iteration)
// Wait for pending transactions before adding more.
shardJoinWG.Wait()
lastIteration := r.joiner.Empty(func(msg types.Message) {
r.dispatchJoinedMessage(&shardJoinWG, msg)
})
shardJoinWG.Wait()
if lastIteration {
r.log.Infoln("Finished all sharded iterations and exhausted all sequence inputs, shutting down.")
return
}
r.resetTargets()
continue runLoop
} else {
r.log.Infoln("Exhausted all sequence inputs, shutting down.")
return
}
}
var tran types.Transaction
var open bool
select {
case tran, open = <-target.TransactionChan():
if !open {
target.CloseAsync() // For good measure.
target = nil
continue runLoop
}
case <-r.shutSig.CloseAtLeisureChan():
return
}
if r.joiner != nil {
r.joiner.Add(tran.Payload.DeepCopy(), finalInSequence, func(msg types.Message) {
r.dispatchJoinedMessage(&shardJoinWG, msg)
})
select {
case tran.ResponseChan <- response.NewAck():
case <-r.shutSig.CloseNowChan():
return
}
} else {
select {
case r.transactions <- tran:
case <-r.shutSig.CloseNowChan():
return
}
}
}
}
// TransactionChan returns a transactions channel for consuming messages from
// this input type.
func (r *Sequence) TransactionChan() <-chan types.Transaction {
return r.transactions
}
// Connected returns a boolean indicating whether this input is currently
// connected to its target.
func (r *Sequence) Connected() bool {
if t, _ := r.getTarget(); t != nil {
return t.Connected()
}
return false
}
// CloseAsync shuts down the Sequence input and stops processing requests.
func (r *Sequence) CloseAsync() {
r.shutSig.CloseAtLeisure()
}
// WaitForClose blocks until the Sequence input has closed down.
func (r *Sequence) WaitForClose(timeout time.Duration) error {
go func() {
if tAfter := timeout - time.Second; tAfter > 0 {
<-time.After(timeout - time.Second)
}
r.shutSig.CloseNow()
}()
select {
case <-r.shutSig.HasClosedChan():
case <-time.After(timeout):
return types.ErrTimeout
}
return nil
}
//------------------------------------------------------------------------------