-
Notifications
You must be signed in to change notification settings - Fork 3
/
kafka_consumer_db_collection.go
217 lines (190 loc) · 6.4 KB
/
kafka_consumer_db_collection.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
package consumer
import (
"context"
"errors"
"sync"
"time"
"github.com/Shopify/sarama"
"github.com/inviqa/kafka-consumer-go/config"
failuremodel "github.com/inviqa/kafka-consumer-go/data/failure/model"
"github.com/inviqa/kafka-consumer-go/data/retry/model"
"github.com/inviqa/kafka-consumer-go/log"
)
// kafkaConsumerDbCollection is a collection of consumers that initially consume messages from Kafka
// but then process retries from a database table instead. The failureProducer used by this collection
// should be a databaseProducer.
type kafkaConsumerDbCollection struct {
cfg *config.Config
mainKafkaConsumer sarama.ConsumerGroup
producer *databaseProducer
retryManager retryManager
handler sarama.ConsumerGroupHandler
handlerMap HandlerMap
saramaCfg *sarama.Config
logger log.Logger
connectToKafka kafkaConnector
// optional fields managed by setters
maintenanceInterval time.Duration
}
type retryManager interface {
GetBatch(ctx context.Context, topic string, sequence uint8, interval time.Duration) ([]model.Retry, error)
MarkSuccessful(ctx context.Context, retry model.Retry) error
MarkErrored(ctx context.Context, retry model.Retry, err error) error
PublishFailure(ctx context.Context, f failuremodel.Failure) error
RunMaintenance(ctx context.Context) error
}
func newKafkaConsumerDbCollection(
cfg *config.Config,
p *databaseProducer,
rm retryManager,
fch chan failuremodel.Failure,
hm HandlerMap,
scfg *sarama.Config,
logger log.Logger,
connector kafkaConnector,
) *kafkaConsumerDbCollection {
if logger == nil {
logger = log.NullLogger{}
}
return &kafkaConsumerDbCollection{
cfg: cfg,
producer: p,
retryManager: rm,
handler: newConsumer(fch, cfg, hm, logger),
handlerMap: hm,
saramaCfg: scfg,
logger: logger,
connectToKafka: connector,
maintenanceInterval: defaultMaintenanceInterval,
}
}
func (cc *kafkaConsumerDbCollection) start(ctx context.Context, wg *sync.WaitGroup) error {
topics := cc.cfg.MainTopics()
if topics == nil || len(topics) == 0 {
return errors.New("no Kafka topics are configured, therefore cannot start consumers")
}
var err error
cc.mainKafkaConsumer, err = cc.startMainTopicConsumer(ctx, wg, topics)
if err != nil {
return err
}
for _, t := range topics {
cc.startDbRetryProcessorsForTopic(ctx, t, cc.cfg.DBRetries[t], wg)
}
cc.producer.listenForFailures(ctx, wg)
cc.periodicRetryManagerMaintenance(ctx)
return nil
}
func (cc *kafkaConsumerDbCollection) periodicRetryManagerMaintenance(ctx context.Context) {
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(cc.maintenanceInterval):
if err := cc.retryManager.RunMaintenance(ctx); err != nil {
cc.logger.Errorf("error running maintenance in kafka consumer DB collection: %s", err)
}
}
}
}()
}
// startMainTopicConsumer starts a sarama.ConsumerGroup to consume messages from Kafka for the given main topic names
func (cc *kafkaConsumerDbCollection) startMainTopicConsumer(ctx context.Context, wg *sync.WaitGroup, topics []string) (sarama.ConsumerGroup, error) {
cc.logger.Infof("starting Kafka consumer group for topics: '%s'", topics)
cl, err := cc.connectToKafka(cc.cfg, cc.saramaCfg, cc.logger)
if err != nil {
return nil, err
}
go func() {
for err := range cl.Errors() {
cc.logger.Errorf("error occurred in consumer group Handler: %w", err)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
if err := cl.Consume(ctx, topics, cc.handler); err != nil {
cc.logger.Errorf("error when consuming from Kafka: %s", err)
}
if ctx.Err() != nil {
return
}
}
}
}()
return cl, nil
}
func (cc *kafkaConsumerDbCollection) startDbRetryProcessorsForTopic(ctx context.Context, topic string, retryConfig []*config.DBTopicRetry, wg *sync.WaitGroup) {
for _, rc := range retryConfig {
wg.Add(1)
go func(retryConfig *config.DBTopicRetry) {
defer wg.Done()
timer := time.NewTimer(dbRetryPollInterval)
for {
select {
case <-timer.C:
cc.processMessagesForRetry(topic, retryConfig)
timer.Reset(dbRetryPollInterval)
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
return
}
time.Sleep(dbRetryPollInterval)
}
}(rc)
}
}
func (cc *kafkaConsumerDbCollection) processMessagesForRetry(topic string, rc *config.DBTopicRetry) {
// We use a standalone context here, with a timeout, this is to allow the current retry
// processing to complete before we exit from the kafka consumer collection (see the
// startDbRetryProcessorsForTopic method for the handling of the main context cancellation).
// At the worst, the context timeout would be exceeded and cancelled, stopping the retry
// batch from being processed, but it's here to prevent the whole process from becoming
// completely locked.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
msgsForRetry, err := cc.retryManager.GetBatch(ctx, topic, rc.Sequence, rc.Interval)
if err != nil {
cc.logger.Errorf("error when fetching messages from the DB for retry: %s", err)
return
}
h, ok := cc.handlerMap.handlerForTopic(rc.Key)
if !ok {
cc.logger.Errorf("no handler found for topic key '%s'", rc.Key)
return
}
for _, msg := range msgsForRetry {
saramaMsg := msg.ToSaramaConsumerMessage()
if err = h(ctx, saramaMsg); err != nil {
cc.logger.Errorf("error processing retried message from DB: %s", err)
if repoErr := cc.retryManager.MarkErrored(ctx, msg, err); repoErr != nil {
cc.logger.Errorf("error marking retried message as errored in the DB: %s", repoErr)
}
} else {
cc.logger.Infof("successfully processed retried message from topic '%s' with original partition %d and offset %d", topic, msg.KafkaPartition, msg.KafkaOffset)
if err = cc.retryManager.MarkSuccessful(ctx, msg); err != nil {
cc.logger.Errorf("error marking retried message as successful in the DB: %s", err)
}
}
}
}
func (cc *kafkaConsumerDbCollection) close() {
if cc.mainKafkaConsumer == nil {
return
}
if err := cc.mainKafkaConsumer.Close(); err != nil {
cc.logger.Errorf("error occurred closing the main Kafka consumer: %w", err)
}
cc.mainKafkaConsumer = nil
}
func (cc *kafkaConsumerDbCollection) setMaintenanceInterval(duration time.Duration) {
cc.maintenanceInterval = duration
}