forked from mantzas/patron
-
Notifications
You must be signed in to change notification settings - Fork 66
/
component.go
429 lines (370 loc) · 11.8 KB
/
component.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
// Package group provides kafka consumer group component implementation.
package group
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"time"
"github.com/Shopify/sarama"
"github.com/beatlabs/patron/component/kafka"
"github.com/beatlabs/patron/correlation"
patronErrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/internal/validation"
"github.com/beatlabs/patron/log"
"github.com/beatlabs/patron/trace"
"github.com/google/uuid"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
)
const (
consumerComponent = "kafka-consumer"
subsystem = "kafka"
messageReceived = "received"
messageProcessed = "processed"
messageErrored = "errored"
messageSkipped = "skipped"
)
const (
defaultRetries = 3
defaultRetryWait = 10 * time.Second
defaultBatchSize = 1
defaultBatchTimeout = 100 * time.Millisecond
defaultFailureStrategy = kafka.ExitStrategy
)
var (
consumerErrors *prometheus.CounterVec
topicPartitionOffsetDiff *prometheus.GaugeVec
messageStatus *prometheus.CounterVec
)
func init() {
consumerErrors = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "component",
Subsystem: subsystem,
Name: "consumer_errors",
Help: "Consumer errors, classified by consumer name",
},
[]string{"name"},
)
topicPartitionOffsetDiff = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "component",
Subsystem: subsystem,
Name: "offset_diff",
Help: "Message offset difference with high watermark, classified by topic and partition",
},
[]string{"group", "topic", "partition"},
)
messageStatus = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "component",
Subsystem: subsystem,
Name: "message_status",
Help: "Message status counter (received, processed, errored) classified by topic and partition",
}, []string{"status", "group", "topic"},
)
prometheus.MustRegister(
consumerErrors,
topicPartitionOffsetDiff,
messageStatus,
)
}
// consumerErrorsInc increments the number of errors encountered by a specific consumer
func consumerErrorsInc(name string) {
consumerErrors.WithLabelValues(name).Inc()
}
// topicPartitionOffsetDiffGaugeSet creates a new Gauge that measures partition offsets.
func topicPartitionOffsetDiffGaugeSet(group, topic string, partition int32, high, offset int64) {
topicPartitionOffsetDiff.WithLabelValues(group, topic, strconv.FormatInt(int64(partition), 10)).Set(float64(high - offset))
}
// messageStatusCountInc increments the messageStatus counter for a certain status.
func messageStatusCountInc(status, group, topic string) {
messageStatus.WithLabelValues(status, group, topic).Inc()
}
// New initializes a new kafka consumer component with support for functional configuration.
// The default failure strategy is the ExitStrategy.
// The default batch size is 1 and the batch timeout is 100ms.
// The default number of retries is 0 and the retry wait is 0.
func New(name, group string, brokers, topics []string, proc kafka.BatchProcessorFunc, oo ...OptionFunc) (*Component, error) {
var errs []error
if name == "" {
errs = append(errs, errors.New("name is required"))
}
if group == "" {
errs = append(errs, errors.New("consumer group is required"))
}
if validation.IsStringSliceEmpty(brokers) {
errs = append(errs, errors.New("brokers are empty or have an empty value"))
}
if validation.IsStringSliceEmpty(topics) {
errs = append(errs, errors.New("topics are empty or have an empty value"))
}
if proc == nil {
errs = append(errs, errors.New("work processor is required"))
}
if len(errs) > 0 {
return nil, patronErrors.Aggregate(errs...)
}
defaultSaramaCfg, err := kafka.DefaultSaramaConfig(name)
if err != nil {
return nil, err
}
cmp := &Component{
name: name,
group: group,
brokers: brokers,
topics: topics,
proc: proc,
retries: defaultRetries,
retryWait: defaultRetryWait,
batchSize: defaultBatchSize,
batchTimeout: defaultBatchTimeout,
failStrategy: defaultFailureStrategy,
saramaConfig: defaultSaramaCfg,
}
for _, optionFunc := range oo {
err = optionFunc(cmp)
if err != nil {
return nil, err
}
}
return cmp, nil
}
// Component is a kafka consumer implementation that processes messages in batch
type Component struct {
name string
group string
topics []string
brokers []string
saramaConfig *sarama.Config
proc kafka.BatchProcessorFunc
failStrategy kafka.FailStrategy
batchSize uint
batchTimeout time.Duration
retries uint
retryWait time.Duration
commitSync bool
}
// Run starts the consumer processing loop to process messages from Kafka.
func (c *Component) Run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
return c.processing(ctx)
}
func (c *Component) processing(ctx context.Context) error {
var componentError error
retries := int(c.retries)
for i := 0; i <= retries; i++ {
handler := newConsumerHandler(ctx, c.name, c.group, c.proc, c.failStrategy, c.batchSize,
c.batchTimeout, c.commitSync)
client, err := sarama.NewConsumerGroup(c.brokers, c.group, c.saramaConfig)
componentError = err
if err != nil {
log.Errorf("error creating consumer group client for kafka component: %v", err)
}
if client != nil {
// `Consume` should be called inside a loop, when a
// server-side re-balance happens, the consumer session will need to be
// recreated to get the new claims
err := client.Consume(ctx, c.topics, handler)
componentError = err
if err != nil {
log.Errorf("error from kafka consumer: %v", err)
}
err = client.Close()
if err != nil {
log.Errorf("error closing kafka consumer: %v", err)
}
}
consumerErrorsInc(c.name)
// check if context was cancelled or deadline exceeded, signaling that the consumer should stop
if ctx.Err() != nil {
log.Infof("kafka component terminating: context cancelled or deadline exceeded")
break
}
if c.retries > 0 {
if handler.processedMessages {
i = 0
componentError = nil
}
log.Errorf("failed run, retry %d/%d with %v wait: %v", i, c.retries, c.retryWait, componentError)
time.Sleep(c.retryWait)
}
// If there is no component error which is a result of not being able to initialize the consumer
// then the handler errored while processing a message. This faulty message is then the reason
// behind the component failure.
if i == retries && componentError == nil {
componentError = fmt.Errorf("message processing failure exhausted %d retries: %w", i, handler.err)
}
}
return componentError
}
// Consumer represents a sarama consumer group consumer
type consumerHandler struct {
ctx context.Context
name string
group string
// buffer
batchSize int
ticker *time.Ticker
// callback
proc kafka.BatchProcessorFunc
// failures strategy
failStrategy kafka.FailStrategy
// committing after every batch
commitSync bool
// lock to protect buffer operation
mu sync.RWMutex
msgBuf []*sarama.ConsumerMessage
// processing error
err error
// whether the handler has processed any messages
processedMessages bool
}
func newConsumerHandler(ctx context.Context, name, group string, processorFunc kafka.BatchProcessorFunc,
fs kafka.FailStrategy, batchSize uint, batchTimeout time.Duration, commitSync bool) *consumerHandler {
return &consumerHandler{
ctx: ctx,
name: name,
group: group,
batchSize: int(batchSize),
ticker: time.NewTicker(batchTimeout),
msgBuf: make([]*sarama.ConsumerMessage, 0, batchSize),
mu: sync.RWMutex{},
proc: processorFunc,
failStrategy: fs,
commitSync: commitSync,
}
}
// Setup is run at the beginning of a new session, before ConsumeClaim
func (c *consumerHandler) Setup(sarama.ConsumerGroupSession) error {
return nil
}
// Cleanup is run at the end of a session, once all ConsumeClaim goroutines have exited
func (c *consumerHandler) Cleanup(sarama.ConsumerGroupSession) error {
return nil
}
// ConsumeClaim must start a consumer loop of ConsumerGroupClaim's Messages().
func (c *consumerHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for {
select {
case msg, ok := <-claim.Messages():
if ok {
log.Debugf("message claimed: value = %s, timestamp = %v, topic = %s", string(msg.Value), msg.Timestamp, msg.Topic)
topicPartitionOffsetDiffGaugeSet(c.group, msg.Topic, msg.Partition, claim.HighWaterMarkOffset(), msg.Offset)
messageStatusCountInc(messageReceived, c.group, msg.Topic)
err := c.insertMessage(session, msg)
if err != nil {
return err
}
} else {
log.Debug("messages channel closed")
return nil
}
case <-c.ticker.C:
c.mu.Lock()
err := c.flush(session)
c.mu.Unlock()
if err != nil {
return err
}
case <-c.ctx.Done():
if c.ctx.Err() != context.Canceled {
log.Infof("closing consumer: %v", c.ctx.Err())
}
return nil
}
}
}
func (c *consumerHandler) flush(session sarama.ConsumerGroupSession) error {
if len(c.msgBuf) > 0 {
messages := make([]kafka.Message, 0, len(c.msgBuf))
for _, msg := range c.msgBuf {
messageStatusCountInc(messageProcessed, c.group, msg.Topic)
ctx, sp := c.getContextWithCorrelation(msg)
messages = append(messages, kafka.NewMessage(ctx, sp, msg))
}
btc := kafka.NewBatch(messages)
err := c.proc(btc)
if err != nil {
if c.ctx.Err() == context.Canceled {
return fmt.Errorf("context was cancelled after processing error: %w", err)
}
err := c.executeFailureStrategy(messages, err)
if err != nil {
return err
}
}
c.processedMessages = true
for _, m := range messages {
trace.SpanSuccess(m.Span())
session.MarkMessage(m.Message(), "")
}
if c.commitSync {
session.Commit()
}
c.msgBuf = c.msgBuf[:0]
}
return nil
}
func (c *consumerHandler) executeFailureStrategy(messages []kafka.Message, err error) error {
switch c.failStrategy {
case kafka.ExitStrategy:
for _, m := range messages {
trace.SpanError(m.Span())
messageStatusCountInc(messageErrored, c.group, m.Message().Topic)
}
log.Errorf("could not process message(s)")
c.err = err
return err
case kafka.SkipStrategy:
for _, m := range messages {
trace.SpanError(m.Span())
messageStatusCountInc(messageErrored, c.group, m.Message().Topic)
messageStatusCountInc(messageSkipped, c.group, m.Message().Topic)
}
log.Errorf("could not process message(s) so skipping with error: %v", err)
default:
log.Errorf("unknown failure strategy executed")
return fmt.Errorf("unknown failure strategy: %v", c.failStrategy)
}
return nil
}
func (c *consumerHandler) getContextWithCorrelation(msg *sarama.ConsumerMessage) (context.Context, opentracing.Span) {
corID := getCorrelationID(msg.Headers)
sp, ctxCh := trace.ConsumerSpan(c.ctx, trace.ComponentOpName(consumerComponent, msg.Topic),
consumerComponent, corID, mapHeader(msg.Headers))
ctxCh = correlation.ContextWithID(ctxCh, corID)
ctxCh = log.WithContext(ctxCh, log.Sub(map[string]interface{}{correlation.ID: corID}))
return ctxCh, sp
}
func (c *consumerHandler) insertMessage(session sarama.ConsumerGroupSession, msg *sarama.ConsumerMessage) error {
c.mu.Lock()
defer c.mu.Unlock()
c.msgBuf = append(c.msgBuf, msg)
if len(c.msgBuf) >= c.batchSize {
return c.flush(session)
}
return nil
}
func getCorrelationID(hh []*sarama.RecordHeader) string {
for _, h := range hh {
if string(h.Key) == correlation.HeaderID {
if len(h.Value) > 0 {
return string(h.Value)
}
break
}
}
log.Debug("correlation header not found, creating new correlation UUID")
return uuid.New().String()
}
func mapHeader(hh []*sarama.RecordHeader) map[string]string {
mp := make(map[string]string)
for _, h := range hh {
mp[string(h.Key)] = string(h.Value)
}
return mp
}