khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [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 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 7 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [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. |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 15 | */ |
Akash Reddy Kankanala | 05aff18 | 2025-05-06 12:57:32 +0530 | [diff] [blame^] | 16 | //nolint:staticcheck |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 17 | package kafka |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 18 | |
| 19 | import ( |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 20 | "context" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 21 | "fmt" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 22 | "sync" |
Scott Baker | 84a55ce | 2020-04-17 10:11:30 -0700 | [diff] [blame] | 23 | "time" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 24 | |
Himani Chawla | 6c38e57 | 2021-03-23 19:44:25 +0530 | [diff] [blame] | 25 | "github.com/golang/protobuf/proto" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v7/pkg/kafka" |
| 27 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/status" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 30 | ) |
| 31 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 32 | const ( |
| 33 | maxConcurrentMessage = 100 |
| 34 | ) |
| 35 | |
Kent Hagerman | ccfa213 | 2019-12-17 13:29:34 -0500 | [diff] [blame] | 36 | // static check to ensure KafkaClient implements kafka.Client |
| 37 | var _ kafka.Client = &KafkaClient{} |
| 38 | |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 39 | type KafkaClient struct { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 40 | topicsChannelMap map[string][]chan proto.Message |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 41 | lock sync.RWMutex |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 42 | alive bool |
| 43 | livenessMutex sync.Mutex |
| 44 | liveness chan bool |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func NewKafkaClient() *KafkaClient { |
| 48 | return &KafkaClient{ |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 49 | topicsChannelMap: make(map[string][]chan proto.Message), |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 50 | lock: sync.RWMutex{}, |
| 51 | } |
| 52 | } |
| 53 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 54 | func (kc *KafkaClient) Start(ctx context.Context) error { |
| 55 | logger.Debug(ctx, "kafka-client-started") |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 56 | return nil |
| 57 | } |
| 58 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 59 | func (kc *KafkaClient) Stop(ctx context.Context) { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 60 | kc.lock.Lock() |
| 61 | defer kc.lock.Unlock() |
| 62 | for topic, chnls := range kc.topicsChannelMap { |
| 63 | for _, c := range chnls { |
| 64 | close(c) |
| 65 | } |
| 66 | delete(kc.topicsChannelMap, topic) |
| 67 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 68 | logger.Debug(ctx, "kafka-client-stopped") |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 71 | func (kc *KafkaClient) CreateTopic(ctx context.Context, topic *kafka.Topic, numPartition int, repFactor int) error { |
| 72 | logger.Debugw(ctx, "CreatingTopic", log.Fields{"topic": topic.Name, "numPartition": numPartition, "replicationFactor": repFactor}) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 73 | kc.lock.Lock() |
| 74 | defer kc.lock.Unlock() |
| 75 | if _, ok := kc.topicsChannelMap[topic.Name]; ok { |
| 76 | return fmt.Errorf("Topic %s already exist", topic.Name) |
| 77 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 78 | ch := make(chan proto.Message) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 79 | kc.topicsChannelMap[topic.Name] = append(kc.topicsChannelMap[topic.Name], ch) |
| 80 | return nil |
| 81 | } |
| 82 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 83 | func (kc *KafkaClient) DeleteTopic(ctx context.Context, topic *kafka.Topic) error { |
| 84 | logger.Debugw(ctx, "DeleteTopic", log.Fields{"topic": topic.Name}) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 85 | kc.lock.Lock() |
| 86 | defer kc.lock.Unlock() |
| 87 | delete(kc.topicsChannelMap, topic.Name) |
| 88 | return nil |
| 89 | } |
| 90 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 91 | func (kc *KafkaClient) Subscribe(ctx context.Context, topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan proto.Message, error) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 92 | logger.Debugw(ctx, "Subscribe", log.Fields{"topic": topic.Name, "args": kvArgs}) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 93 | kc.lock.Lock() |
| 94 | defer kc.lock.Unlock() |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 95 | ch := make(chan proto.Message, maxConcurrentMessage) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 96 | kc.topicsChannelMap[topic.Name] = append(kc.topicsChannelMap[topic.Name], ch) |
| 97 | return ch, nil |
| 98 | } |
| 99 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 100 | func removeChannel(s []chan proto.Message, i int) []chan proto.Message { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 101 | s[i] = s[len(s)-1] |
| 102 | return s[:len(s)-1] |
| 103 | } |
| 104 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 105 | func (kc *KafkaClient) UnSubscribe(ctx context.Context, topic *kafka.Topic, ch <-chan proto.Message) error { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 106 | logger.Debugw(ctx, "UnSubscribe", log.Fields{"topic": topic.Name}) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 107 | kc.lock.Lock() |
| 108 | defer kc.lock.Unlock() |
| 109 | if chnls, ok := kc.topicsChannelMap[topic.Name]; ok { |
| 110 | idx := -1 |
| 111 | for i, c := range chnls { |
| 112 | if c == ch { |
| 113 | close(c) |
| 114 | idx = i |
| 115 | } |
| 116 | } |
| 117 | if idx >= 0 { |
| 118 | kc.topicsChannelMap[topic.Name] = removeChannel(kc.topicsChannelMap[topic.Name], idx) |
| 119 | } |
| 120 | } |
| 121 | return nil |
| 122 | } |
| 123 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 124 | func (kc *KafkaClient) SubscribeForMetadata(ctx context.Context, _ func(fromTopic string, timestamp time.Time)) { |
| 125 | logger.Debug(ctx, "SubscribeForMetadata - unimplemented") |
Kent Hagerman | ccfa213 | 2019-12-17 13:29:34 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 128 | func (kc *KafkaClient) Send(ctx context.Context, msg interface{}, topic *kafka.Topic, keys ...string) error { |
Himani Chawla | 6c38e57 | 2021-03-23 19:44:25 +0530 | [diff] [blame] | 129 | // Assert message is a proto message |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 130 | protoMsg, ok := msg.(proto.Message) |
| 131 | if !ok { |
Himani Chawla | 6c38e57 | 2021-03-23 19:44:25 +0530 | [diff] [blame] | 132 | logger.Warnw(ctx, "message-not-a-proto-message", log.Fields{"msg": msg}) |
| 133 | return status.Error(codes.InvalidArgument, "msg-not-a-proto-msg") |
| 134 | } |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 135 | kc.lock.RLock() |
| 136 | defer kc.lock.RUnlock() |
| 137 | for _, ch := range kc.topicsChannelMap[topic.Name] { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 138 | select { |
| 139 | case ch <- protoMsg: |
| 140 | logger.Debugw(ctx, "publishing", log.Fields{"toTopic": topic.Name, "msg": protoMsg}) |
| 141 | default: |
| 142 | logger.Debugw(ctx, "ignoring-event-channel-busy", log.Fields{"toTopic": topic.Name, "msg": protoMsg}) |
| 143 | } |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 144 | } |
| 145 | return nil |
| 146 | } |
| 147 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 148 | func (kc *KafkaClient) SendLiveness(ctx context.Context) error { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 149 | kc.livenessMutex.Lock() |
| 150 | defer kc.livenessMutex.Unlock() |
| 151 | if kc.liveness != nil { |
| 152 | kc.liveness <- true // I am a mock |
| 153 | } |
| 154 | return nil |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 155 | } |
| 156 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 157 | func (kc *KafkaClient) EnableLivenessChannel(ctx context.Context, enable bool) chan bool { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 158 | logger.Infow(ctx, "kafka-enable-liveness-channel", log.Fields{"enable": enable}) |
| 159 | if enable { |
| 160 | kc.livenessMutex.Lock() |
| 161 | defer kc.livenessMutex.Unlock() |
| 162 | if kc.liveness == nil { |
| 163 | logger.Info(ctx, "kafka-create-liveness-channel") |
| 164 | kc.liveness = make(chan bool, 10) |
| 165 | // post intial state to the channel |
| 166 | kc.liveness <- kc.alive |
| 167 | } |
| 168 | } else { |
| 169 | panic("Turning off liveness reporting is not supported") |
| 170 | } |
| 171 | return kc.liveness |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 172 | } |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 173 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 174 | func (kc *KafkaClient) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool { |
| 175 | logger.Debug(ctx, "EnableHealthinessChannel - unimplemented") |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 176 | return nil |
| 177 | } |
kesavand | d85e52b | 2022-03-15 16:38:08 +0530 | [diff] [blame] | 178 | |
| 179 | func (kc *KafkaClient) ListTopics(ctx context.Context) ([]string, error) { |
| 180 | topics := []string{"voltha.events", "myTopic"} |
| 181 | return topics, nil |
| 182 | } |