/
kafka.go
462 lines (394 loc) · 13.5 KB
/
kafka.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
package writer
import (
"context"
"crypto/tls"
"fmt"
"strconv"
"strings"
"sync"
"time"
batchInternal "github.com/Jeffail/benthos/v3/internal/batch"
"github.com/Jeffail/benthos/v3/internal/bloblang/field"
"github.com/Jeffail/benthos/v3/internal/component/output"
"github.com/Jeffail/benthos/v3/internal/interop"
"github.com/Jeffail/benthos/v3/lib/log"
"github.com/Jeffail/benthos/v3/lib/message/batch"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/types"
"github.com/Jeffail/benthos/v3/lib/util/hash/murmur2"
"github.com/Jeffail/benthos/v3/lib/util/kafka/sasl"
"github.com/Jeffail/benthos/v3/lib/util/retries"
btls "github.com/Jeffail/benthos/v3/lib/util/tls"
"github.com/Shopify/sarama"
"github.com/cenkalti/backoff/v4"
)
//------------------------------------------------------------------------------
// KafkaConfig contains configuration fields for the Kafka output type.
type KafkaConfig struct {
Addresses []string `json:"addresses" yaml:"addresses"`
ClientID string `json:"client_id" yaml:"client_id"`
RackID string `json:"rack_id" yaml:"rack_id"`
Key string `json:"key" yaml:"key"`
Partitioner string `json:"partitioner" yaml:"partitioner"`
Partition string `json:"partition" yaml:"partition"`
Topic string `json:"topic" yaml:"topic"`
Compression string `json:"compression" yaml:"compression"`
MaxMsgBytes int `json:"max_msg_bytes" yaml:"max_msg_bytes"`
Timeout string `json:"timeout" yaml:"timeout"`
AckReplicas bool `json:"ack_replicas" yaml:"ack_replicas"`
TargetVersion string `json:"target_version" yaml:"target_version"`
TLS btls.Config `json:"tls" yaml:"tls"`
SASL sasl.Config `json:"sasl" yaml:"sasl"`
MaxInFlight int `json:"max_in_flight" yaml:"max_in_flight"`
retries.Config `json:",inline" yaml:",inline"`
RetryAsBatch bool `json:"retry_as_batch" yaml:"retry_as_batch"`
Batching batch.PolicyConfig `json:"batching" yaml:"batching"`
StaticHeaders map[string]string `json:"static_headers" yaml:"static_headers"`
Metadata output.Metadata `json:"metadata" yaml:"metadata"`
InjectTracingMap string `json:"inject_tracing_map" yaml:"inject_tracing_map"`
// TODO: V4 remove this.
RoundRobinPartitions bool `json:"round_robin_partitions" yaml:"round_robin_partitions"`
}
// NewKafkaConfig creates a new KafkaConfig with default values.
func NewKafkaConfig() KafkaConfig {
rConf := retries.NewConfig()
rConf.Backoff.InitialInterval = "3s"
rConf.Backoff.MaxInterval = "10s"
rConf.Backoff.MaxElapsedTime = "30s"
return KafkaConfig{
Addresses: []string{"localhost:9092"},
ClientID: "benthos_kafka_output",
RackID: "",
Key: "",
RoundRobinPartitions: false,
Partitioner: "fnv1a_hash",
Partition: "",
Topic: "benthos_stream",
Compression: "none",
MaxMsgBytes: 1000000,
Timeout: "5s",
AckReplicas: false,
TargetVersion: sarama.V1_0_0_0.String(),
StaticHeaders: map[string]string{},
Metadata: output.NewMetadata(),
TLS: btls.NewConfig(),
SASL: sasl.NewConfig(),
MaxInFlight: 1,
Config: rConf,
RetryAsBatch: false,
Batching: batch.NewPolicyConfig(),
}
}
//------------------------------------------------------------------------------
// Kafka is a writer type that writes messages into kafka.
type Kafka struct {
log log.Modular
mgr types.Manager
stats metrics.Type
backoffCtor func() backoff.BackOff
tlsConf *tls.Config
timeout time.Duration
addresses []string
version sarama.KafkaVersion
conf KafkaConfig
key *field.Expression
topic *field.Expression
partition *field.Expression
producer sarama.SyncProducer
compression sarama.CompressionCodec
partitioner sarama.PartitionerConstructor
staticHeaders map[string]string
metaFilter *output.MetadataFilter
connMut sync.RWMutex
}
// NewKafka creates a new Kafka writer type.
func NewKafka(conf KafkaConfig, mgr types.Manager, log log.Modular, stats metrics.Type) (*Kafka, error) {
compression, err := strToCompressionCodec(conf.Compression)
if err != nil {
return nil, err
}
// for backward compatitility
if conf.RoundRobinPartitions {
conf.Partitioner = "round_robin"
log.Warnln("The field 'round_robin_partitions' is deprecated, please use the 'partitioner' field (set to 'round_robin') instead.")
}
if conf.Partition == "" && conf.Partitioner == "manual" {
return nil, fmt.Errorf("partition field required for 'manual' partitioner")
} else if len(conf.Partition) > 0 && conf.Partitioner != "manual" {
return nil, fmt.Errorf("partition field can only be specified for 'manual' partitioner")
}
partitioner, err := strToPartitioner(conf.Partitioner)
if err != nil {
return nil, err
}
k := Kafka{
log: log,
mgr: mgr,
stats: stats,
conf: conf,
compression: compression,
partitioner: partitioner,
staticHeaders: conf.StaticHeaders,
}
if k.metaFilter, err = conf.Metadata.Filter(); err != nil {
return nil, fmt.Errorf("failed to construct metadata filter: %w", err)
}
if k.key, err = interop.NewBloblangField(mgr, conf.Key); err != nil {
return nil, fmt.Errorf("failed to parse key expression: %v", err)
}
if k.topic, err = interop.NewBloblangField(mgr, conf.Topic); err != nil {
return nil, fmt.Errorf("failed to parse topic expression: %v", err)
}
if k.partition, err = interop.NewBloblangField(mgr, conf.Partition); err != nil {
return nil, fmt.Errorf("failed to parse parition expression: %v", err)
}
if k.backoffCtor, err = conf.Config.GetCtor(); err != nil {
return nil, err
}
if tout := conf.Timeout; len(tout) > 0 {
var err error
if k.timeout, err = time.ParseDuration(tout); err != nil {
return nil, fmt.Errorf("failed to parse timeout string: %v", err)
}
}
if conf.TLS.Enabled {
var err error
if k.tlsConf, err = conf.TLS.Get(); err != nil {
return nil, err
}
}
if k.version, err = sarama.ParseKafkaVersion(conf.TargetVersion); err != nil {
return nil, err
}
for _, addr := range conf.Addresses {
for _, splitAddr := range strings.Split(addr, ",") {
if trimmed := strings.TrimSpace(splitAddr); len(trimmed) > 0 {
k.addresses = append(k.addresses, trimmed)
}
}
}
return &k, nil
}
//------------------------------------------------------------------------------
func strToCompressionCodec(str string) (sarama.CompressionCodec, error) {
switch str {
case "none":
return sarama.CompressionNone, nil
case "snappy":
return sarama.CompressionSnappy, nil
case "lz4":
return sarama.CompressionLZ4, nil
case "gzip":
return sarama.CompressionGZIP, nil
case "zstd":
return sarama.CompressionZSTD, nil
}
return sarama.CompressionNone, fmt.Errorf("compression codec not recognised: %v", str)
}
//------------------------------------------------------------------------------
func strToPartitioner(str string) (sarama.PartitionerConstructor, error) {
switch str {
case "fnv1a_hash":
return sarama.NewHashPartitioner, nil
case "murmur2_hash":
return sarama.NewCustomPartitioner(
sarama.WithAbsFirst(),
sarama.WithCustomHashFunction(murmur2.New32),
), nil
case "random":
return sarama.NewRandomPartitioner, nil
case "round_robin":
return sarama.NewRoundRobinPartitioner, nil
case "manual":
return sarama.NewManualPartitioner, nil
default:
}
return nil, fmt.Errorf("partitioner not recognised: %v", str)
}
//------------------------------------------------------------------------------
func (k *Kafka) buildSystemHeaders(part types.Part) []sarama.RecordHeader {
if k.version.IsAtLeast(sarama.V0_11_0_0) {
out := []sarama.RecordHeader{}
k.metaFilter.Iter(part.Metadata(), func(k, v string) error {
out = append(out, sarama.RecordHeader{
Key: []byte(k),
Value: []byte(v),
})
return nil
})
return out
}
// no headers before version 0.11
return nil
}
//------------------------------------------------------------------------------
func (k *Kafka) buildUserDefinedHeaders(staticHeaders map[string]string) []sarama.RecordHeader {
if k.version.IsAtLeast(sarama.V0_11_0_0) {
out := make([]sarama.RecordHeader, 0, len(staticHeaders))
for name, value := range staticHeaders {
out = append(out, sarama.RecordHeader{
Key: []byte(name),
Value: []byte(value),
})
}
return out
}
// no headers before version 0.11
return nil
}
//------------------------------------------------------------------------------
// ConnectWithContext attempts to establish a connection to a Kafka broker.
func (k *Kafka) ConnectWithContext(ctx context.Context) error {
return k.Connect()
}
// Connect attempts to establish a connection to a Kafka broker.
func (k *Kafka) Connect() error {
k.connMut.Lock()
defer k.connMut.Unlock()
if k.producer != nil {
return nil
}
config := sarama.NewConfig()
config.ClientID = k.conf.ClientID
config.RackID = k.conf.RackID
config.Version = k.version
config.Producer.Compression = k.compression
config.Producer.Partitioner = k.partitioner
config.Producer.MaxMessageBytes = k.conf.MaxMsgBytes
config.Producer.Timeout = k.timeout
config.Producer.Return.Errors = true
config.Producer.Return.Successes = true
config.Net.TLS.Enable = k.conf.TLS.Enabled
if k.conf.TLS.Enabled {
config.Net.TLS.Config = k.tlsConf
}
if err := k.conf.SASL.Apply(k.mgr, config); err != nil {
return err
}
if k.conf.AckReplicas {
config.Producer.RequiredAcks = sarama.WaitForAll
} else {
config.Producer.RequiredAcks = sarama.WaitForLocal
}
var err error
k.producer, err = sarama.NewSyncProducer(k.addresses, config)
if err == nil {
k.log.Infof("Sending Kafka messages to addresses: %s\n", k.addresses)
}
return err
}
// Write will attempt to write a message to Kafka, wait for acknowledgement, and
// returns an error if applicable.
func (k *Kafka) Write(msg types.Message) error {
return k.WriteWithContext(context.Background(), msg)
}
// WriteWithContext will attempt to write a message to Kafka, wait for
// acknowledgement, and returns an error if applicable.
func (k *Kafka) WriteWithContext(ctx context.Context, msg types.Message) error {
k.connMut.RLock()
producer := k.producer
k.connMut.RUnlock()
if producer == nil {
return types.ErrNotConnected
}
boff := k.backoffCtor()
userDefinedHeaders := k.buildUserDefinedHeaders(k.staticHeaders)
msgs := []*sarama.ProducerMessage{}
err := msg.Iter(func(i int, p types.Part) error {
key := k.key.Bytes(i, msg)
nextMsg := &sarama.ProducerMessage{
Topic: k.topic.String(i, msg),
Value: sarama.ByteEncoder(p.Get()),
Headers: append(k.buildSystemHeaders(p), userDefinedHeaders...),
Metadata: i, // Store the original index for later reference.
}
if len(key) > 0 {
nextMsg.Key = sarama.ByteEncoder(key)
}
// Only parse and set the partition if we are configured for manual
// partitioner. Although samara will (currently) ignore the partition
// field when not using a manual partitioner, we should only set it when
// we explicitly want that.
if k.conf.Partitioner == "manual" {
partitionString := k.partition.String(i, msg)
if partitionString == "" {
return fmt.Errorf("partition expression failed to produce a value")
}
partitionInt, err := strconv.Atoi(partitionString)
if err != nil {
return fmt.Errorf("failed to parse valid integer from partition expression: %w", err)
}
if partitionInt < 0 {
return fmt.Errorf("invalid partition parsed from expression, must be >= 0, got %v", partitionInt)
}
// samara requires a 32-bit integer for the partition field
nextMsg.Partition = int32(partitionInt)
}
msgs = append(msgs, nextMsg)
return nil
})
if err != nil {
return err
}
err = producer.SendMessages(msgs)
for err != nil {
if pErrs, ok := err.(sarama.ProducerErrors); !k.conf.RetryAsBatch && ok {
if len(pErrs) == 0 {
break
}
batchErr := batchInternal.NewError(msg, pErrs[0].Err)
msgs = nil
for _, pErr := range pErrs {
if mIndex, ok := pErr.Msg.Metadata.(int); ok {
batchErr.Failed(mIndex, pErr.Err)
}
msgs = append(msgs, pErr.Msg)
}
if len(pErrs) == batchErr.IndexedErrors() {
err = batchErr
} else {
// If these lengths don't match then somehow we failed to obtain
// the indexes from metadata, which implies something is wrong
// with our logic here.
k.log.Warnln("Unable to determine batch index of errors")
}
k.log.Errorf("Failed to send '%v' messages: %v\n", len(pErrs), err)
} else {
k.log.Errorf("Failed to send messages: %v\n", err)
}
tNext := boff.NextBackOff()
if tNext == backoff.Stop {
return err
}
select {
case <-ctx.Done():
return err
case <-time.After(tNext):
}
// Recheck connection is alive
k.connMut.RLock()
producer = k.producer
k.connMut.RUnlock()
if producer == nil {
return types.ErrNotConnected
}
err = producer.SendMessages(msgs)
}
return nil
}
// CloseAsync shuts down the Kafka writer and stops processing messages.
func (k *Kafka) CloseAsync() {
go func() {
k.connMut.Lock()
if k.producer != nil {
k.producer.Close()
k.producer = nil
}
k.connMut.Unlock()
}()
}
// WaitForClose blocks until the Kafka writer has closed down.
func (k *Kafka) WaitForClose(timeout time.Duration) error {
return nil
}
//------------------------------------------------------------------------------