Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 3 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 7 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 9 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 15 | */ |
Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame^] | 16 | //nolint:staticcheck |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 17 | package kafka |
| 18 | |
| 19 | import ( |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 20 | "context" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 21 | "errors" |
| 22 | "fmt" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 23 | "strings" |
| 24 | "sync" |
| 25 | "time" |
| 26 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 27 | "github.com/Shopify/sarama" |
| 28 | scc "github.com/bsm/sarama-cluster" |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 29 | "github.com/eapache/go-resiliency/breaker" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 30 | "github.com/golang/protobuf/proto" |
| 31 | "github.com/google/uuid" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 32 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 35 | // consumerChannels represents one or more consumers listening on a kafka topic. Once a message is received on that |
| 36 | // topic, the consumer(s) broadcasts the message to all the listening channels. The consumer can be a partition |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 37 | // consumer or a group consumer |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 38 | type consumerChannels struct { |
| 39 | consumers []interface{} |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 40 | channels []chan proto.Message |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Kent Hagerman | ccfa213 | 2019-12-17 13:29:34 -0500 | [diff] [blame] | 43 | // static check to ensure SaramaClient implements Client |
| 44 | var _ Client = &SaramaClient{} |
| 45 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 46 | // SaramaClient represents the messaging proxy |
| 47 | type SaramaClient struct { |
| 48 | cAdmin sarama.ClusterAdmin |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 49 | KafkaAddress string |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 50 | producer sarama.AsyncProducer |
| 51 | consumer sarama.Consumer |
| 52 | groupConsumers map[string]*scc.Consumer |
| 53 | lockOfGroupConsumers sync.RWMutex |
| 54 | consumerGroupPrefix string |
| 55 | consumerType int |
| 56 | consumerGroupName string |
| 57 | producerFlushFrequency int |
| 58 | producerFlushMessages int |
| 59 | producerFlushMaxmessages int |
| 60 | producerRetryMax int |
| 61 | producerRetryBackOff time.Duration |
| 62 | producerReturnSuccess bool |
| 63 | producerReturnErrors bool |
| 64 | consumerMaxwait int |
| 65 | maxProcessingTime int |
| 66 | numPartitions int |
| 67 | numReplicas int |
| 68 | autoCreateTopic bool |
| 69 | doneCh chan int |
Scott Baker | 84a55ce | 2020-04-17 10:11:30 -0700 | [diff] [blame] | 70 | metadataCallback func(fromTopic string, timestamp time.Time) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 71 | topicToConsumerChannelMap map[string]*consumerChannels |
| 72 | lockTopicToConsumerChannelMap sync.RWMutex |
| 73 | topicLockMap map[string]*sync.RWMutex |
| 74 | lockOfTopicLockMap sync.RWMutex |
| 75 | metadataMaxRetry int |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 76 | alive bool |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 77 | livenessMutex sync.Mutex |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 78 | liveness chan bool |
| 79 | livenessChannelInterval time.Duration |
| 80 | lastLivenessTime time.Time |
| 81 | started bool |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 82 | healthinessMutex sync.Mutex |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 83 | healthy bool |
| 84 | healthiness chan bool |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | type SaramaClientOption func(*SaramaClient) |
| 88 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 89 | func Address(address string) SaramaClientOption { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 90 | return func(args *SaramaClient) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 91 | args.KafkaAddress = address |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | func ConsumerGroupPrefix(prefix string) SaramaClientOption { |
| 96 | return func(args *SaramaClient) { |
| 97 | args.consumerGroupPrefix = prefix |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func ConsumerGroupName(name string) SaramaClientOption { |
| 102 | return func(args *SaramaClient) { |
| 103 | args.consumerGroupName = name |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func ConsumerType(consumer int) SaramaClientOption { |
| 108 | return func(args *SaramaClient) { |
| 109 | args.consumerType = consumer |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func ProducerFlushFrequency(frequency int) SaramaClientOption { |
| 114 | return func(args *SaramaClient) { |
| 115 | args.producerFlushFrequency = frequency |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func ProducerFlushMessages(num int) SaramaClientOption { |
| 120 | return func(args *SaramaClient) { |
| 121 | args.producerFlushMessages = num |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func ProducerFlushMaxMessages(num int) SaramaClientOption { |
| 126 | return func(args *SaramaClient) { |
| 127 | args.producerFlushMaxmessages = num |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func ProducerMaxRetries(num int) SaramaClientOption { |
| 132 | return func(args *SaramaClient) { |
| 133 | args.producerRetryMax = num |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func ProducerRetryBackoff(duration time.Duration) SaramaClientOption { |
| 138 | return func(args *SaramaClient) { |
| 139 | args.producerRetryBackOff = duration |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func ProducerReturnOnErrors(opt bool) SaramaClientOption { |
| 144 | return func(args *SaramaClient) { |
| 145 | args.producerReturnErrors = opt |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func ProducerReturnOnSuccess(opt bool) SaramaClientOption { |
| 150 | return func(args *SaramaClient) { |
| 151 | args.producerReturnSuccess = opt |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func ConsumerMaxWait(wait int) SaramaClientOption { |
| 156 | return func(args *SaramaClient) { |
| 157 | args.consumerMaxwait = wait |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func MaxProcessingTime(pTime int) SaramaClientOption { |
| 162 | return func(args *SaramaClient) { |
| 163 | args.maxProcessingTime = pTime |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func NumPartitions(number int) SaramaClientOption { |
| 168 | return func(args *SaramaClient) { |
| 169 | args.numPartitions = number |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func NumReplicas(number int) SaramaClientOption { |
| 174 | return func(args *SaramaClient) { |
| 175 | args.numReplicas = number |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func AutoCreateTopic(opt bool) SaramaClientOption { |
| 180 | return func(args *SaramaClient) { |
| 181 | args.autoCreateTopic = opt |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func MetadatMaxRetries(retry int) SaramaClientOption { |
| 186 | return func(args *SaramaClient) { |
| 187 | args.metadataMaxRetry = retry |
| 188 | } |
| 189 | } |
| 190 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 191 | func LivenessChannelInterval(opt time.Duration) SaramaClientOption { |
| 192 | return func(args *SaramaClient) { |
| 193 | args.livenessChannelInterval = opt |
| 194 | } |
| 195 | } |
| 196 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 197 | func NewSaramaClient(opts ...SaramaClientOption) *SaramaClient { |
| 198 | client := &SaramaClient{ |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 199 | KafkaAddress: DefaultKafkaAddress, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 200 | } |
| 201 | client.consumerType = DefaultConsumerType |
| 202 | client.producerFlushFrequency = DefaultProducerFlushFrequency |
| 203 | client.producerFlushMessages = DefaultProducerFlushMessages |
| 204 | client.producerFlushMaxmessages = DefaultProducerFlushMaxmessages |
| 205 | client.producerReturnErrors = DefaultProducerReturnErrors |
| 206 | client.producerReturnSuccess = DefaultProducerReturnSuccess |
| 207 | client.producerRetryMax = DefaultProducerRetryMax |
| 208 | client.producerRetryBackOff = DefaultProducerRetryBackoff |
| 209 | client.consumerMaxwait = DefaultConsumerMaxwait |
| 210 | client.maxProcessingTime = DefaultMaxProcessingTime |
| 211 | client.numPartitions = DefaultNumberPartitions |
| 212 | client.numReplicas = DefaultNumberReplicas |
| 213 | client.autoCreateTopic = DefaultAutoCreateTopic |
| 214 | client.metadataMaxRetry = DefaultMetadataMaxRetry |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 215 | client.livenessChannelInterval = DefaultLivenessChannelInterval |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 216 | |
| 217 | for _, option := range opts { |
| 218 | option(client) |
| 219 | } |
| 220 | |
| 221 | client.groupConsumers = make(map[string]*scc.Consumer) |
| 222 | |
| 223 | client.lockTopicToConsumerChannelMap = sync.RWMutex{} |
| 224 | client.topicLockMap = make(map[string]*sync.RWMutex) |
| 225 | client.lockOfTopicLockMap = sync.RWMutex{} |
| 226 | client.lockOfGroupConsumers = sync.RWMutex{} |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 227 | |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 228 | // healthy and alive until proven otherwise |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 229 | client.alive = true |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 230 | client.healthy = true |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 231 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 232 | return client |
| 233 | } |
| 234 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 235 | func (sc *SaramaClient) Start(ctx context.Context) error { |
| 236 | logger.Info(ctx, "Starting-kafka-sarama-client") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 237 | |
| 238 | // Create the Done channel |
| 239 | sc.doneCh = make(chan int, 1) |
| 240 | |
| 241 | var err error |
| 242 | |
| 243 | // Add a cleanup in case of failure to startup |
| 244 | defer func() { |
| 245 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 246 | sc.Stop(ctx) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 247 | } |
| 248 | }() |
| 249 | |
| 250 | // Create the Cluster Admin |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 251 | if err = sc.createClusterAdmin(ctx); err != nil { |
| 252 | logger.Errorw(ctx, "Cannot-create-cluster-admin", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 253 | return err |
| 254 | } |
| 255 | |
| 256 | // Create the Publisher |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 257 | if err := sc.createPublisher(ctx); err != nil { |
| 258 | logger.Errorw(ctx, "Cannot-create-kafka-publisher", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 259 | return err |
| 260 | } |
| 261 | |
| 262 | if sc.consumerType == DefaultConsumerType { |
| 263 | // Create the master consumers |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 264 | if err := sc.createConsumer(ctx); err != nil { |
| 265 | logger.Errorw(ctx, "Cannot-create-kafka-consumers", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 266 | return err |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Create the topic to consumers/channel map |
| 271 | sc.topicToConsumerChannelMap = make(map[string]*consumerChannels) |
| 272 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 273 | logger.Info(ctx, "kafka-sarama-client-started") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 274 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 275 | sc.started = true |
| 276 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 277 | return nil |
| 278 | } |
| 279 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 280 | func (sc *SaramaClient) Stop(ctx context.Context) { |
| 281 | logger.Info(ctx, "stopping-sarama-client") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 282 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 283 | sc.started = false |
| 284 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 285 | //Send a message over the done channel to close all long running routines |
| 286 | sc.doneCh <- 1 |
| 287 | |
| 288 | if sc.producer != nil { |
| 289 | if err := sc.producer.Close(); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 290 | logger.Errorw(ctx, "closing-producer-failed", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | if sc.consumer != nil { |
| 295 | if err := sc.consumer.Close(); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 296 | logger.Errorw(ctx, "closing-partition-consumer-failed", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | for key, val := range sc.groupConsumers { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 301 | logger.Debugw(ctx, "closing-group-consumer", log.Fields{"topic": key}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 302 | if err := val.Close(); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 303 | logger.Errorw(ctx, "closing-group-consumer-failed", log.Fields{"error": err, "topic": key}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | if sc.cAdmin != nil { |
| 308 | if err := sc.cAdmin.Close(); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 309 | logger.Errorw(ctx, "closing-cluster-admin-failed", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | //TODO: Clear the consumers map |
| 314 | //sc.clearConsumerChannelMap() |
| 315 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 316 | logger.Info(ctx, "sarama-client-stopped") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 319 | // createTopic is an internal function to create a topic on the Kafka Broker. No locking is required as |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 320 | // the invoking function must hold the lock |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 321 | func (sc *SaramaClient) createTopic(ctx context.Context, topic *Topic, numPartition int, repFactor int) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 322 | // Set the topic details |
| 323 | topicDetail := &sarama.TopicDetail{} |
| 324 | topicDetail.NumPartitions = int32(numPartition) |
| 325 | topicDetail.ReplicationFactor = int16(repFactor) |
| 326 | topicDetail.ConfigEntries = make(map[string]*string) |
| 327 | topicDetails := make(map[string]*sarama.TopicDetail) |
| 328 | topicDetails[topic.Name] = topicDetail |
| 329 | |
| 330 | if err := sc.cAdmin.CreateTopic(topic.Name, topicDetail, false); err != nil { |
kesavand | d85e52b | 2022-03-15 16:38:08 +0530 | [diff] [blame] | 331 | switch typedErr := err.(type) { |
| 332 | case *sarama.TopicError: |
| 333 | if typedErr.Err == sarama.ErrTopicAlreadyExists { |
| 334 | err = nil |
| 335 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 336 | } |
kesavand | d85e52b | 2022-03-15 16:38:08 +0530 | [diff] [blame] | 337 | if err != nil { |
| 338 | logger.Errorw(ctx, "create-topic-failure", log.Fields{"error": err}) |
| 339 | return err |
| 340 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 341 | } |
| 342 | // TODO: Wait until the topic has been created. No API is available in the Sarama clusterAdmin to |
| 343 | // do so. |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 344 | logger.Debugw(ctx, "topic-created", log.Fields{"topic": topic, "numPartition": numPartition, "replicationFactor": repFactor}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 345 | return nil |
| 346 | } |
| 347 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 348 | // CreateTopic is a public API to create a topic on the Kafka Broker. It uses a lock on a specific topic to |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 349 | // ensure no two go routines are performing operations on the same topic |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 350 | func (sc *SaramaClient) CreateTopic(ctx context.Context, topic *Topic, numPartition int, repFactor int) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 351 | sc.lockTopic(topic) |
| 352 | defer sc.unLockTopic(topic) |
| 353 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 354 | return sc.createTopic(ctx, topic, numPartition, repFactor) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 357 | // DeleteTopic removes a topic from the kafka Broker |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 358 | func (sc *SaramaClient) DeleteTopic(ctx context.Context, topic *Topic) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 359 | sc.lockTopic(topic) |
| 360 | defer sc.unLockTopic(topic) |
| 361 | |
| 362 | // Remove the topic from the broker |
| 363 | if err := sc.cAdmin.DeleteTopic(topic.Name); err != nil { |
| 364 | if err == sarama.ErrUnknownTopicOrPartition { |
| 365 | // Not an error as does not exist |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 366 | logger.Debugw(ctx, "topic-not-exist", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 367 | return nil |
| 368 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 369 | logger.Errorw(ctx, "delete-topic-failed", log.Fields{"topic": topic, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 370 | return err |
| 371 | } |
| 372 | |
| 373 | // Clear the topic from the consumer channel. This will also close any consumers listening on that topic. |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 374 | if err := sc.clearTopicFromConsumerChannelMap(ctx, *topic); err != nil { |
| 375 | logger.Errorw(ctx, "failure-clearing-channels", log.Fields{"topic": topic, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 376 | return err |
| 377 | } |
| 378 | return nil |
| 379 | } |
| 380 | |
| 381 | // Subscribe registers a caller to a topic. It returns a channel that the caller can use to receive |
| 382 | // messages from that topic |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 383 | func (sc *SaramaClient) Subscribe(ctx context.Context, topic *Topic, kvArgs ...*KVArg) (<-chan proto.Message, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 384 | sc.lockTopic(topic) |
| 385 | defer sc.unLockTopic(topic) |
| 386 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 387 | logger.Debugw(ctx, "subscribe", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 388 | |
| 389 | // If a consumers already exist for that topic then resuse it |
| 390 | if consumerCh := sc.getConsumerChannel(topic); consumerCh != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 391 | logger.Debugw(ctx, "topic-already-subscribed", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 392 | // Create a channel specific for that consumers and add it to the consumers channel map |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 393 | ch := make(chan proto.Message) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 394 | sc.addChannelToConsumerChannelMap(ctx, topic, ch) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 395 | return ch, nil |
| 396 | } |
| 397 | |
| 398 | // Register for the topic and set it up |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 399 | var consumerListeningChannel chan proto.Message |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 400 | var err error |
| 401 | |
| 402 | // Use the consumerType option to figure out the type of consumer to launch |
| 403 | if sc.consumerType == PartitionConsumer { |
| 404 | if sc.autoCreateTopic { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 405 | if err = sc.createTopic(ctx, topic, sc.numPartitions, sc.numReplicas); err != nil { |
| 406 | logger.Errorw(ctx, "create-topic-failure", log.Fields{"error": err, "topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 407 | return nil, err |
| 408 | } |
| 409 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 410 | if consumerListeningChannel, err = sc.setupPartitionConsumerChannel(ctx, topic, getOffset(kvArgs...)); err != nil { |
| 411 | logger.Warnw(ctx, "create-consumers-channel-failure", log.Fields{"error": err, "topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 412 | return nil, err |
| 413 | } |
| 414 | } else if sc.consumerType == GroupCustomer { |
| 415 | // TODO: create topic if auto create is on. There is an issue with the sarama cluster library that |
| 416 | // does not consume from a precreated topic in some scenarios |
| 417 | //if sc.autoCreateTopic { |
| 418 | // if err = sc.createTopic(topic, sc.numPartitions, sc.numReplicas); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 419 | // logger.Errorw(ctx, "create-topic-failure", logger.Fields{"error": err, "topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 420 | // return nil, err |
| 421 | // } |
| 422 | //} |
| 423 | //groupId := sc.consumerGroupName |
| 424 | groupId := getGroupId(kvArgs...) |
| 425 | // Include the group prefix |
| 426 | if groupId != "" { |
| 427 | groupId = sc.consumerGroupPrefix + groupId |
| 428 | } else { |
| 429 | // Need to use a unique group Id per topic |
| 430 | groupId = sc.consumerGroupPrefix + topic.Name |
| 431 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 432 | if consumerListeningChannel, err = sc.setupGroupConsumerChannel(ctx, topic, groupId, getOffset(kvArgs...)); err != nil { |
| 433 | logger.Warnw(ctx, "create-consumers-channel-failure", log.Fields{"error": err, "topic": topic.Name, "groupId": groupId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 434 | return nil, err |
| 435 | } |
| 436 | |
| 437 | } else { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 438 | logger.Warnw(ctx, "unknown-consumer-type", log.Fields{"consumer-type": sc.consumerType}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 439 | return nil, errors.New("unknown-consumer-type") |
| 440 | } |
| 441 | |
| 442 | return consumerListeningChannel, nil |
| 443 | } |
| 444 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 445 | // UnSubscribe unsubscribe a consumer from a given topic |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 446 | func (sc *SaramaClient) UnSubscribe(ctx context.Context, topic *Topic, ch <-chan proto.Message) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 447 | sc.lockTopic(topic) |
| 448 | defer sc.unLockTopic(topic) |
| 449 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 450 | logger.Debugw(ctx, "unsubscribing-channel-from-topic", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 451 | var err error |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 452 | if err = sc.removeChannelFromConsumerChannelMap(ctx, *topic, ch); err != nil { |
| 453 | logger.Errorw(ctx, "failed-removing-channel", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 454 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 455 | if err = sc.deleteFromGroupConsumers(ctx, topic.Name); err != nil { |
| 456 | logger.Errorw(ctx, "failed-deleting-group-consumer", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 457 | } |
| 458 | return err |
| 459 | } |
| 460 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 461 | func (sc *SaramaClient) SubscribeForMetadata(ctx context.Context, callback func(fromTopic string, timestamp time.Time)) { |
Kent Hagerman | ccfa213 | 2019-12-17 13:29:34 -0500 | [diff] [blame] | 462 | sc.metadataCallback = callback |
| 463 | } |
| 464 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 465 | func (sc *SaramaClient) updateLiveness(ctx context.Context, alive bool) { |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 466 | // Post a consistent stream of liveness data to the channel, |
| 467 | // so that in a live state, the core does not timeout and |
| 468 | // send a forced liveness message. Production of liveness |
| 469 | // events to the channel is rate-limited by livenessChannelInterval. |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 470 | sc.livenessMutex.Lock() |
| 471 | defer sc.livenessMutex.Unlock() |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 472 | if sc.liveness != nil { |
| 473 | if sc.alive != alive { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 474 | logger.Info(ctx, "update-liveness-channel-because-change") |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 475 | sc.liveness <- alive |
| 476 | sc.lastLivenessTime = time.Now() |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 477 | } else if time.Since(sc.lastLivenessTime) > sc.livenessChannelInterval { |
Akash Soni | 7b911a0 | 2024-06-20 13:06:21 +0530 | [diff] [blame] | 478 | logger.Debugf(ctx, "update-liveness-channel-because-interval") |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 479 | sc.liveness <- alive |
| 480 | sc.lastLivenessTime = time.Now() |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // Only emit a log message when the state changes |
| 485 | if sc.alive != alive { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 486 | logger.Info(ctx, "set-client-alive", log.Fields{"alive": alive}) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 487 | sc.alive = alive |
| 488 | } |
| 489 | } |
| 490 | |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 491 | // Once unhealthy, we never go back |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 492 | func (sc *SaramaClient) setUnhealthy(ctx context.Context) { |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 493 | sc.healthy = false |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 494 | sc.healthinessMutex.Lock() |
| 495 | defer sc.healthinessMutex.Unlock() |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 496 | if sc.healthiness != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 497 | logger.Infow(ctx, "set-client-unhealthy", log.Fields{"healthy": sc.healthy}) |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 498 | sc.healthiness <- sc.healthy |
| 499 | } |
| 500 | } |
| 501 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 502 | func (sc *SaramaClient) isLivenessError(ctx context.Context, err error) bool { |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 503 | // Sarama producers and consumers encapsulate the error inside |
| 504 | // a ProducerError or ConsumerError struct. |
| 505 | if prodError, ok := err.(*sarama.ProducerError); ok { |
| 506 | err = prodError.Err |
| 507 | } else if consumerError, ok := err.(*sarama.ConsumerError); ok { |
| 508 | err = consumerError.Err |
| 509 | } |
| 510 | |
| 511 | // Sarama-Cluster will compose the error into a ClusterError struct, |
| 512 | // which we can't do a compare by reference. To handle that, we the |
| 513 | // best we can do is compare the error strings. |
| 514 | |
| 515 | switch err.Error() { |
| 516 | case context.DeadlineExceeded.Error(): |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 517 | logger.Info(ctx, "is-liveness-error-timeout") |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 518 | return true |
| 519 | case sarama.ErrOutOfBrokers.Error(): // "Kafka: client has run out of available brokers" |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 520 | logger.Info(ctx, "is-liveness-error-no-brokers") |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 521 | return true |
| 522 | case sarama.ErrShuttingDown.Error(): // "Kafka: message received by producer in process of shutting down" |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 523 | logger.Info(ctx, "is-liveness-error-shutting-down") |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 524 | return true |
| 525 | case sarama.ErrControllerNotAvailable.Error(): // "Kafka: controller is not available" |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 526 | logger.Info(ctx, "is-liveness-error-not-available") |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 527 | return true |
| 528 | case breaker.ErrBreakerOpen.Error(): // "circuit breaker is open" |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 529 | logger.Info(ctx, "is-liveness-error-circuit-breaker-open") |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 530 | return true |
| 531 | } |
| 532 | |
| 533 | if strings.HasSuffix(err.Error(), "connection refused") { // "dial tcp 10.244.1.176:9092: connect: connection refused" |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 534 | logger.Info(ctx, "is-liveness-error-connection-refused") |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 535 | return true |
| 536 | } |
| 537 | |
Scott Baker | 718bee0 | 2020-01-07 09:52:02 -0800 | [diff] [blame] | 538 | if strings.HasSuffix(err.Error(), "i/o timeout") { // "dial tcp 10.244.1.176:9092: i/o timeout" |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 539 | logger.Info(ctx, "is-liveness-error-io-timeout") |
Scott Baker | 718bee0 | 2020-01-07 09:52:02 -0800 | [diff] [blame] | 540 | return true |
| 541 | } |
| 542 | |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 543 | // Other errors shouldn't trigger a loss of liveness |
| 544 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 545 | logger.Infow(ctx, "is-liveness-error-ignored", log.Fields{"err": err}) |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 546 | |
| 547 | return false |
| 548 | } |
| 549 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 550 | // send formats and sends the request onto the kafka messaging bus. |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 551 | func (sc *SaramaClient) Send(ctx context.Context, msg interface{}, topic *Topic, keys ...string) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 552 | |
| 553 | // Assert message is a proto message |
| 554 | var protoMsg proto.Message |
| 555 | var ok bool |
| 556 | // ascertain the value interface type is a proto.Message |
| 557 | if protoMsg, ok = msg.(proto.Message); !ok { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 558 | logger.Warnw(ctx, "message-not-proto-message", log.Fields{"msg": msg}) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 559 | return fmt.Errorf("not-a-proto-msg-%s", msg) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | var marshalled []byte |
| 563 | var err error |
| 564 | // Create the Sarama producer message |
| 565 | if marshalled, err = proto.Marshal(protoMsg); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 566 | logger.Errorw(ctx, "marshalling-failed", log.Fields{"msg": protoMsg, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 567 | return err |
| 568 | } |
| 569 | key := "" |
| 570 | if len(keys) > 0 { |
| 571 | key = keys[0] // Only the first key is relevant |
| 572 | } |
| 573 | kafkaMsg := &sarama.ProducerMessage{ |
| 574 | Topic: topic.Name, |
| 575 | Key: sarama.StringEncoder(key), |
| 576 | Value: sarama.ByteEncoder(marshalled), |
| 577 | } |
| 578 | |
| 579 | // Send message to kafka |
| 580 | sc.producer.Input() <- kafkaMsg |
| 581 | // Wait for result |
| 582 | // TODO: Use a lock or a different mechanism to ensure the response received corresponds to the message sent. |
| 583 | select { |
| 584 | case ok := <-sc.producer.Successes(): |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 585 | logger.Debugw(ctx, "message-sent", log.Fields{"status": ok.Topic}) |
| 586 | sc.updateLiveness(ctx, true) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 587 | case notOk := <-sc.producer.Errors(): |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 588 | logger.Debugw(ctx, "error-sending", log.Fields{"status": notOk}) |
| 589 | if sc.isLivenessError(ctx, notOk) { |
| 590 | sc.updateLiveness(ctx, false) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 591 | } |
| 592 | return notOk |
| 593 | } |
| 594 | return nil |
| 595 | } |
| 596 | |
| 597 | // Enable the liveness monitor channel. This channel will report |
| 598 | // a "true" or "false" on every publish, which indicates whether |
| 599 | // or not the channel is still live. This channel is then picked up |
| 600 | // by the service (i.e. rw_core / ro_core) to update readiness status |
| 601 | // and/or take other actions. |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 602 | func (sc *SaramaClient) EnableLivenessChannel(ctx context.Context, enable bool) chan bool { |
| 603 | logger.Infow(ctx, "kafka-enable-liveness-channel", log.Fields{"enable": enable}) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 604 | if enable { |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 605 | sc.livenessMutex.Lock() |
| 606 | defer sc.livenessMutex.Unlock() |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 607 | if sc.liveness == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 608 | logger.Info(ctx, "kafka-create-liveness-channel") |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 609 | // At least 1, so we can immediately post to it without blocking |
| 610 | // Setting a bigger number (10) allows the monitor to fall behind |
| 611 | // without blocking others. The monitor shouldn't really fall |
| 612 | // behind... |
| 613 | sc.liveness = make(chan bool, 10) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 614 | // post initial state to the channel |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 615 | sc.liveness <- sc.alive |
| 616 | } |
| 617 | } else { |
| 618 | // TODO: Think about whether we need the ability to turn off |
| 619 | // liveness monitoring |
| 620 | panic("Turning off liveness reporting is not supported") |
| 621 | } |
| 622 | return sc.liveness |
| 623 | } |
| 624 | |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 625 | // Enable the Healthiness monitor channel. This channel will report "false" |
| 626 | // if the kafka consumers die, or some other problem occurs which is |
| 627 | // catastrophic that would require re-creating the client. |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 628 | func (sc *SaramaClient) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool { |
| 629 | logger.Infow(ctx, "kafka-enable-healthiness-channel", log.Fields{"enable": enable}) |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 630 | if enable { |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 631 | sc.healthinessMutex.Lock() |
| 632 | defer sc.healthinessMutex.Unlock() |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 633 | if sc.healthiness == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 634 | logger.Info(ctx, "kafka-create-healthiness-channel") |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 635 | // At least 1, so we can immediately post to it without blocking |
| 636 | // Setting a bigger number (10) allows the monitor to fall behind |
| 637 | // without blocking others. The monitor shouldn't really fall |
| 638 | // behind... |
| 639 | sc.healthiness = make(chan bool, 10) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 640 | // post initial state to the channel |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 641 | sc.healthiness <- sc.healthy |
| 642 | } |
| 643 | } else { |
| 644 | // TODO: Think about whether we need the ability to turn off |
| 645 | // liveness monitoring |
| 646 | panic("Turning off healthiness reporting is not supported") |
| 647 | } |
| 648 | return sc.healthiness |
| 649 | } |
| 650 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 651 | // send an empty message on the liveness channel to check whether connectivity has |
| 652 | // been restored. |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 653 | func (sc *SaramaClient) SendLiveness(ctx context.Context) error { |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 654 | if !sc.started { |
| 655 | return fmt.Errorf("SendLiveness() called while not started") |
| 656 | } |
| 657 | |
| 658 | kafkaMsg := &sarama.ProducerMessage{ |
| 659 | Topic: "_liveness_test", |
| 660 | Value: sarama.StringEncoder(time.Now().Format(time.RFC3339)), // for debugging / informative use |
| 661 | } |
| 662 | |
| 663 | // Send message to kafka |
| 664 | sc.producer.Input() <- kafkaMsg |
| 665 | // Wait for result |
| 666 | // TODO: Use a lock or a different mechanism to ensure the response received corresponds to the message sent. |
| 667 | select { |
| 668 | case ok := <-sc.producer.Successes(): |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 669 | logger.Debugw(ctx, "liveness-message-sent", log.Fields{"status": ok.Topic}) |
| 670 | sc.updateLiveness(ctx, true) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 671 | case notOk := <-sc.producer.Errors(): |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 672 | logger.Debugw(ctx, "liveness-error-sending", log.Fields{"status": notOk}) |
| 673 | if sc.isLivenessError(ctx, notOk) { |
| 674 | sc.updateLiveness(ctx, false) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 675 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 676 | return notOk |
| 677 | } |
| 678 | return nil |
| 679 | } |
| 680 | |
| 681 | // getGroupId returns the group id from the key-value args. |
| 682 | func getGroupId(kvArgs ...*KVArg) string { |
| 683 | for _, arg := range kvArgs { |
| 684 | if arg.Key == GroupIdKey { |
| 685 | return arg.Value.(string) |
| 686 | } |
| 687 | } |
| 688 | return "" |
| 689 | } |
| 690 | |
| 691 | // getOffset returns the offset from the key-value args. |
| 692 | func getOffset(kvArgs ...*KVArg) int64 { |
| 693 | for _, arg := range kvArgs { |
| 694 | if arg.Key == Offset { |
| 695 | return arg.Value.(int64) |
| 696 | } |
| 697 | } |
| 698 | return sarama.OffsetNewest |
| 699 | } |
| 700 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 701 | func (sc *SaramaClient) createClusterAdmin(ctx context.Context) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 702 | config := sarama.NewConfig() |
| 703 | config.Version = sarama.V1_0_0_0 |
| 704 | |
| 705 | // Create a cluster Admin |
| 706 | var cAdmin sarama.ClusterAdmin |
| 707 | var err error |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 708 | if cAdmin, err = sarama.NewClusterAdmin([]string{sc.KafkaAddress}, config); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 709 | logger.Errorw(ctx, "cluster-admin-failure", log.Fields{"error": err, "broker-address": sc.KafkaAddress}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 710 | return err |
| 711 | } |
| 712 | sc.cAdmin = cAdmin |
| 713 | return nil |
| 714 | } |
| 715 | |
| 716 | func (sc *SaramaClient) lockTopic(topic *Topic) { |
| 717 | sc.lockOfTopicLockMap.Lock() |
| 718 | if _, exist := sc.topicLockMap[topic.Name]; exist { |
| 719 | sc.lockOfTopicLockMap.Unlock() |
| 720 | sc.topicLockMap[topic.Name].Lock() |
| 721 | } else { |
| 722 | sc.topicLockMap[topic.Name] = &sync.RWMutex{} |
| 723 | sc.lockOfTopicLockMap.Unlock() |
| 724 | sc.topicLockMap[topic.Name].Lock() |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | func (sc *SaramaClient) unLockTopic(topic *Topic) { |
| 729 | sc.lockOfTopicLockMap.Lock() |
| 730 | defer sc.lockOfTopicLockMap.Unlock() |
| 731 | if _, exist := sc.topicLockMap[topic.Name]; exist { |
| 732 | sc.topicLockMap[topic.Name].Unlock() |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | func (sc *SaramaClient) addTopicToConsumerChannelMap(id string, arg *consumerChannels) { |
| 737 | sc.lockTopicToConsumerChannelMap.Lock() |
| 738 | defer sc.lockTopicToConsumerChannelMap.Unlock() |
| 739 | if _, exist := sc.topicToConsumerChannelMap[id]; !exist { |
| 740 | sc.topicToConsumerChannelMap[id] = arg |
| 741 | } |
| 742 | } |
| 743 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 744 | func (sc *SaramaClient) getConsumerChannel(topic *Topic) *consumerChannels { |
| 745 | sc.lockTopicToConsumerChannelMap.RLock() |
| 746 | defer sc.lockTopicToConsumerChannelMap.RUnlock() |
| 747 | |
| 748 | if consumerCh, exist := sc.topicToConsumerChannelMap[topic.Name]; exist { |
| 749 | return consumerCh |
| 750 | } |
| 751 | return nil |
| 752 | } |
| 753 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 754 | func (sc *SaramaClient) addChannelToConsumerChannelMap(ctx context.Context, topic *Topic, ch chan proto.Message) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 755 | sc.lockTopicToConsumerChannelMap.Lock() |
| 756 | defer sc.lockTopicToConsumerChannelMap.Unlock() |
| 757 | if consumerCh, exist := sc.topicToConsumerChannelMap[topic.Name]; exist { |
| 758 | consumerCh.channels = append(consumerCh.channels, ch) |
| 759 | return |
| 760 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 761 | logger.Warnw(ctx, "consumers-channel-not-exist", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 764 | // closeConsumers closes a list of sarama consumers. The consumers can either be a partition consumers or a group consumers |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 765 | func closeConsumers(ctx context.Context, consumers []interface{}) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 766 | var err error |
| 767 | for _, consumer := range consumers { |
| 768 | // Is it a partition consumers? |
| 769 | if partionConsumer, ok := consumer.(sarama.PartitionConsumer); ok { |
| 770 | if errTemp := partionConsumer.Close(); errTemp != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 771 | logger.Debugw(ctx, "partition!!!", log.Fields{"err": errTemp}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 772 | if strings.Compare(errTemp.Error(), sarama.ErrUnknownTopicOrPartition.Error()) == 0 { |
| 773 | // This can occur on race condition |
| 774 | err = nil |
| 775 | } else { |
| 776 | err = errTemp |
| 777 | } |
| 778 | } |
| 779 | } else if groupConsumer, ok := consumer.(*scc.Consumer); ok { |
| 780 | if errTemp := groupConsumer.Close(); errTemp != nil { |
| 781 | if strings.Compare(errTemp.Error(), sarama.ErrUnknownTopicOrPartition.Error()) == 0 { |
| 782 | // This can occur on race condition |
| 783 | err = nil |
| 784 | } else { |
| 785 | err = errTemp |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | return err |
| 791 | } |
| 792 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 793 | func (sc *SaramaClient) removeChannelFromConsumerChannelMap(ctx context.Context, topic Topic, ch <-chan proto.Message) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 794 | sc.lockTopicToConsumerChannelMap.Lock() |
| 795 | defer sc.lockTopicToConsumerChannelMap.Unlock() |
| 796 | if consumerCh, exist := sc.topicToConsumerChannelMap[topic.Name]; exist { |
| 797 | // Channel will be closed in the removeChannel method |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 798 | consumerCh.channels = removeChannel(ctx, consumerCh.channels, ch) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 799 | // If there are no more channels then we can close the consumers itself |
| 800 | if len(consumerCh.channels) == 0 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 801 | logger.Debugw(ctx, "closing-consumers", log.Fields{"topic": topic}) |
| 802 | err := closeConsumers(ctx, consumerCh.consumers) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 803 | //err := consumerCh.consumers.Close() |
| 804 | delete(sc.topicToConsumerChannelMap, topic.Name) |
| 805 | return err |
| 806 | } |
| 807 | return nil |
| 808 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 809 | logger.Warnw(ctx, "topic-does-not-exist", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 810 | return errors.New("topic-does-not-exist") |
| 811 | } |
| 812 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 813 | func (sc *SaramaClient) clearTopicFromConsumerChannelMap(ctx context.Context, topic Topic) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 814 | sc.lockTopicToConsumerChannelMap.Lock() |
| 815 | defer sc.lockTopicToConsumerChannelMap.Unlock() |
| 816 | if consumerCh, exist := sc.topicToConsumerChannelMap[topic.Name]; exist { |
| 817 | for _, ch := range consumerCh.channels { |
| 818 | // Channel will be closed in the removeChannel method |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 819 | removeChannel(ctx, consumerCh.channels, ch) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 820 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 821 | err := closeConsumers(ctx, consumerCh.consumers) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 822 | //if err == sarama.ErrUnknownTopicOrPartition { |
| 823 | // // Not an error |
| 824 | // err = nil |
| 825 | //} |
| 826 | //err := consumerCh.consumers.Close() |
| 827 | delete(sc.topicToConsumerChannelMap, topic.Name) |
| 828 | return err |
| 829 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 830 | logger.Debugw(ctx, "topic-does-not-exist", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 831 | return nil |
| 832 | } |
| 833 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 834 | // createPublisher creates the publisher which is used to send a message onto kafka |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 835 | func (sc *SaramaClient) createPublisher(ctx context.Context) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 836 | // This Creates the publisher |
| 837 | config := sarama.NewConfig() |
Himani Chawla | f87a6a9 | 2021-04-01 17:44:16 +0530 | [diff] [blame] | 838 | config.Version = sarama.V1_0_0_0 |
kesavand | d85e52b | 2022-03-15 16:38:08 +0530 | [diff] [blame] | 839 | config.Producer.Partitioner = sarama.NewHashPartitioner |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 840 | config.Producer.Flush.Frequency = time.Duration(sc.producerFlushFrequency) |
| 841 | config.Producer.Flush.Messages = sc.producerFlushMessages |
| 842 | config.Producer.Flush.MaxMessages = sc.producerFlushMaxmessages |
| 843 | config.Producer.Return.Errors = sc.producerReturnErrors |
| 844 | config.Producer.Return.Successes = sc.producerReturnSuccess |
| 845 | //config.Producer.RequiredAcks = sarama.WaitForAll |
| 846 | config.Producer.RequiredAcks = sarama.WaitForLocal |
| 847 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 848 | brokers := []string{sc.KafkaAddress} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 849 | |
| 850 | if producer, err := sarama.NewAsyncProducer(brokers, config); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 851 | logger.Errorw(ctx, "error-starting-publisher", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 852 | return err |
| 853 | } else { |
| 854 | sc.producer = producer |
| 855 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 856 | logger.Info(ctx, "Kafka-publisher-created") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 857 | return nil |
| 858 | } |
| 859 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 860 | func (sc *SaramaClient) createConsumer(ctx context.Context) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 861 | config := sarama.NewConfig() |
Himani Chawla | f87a6a9 | 2021-04-01 17:44:16 +0530 | [diff] [blame] | 862 | config.Version = sarama.V1_0_0_0 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 863 | config.Consumer.Return.Errors = true |
| 864 | config.Consumer.Fetch.Min = 1 |
| 865 | config.Consumer.MaxWaitTime = time.Duration(sc.consumerMaxwait) * time.Millisecond |
| 866 | config.Consumer.MaxProcessingTime = time.Duration(sc.maxProcessingTime) * time.Millisecond |
| 867 | config.Consumer.Offsets.Initial = sarama.OffsetNewest |
| 868 | config.Metadata.Retry.Max = sc.metadataMaxRetry |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 869 | brokers := []string{sc.KafkaAddress} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 870 | |
| 871 | if consumer, err := sarama.NewConsumer(brokers, config); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 872 | logger.Errorw(ctx, "error-starting-consumers", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 873 | return err |
| 874 | } else { |
| 875 | sc.consumer = consumer |
| 876 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 877 | logger.Info(ctx, "Kafka-consumers-created") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 878 | return nil |
| 879 | } |
| 880 | |
| 881 | // createGroupConsumer creates a consumers group |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 882 | func (sc *SaramaClient) createGroupConsumer(ctx context.Context, topic *Topic, groupId string, initialOffset int64, retries int) (*scc.Consumer, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 883 | config := scc.NewConfig() |
Himani Chawla | f87a6a9 | 2021-04-01 17:44:16 +0530 | [diff] [blame] | 884 | config.Version = sarama.V1_0_0_0 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 885 | config.ClientID = uuid.New().String() |
| 886 | config.Group.Mode = scc.ConsumerModeMultiplex |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 887 | config.Consumer.Group.Heartbeat.Interval, _ = time.ParseDuration("1s") |
| 888 | config.Consumer.Return.Errors = true |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 889 | //config.Group.Return.Notifications = false |
| 890 | //config.Consumer.MaxWaitTime = time.Duration(DefaultConsumerMaxwait) * time.Millisecond |
| 891 | //config.Consumer.MaxProcessingTime = time.Duration(DefaultMaxProcessingTime) * time.Millisecond |
| 892 | config.Consumer.Offsets.Initial = initialOffset |
| 893 | //config.Consumer.Offsets.Initial = sarama.OffsetOldest |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 894 | brokers := []string{sc.KafkaAddress} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 895 | |
| 896 | topics := []string{topic.Name} |
| 897 | var consumer *scc.Consumer |
| 898 | var err error |
| 899 | |
| 900 | if consumer, err = scc.NewConsumer(brokers, groupId, topics, config); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 901 | logger.Errorw(ctx, "create-group-consumers-failure", log.Fields{"error": err, "topic": topic.Name, "groupId": groupId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 902 | return nil, err |
| 903 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 904 | logger.Debugw(ctx, "create-group-consumers-success", log.Fields{"topic": topic.Name, "groupId": groupId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 905 | |
| 906 | //sc.groupConsumers[topic.Name] = consumer |
| 907 | sc.addToGroupConsumers(topic.Name, consumer) |
| 908 | return consumer, nil |
| 909 | } |
| 910 | |
| 911 | // dispatchToConsumers sends the intercontainermessage received on a given topic to all subscribers for that |
| 912 | // topic via the unique channel each subscriber received during subscription |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 913 | func (sc *SaramaClient) dispatchToConsumers(consumerCh *consumerChannels, protoMessage proto.Message, fromTopic string, ts time.Time) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 914 | // Need to go over all channels and publish messages to them - do we need to copy msg? |
| 915 | sc.lockTopicToConsumerChannelMap.RLock() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 916 | for _, ch := range consumerCh.channels { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 917 | go func(c chan proto.Message) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 918 | c <- protoMessage |
| 919 | }(ch) |
| 920 | } |
Kent Hagerman | ccfa213 | 2019-12-17 13:29:34 -0500 | [diff] [blame] | 921 | sc.lockTopicToConsumerChannelMap.RUnlock() |
| 922 | |
| 923 | if callback := sc.metadataCallback; callback != nil { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 924 | callback(fromTopic, ts) |
Kent Hagerman | ccfa213 | 2019-12-17 13:29:34 -0500 | [diff] [blame] | 925 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 928 | func (sc *SaramaClient) consumeFromAPartition(ctx context.Context, topic *Topic, consumer sarama.PartitionConsumer, consumerChnls *consumerChannels) { |
| 929 | logger.Debugw(ctx, "starting-partition-consumption-loop", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 930 | startloop: |
| 931 | for { |
| 932 | select { |
| 933 | case err, ok := <-consumer.Errors(): |
| 934 | if ok { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 935 | if sc.isLivenessError(ctx, err) { |
| 936 | sc.updateLiveness(ctx, false) |
| 937 | logger.Warnw(ctx, "partition-consumers-error", log.Fields{"error": err}) |
cbabu | d497865 | 2019-12-04 08:04:21 +0100 | [diff] [blame] | 938 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 939 | } else { |
| 940 | // Channel is closed |
| 941 | break startloop |
| 942 | } |
| 943 | case msg, ok := <-consumer.Messages(): |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 944 | //logger.Debugw(ctx, "message-received", logger.Fields{"msg": msg, "receivedTopic": msg.Topic}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 945 | if !ok { |
| 946 | // channel is closed |
| 947 | break startloop |
| 948 | } |
| 949 | msgBody := msg.Value |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 950 | sc.updateLiveness(ctx, true) |
| 951 | logger.Debugw(ctx, "message-received", log.Fields{"timestamp": msg.Timestamp, "receivedTopic": msg.Topic}) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 952 | var protoMsg proto.Message |
| 953 | if err := proto.Unmarshal(msgBody, protoMsg); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 954 | logger.Warnw(ctx, "partition-invalid-message", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 955 | continue |
| 956 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 957 | go sc.dispatchToConsumers(consumerChnls, protoMsg, msg.Topic, msg.Timestamp) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 958 | case <-sc.doneCh: |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 959 | logger.Infow(ctx, "partition-received-exit-signal", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 960 | break startloop |
| 961 | } |
| 962 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 963 | logger.Infow(ctx, "partition-consumer-stopped", log.Fields{"topic": topic.Name}) |
| 964 | sc.setUnhealthy(ctx) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 967 | func (sc *SaramaClient) consumeGroupMessages(ctx context.Context, topic *Topic, consumer *scc.Consumer, consumerChnls *consumerChannels) { |
| 968 | logger.Debugw(ctx, "starting-group-consumption-loop", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 969 | |
| 970 | startloop: |
| 971 | for { |
| 972 | select { |
| 973 | case err, ok := <-consumer.Errors(): |
| 974 | if ok { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 975 | if sc.isLivenessError(ctx, err) { |
| 976 | sc.updateLiveness(ctx, false) |
Scott Baker | fa2f6ee | 2019-11-19 14:53:14 -0800 | [diff] [blame] | 977 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 978 | logger.Warnw(ctx, "group-consumers-error", log.Fields{"topic": topic.Name, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 979 | } else { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 980 | logger.Warnw(ctx, "group-consumers-closed-err", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 981 | // channel is closed |
| 982 | break startloop |
| 983 | } |
| 984 | case msg, ok := <-consumer.Messages(): |
| 985 | if !ok { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 986 | logger.Warnw(ctx, "group-consumers-closed-msg", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 987 | // Channel closed |
| 988 | break startloop |
| 989 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 990 | sc.updateLiveness(ctx, true) |
| 991 | logger.Debugw(ctx, "message-received", log.Fields{"timestamp": msg.Timestamp, "receivedTopic": msg.Topic}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 992 | msgBody := msg.Value |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 993 | var protoMsg proto.Message |
| 994 | if err := proto.Unmarshal(msgBody, protoMsg); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 995 | logger.Warnw(ctx, "invalid-message", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 996 | continue |
| 997 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 998 | go sc.dispatchToConsumers(consumerChnls, protoMsg, msg.Topic, msg.Timestamp) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 999 | consumer.MarkOffset(msg, "") |
| 1000 | case ntf := <-consumer.Notifications(): |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1001 | logger.Debugw(ctx, "group-received-notification", log.Fields{"notification": ntf}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1002 | case <-sc.doneCh: |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1003 | logger.Infow(ctx, "group-received-exit-signal", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1004 | break startloop |
| 1005 | } |
| 1006 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1007 | logger.Infow(ctx, "group-consumer-stopped", log.Fields{"topic": topic.Name}) |
| 1008 | sc.setUnhealthy(ctx) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1011 | func (sc *SaramaClient) startConsumers(ctx context.Context, topic *Topic) error { |
| 1012 | logger.Debugw(ctx, "starting-consumers", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1013 | var consumerCh *consumerChannels |
| 1014 | if consumerCh = sc.getConsumerChannel(topic); consumerCh == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1015 | logger.Errorw(ctx, "consumers-not-exist", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1016 | return errors.New("consumers-not-exist") |
| 1017 | } |
| 1018 | // For each consumer listening for that topic, start a consumption loop |
| 1019 | for _, consumer := range consumerCh.consumers { |
| 1020 | if pConsumer, ok := consumer.(sarama.PartitionConsumer); ok { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1021 | go sc.consumeFromAPartition(ctx, topic, pConsumer, consumerCh) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1022 | } else if gConsumer, ok := consumer.(*scc.Consumer); ok { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1023 | go sc.consumeGroupMessages(ctx, topic, gConsumer, consumerCh) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1024 | } else { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1025 | logger.Errorw(ctx, "invalid-consumer", log.Fields{"topic": topic}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1026 | return errors.New("invalid-consumer") |
| 1027 | } |
| 1028 | } |
| 1029 | return nil |
| 1030 | } |
| 1031 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 1032 | // // setupConsumerChannel creates a consumerChannels object for that topic and add it to the consumerChannels map |
| 1033 | // // for that topic. It also starts the routine that listens for messages on that topic. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1034 | func (sc *SaramaClient) setupPartitionConsumerChannel(ctx context.Context, topic *Topic, initialOffset int64) (chan proto.Message, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1035 | var pConsumers []sarama.PartitionConsumer |
| 1036 | var err error |
| 1037 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1038 | if pConsumers, err = sc.createPartitionConsumers(ctx, topic, initialOffset); err != nil { |
| 1039 | logger.Errorw(ctx, "creating-partition-consumers-failure", log.Fields{"error": err, "topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1040 | return nil, err |
| 1041 | } |
| 1042 | |
| 1043 | consumersIf := make([]interface{}, 0) |
| 1044 | for _, pConsumer := range pConsumers { |
| 1045 | consumersIf = append(consumersIf, pConsumer) |
| 1046 | } |
| 1047 | |
| 1048 | // Create the consumers/channel structure and set the consumers and create a channel on that topic - for now |
| 1049 | // unbuffered to verify race conditions. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1050 | consumerListeningChannel := make(chan proto.Message) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1051 | cc := &consumerChannels{ |
| 1052 | consumers: consumersIf, |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1053 | channels: []chan proto.Message{consumerListeningChannel}, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | // Add the consumers channel to the map |
| 1057 | sc.addTopicToConsumerChannelMap(topic.Name, cc) |
| 1058 | |
| 1059 | //Start a consumers to listen on that specific topic |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1060 | go func() { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1061 | if err := sc.startConsumers(ctx, topic); err != nil { |
| 1062 | logger.Errorw(ctx, "start-consumers-failed", log.Fields{ |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1063 | "topic": topic, |
| 1064 | "error": err}) |
| 1065 | } |
| 1066 | }() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1067 | |
| 1068 | return consumerListeningChannel, nil |
| 1069 | } |
| 1070 | |
| 1071 | // setupConsumerChannel creates a consumerChannels object for that topic and add it to the consumerChannels map |
| 1072 | // for that topic. It also starts the routine that listens for messages on that topic. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1073 | func (sc *SaramaClient) setupGroupConsumerChannel(ctx context.Context, topic *Topic, groupId string, initialOffset int64) (chan proto.Message, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1074 | // TODO: Replace this development partition consumers with a group consumers |
| 1075 | var pConsumer *scc.Consumer |
| 1076 | var err error |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1077 | if pConsumer, err = sc.createGroupConsumer(ctx, topic, groupId, initialOffset, DefaultMaxRetries); err != nil { |
| 1078 | logger.Errorw(ctx, "creating-partition-consumers-failure", log.Fields{"error": err, "topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1079 | return nil, err |
| 1080 | } |
| 1081 | // Create the consumers/channel structure and set the consumers and create a channel on that topic - for now |
| 1082 | // unbuffered to verify race conditions. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1083 | consumerListeningChannel := make(chan proto.Message) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1084 | cc := &consumerChannels{ |
| 1085 | consumers: []interface{}{pConsumer}, |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1086 | channels: []chan proto.Message{consumerListeningChannel}, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | // Add the consumers channel to the map |
| 1090 | sc.addTopicToConsumerChannelMap(topic.Name, cc) |
| 1091 | |
| 1092 | //Start a consumers to listen on that specific topic |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1093 | go func() { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1094 | if err := sc.startConsumers(ctx, topic); err != nil { |
| 1095 | logger.Errorw(ctx, "start-consumers-failed", log.Fields{ |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1096 | "topic": topic, |
| 1097 | "error": err}) |
| 1098 | } |
| 1099 | }() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1100 | |
| 1101 | return consumerListeningChannel, nil |
| 1102 | } |
| 1103 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1104 | func (sc *SaramaClient) createPartitionConsumers(ctx context.Context, topic *Topic, initialOffset int64) ([]sarama.PartitionConsumer, error) { |
| 1105 | logger.Debugw(ctx, "creating-partition-consumers", log.Fields{"topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1106 | partitionList, err := sc.consumer.Partitions(topic.Name) |
| 1107 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1108 | logger.Warnw(ctx, "get-partition-failure", log.Fields{"error": err, "topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1109 | return nil, err |
| 1110 | } |
| 1111 | |
| 1112 | pConsumers := make([]sarama.PartitionConsumer, 0) |
| 1113 | for _, partition := range partitionList { |
| 1114 | var pConsumer sarama.PartitionConsumer |
| 1115 | if pConsumer, err = sc.consumer.ConsumePartition(topic.Name, partition, initialOffset); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1116 | logger.Warnw(ctx, "consumers-partition-failure", log.Fields{"error": err, "topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1117 | return nil, err |
| 1118 | } |
| 1119 | pConsumers = append(pConsumers, pConsumer) |
| 1120 | } |
| 1121 | return pConsumers, nil |
| 1122 | } |
| 1123 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1124 | func removeChannel(ctx context.Context, channels []chan proto.Message, ch <-chan proto.Message) []chan proto.Message { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1125 | var i int |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1126 | var channel chan proto.Message |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1127 | for i, channel = range channels { |
| 1128 | if channel == ch { |
| 1129 | channels[len(channels)-1], channels[i] = channels[i], channels[len(channels)-1] |
| 1130 | close(channel) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1131 | logger.Debug(ctx, "channel-closed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1132 | return channels[:len(channels)-1] |
| 1133 | } |
| 1134 | } |
| 1135 | return channels |
| 1136 | } |
| 1137 | |
| 1138 | func (sc *SaramaClient) addToGroupConsumers(topic string, consumer *scc.Consumer) { |
| 1139 | sc.lockOfGroupConsumers.Lock() |
| 1140 | defer sc.lockOfGroupConsumers.Unlock() |
| 1141 | if _, exist := sc.groupConsumers[topic]; !exist { |
| 1142 | sc.groupConsumers[topic] = consumer |
| 1143 | } |
| 1144 | } |
| 1145 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1146 | func (sc *SaramaClient) deleteFromGroupConsumers(ctx context.Context, topic string) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1147 | sc.lockOfGroupConsumers.Lock() |
| 1148 | defer sc.lockOfGroupConsumers.Unlock() |
| 1149 | if _, exist := sc.groupConsumers[topic]; exist { |
| 1150 | consumer := sc.groupConsumers[topic] |
| 1151 | delete(sc.groupConsumers, topic) |
| 1152 | if err := consumer.Close(); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1153 | logger.Errorw(ctx, "failure-closing-consumer", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1154 | return err |
| 1155 | } |
| 1156 | } |
| 1157 | return nil |
| 1158 | } |
kesavand | d85e52b | 2022-03-15 16:38:08 +0530 | [diff] [blame] | 1159 | |
| 1160 | func (sc *SaramaClient) ListTopics(ctx context.Context) ([]string, error) { |
| 1161 | |
| 1162 | config := sarama.NewConfig() |
| 1163 | client, err := sarama.NewClient([]string{sc.KafkaAddress}, config) |
| 1164 | if err != nil { |
| 1165 | logger.Debugw(ctx, "list-topics-failure", log.Fields{"error": err, "broker-address": sc.KafkaAddress}) |
| 1166 | return nil, err |
| 1167 | } |
| 1168 | |
| 1169 | topics, err := client.Topics() |
| 1170 | if err != nil { |
| 1171 | logger.Debugw(ctx, "list-topics-failure", log.Fields{"error": err, "broker-address": sc.KafkaAddress}) |
| 1172 | return nil, err |
| 1173 | } |
| 1174 | |
| 1175 | return topics, nil |
| 1176 | } |