-
Notifications
You must be signed in to change notification settings - Fork 2
/
kafka.go
195 lines (168 loc) · 4.96 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
// Copyright (C) 2016 Eneo Tecnologia S.L.
// Diego Fernández Barrera <bigomby@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"os"
"os/signal"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/IBM/sarama"
"github.com/redBorder/rbforwarder"
cluster "github.com/redBorder/sarama-cluster"
"github.com/sirupsen/logrus"
)
// KafkaConfig stores the configuration for the Kafka source
type KafkaConfig struct {
topics []string // Topics where listen for messages
brokers []string // Brokers to connect
consumergroup string // ID for the consumer
consumerGroupConfig *cluster.Config
deflate bool
}
// KafkaConsumer get messages from multiple kafka topics
type KafkaConsumer struct {
forwarder *rbforwarder.RBForwarder // The backend to send messages
consumer *cluster.Consumer
closed chan struct{}
reset chan struct{}
Config KafkaConfig // Cofiguration after the parsing
wg sync.WaitGroup
}
// Start starts reading messages from kafka and pushing them to the pipeline
func (k *KafkaConsumer) Start() {
var eventsReported uint64
var eventsSent uint64
var messages uint32
var err error
k.closed = make(chan struct{})
k.reset = make(chan struct{})
logger = Logger.WithFields(logrus.Fields{
"prefix": "k2http",
})
if *counter > 0 {
go func() {
for {
<-time.After(time.Duration(*counter) * time.Second)
logger.Infof("Messages per second: %d", atomic.LoadUint32(&messages)/uint32(*counter))
atomic.StoreUint32(&messages, 0)
}
}()
}
// Start processing reports
k.wg.Add(1)
go func() {
for r := range k.forwarder.GetOrderedReports() {
report := r.(rbforwarder.Report)
message := report.Opaque.(*sarama.ConsumerMessage)
if report.Code != 0 {
logger.
WithField("STATUS", report.Status).
WithField("OFFSET", message.Offset).
Error("REPORT")
}
k.consumer.MarkOffset(message, "")
eventsReported++
atomic.AddUint32(&messages, 1)
}
k.wg.Done()
}()
// Init consumer, consume errors & messages
consumerLoop:
for {
k.consumer, err = cluster.NewConsumer(
k.Config.brokers,
k.Config.consumergroup,
k.Config.topics,
k.Config.consumerGroupConfig,
)
k.Config.consumerGroupConfig.Consumer.Return.Errors = true
if err != nil {
logger.Fatal("Failed to start consumer: ", err)
break
}
logger.
WithField("brokers", k.Config.brokers).
WithField("consumergroup", k.Config.consumergroup).
WithField("topics", k.Config.topics).
Info("Started consumer")
go func() {
for err := range k.consumer.Errors() {
if strings.Contains(err.Error(), "offset is outside the range") {
logger.Infof("Resetting offsets due to out of range error")
k.reset <- struct{}{}
return
}
}
}()
for {
select {
case <-k.closed:
break consumerLoop
case <-k.reset:
logger.Infof("Reset signal received, closing consumer to reset offsets")
if err := k.consumer.Close(); err != nil {
logger.Error("Failed to close consumer: ", err)
}
k.Config.consumerGroupConfig.Consumer.Offsets.Initial = sarama.OffsetOldest
continue consumerLoop
case message := <-k.consumer.Messages():
if message == nil {
k.Config.consumerGroupConfig.Consumer.Offsets.Initial = sarama.OffsetOldest
if err := k.consumer.Close(); err != nil {
logger.Error("Failed to close consumer: ", err)
}
logger.Error("Message is nill")
break
}
opts := map[string]interface{}{
"http_endpoint": message.Topic,
"batch_group": message.Topic,
}
if k.Config.deflate {
opts["http_headers"] = map[string]string{
"Content-Encoding": "deflate",
}
}
k.forwarder.Produce(message.Value, opts, message)
eventsSent++
}
}
}
k.wg.Wait()
logger.Infof("TOTAL SENT MESSAGES: %d", eventsSent)
logger.Infof("TOTAL REPORTS: %d", eventsReported)
return
}
// Close closes the connection with Kafka
func (k *KafkaConsumer) Close() {
logger.Info("Terminating... Press ctrl+c again to force exit")
ctrlc := make(chan os.Signal, 1)
signal.Notify(ctrlc, os.Interrupt)
go func() {
<-ctrlc
logger.Fatal("Forced exit")
}()
k.closed <- struct{}{}
if err := k.consumer.Close(); err != nil {
logger.Println("Failed to close consumer: ", err)
} else {
logger.Info("Consumer terminated")
}
<-time.After(5 * time.Second)
k.forwarder.Close()
}