-
Notifications
You must be signed in to change notification settings - Fork 835
/
processor.go
370 lines (319 loc) · 10.3 KB
/
processor.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
package redis
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/go-redis/redis/v7"
"github.com/benthosdev/benthos/v4/public/bloblang"
"github.com/benthosdev/benthos/v4/public/service"
)
func redisProcConfig() *service.ConfigSpec {
spec := service.NewConfigSpec().
Stable().
Summary(`Performs actions against Redis that aren't possible using a ` + "[`cache`](/docs/components/processors/cache)" + ` processor. Actions are
performed for each message and the message contents are replaced with the result. In order to merge the result into the original message compose this processor within a ` + "[`branch` processor](/docs/components/processors/branch)" + `.`).
Categories("Integration")
for _, f := range clientFields() {
spec = spec.Field(f)
}
return spec.
Field(service.NewInterpolatedStringField("command").
Description("The command to execute.").
Version("4.3.0").
Example("scard").
Example("incrby").
Example(`${! meta("command") }`).
Default("")).
Field(service.NewBloblangField("args_mapping").
Description("A [Bloblang mapping](/docs/guides/bloblang/about) which should evaluate to an array of values matching in size to the number of arguments required for the specified Redis command.").
Version("4.3.0").
Example("root = [ this.key ]").
Example(`root = [ meta("kafka_key"), this.count ]`).
Default(``)).
Field(service.NewStringAnnotatedEnumField("operator", map[string]string{
"keys": `Returns an array of strings containing all the keys that match the pattern specified by the ` + "`key` field" + `.`,
"scard": `Returns the cardinality of a set, or ` + "`0`" + ` if the key does not exist.`,
"sadd": `Adds a new member to a set. Returns ` + "`1`" + ` if the member was added.`,
"incrby": `Increments the number stored at ` + "`key`" + ` by the message content. If the key does not exist, it is set to ` + "`0`" + ` before performing the operation. Returns the value of ` + "`key`" + ` after the increment.`,
}).
Description("The operator to apply.").
Deprecated().
Optional()).
Field(service.NewInterpolatedStringField("key").
Description("A key to use for the target operator.").
Deprecated().
Optional()).
Field(service.NewIntField("retries").
Description("The maximum number of retries before abandoning a request.").
Default(3).
Advanced()).
Field(service.NewIntField("retry_period").
Description("The time to wait before consecutive retry attempts.").
Default("500ms").
Advanced()).
LintRule(`
root = if this.contains("operator") && this.contains("command") {
[ "only one of 'operator' (old style) or 'command' (new style) fields should be specified" ]
}
`).
Example("Querying Cardinality",
`If given payloads containing a metadata field `+"`set_key`"+` it's possible to query and store the cardinality of the set for each message using a `+"[`branch` processor](/docs/components/processors/branch)"+` in order to augment rather than replace the message contents:`,
`
pipeline:
processors:
- branch:
processors:
- redis:
url: TODO
command: scard
args_mapping: 'root = [ meta("set_key") ]'
result_map: 'root.cardinality = this'
`).
Example("Running Total",
`If we have JSON data containing number of friends visited during covid 19:
`+"```json"+`
{"name":"ash","month":"feb","year":2019,"friends_visited":10}
{"name":"ash","month":"apr","year":2019,"friends_visited":-2}
{"name":"bob","month":"feb","year":2019,"friends_visited":3}
{"name":"bob","month":"apr","year":2019,"friends_visited":1}
`+"```"+`
We can add a field that contains the running total number of friends visited:
`+"```json"+`
{"name":"ash","month":"feb","year":2019,"friends_visited":10,"total":10}
{"name":"ash","month":"apr","year":2019,"friends_visited":-2,"total":8}
{"name":"bob","month":"feb","year":2019,"friends_visited":3,"total":3}
{"name":"bob","month":"apr","year":2019,"friends_visited":1,"total":4}
`+"```"+`
Using the `+"`incrby`"+` command:`,
`
pipeline:
processors:
- branch:
processors:
- redis:
url: TODO
command: incrby
args_mapping: 'root = [ this.name, this.friends_visited ]'
result_map: 'root.total = this'
`)
}
func init() {
err := service.RegisterBatchProcessor(
"redis", redisProcConfig(),
func(conf *service.ParsedConfig, mgr *service.Resources) (service.BatchProcessor, error) {
return newRedisProcFromConfig(conf, mgr)
})
if err != nil {
panic(err)
}
}
//------------------------------------------------------------------------------
type redisProc struct {
log *service.Logger
key *service.InterpolatedString
operator redisOperator
command *service.InterpolatedString
argsMapping *bloblang.Executor
client redis.UniversalClient
retries int
retryPeriod time.Duration
}
func newRedisProcFromConfig(conf *service.ParsedConfig, res *service.Resources) (*redisProc, error) {
client, err := getClient(conf)
if err != nil {
return nil, err
}
retries, err := conf.FieldInt("retries")
if err != nil {
return nil, err
}
retryPeriod, err := conf.FieldDuration("retry_period")
if err != nil {
return nil, err
}
command, err := conf.FieldInterpolatedString("command")
if err != nil {
return nil, err
}
var argsMapping *bloblang.Executor
if testStr, _ := conf.FieldString("args_mapping"); testStr != "" {
if argsMapping, err = conf.FieldBloblang("args_mapping"); err != nil {
return nil, err
}
}
r := &redisProc{
log: res.Logger(),
command: command,
argsMapping: argsMapping,
retries: retries,
retryPeriod: retryPeriod,
client: client,
}
if conf.Contains("key") {
if r.key, err = conf.FieldInterpolatedString("key"); err != nil {
return nil, err
}
}
if conf.Contains("operator") {
operatorStr, err := conf.FieldString("operator")
if err != nil {
return nil, err
}
if r.operator, err = getRedisOperator(operatorStr); err != nil {
return nil, err
}
}
return r, nil
}
type redisOperator func(r *redisProc, key string, part *service.Message) error
func newRedisKeysOperator() redisOperator {
return func(r *redisProc, key string, part *service.Message) error {
res, err := r.client.Keys(key).Result()
for i := 0; i <= r.retries && err != nil; i++ {
r.log.Errorf("Keys command failed: %v\n", err)
<-time.After(r.retryPeriod)
res, err = r.client.Keys(key).Result()
}
if err != nil {
return err
}
iRes := make([]interface{}, 0, len(res))
for _, v := range res {
iRes = append(iRes, v)
}
part.SetStructured(iRes)
return nil
}
}
func newRedisSCardOperator() redisOperator {
return func(r *redisProc, key string, part *service.Message) error {
res, err := r.client.SCard(key).Result()
for i := 0; i <= r.retries && err != nil; i++ {
r.log.Errorf("SCard command failed: %v\n", err)
<-time.After(r.retryPeriod)
res, err = r.client.SCard(key).Result()
}
if err != nil {
return err
}
part.SetBytes(strconv.AppendInt(nil, res, 10))
return nil
}
}
func newRedisSAddOperator() redisOperator {
return func(r *redisProc, key string, part *service.Message) error {
mBytes, err := part.AsBytes()
if err != nil {
return err
}
res, err := r.client.SAdd(key, mBytes).Result()
for i := 0; i <= r.retries && err != nil; i++ {
r.log.Errorf("SAdd command failed: %v\n", err)
<-time.After(r.retryPeriod)
res, err = r.client.SAdd(key, mBytes).Result()
}
if err != nil {
return err
}
part.SetBytes(strconv.AppendInt(nil, res, 10))
return nil
}
}
func newRedisIncrByOperator() redisOperator {
return func(r *redisProc, key string, part *service.Message) error {
mBytes, err := part.AsBytes()
if err != nil {
return err
}
valueInt, err := strconv.Atoi(string(mBytes))
if err != nil {
return err
}
res, err := r.client.IncrBy(key, int64(valueInt)).Result()
for i := 0; i <= r.retries && err != nil; i++ {
r.log.Errorf("incrby command failed: %v\n", err)
<-time.After(r.retryPeriod)
res, err = r.client.IncrBy(key, int64(valueInt)).Result()
}
if err != nil {
return err
}
part.SetBytes(strconv.AppendInt(nil, res, 10))
return nil
}
}
func getRedisOperator(opStr string) (redisOperator, error) {
switch opStr {
case "keys":
return newRedisKeysOperator(), nil
case "sadd":
return newRedisSAddOperator(), nil
case "scard":
return newRedisSCardOperator(), nil
case "incrby":
return newRedisIncrByOperator(), nil
}
return nil, fmt.Errorf("operator not recognised: %v", opStr)
}
func (r *redisProc) execRaw(ctx context.Context, index int, inBatch service.MessageBatch, msg *service.Message) error {
resMsg, err := inBatch.BloblangQuery(index, r.argsMapping)
if err != nil {
return fmt.Errorf("args mapping failed: %v", err)
}
iargs, err := resMsg.AsStructured()
if err != nil {
return err
}
args, ok := iargs.([]interface{})
if !ok {
return fmt.Errorf("mapping returned non-array result: %T", iargs)
}
for i, v := range args {
n, isN := v.(json.Number)
if !isN {
continue
}
var nerr error
if args[i], nerr = n.Int64(); nerr != nil {
if args[i], nerr = n.Float64(); nerr != nil {
args[i] = n.String()
}
}
}
command := inBatch.InterpolatedString(index, r.command)
args = append([]interface{}{command}, args...)
res, err := r.client.DoContext(ctx, args...).Result()
for i := 0; i <= r.retries && err != nil; i++ {
r.log.Errorf("%v command failed: %v", command, err)
<-time.After(r.retryPeriod)
res, err = r.client.DoContext(ctx, args...).Result()
}
if err != nil {
return err
}
msg.SetStructured(res)
return nil
}
func (r *redisProc) ProcessBatch(ctx context.Context, inBatch service.MessageBatch) ([]service.MessageBatch, error) {
newMsg := inBatch.Copy()
for index, part := range newMsg {
if r.operator != nil {
key := inBatch.InterpolatedString(index, r.key)
if err := r.operator(r, key, part); err != nil {
r.log.Debugf("Operator failed for key '%s': %v", key, err)
part.SetError(fmt.Errorf("redis operator failed: %w", err))
}
} else {
if err := r.execRaw(ctx, index, inBatch, part); err != nil {
r.log.Debugf("Args mapping failed: %v", err)
part.SetError(err)
}
}
}
return []service.MessageBatch{newMsg}, nil
}
func (r *redisProc) Close(ctx context.Context) error {
return r.client.Close()
}