/
kafka_message_consumer.go
304 lines (236 loc) · 9.26 KB
/
kafka_message_consumer.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
package main
import (
"context"
"errors"
"os"
"os/signal"
"syscall"
"time"
"github.com/RedHatInsights/cloud-connector/internal/cloud_connector"
"github.com/RedHatInsights/cloud-connector/internal/config"
"github.com/RedHatInsights/cloud-connector/internal/connection_repository"
"github.com/RedHatInsights/cloud-connector/internal/controller"
"github.com/RedHatInsights/cloud-connector/internal/controller/api"
"github.com/RedHatInsights/cloud-connector/internal/mqtt"
"github.com/RedHatInsights/cloud-connector/internal/platform/db"
"github.com/RedHatInsights/cloud-connector/internal/platform/logger"
"github.com/RedHatInsights/cloud-connector/internal/platform/queue"
"github.com/RedHatInsights/cloud-connector/internal/platform/utils"
"github.com/RedHatInsights/cloud-connector/internal/platform/utils/tls_utils"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
kafka "github.com/segmentio/kafka-go"
"github.com/sirupsen/logrus"
)
func startKafkaMessageConsumer(mgmtAddr string) {
logger.Log.Info("Starting Cloud-Connector Kafka Message consumer")
cfg := config.GetConfig()
logger.Log.Info("Cloud-Connector configuration:\n", cfg)
database, err := db.InitializeDatabaseConnection(cfg)
if err != nil {
logger.LogFatalError("Unable to connect to database: ", err)
}
tlsConfigFuncs, err := buildBrokerTlsConfigFuncList(cfg)
if err != nil {
logger.LogFatalError("TLS configuration error for MQTT Broker connection", err)
}
tlsConfig, err := tls_utils.NewTlsConfig(tlsConfigFuncs...)
if err != nil {
logger.LogFatalError("Unable to configure TLS for MQTT Broker connection", err)
}
connectionRegistrar, err := connection_repository.NewSqlConnectionRegistrar(cfg, database)
if err != nil {
logger.LogFatalError("Failed to create SQL Connection Registrar", err)
}
accountResolver, err := controller.NewAccountIdResolver(cfg.ClientIdToAccountIdImpl, cfg)
if err != nil {
logger.LogFatalError("Failed to create Account ID Resolver", err)
}
connectedClientRecorder, err := controller.NewConnectedClientRecorder(cfg.ConnectedClientRecorderImpl, cfg)
if err != nil {
logger.LogFatalError("Failed to create Connected Client Recorder", err)
}
sourcesRecorder, err := controller.NewSourcesRecorder(cfg.SourcesRecorderImpl, cfg)
if err != nil {
logger.LogFatalError("Failed to create Sources Recorder", err)
}
mqttTopicBuilder := mqtt.NewTopicBuilder(cfg.MqttTopicPrefix)
mqttTopicVerifier := mqtt.NewTopicVerifier(cfg.MqttTopicPrefix)
kafkaReader, err := queue.StartConsumer(buildRhcMessageKafkaConsumerConfig(cfg))
if err != nil {
logger.LogFatalError("Unable to start kafka consumer", err)
}
brokerOptions, err := buildDefaultMqttBrokerConfigFuncList(cfg.MqttBrokerAddress, tlsConfig, cfg)
if err != nil {
logger.LogFatalError("Unable to configure MQTT Broker connection", err)
}
connectedChan := make(chan struct{})
brokerOptions = append(brokerOptions, mqtt.WithOnConnectHandler(notifyOnIntialMqttConnection(connectedChan)))
mqttConnectionFailedChan := make(chan error)
brokerOptions = buildOnConnectionLostMqttOptions(cfg, mqttConnectionFailedChan, brokerOptions)
mqttClient, err := mqtt.CreateBrokerConnection(cfg.MqttBrokerAddress, brokerOptions...)
if err != nil {
logger.LogFatalError("Unable to establish MQTT broker connection", err)
}
select {
case <-connectedChan:
logger.Log.Debug("Successfully connected to MQTT broker")
break
case <-time.After(2 * time.Second):
logger.Log.Fatal("Failed to connect to MQTT broker")
}
messageProcessor := handleMessage(
cfg,
mqttClient,
mqttTopicVerifier,
mqttTopicBuilder,
connectionRegistrar,
accountResolver,
connectedClientRecorder,
sourcesRecorder)
shutdownCtx, shutdownCtxCancel := context.WithCancel(context.Background())
// If the kafka consumer runs into a fatal error, notify the
// main thread so that it can shutdown the process
fatalProcessingError := make(chan struct{})
go consumeMqttMessagesFromKafka(kafkaReader, messageProcessor, shutdownCtx, fatalProcessingError)
apiMux := mux.NewRouter()
monitoringServer := api.NewMonitoringServer(apiMux, cfg)
monitoringServer.Routes()
apiSrv := utils.StartHTTPServer(mgmtAddr, "management", apiMux)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-signalChan:
logger.Log.Info("Received signal to shutdown: ", sig)
shutdownCtxCancel() // Notify the consumer to shutdown
case <-fatalProcessingError:
logger.Log.Info("Received a fatal processing error...shutting down!")
case err = <-mqttConnectionFailedChan:
logger.Log.Info("MQTT connection dropped: ", err)
shutdownCtxCancel() // Notify the consumer to shutdown
}
ctx, cancel := context.WithTimeout(context.Background(), cfg.HttpShutdownTimeout)
defer cancel()
utils.ShutdownHTTPServer(ctx, "management", apiSrv)
mqttClient.Disconnect(cfg.MqttDisconnectQuiesceTime)
logger.Log.Info("Cloud-Connector shutting down")
}
func getHeaderValueAsString(headers []kafka.Header, headerName string) string {
for _, header := range headers {
if header.Key == headerName {
return string(header.Value)
}
}
return ""
}
func handleMessage(cfg *config.Config, mqttClient MQTT.Client, topicVerifier *mqtt.TopicVerifier, topicBuilder *mqtt.TopicBuilder, connectionRegistrar connection_repository.ConnectionRegistrar, accountResolver controller.AccountIdResolver, connectedClientRecorder controller.ConnectedClientRecorder, sourcesRecorder controller.SourcesRecorder) func(*logrus.Entry, *kafka.Message) error {
controlMessageHandler := cloud_connector.HandleControlMessage(
cfg,
mqttClient,
topicBuilder,
connectionRegistrar,
accountResolver,
connectedClientRecorder,
sourcesRecorder)
return func(log *logrus.Entry, msg *kafka.Message) error {
logger.Log.Tracef("%% Message %s\n", string(msg.Value))
if msg.Headers == nil {
logger.Log.Debug("Unable to process message. Message does not have headers!")
return nil
}
topic := getHeaderValueAsString(msg.Headers, mqtt.TopicKafkaHeaderKey)
mqttMessageID := getHeaderValueAsString(msg.Headers, mqtt.MessageIDKafkaHeaderKey)
dateReceived := getHeaderValueAsString(msg.Headers, mqtt.DateReceivedHeaderKey)
log = log.WithFields(logrus.Fields{"mqtt_message_id": mqttMessageID,
"client_id": string(msg.Key),
"date_received": dateReceived})
log.Debug("Read message off of kafka topic")
if len(topic) == 0 {
log.Debug("Unable to process message. Message does not have topic header!")
return nil
}
payload := string(msg.Value)
log.Debugf("Received control message on topic: %s\nMessage: %s\n", topic, payload)
topicType, clientID, err := topicVerifier.VerifyIncomingTopic(topic)
if err != nil {
log.WithFields(logrus.Fields{"error": err}).Debug("Unable to process message. Unable to parse topic!")
return nil
}
if topicType != mqtt.ControlTopicType {
log.Debug("Invalid topic type read from kafka. Skipping message...")
return nil
}
return controlMessageHandler(mqttClient, clientID, payload)
}
}
func consumeMqttMessagesFromKafka(kafkaReader *kafka.Reader, process func(*logrus.Entry, *kafka.Message) error, ctx context.Context, fatalProcessingError chan struct{}) {
for {
m, err := kafkaReader.FetchMessage(ctx)
if err != nil {
if errors.Is(err, context.Canceled) != true {
logger.LogError("Failed to fetch message from kafka", err)
// Notify the main thread to shutdown
fatalProcessingError <- struct{}{}
}
break
}
log := logger.Log.WithFields(logrus.Fields{
"client_id": string(m.Key),
"partition": m.Partition,
"offset": m.Offset})
metrics.kafkaMessageReceivedCounter.Inc()
err = process(log, &m)
if err != nil {
logger.LogWithError(log, "Error handling message:", err)
// Notify the main thread to shutdown
fatalProcessingError <- struct{}{}
break
}
// explicitly commit the message
err = kafkaReader.CommitMessages(ctx, m)
if err != nil {
logger.LogWithError(log, "Failed to commit message to kafka", err)
// Notify the main thread to shutdown
fatalProcessingError <- struct{}{}
break
}
}
logger.Log.Infof("Stopped reading kafka messages")
if err := kafkaReader.Close(); err != nil {
logger.LogError("Failed to close kafka reader", err)
}
}
func buildRhcMessageKafkaConsumerConfig(cfg *config.Config) *queue.ConsumerConfig {
var kafkaSaslCfg *queue.SaslConfig
if cfg.KafkaSASLMechanism != "" {
kafkaSaslCfg = &queue.SaslConfig{
SaslMechanism: cfg.KafkaSASLMechanism,
SaslUsername: cfg.KafkaUsername,
SaslPassword: cfg.KafkaPassword,
KafkaCA: cfg.KafkaCA,
}
}
rhcMessageKafkaConsumer := queue.ConsumerConfig{
Brokers: cfg.RhcMessageKafkaBrokers,
SaslConfig: kafkaSaslCfg,
Topic: cfg.RhcMessageKafkaTopic,
GroupID: cfg.RhcMessageKafkaConsumerGroup,
}
return &rhcMessageKafkaConsumer
}
type mqttMetrics struct {
kafkaMessageReceivedCounter prometheus.Counter
}
func newMqttMetrics() *mqttMetrics {
metrics := new(mqttMetrics)
metrics.kafkaMessageReceivedCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "cloud_connector_kafka_message_received_count",
Help: "The number of kafka messages received",
})
return metrics
}
var (
metrics = newMqttMetrics()
)