-
Notifications
You must be signed in to change notification settings - Fork 50
/
consumer_base.go
475 lines (395 loc) · 12.6 KB
/
consumer_base.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
package stream
import (
"context"
"fmt"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/justtrackio/gosoline/pkg/appctx"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/clock"
"github.com/justtrackio/gosoline/pkg/coffin"
"github.com/justtrackio/gosoline/pkg/funk"
"github.com/justtrackio/gosoline/pkg/kernel"
"github.com/justtrackio/gosoline/pkg/log"
"github.com/justtrackio/gosoline/pkg/metric"
"github.com/justtrackio/gosoline/pkg/tracing"
"github.com/justtrackio/gosoline/pkg/uuid"
)
const (
metricNameConsumerDuration = "Duration"
metricNameConsumerError = "Error"
metricNameConsumerProcessedCount = "ProcessedCount"
metricNameConsumerRetryGetCount = "RetryGetCount"
metricNameConsumerRetryPutCount = "RetryPutCount"
dataSourceInput = "input"
dataSourceRetry = "retry"
metadataKeyConsumers = "stream.consumers"
)
type ConsumerMetadata struct {
Name string `json:"name"`
RetryEnabled bool `json:"retry_enabled"`
RetryType string `json:"retry_type"`
RunnerCount int `json:"runner_count"`
}
//go:generate mockery --name RunnableCallback
type RunnableCallback interface {
Run(ctx context.Context) error
}
type BaseConsumerCallback interface {
GetModel(attributes map[string]string) interface{}
}
type ConsumerSettings struct {
Input string `cfg:"input" default:"consumer" validate:"required"`
RunnerCount int `cfg:"runner_count" default:"1" validate:"min=1"`
Encoding EncodingType `cfg:"encoding" default:"application/json"`
IdleTimeout time.Duration `cfg:"idle_timeout" default:"10s"`
Retry ConsumerRetrySettings `cfg:"retry"`
}
type ConsumerRetrySettings struct {
Enabled bool `cfg:"enabled"`
Type string `cfg:"type" default:"sqs"`
}
type consumerData struct {
msg *Message
src string
input Input
}
type baseConsumer struct {
kernel.EssentialModule
kernel.ApplicationStage
ConsumerAcknowledge
clock clock.Clock
uuidGen uuid.Uuid
logger log.Logger
metricWriter metric.Writer
tracer tracing.Tracer
encoder MessageEncoder
retryInput Input
retryHandler RetryHandler
wg sync.WaitGroup
stopped sync.Once
cancel context.CancelFunc
data chan *consumerData
id string
name string
settings *ConsumerSettings
consumerCallback interface{}
processed int32
}
func NewBaseConsumer(ctx context.Context, config cfg.Config, logger log.Logger, name string, consumerCallback BaseConsumerCallback) (*baseConsumer, error) {
uuidGen := uuid.New()
logger = logger.WithChannel(fmt.Sprintf("consumer-%s", name))
settings := readConsumerSettings(config, name)
appId := cfg.GetAppIdFromConfig(config)
tracer, err := tracing.ProvideTracer(config, logger)
if err != nil {
return nil, fmt.Errorf("can not create tracer: %w", err)
}
defaultMetrics := getConsumerDefaultMetrics(name)
metricWriter := metric.NewWriter(defaultMetrics...)
var input, retryInput Input
var retryHandler RetryHandler
if input, err = NewConfigurableInput(ctx, config, logger, settings.Input); err != nil {
return nil, err
}
encoder := NewMessageEncoder(&MessageEncoderSettings{
Encoding: settings.Encoding,
})
// if our input knows how to retry already,
if retryingInput, ok := input.(RetryingInput); ok {
settings.Retry.Enabled = true
retryInput, retryHandler = retryingInput.GetRetryHandler()
} else if retryInput, retryHandler, err = NewRetryHandler(ctx, config, logger, &settings.Retry, name); err != nil {
return nil, fmt.Errorf("can not create retry handler: %w", err)
}
consumerMetadata := ConsumerMetadata{
Name: name,
RetryEnabled: settings.Retry.Enabled,
RetryType: settings.Retry.Type,
RunnerCount: settings.RunnerCount,
}
if err = appctx.MetadataAppend(ctx, metadataKeyConsumers, consumerMetadata); err != nil {
return nil, fmt.Errorf("can not access the appctx metadata: %w", err)
}
return NewBaseConsumerWithInterfaces(uuidGen, logger, metricWriter, tracer, input, encoder, retryInput, retryHandler, consumerCallback, settings, name, appId), nil
}
func NewBaseConsumerWithInterfaces(
uuidGen uuid.Uuid,
logger log.Logger,
metricWriter metric.Writer,
tracer tracing.Tracer,
input Input,
encoder MessageEncoder,
retryInput Input,
retryHandler RetryHandler,
consumerCallback interface{},
settings *ConsumerSettings,
name string,
appId cfg.AppId,
) *baseConsumer {
return &baseConsumer{
name: name,
id: fmt.Sprintf("consumer-%s-%s-%s-%s", appId.Family, appId.Group, appId.Application, name),
clock: clock.Provider,
uuidGen: uuidGen,
logger: logger,
metricWriter: metricWriter,
tracer: tracer,
ConsumerAcknowledge: NewConsumerAcknowledgeWithInterfaces(logger, input),
encoder: encoder,
retryInput: retryInput,
retryHandler: retryHandler,
settings: settings,
consumerCallback: consumerCallback,
data: make(chan *consumerData),
}
}
func (c *baseConsumer) run(kernelCtx context.Context, inputRunner func(ctx context.Context) error) error {
defer c.logger.Info("leaving consumer %s", c.name)
c.logger.Info("running consumer %s with input %s", c.name, c.settings.Input)
// create ctx whose done channel is closed on dying coffin
cfn, dyingCtx := coffin.WithContext(context.Background())
// create ctx whose done channel is closed on dying coffin and manual cancel
manualCtx := cfn.Context(context.Background())
manualCtx, c.cancel = context.WithCancel(manualCtx)
cfn.Go(func() error {
cfn.GoWithContextf(manualCtx, c.logConsumeCounter, "panic during counter log")
cfn.GoWithContextf(manualCtx, c.runConsumerCallback, "panic during run of the consumerCallback")
// run the input after the counters are running to make sure our coffin does not immediately
// die just because Run() immediately returns
cfn.GoWithContextf(dyingCtx, c.input.Run, "panic during run of the consumer input")
cfn.GoWithContextf(dyingCtx, c.retryInput.Run, "panic during run of the retry handler")
cfn.GoWithContextf(dyingCtx, c.ingestData, "panic during shoveling the data")
c.wg.Add(c.settings.RunnerCount)
for i := 0; i < c.settings.RunnerCount; i++ {
cfn.GoWithContextf(manualCtx, inputRunner, "panic during consuming")
}
cfn.Gof(c.stopConsuming, "panic during stopping the consuming")
cfn.GoWithContext(manualCtx, func(manualCtx context.Context) error {
// wait for kernel or coffin cancel...
select {
case <-manualCtx.Done():
case <-kernelCtx.Done():
}
// and stop the input
c.stopIncomingData()
return nil
})
return nil
})
if err := cfn.Wait(); err != nil {
return fmt.Errorf("error while waiting for all routines to stop: %w", err)
}
return nil
}
func (c *baseConsumer) logConsumeCounter(ctx context.Context) error {
logger := c.logger.WithContext(ctx)
defer logger.Debug("logConsumeCounter is ending")
ticker := time.NewTicker(c.settings.IdleTimeout)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
processed := atomic.SwapInt32(&c.processed, 0)
logger.WithFields(log.Fields{
"count": processed,
"name": c.name,
}).Info("processed %v messages", processed)
}
}
}
func (c *baseConsumer) runConsumerCallback(ctx context.Context) error {
logger := c.logger.WithContext(ctx)
defer logger.Debug("runConsumerCallback is ending")
if runnable, ok := c.consumerCallback.(RunnableCallback); ok {
return runnable.Run(ctx)
}
return nil
}
func (c *baseConsumer) ingestData(ctx context.Context) error {
defer c.logger.Debug("ingestData is ending")
defer close(c.data)
cfn := coffin.New()
cfn.GoWithContextf(ctx, c.ingestDataFromSource(c.input, dataSourceInput), "panic during shoveling data from input")
cfn.GoWithContextf(ctx, c.ingestDataFromSource(c.retryInput, dataSourceRetry), "panic during shoveling data from retry")
return cfn.Wait()
}
func (c *baseConsumer) ingestDataFromSource(input Input, src string) func(ctx context.Context) error {
return func(ctx context.Context) error {
defer c.logger.Debug("ingestDataFromSource %s is ending", src)
defer c.stopIncomingData()
for {
select {
case <-ctx.Done():
return nil
case msg, ok := <-input.Data():
if !ok {
return nil
}
if retryId, ok := msg.Attributes[AttributeRetryId]; ok {
c.logger.Warn("retrying message with id %s", retryId)
c.writeMetricRetryCount(metricNameConsumerRetryGetCount)
}
c.data <- &consumerData{
msg: msg,
src: src,
input: input,
}
}
}
}
}
// this one acts as a fallback which should stop all still running routines
func (c *baseConsumer) stopConsuming() error {
defer c.logger.Debug("stopConsuming is ending")
c.wg.Wait()
c.stopIncomingData()
c.cancel()
return nil
}
func (c *baseConsumer) stopIncomingData() {
c.stopped.Do(func() {
defer c.logger.Debug("stopIncomingData is ending")
c.retryInput.Stop()
c.input.Stop()
})
}
func (c *baseConsumer) recover(ctx context.Context, msg *Message) {
var err error
if err = coffin.ResolveRecovery(recover()); err == nil {
return
}
c.handleError(ctx, err, "a panic occurred during the consume operation")
if msg == nil || c.hasNativeRetry() {
return
}
c.retry(ctx, msg)
}
func (c *baseConsumer) retry(ctx context.Context, msg *Message) {
if !c.settings.Retry.Enabled {
return
}
retryMsg, retryId := c.buildRetryMessage(msg)
ctx = log.AppendGlobalContextFields(ctx, log.Fields{
"retry_id": retryId,
})
c.logger.WithContext(ctx).Warn("putting message with id %s into retry", retryId)
c.writeMetricRetryCount(metricNameConsumerRetryPutCount)
if err := c.retryHandler.Put(ctx, retryMsg); err != nil {
c.handleError(ctx, err, "can not put the message into the retry handler")
}
}
func (c *baseConsumer) hasNativeRetry() bool {
_, ok := c.input.(RetryingInput)
return ok
}
func (c *baseConsumer) buildRetryMessage(msg *Message) (retryMsg *Message, retryId string) {
if retryId, ok := msg.Attributes[AttributeRetryId]; ok {
return msg, retryId
}
retryId = c.uuidGen.NewV4()
retryMsg = &Message{
Attributes: funk.MergeMaps(msg.Attributes, map[string]string{
AttributeRetry: strconv.FormatBool(true),
AttributeRetryId: retryId,
}),
Body: msg.Body,
}
return retryMsg, retryId
}
func (c *baseConsumer) handleError(ctx context.Context, err error, msg string) {
c.logger.WithContext(ctx).Error("%s: %w", msg, err)
c.metricWriter.Write(metric.Data{
&metric.Datum{
MetricName: metricNameConsumerError,
Dimensions: map[string]string{
"Consumer": c.name,
},
Value: 1.0,
},
})
}
func (c *baseConsumer) writeMetricDurationAndProcessedCount(duration time.Duration, processedCount int) {
c.metricWriter.Write(metric.Data{
&metric.Datum{
Priority: metric.PriorityHigh,
MetricName: metricNameConsumerDuration,
Dimensions: map[string]string{
"Consumer": c.name,
},
Unit: metric.UnitMillisecondsAverage,
Value: float64(duration.Milliseconds()),
},
&metric.Datum{
MetricName: metricNameConsumerProcessedCount,
Dimensions: map[string]string{
"Consumer": c.name,
},
Value: float64(processedCount),
},
})
}
func (c *baseConsumer) writeMetricRetryCount(metricName string) {
c.metricWriter.Write(metric.Data{
&metric.Datum{
MetricName: metricName,
Dimensions: map[string]string{
"Consumer": c.name,
},
Value: float64(1),
},
})
}
func getConsumerDefaultMetrics(name string) metric.Data {
return metric.Data{
{
Priority: metric.PriorityHigh,
MetricName: metricNameConsumerProcessedCount,
Dimensions: map[string]string{
"Consumer": name,
},
Unit: metric.UnitCount,
Value: 0.0,
},
{
Priority: metric.PriorityHigh,
MetricName: metricNameConsumerError,
Dimensions: map[string]string{
"Consumer": name,
},
Unit: metric.UnitCount,
Value: 0.0,
},
{
Priority: metric.PriorityHigh,
MetricName: metricNameConsumerRetryPutCount,
Dimensions: map[string]string{
"Consumer": name,
},
Unit: metric.UnitCount,
Value: 0.0,
},
{
Priority: metric.PriorityHigh,
MetricName: metricNameConsumerRetryGetCount,
Dimensions: map[string]string{
"Consumer": name,
},
Unit: metric.UnitCount,
Value: 0.0,
},
}
}
func ConfigurableConsumerKey(name string) string {
return fmt.Sprintf("stream.consumer.%s", name)
}
func readConsumerSettings(config cfg.Config, name string) *ConsumerSettings {
settings := &ConsumerSettings{}
key := ConfigurableConsumerKey(name)
config.UnmarshalKey(key, settings, cfg.UnmarshalWithDefaultForKey("encoding", defaultMessageBodyEncoding))
return settings
}