/
output_switch.go
469 lines (429 loc) · 13.1 KB
/
output_switch.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
package pure
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/Jeffail/gabs/v2"
"github.com/benthosdev/benthos/v4/internal/batch"
"github.com/benthosdev/benthos/v4/internal/bloblang/mapping"
"github.com/benthosdev/benthos/v4/internal/bundle"
"github.com/benthosdev/benthos/v4/internal/component"
"github.com/benthosdev/benthos/v4/internal/component/output"
"github.com/benthosdev/benthos/v4/internal/component/output/processors"
"github.com/benthosdev/benthos/v4/internal/docs"
"github.com/benthosdev/benthos/v4/internal/log"
"github.com/benthosdev/benthos/v4/internal/message"
"github.com/benthosdev/benthos/v4/internal/shutdown"
)
var (
// ErrSwitchNoConditionMet is returned when a message does not match any
// output conditions.
ErrSwitchNoConditionMet = errors.New("no switch output conditions were met by message")
// ErrSwitchNoCasesMatched is returned when a message does not match any
// output cases.
ErrSwitchNoCasesMatched = errors.New("no switch cases were matched by message")
// ErrSwitchNoOutputs is returned when creating a switchOutput type with less than
// 2 outputs.
ErrSwitchNoOutputs = errors.New("attempting to create switch with fewer than 2 cases")
)
func init() {
err := bundle.AllOutputs.Add(processors.WrapConstructor(func(c output.Config, nm bundle.NewManagement) (output.Streamed, error) {
return newSwitchOutput(c.Switch, nm)
}), docs.ComponentSpec{
Name: "switch",
Summary: `
The switch output type allows you to route messages to different outputs based on their contents.`,
Description: `
Messages must successfully route to one or more outputs, otherwise this is considered an error and the message is reprocessed. In order to explicitly drop messages that do not match your cases add one final case with a [drop output](/docs/components/outputs/drop).`,
Config: docs.FieldComponent().WithChildren(
docs.FieldBool(
"retry_until_success", `
If a selected output fails to send a message this field determines whether it is
reattempted indefinitely. If set to false the error is instead propagated back
to the input level.
If a message can be routed to >1 outputs it is usually best to set this to true
in order to avoid duplicate messages being routed to an output.`,
).HasDefault(false),
docs.FieldBool(
"strict_mode", `
This field determines whether an error should be reported if no condition is met.
If set to true, an error is propagated back to the input level. The default
behavior is false, which will drop the message.`,
).Advanced().HasDefault(false),
docs.FieldObject(
"cases",
"A list of switch cases, outlining outputs that can be routed to.",
[]interface{}{
map[string]interface{}{
"check": `this.urls.contains("http://benthos.dev")`,
"output": map[string]interface{}{
"cache": map[string]interface{}{
"target": "foo",
"key": "${!json(\"id\")}",
},
},
"continue": true,
},
map[string]interface{}{
"output": map[string]interface{}{
"s3": map[string]interface{}{
"bucket": "bar",
"path": "${!json(\"id\")}",
},
},
},
},
).Array().WithChildren(
docs.FieldBloblang(
"check",
"A [Bloblang query](/docs/guides/bloblang/about/) that should return a boolean value indicating whether a message should be routed to the case output. If left empty the case always passes.",
`this.type == "foo"`,
`this.contents.urls.contains("https://benthos.dev/")`,
).HasDefault(""),
docs.FieldOutput(
"output", "An [output](/docs/components/outputs/about/) for messages that pass the check to be routed to.",
).HasDefault(map[string]interface{}{}),
docs.FieldBool(
"continue",
"Indicates whether, if this case passes for a message, the next case should also be tested.",
).HasDefault(false).Advanced(),
).HasDefault([]interface{}{}),
).LinterFunc(func(ctx docs.LintContext, line, col int, value interface{}) []docs.Lint {
if _, ok := value.(map[string]interface{}); !ok {
return nil
}
gObj := gabs.Wrap(value)
retry, exists := gObj.S("retry_until_success").Data().(bool)
if !exists || !retry {
return nil
}
for _, cObj := range gObj.S("cases").Children() {
typeStr, _ := cObj.S("output", "type").Data().(string)
isReject := cObj.Exists("output", "reject")
if typeStr == "reject" || isReject {
return []docs.Lint{
docs.NewLintError(line, "a `switch` output with a `reject` case output must have the field `switch.retry_until_success` set to `false`, otherwise the `reject` child output will result in infinite retries"),
}
}
}
return nil
}),
Categories: []string{
"Utility",
},
Examples: []docs.AnnotatedExample{
{
Title: "Basic Multiplexing",
Summary: `
The most common use for a switch output is to multiplex messages across a range of output destinations. The following config checks the contents of the field ` + "`type` of messages and sends `foo` type messages to an `amqp_1` output, `bar` type messages to a `gcp_pubsub` output, and everything else to a `redis_streams` output" + `.
Outputs can have their own processors associated with them, and in this example the ` + "`redis_streams`" + ` output has a processor that enforces the presence of a type field before sending it.`,
Config: `
output:
switch:
cases:
- check: this.type == "foo"
output:
amqp_1:
url: amqps://guest:guest@localhost:5672/
target_address: queue:/the_foos
- check: this.type == "bar"
output:
gcp_pubsub:
project: dealing_with_mike
topic: mikes_bars
- output:
redis_streams:
url: tcp://localhost:6379
stream: everything_else
processors:
- bloblang: |
root = this
root.type = this.type | "unknown"
`,
},
{
Title: "Control Flow",
Summary: `
The ` + "`continue`" + ` field allows messages that have passed a case to be tested against the next one also. This can be useful when combining non-mutually-exclusive case checks.
In the following example a message that passes both the check of the first case as well as the second will be routed to both.`,
Config: `
output:
switch:
cases:
- check: 'this.user.interests.contains("walks").catch(false)'
output:
amqp_1:
url: amqps://guest:guest@localhost:5672/
target_address: queue:/people_what_think_good
continue: true
- check: 'this.user.dislikes.contains("videogames").catch(false)'
output:
gcp_pubsub:
project: people
topic: that_i_dont_want_to_hang_with
`,
},
},
})
if err != nil {
panic(err)
}
}
type switchOutput struct {
logger log.Modular
transactions <-chan message.Transaction
strictMode bool
outputTSChans []chan message.Transaction
outputs []output.Streamed
checks []*mapping.Executor
continues []bool
fallthroughs []bool
shutSig *shutdown.Signaller
}
func newSwitchOutput(conf output.SwitchConfig, mgr bundle.NewManagement) (output.Streamed, error) {
o := &switchOutput{
logger: mgr.Logger(),
transactions: nil,
strictMode: conf.StrictMode,
shutSig: shutdown.NewSignaller(),
}
lCases := len(conf.Cases)
if lCases < 2 {
return nil, ErrSwitchNoOutputs
}
if lCases > 0 {
o.outputs = make([]output.Streamed, lCases)
o.checks = make([]*mapping.Executor, lCases)
o.continues = make([]bool, lCases)
o.fallthroughs = make([]bool, lCases)
}
var err error
for i, cConf := range conf.Cases {
oMgr := mgr.IntoPath("switch", strconv.Itoa(i), "output")
if o.outputs[i], err = oMgr.NewOutput(cConf.Output); err != nil {
return nil, err
}
if conf.RetryUntilSuccess {
if o.outputs[i], err = RetryOutputIndefinitely(oMgr, o.outputs[i]); err != nil {
return nil, fmt.Errorf("failed to create case '%v' output type '%v': %v", i, cConf.Output.Type, err)
}
}
if len(cConf.Check) > 0 {
if o.checks[i], err = mgr.BloblEnvironment().NewMapping(cConf.Check); err != nil {
return nil, fmt.Errorf("failed to parse case '%v' check mapping: %v", i, err)
}
}
o.continues[i] = cConf.Continue
}
o.outputTSChans = make([]chan message.Transaction, len(o.outputs))
for i := range o.outputTSChans {
o.outputTSChans[i] = make(chan message.Transaction)
if err := o.outputs[i].Consume(o.outputTSChans[i]); err != nil {
return nil, err
}
}
return o, nil
}
func (o *switchOutput) Consume(transactions <-chan message.Transaction) error {
if o.transactions != nil {
return component.ErrAlreadyStarted
}
o.transactions = transactions
go o.loop()
return nil
}
func (o *switchOutput) Connected() bool {
for _, out := range o.outputs {
if !out.Connected() {
return false
}
}
return true
}
func (o *switchOutput) dispatchToTargets(
group *message.SortGroup,
sourceMessage *message.Batch,
outputTargets [][]*message.Part,
ackFn func(context.Context, error) error,
) {
var setErr func(error)
var setErrForPart func(*message.Part, error)
var getErr func() error
{
var generalErr error
var batchErr *batch.Error
var errLock sync.Mutex
setErr = func(err error) {
if err == nil {
return
}
errLock.Lock()
generalErr = err
errLock.Unlock()
}
setErrForPart = func(part *message.Part, err error) {
if err == nil {
return
}
errLock.Lock()
defer errLock.Unlock()
index := group.GetIndex(part)
if index == -1 {
generalErr = err
return
}
if batchErr == nil {
batchErr = batch.NewError(sourceMessage, err)
}
batchErr.Failed(index, err)
}
getErr = func() error {
if batchErr != nil {
return batchErr
}
return generalErr
}
}
var pendingResponses int64
for _, parts := range outputTargets {
if len(parts) == 0 {
continue
}
pendingResponses++
}
if pendingResponses == 0 {
ctx, done := o.shutSig.CloseAtLeisureCtx(context.Background())
defer done()
_ = ackFn(ctx, nil)
}
for target, parts := range outputTargets {
if len(parts) == 0 {
continue
}
msgCopy, i := message.QuickBatch(nil), target
msgCopy.SetAll(parts)
select {
case o.outputTSChans[i] <- message.NewTransactionFunc(msgCopy, func(ctx context.Context, err error) error {
if err != nil {
if bErr, ok := err.(*batch.Error); ok {
bErr.WalkParts(func(i int, p *message.Part, e error) bool {
if e != nil {
setErrForPart(p, e)
}
return true
})
} else {
_ = msgCopy.Iter(func(i int, p *message.Part) error {
setErrForPart(p, err)
return nil
})
}
}
if atomic.AddInt64(&pendingResponses, -1) <= 0 {
return ackFn(ctx, getErr())
}
return nil
}):
case <-o.shutSig.CloseAtLeisureChan():
setErr(component.ErrTypeClosed)
return
}
}
}
func (o *switchOutput) loop() {
ackInterruptChan := make(chan struct{})
var ackPending int64
defer func() {
// Wait for pending acks to be resolved, or forceful termination
ackWaitLoop:
for atomic.LoadInt64(&ackPending) > 0 {
select {
case <-ackInterruptChan:
case <-time.After(time.Millisecond * 100):
// Just incase an interrupt doesn't arrive.
case <-o.shutSig.CloseAtLeisureChan():
break ackWaitLoop
}
}
for _, tChan := range o.outputTSChans {
close(tChan)
}
for _, output := range o.outputs {
output.CloseAsync()
}
for _, output := range o.outputs {
_ = output.WaitForClose(shutdown.MaximumShutdownWait())
}
o.shutSig.ShutdownComplete()
}()
shutCtx, done := o.shutSig.CloseAtLeisureCtx(context.Background())
defer done()
for !o.shutSig.ShouldCloseAtLeisure() {
var ts message.Transaction
var open bool
select {
case ts, open = <-o.transactions:
if !open {
return
}
case <-o.shutSig.CloseAtLeisureChan():
return
}
group, trackedMsg := message.NewSortGroup(ts.Payload)
outputTargets := make([][]*message.Part, len(o.checks))
if checksErr := trackedMsg.Iter(func(i int, p *message.Part) error {
routedAtLeastOnce := false
for j, exe := range o.checks {
test := true
if exe != nil {
var err error
if test, err = exe.QueryPart(i, trackedMsg); err != nil {
test = false
o.logger.Errorf("Failed to test case %v: %v\n", j, err)
}
}
if test {
routedAtLeastOnce = true
outputTargets[j] = append(outputTargets[j], p.Copy())
if !o.continues[j] {
return nil
}
}
}
if !routedAtLeastOnce && o.strictMode {
return ErrSwitchNoConditionMet
}
return nil
}); checksErr != nil {
if err := ts.Ack(shutCtx, checksErr); err != nil && shutCtx.Err() != nil {
return
}
continue
}
_ = atomic.AddInt64(&ackPending, 1)
o.dispatchToTargets(group, trackedMsg, outputTargets, func(ctx context.Context, err error) error {
ackErr := ts.Ack(ctx, err)
_ = atomic.AddInt64(&ackPending, -1)
select {
case ackInterruptChan <- struct{}{}:
default:
}
return ackErr
})
}
}
func (o *switchOutput) CloseAsync() {
o.shutSig.CloseAtLeisure()
}
func (o *switchOutput) WaitForClose(timeout time.Duration) error {
select {
case <-o.shutSig.HasClosedChan():
case <-time.After(timeout):
return component.ErrTimeout
}
return nil
}