Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 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 |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 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. |
| 15 | */ |
| 16 | package kafka |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
| 21 | "fmt" |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 22 | "google.golang.org/grpc/codes" |
| 23 | "google.golang.org/grpc/status" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 24 | "reflect" |
| 25 | "strings" |
| 26 | "sync" |
| 27 | "time" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 28 | |
| 29 | "github.com/golang/protobuf/proto" |
| 30 | "github.com/golang/protobuf/ptypes" |
| 31 | "github.com/golang/protobuf/ptypes/any" |
| 32 | "github.com/google/uuid" |
| 33 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 34 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 37 | const ( |
| 38 | DefaultMaxRetries = 3 |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 39 | DefaultRequestTimeout = 60000 // 60000 milliseconds - to handle a wider latency range |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | const ( |
| 43 | TransactionKey = "transactionID" |
| 44 | FromTopic = "fromTopic" |
| 45 | ) |
| 46 | |
| 47 | var ErrorTransactionNotAcquired = errors.New("transaction-not-acquired") |
| 48 | var ErrorTransactionInvalidId = errors.New("transaction-invalid-id") |
| 49 | |
| 50 | // requestHandlerChannel represents an interface associated with a channel. Whenever, an event is |
| 51 | // obtained from that channel, this interface is invoked. This is used to handle |
| 52 | // async requests into the Core via the kafka messaging bus |
| 53 | type requestHandlerChannel struct { |
| 54 | requesthandlerInterface interface{} |
| 55 | ch <-chan *ic.InterContainerMessage |
| 56 | } |
| 57 | |
| 58 | // transactionChannel represents a combination of a topic and a channel onto which a response received |
| 59 | // on the kafka bus will be sent to |
| 60 | type transactionChannel struct { |
| 61 | topic *Topic |
| 62 | ch chan *ic.InterContainerMessage |
| 63 | } |
| 64 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 65 | type InterContainerProxy interface { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 66 | Start() error |
| 67 | Stop() |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 68 | GetDefaultTopic() *Topic |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 69 | DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 70 | InvokeRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic, waitForResponse bool, key string, kvArgs ...*KVArg) (bool, *any.Any) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 71 | InvokeAsyncRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic, waitForResponse bool, key string, kvArgs ...*KVArg) chan *RpcResponse |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 72 | SubscribeWithRequestHandlerInterface(topic Topic, handler interface{}) error |
| 73 | SubscribeWithDefaultRequestHandler(topic Topic, initialOffset int64) error |
| 74 | UnSubscribeFromRequestHandler(topic Topic) error |
| 75 | DeleteTopic(topic Topic) error |
| 76 | EnableLivenessChannel(enable bool) chan bool |
| 77 | SendLiveness() error |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // interContainerProxy represents the messaging proxy |
| 81 | type interContainerProxy struct { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 82 | kafkaAddress string |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 83 | defaultTopic *Topic |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 84 | defaultRequestHandlerInterface interface{} |
| 85 | deviceDiscoveryTopic *Topic |
| 86 | kafkaClient Client |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 87 | doneCh chan struct{} |
| 88 | doneOnce sync.Once |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 89 | |
| 90 | // This map is used to map a topic to an interface and channel. When a request is received |
| 91 | // on that channel (registered to the topic) then that interface is invoked. |
| 92 | topicToRequestHandlerChannelMap map[string]*requestHandlerChannel |
| 93 | lockTopicRequestHandlerChannelMap sync.RWMutex |
| 94 | |
| 95 | // This map is used to map a channel to a response topic. This channel handles all responses on that |
| 96 | // channel for that topic and forward them to the appropriate consumers channel, using the |
| 97 | // transactionIdToChannelMap. |
| 98 | topicToResponseChannelMap map[string]<-chan *ic.InterContainerMessage |
| 99 | lockTopicResponseChannelMap sync.RWMutex |
| 100 | |
| 101 | // This map is used to map a transaction to a consumers channel. This is used whenever a request has been |
| 102 | // sent out and we are waiting for a response. |
| 103 | transactionIdToChannelMap map[string]*transactionChannel |
| 104 | lockTransactionIdToChannelMap sync.RWMutex |
| 105 | } |
| 106 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 107 | type InterContainerProxyOption func(*interContainerProxy) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 108 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 109 | func InterContainerAddress(address string) InterContainerProxyOption { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 110 | return func(args *interContainerProxy) { |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 111 | args.kafkaAddress = address |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | func DefaultTopic(topic *Topic) InterContainerProxyOption { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 116 | return func(args *interContainerProxy) { |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 117 | args.defaultTopic = topic |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | func DeviceDiscoveryTopic(topic *Topic) InterContainerProxyOption { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 122 | return func(args *interContainerProxy) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 123 | args.deviceDiscoveryTopic = topic |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func RequestHandlerInterface(handler interface{}) InterContainerProxyOption { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 128 | return func(args *interContainerProxy) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 129 | args.defaultRequestHandlerInterface = handler |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func MsgClient(client Client) InterContainerProxyOption { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 134 | return func(args *interContainerProxy) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 135 | args.kafkaClient = client |
| 136 | } |
| 137 | } |
| 138 | |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 139 | func newInterContainerProxy(opts ...InterContainerProxyOption) *interContainerProxy { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 140 | proxy := &interContainerProxy{ |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 141 | kafkaAddress: DefaultKafkaAddress, |
| 142 | doneCh: make(chan struct{}), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | for _, option := range opts { |
| 146 | option(proxy) |
| 147 | } |
| 148 | |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 149 | return proxy |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 152 | func NewInterContainerProxy(opts ...InterContainerProxyOption) InterContainerProxy { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 153 | return newInterContainerProxy(opts...) |
| 154 | } |
| 155 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 156 | func (kp *interContainerProxy) Start() error { |
| 157 | logger.Info("Starting-Proxy") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 158 | |
| 159 | // Kafka MsgClient should already have been created. If not, output fatal error |
| 160 | if kp.kafkaClient == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 161 | logger.Fatal("kafka-client-not-set") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 164 | // Start the kafka client |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 165 | if err := kp.kafkaClient.Start(); err != nil { |
| 166 | logger.Errorw("Cannot-create-kafka-proxy", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 167 | return err |
| 168 | } |
| 169 | |
| 170 | // Create the topic to response channel map |
| 171 | kp.topicToResponseChannelMap = make(map[string]<-chan *ic.InterContainerMessage) |
| 172 | // |
| 173 | // Create the transactionId to Channel Map |
| 174 | kp.transactionIdToChannelMap = make(map[string]*transactionChannel) |
| 175 | |
| 176 | // Create the topic to request channel map |
| 177 | kp.topicToRequestHandlerChannelMap = make(map[string]*requestHandlerChannel) |
| 178 | |
| 179 | return nil |
| 180 | } |
| 181 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 182 | func (kp *interContainerProxy) Stop() { |
| 183 | logger.Info("stopping-intercontainer-proxy") |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 184 | kp.doneOnce.Do(func() { close(kp.doneCh) }) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 185 | // TODO : Perform cleanup |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 186 | kp.kafkaClient.Stop() |
| 187 | err := kp.deleteAllTopicRequestHandlerChannelMap() |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 188 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 189 | logger.Errorw("failed-delete-all-topic-request-handler-channel-map", log.Fields{"error": err}) |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 190 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 191 | err = kp.deleteAllTopicResponseChannelMap() |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 192 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 193 | logger.Errorw("failed-delete-all-topic-response-channel-map", log.Fields{"error": err}) |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 194 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 195 | kp.deleteAllTransactionIdToChannelMap() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 198 | func (kp *interContainerProxy) GetDefaultTopic() *Topic { |
| 199 | return kp.defaultTopic |
| 200 | } |
| 201 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 202 | // DeviceDiscovered publish the discovered device onto the kafka messaging bus |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 203 | func (kp *interContainerProxy) DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error { |
| 204 | logger.Debugw("sending-device-discovery-msg", log.Fields{"deviceId": deviceId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 205 | // Simple validation |
| 206 | if deviceId == "" || deviceType == "" { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 207 | logger.Errorw("invalid-parameters", log.Fields{"id": deviceId, "type": deviceType}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 208 | return errors.New("invalid-parameters") |
| 209 | } |
| 210 | // Create the device discovery message |
| 211 | header := &ic.Header{ |
| 212 | Id: uuid.New().String(), |
| 213 | Type: ic.MessageType_DEVICE_DISCOVERED, |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 214 | FromTopic: kp.defaultTopic.Name, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 215 | ToTopic: kp.deviceDiscoveryTopic.Name, |
Scott Baker | 84a55ce | 2020-04-17 10:11:30 -0700 | [diff] [blame] | 216 | Timestamp: ptypes.TimestampNow(), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 217 | } |
| 218 | body := &ic.DeviceDiscovered{ |
| 219 | Id: deviceId, |
| 220 | DeviceType: deviceType, |
| 221 | ParentId: parentId, |
| 222 | Publisher: publisher, |
| 223 | } |
| 224 | |
| 225 | var marshalledData *any.Any |
| 226 | var err error |
| 227 | if marshalledData, err = ptypes.MarshalAny(body); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 228 | logger.Errorw("cannot-marshal-request", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 229 | return err |
| 230 | } |
| 231 | msg := &ic.InterContainerMessage{ |
| 232 | Header: header, |
| 233 | Body: marshalledData, |
| 234 | } |
| 235 | |
| 236 | // Send the message |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 237 | if err := kp.kafkaClient.Send(msg, kp.deviceDiscoveryTopic); err != nil { |
| 238 | logger.Errorw("cannot-send-device-discovery-message", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 239 | return err |
| 240 | } |
| 241 | return nil |
| 242 | } |
| 243 | |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 244 | // InvokeAsyncRPC is used to make an RPC request asynchronously |
| 245 | func (kp *interContainerProxy) InvokeAsyncRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic, |
| 246 | waitForResponse bool, key string, kvArgs ...*KVArg) chan *RpcResponse { |
| 247 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 248 | logger.Debugw("InvokeAsyncRPC", log.Fields{"rpc": rpc, "key": key}) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 249 | // If a replyToTopic is provided then we use it, otherwise just use the default toTopic. The replyToTopic is |
| 250 | // typically the device ID. |
| 251 | responseTopic := replyToTopic |
| 252 | if responseTopic == nil { |
| 253 | responseTopic = kp.GetDefaultTopic() |
| 254 | } |
| 255 | |
| 256 | chnl := make(chan *RpcResponse) |
| 257 | |
| 258 | go func() { |
| 259 | |
| 260 | // once we're done, |
| 261 | // close the response channel |
| 262 | defer close(chnl) |
| 263 | |
| 264 | var err error |
| 265 | var protoRequest *ic.InterContainerMessage |
| 266 | |
| 267 | // Encode the request |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 268 | protoRequest, err = encodeRequest(rpc, toTopic, responseTopic, key, kvArgs...) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 269 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 270 | logger.Warnw("cannot-format-request", log.Fields{"rpc": rpc, "error": err}) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 271 | chnl <- NewResponse(RpcFormattingError, err, nil) |
| 272 | return |
| 273 | } |
| 274 | |
| 275 | // Subscribe for response, if needed, before sending request |
| 276 | var ch <-chan *ic.InterContainerMessage |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 277 | if ch, err = kp.subscribeForResponse(*responseTopic, protoRequest.Header.Id); err != nil { |
| 278 | logger.Errorw("failed-to-subscribe-for-response", log.Fields{"error": err, "toTopic": toTopic.Name}) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 279 | chnl <- NewResponse(RpcTransportError, err, nil) |
| 280 | return |
| 281 | } |
| 282 | |
| 283 | // Send request - if the topic is formatted with a device Id then we will send the request using a |
| 284 | // specific key, hence ensuring a single partition is used to publish the request. This ensures that the |
| 285 | // subscriber on that topic will receive the request in the order it was sent. The key used is the deviceId. |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 286 | logger.Debugw("sending-msg", log.Fields{"rpc": rpc, "toTopic": toTopic, "replyTopic": responseTopic, "key": key, "xId": protoRequest.Header.Id}) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 287 | |
| 288 | // if the message is not sent on kafka publish an event an close the channel |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 289 | if err = kp.kafkaClient.Send(protoRequest, toTopic, key); err != nil { |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 290 | chnl <- NewResponse(RpcTransportError, err, nil) |
| 291 | return |
| 292 | } |
| 293 | |
| 294 | // if the client is not waiting for a response send the ack and close the channel |
| 295 | chnl <- NewResponse(RpcSent, nil, nil) |
| 296 | if !waitForResponse { |
| 297 | return |
| 298 | } |
| 299 | |
| 300 | defer func() { |
| 301 | // Remove the subscription for a response on return |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 302 | if err := kp.unSubscribeForResponse(protoRequest.Header.Id); err != nil { |
| 303 | logger.Warnw("invoke-async-rpc-unsubscriber-for-response-failed", log.Fields{"err": err}) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 304 | } |
| 305 | }() |
| 306 | |
| 307 | // Wait for response as well as timeout or cancellation |
| 308 | select { |
| 309 | case msg, ok := <-ch: |
| 310 | if !ok { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 311 | logger.Warnw("channel-closed", log.Fields{"rpc": rpc, "replyTopic": replyToTopic.Name}) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 312 | chnl <- NewResponse(RpcTransportError, status.Error(codes.Aborted, "channel closed"), nil) |
| 313 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 314 | logger.Debugw("received-response", log.Fields{"rpc": rpc, "msgHeader": msg.Header}) |
| 315 | if responseBody, err := decodeResponse(msg); err != nil { |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 316 | chnl <- NewResponse(RpcReply, err, nil) |
| 317 | } else { |
| 318 | if responseBody.Success { |
| 319 | chnl <- NewResponse(RpcReply, nil, responseBody.Result) |
| 320 | } else { |
| 321 | // response body contains an error |
| 322 | unpackErr := &ic.Error{} |
| 323 | if err := ptypes.UnmarshalAny(responseBody.Result, unpackErr); err != nil { |
| 324 | chnl <- NewResponse(RpcReply, err, nil) |
| 325 | } else { |
| 326 | chnl <- NewResponse(RpcReply, status.Error(codes.Internal, unpackErr.Reason), nil) |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | case <-ctx.Done(): |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 331 | logger.Errorw("context-cancelled", log.Fields{"rpc": rpc, "ctx": ctx.Err()}) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 332 | err := status.Error(codes.DeadlineExceeded, ctx.Err().Error()) |
| 333 | chnl <- NewResponse(RpcTimeout, err, nil) |
| 334 | case <-kp.doneCh: |
| 335 | chnl <- NewResponse(RpcSystemClosing, nil, nil) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 336 | logger.Warnw("received-exit-signal", log.Fields{"toTopic": toTopic.Name, "rpc": rpc}) |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 337 | } |
| 338 | }() |
| 339 | return chnl |
| 340 | } |
| 341 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 342 | // InvokeRPC is used to send a request to a given topic |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 343 | func (kp *interContainerProxy) InvokeRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 344 | waitForResponse bool, key string, kvArgs ...*KVArg) (bool, *any.Any) { |
| 345 | |
| 346 | // If a replyToTopic is provided then we use it, otherwise just use the default toTopic. The replyToTopic is |
| 347 | // typically the device ID. |
| 348 | responseTopic := replyToTopic |
| 349 | if responseTopic == nil { |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 350 | responseTopic = kp.defaultTopic |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // Encode the request |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 354 | protoRequest, err := encodeRequest(rpc, toTopic, responseTopic, key, kvArgs...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 355 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 356 | logger.Warnw("cannot-format-request", log.Fields{"rpc": rpc, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 357 | return false, nil |
| 358 | } |
| 359 | |
| 360 | // Subscribe for response, if needed, before sending request |
| 361 | var ch <-chan *ic.InterContainerMessage |
| 362 | if waitForResponse { |
| 363 | var err error |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 364 | if ch, err = kp.subscribeForResponse(*responseTopic, protoRequest.Header.Id); err != nil { |
| 365 | logger.Errorw("failed-to-subscribe-for-response", log.Fields{"error": err, "toTopic": toTopic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | // Send request - if the topic is formatted with a device Id then we will send the request using a |
| 370 | // specific key, hence ensuring a single partition is used to publish the request. This ensures that the |
| 371 | // subscriber on that topic will receive the request in the order it was sent. The key used is the deviceId. |
| 372 | //key := GetDeviceIdFromTopic(*toTopic) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 373 | logger.Debugw("sending-msg", log.Fields{"rpc": rpc, "toTopic": toTopic, "replyTopic": responseTopic, "key": key, "xId": protoRequest.Header.Id}) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 374 | go func() { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 375 | if err := kp.kafkaClient.Send(protoRequest, toTopic, key); err != nil { |
| 376 | logger.Errorw("send-failed", log.Fields{ |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 377 | "topic": toTopic, |
| 378 | "key": key, |
| 379 | "error": err}) |
| 380 | } |
| 381 | }() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 382 | |
| 383 | if waitForResponse { |
| 384 | // Create a child context based on the parent context, if any |
| 385 | var cancel context.CancelFunc |
| 386 | childCtx := context.Background() |
| 387 | if ctx == nil { |
| 388 | ctx, cancel = context.WithTimeout(context.Background(), DefaultRequestTimeout*time.Millisecond) |
| 389 | } else { |
| 390 | childCtx, cancel = context.WithTimeout(ctx, DefaultRequestTimeout*time.Millisecond) |
| 391 | } |
| 392 | defer cancel() |
| 393 | |
| 394 | // Wait for response as well as timeout or cancellation |
| 395 | // Remove the subscription for a response on return |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 396 | defer func() { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 397 | if err := kp.unSubscribeForResponse(protoRequest.Header.Id); err != nil { |
| 398 | logger.Errorw("response-unsubscribe-failed", log.Fields{ |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 399 | "id": protoRequest.Header.Id, |
| 400 | "error": err}) |
| 401 | } |
| 402 | }() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 403 | select { |
| 404 | case msg, ok := <-ch: |
| 405 | if !ok { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 406 | logger.Warnw("channel-closed", log.Fields{"rpc": rpc, "replyTopic": replyToTopic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 407 | protoError := &ic.Error{Reason: "channel-closed"} |
| 408 | var marshalledArg *any.Any |
| 409 | if marshalledArg, err = ptypes.MarshalAny(protoError); err != nil { |
| 410 | return false, nil // Should never happen |
| 411 | } |
| 412 | return false, marshalledArg |
| 413 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 414 | logger.Debugw("received-response", log.Fields{"rpc": rpc, "msgHeader": msg.Header}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 415 | var responseBody *ic.InterContainerResponseBody |
| 416 | var err error |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 417 | if responseBody, err = decodeResponse(msg); err != nil { |
| 418 | logger.Errorw("decode-response-error", log.Fields{"error": err}) |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 419 | // FIXME we should return something |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 420 | } |
| 421 | return responseBody.Success, responseBody.Result |
| 422 | case <-ctx.Done(): |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 423 | logger.Debugw("context-cancelled", log.Fields{"rpc": rpc, "ctx": ctx.Err()}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 424 | // pack the error as proto any type |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 425 | protoError := &ic.Error{Reason: ctx.Err().Error(), Code: ic.ErrorCode_DEADLINE_EXCEEDED} |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 426 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 427 | var marshalledArg *any.Any |
| 428 | if marshalledArg, err = ptypes.MarshalAny(protoError); err != nil { |
| 429 | return false, nil // Should never happen |
| 430 | } |
| 431 | return false, marshalledArg |
| 432 | case <-childCtx.Done(): |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 433 | logger.Debugw("context-cancelled", log.Fields{"rpc": rpc, "ctx": childCtx.Err()}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 434 | // pack the error as proto any type |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 435 | protoError := &ic.Error{Reason: childCtx.Err().Error(), Code: ic.ErrorCode_DEADLINE_EXCEEDED} |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 436 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 437 | var marshalledArg *any.Any |
| 438 | if marshalledArg, err = ptypes.MarshalAny(protoError); err != nil { |
| 439 | return false, nil // Should never happen |
| 440 | } |
| 441 | return false, marshalledArg |
| 442 | case <-kp.doneCh: |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 443 | logger.Infow("received-exit-signal", log.Fields{"toTopic": toTopic.Name, "rpc": rpc}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 444 | return true, nil |
| 445 | } |
| 446 | } |
| 447 | return true, nil |
| 448 | } |
| 449 | |
| 450 | // SubscribeWithRequestHandlerInterface allows a caller to assign a target object to be invoked automatically |
| 451 | // when a message is received on a given topic |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 452 | func (kp *interContainerProxy) SubscribeWithRequestHandlerInterface(topic Topic, handler interface{}) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 453 | |
| 454 | // Subscribe to receive messages for that topic |
| 455 | var ch <-chan *ic.InterContainerMessage |
| 456 | var err error |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 457 | if ch, err = kp.kafkaClient.Subscribe(&topic); err != nil { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 458 | //if ch, err = kp.Subscribe(topic); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 459 | logger.Errorw("failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 460 | return err |
| 461 | } |
| 462 | |
| 463 | kp.defaultRequestHandlerInterface = handler |
| 464 | kp.addToTopicRequestHandlerChannelMap(topic.Name, &requestHandlerChannel{requesthandlerInterface: handler, ch: ch}) |
| 465 | // Launch a go routine to receive and process kafka messages |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 466 | go kp.waitForMessages(ch, topic, handler) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 467 | |
| 468 | return nil |
| 469 | } |
| 470 | |
| 471 | // SubscribeWithDefaultRequestHandler allows a caller to add a topic to an existing target object to be invoked automatically |
| 472 | // when a message is received on a given topic. So far there is only 1 target registered per microservice |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 473 | func (kp *interContainerProxy) SubscribeWithDefaultRequestHandler(topic Topic, initialOffset int64) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 474 | // Subscribe to receive messages for that topic |
| 475 | var ch <-chan *ic.InterContainerMessage |
| 476 | var err error |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 477 | if ch, err = kp.kafkaClient.Subscribe(&topic, &KVArg{Key: Offset, Value: initialOffset}); err != nil { |
| 478 | logger.Errorw("failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 479 | return err |
| 480 | } |
| 481 | kp.addToTopicRequestHandlerChannelMap(topic.Name, &requestHandlerChannel{requesthandlerInterface: kp.defaultRequestHandlerInterface, ch: ch}) |
| 482 | |
| 483 | // Launch a go routine to receive and process kafka messages |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 484 | go kp.waitForMessages(ch, topic, kp.defaultRequestHandlerInterface) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 485 | |
| 486 | return nil |
| 487 | } |
| 488 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 489 | func (kp *interContainerProxy) UnSubscribeFromRequestHandler(topic Topic) error { |
| 490 | return kp.deleteFromTopicRequestHandlerChannelMap(topic.Name) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 493 | func (kp *interContainerProxy) deleteFromTopicResponseChannelMap(topic string) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 494 | kp.lockTopicResponseChannelMap.Lock() |
| 495 | defer kp.lockTopicResponseChannelMap.Unlock() |
| 496 | if _, exist := kp.topicToResponseChannelMap[topic]; exist { |
| 497 | // Unsubscribe to this topic first - this will close the subscribed channel |
| 498 | var err error |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 499 | if err = kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil { |
| 500 | logger.Errorw("unsubscribing-error", log.Fields{"topic": topic}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 501 | } |
| 502 | delete(kp.topicToResponseChannelMap, topic) |
| 503 | return err |
| 504 | } else { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 505 | return fmt.Errorf("%s-Topic-not-found", topic) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 509 | // nolint: unused |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 510 | func (kp *interContainerProxy) deleteAllTopicResponseChannelMap() error { |
| 511 | logger.Debug("delete-all-topic-response-channel") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 512 | kp.lockTopicResponseChannelMap.Lock() |
| 513 | defer kp.lockTopicResponseChannelMap.Unlock() |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 514 | var unsubscribeFailTopics []string |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 515 | for topic := range kp.topicToResponseChannelMap { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 516 | // Unsubscribe to this topic first - this will close the subscribed channel |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 517 | if err := kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil { |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 518 | unsubscribeFailTopics = append(unsubscribeFailTopics, topic) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 519 | logger.Errorw("unsubscribing-error", log.Fields{"topic": topic, "error": err}) |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 520 | // Do not return. Continue to try to unsubscribe to other topics. |
| 521 | } else { |
| 522 | // Only delete from channel map if successfully unsubscribed. |
| 523 | delete(kp.topicToResponseChannelMap, topic) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 524 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 525 | } |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 526 | if len(unsubscribeFailTopics) > 0 { |
| 527 | return fmt.Errorf("unsubscribe-errors: %v", unsubscribeFailTopics) |
| 528 | } |
| 529 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 532 | func (kp *interContainerProxy) addToTopicRequestHandlerChannelMap(topic string, arg *requestHandlerChannel) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 533 | kp.lockTopicRequestHandlerChannelMap.Lock() |
| 534 | defer kp.lockTopicRequestHandlerChannelMap.Unlock() |
| 535 | if _, exist := kp.topicToRequestHandlerChannelMap[topic]; !exist { |
| 536 | kp.topicToRequestHandlerChannelMap[topic] = arg |
| 537 | } |
| 538 | } |
| 539 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 540 | func (kp *interContainerProxy) deleteFromTopicRequestHandlerChannelMap(topic string) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 541 | kp.lockTopicRequestHandlerChannelMap.Lock() |
| 542 | defer kp.lockTopicRequestHandlerChannelMap.Unlock() |
| 543 | if _, exist := kp.topicToRequestHandlerChannelMap[topic]; exist { |
| 544 | // Close the kafka client client first by unsubscribing to this topic |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 545 | if err := kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch); err != nil { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 546 | return err |
| 547 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 548 | delete(kp.topicToRequestHandlerChannelMap, topic) |
| 549 | return nil |
| 550 | } else { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 551 | return fmt.Errorf("%s-Topic-not-found", topic) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 555 | // nolint: unused |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 556 | func (kp *interContainerProxy) deleteAllTopicRequestHandlerChannelMap() error { |
| 557 | logger.Debug("delete-all-topic-request-channel") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 558 | kp.lockTopicRequestHandlerChannelMap.Lock() |
| 559 | defer kp.lockTopicRequestHandlerChannelMap.Unlock() |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 560 | var unsubscribeFailTopics []string |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 561 | for topic := range kp.topicToRequestHandlerChannelMap { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 562 | // Close the kafka client client first by unsubscribing to this topic |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 563 | if err := kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch); err != nil { |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 564 | unsubscribeFailTopics = append(unsubscribeFailTopics, topic) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 565 | logger.Errorw("unsubscribing-error", log.Fields{"topic": topic, "error": err}) |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 566 | // Do not return. Continue to try to unsubscribe to other topics. |
| 567 | } else { |
| 568 | // Only delete from channel map if successfully unsubscribed. |
| 569 | delete(kp.topicToRequestHandlerChannelMap, topic) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 570 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 571 | } |
Scott Baker | a2da2f4 | 2020-02-20 16:27:34 -0800 | [diff] [blame] | 572 | if len(unsubscribeFailTopics) > 0 { |
| 573 | return fmt.Errorf("unsubscribe-errors: %v", unsubscribeFailTopics) |
| 574 | } |
| 575 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 578 | func (kp *interContainerProxy) addToTransactionIdToChannelMap(id string, topic *Topic, arg chan *ic.InterContainerMessage) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 579 | kp.lockTransactionIdToChannelMap.Lock() |
| 580 | defer kp.lockTransactionIdToChannelMap.Unlock() |
| 581 | if _, exist := kp.transactionIdToChannelMap[id]; !exist { |
| 582 | kp.transactionIdToChannelMap[id] = &transactionChannel{topic: topic, ch: arg} |
| 583 | } |
| 584 | } |
| 585 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 586 | func (kp *interContainerProxy) deleteFromTransactionIdToChannelMap(id string) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 587 | kp.lockTransactionIdToChannelMap.Lock() |
| 588 | defer kp.lockTransactionIdToChannelMap.Unlock() |
| 589 | if transChannel, exist := kp.transactionIdToChannelMap[id]; exist { |
| 590 | // Close the channel first |
| 591 | close(transChannel.ch) |
| 592 | delete(kp.transactionIdToChannelMap, id) |
| 593 | } |
| 594 | } |
| 595 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 596 | func (kp *interContainerProxy) deleteTopicTransactionIdToChannelMap(id string) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 597 | kp.lockTransactionIdToChannelMap.Lock() |
| 598 | defer kp.lockTransactionIdToChannelMap.Unlock() |
| 599 | for key, value := range kp.transactionIdToChannelMap { |
| 600 | if value.topic.Name == id { |
| 601 | close(value.ch) |
| 602 | delete(kp.transactionIdToChannelMap, key) |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 607 | // nolint: unused |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 608 | func (kp *interContainerProxy) deleteAllTransactionIdToChannelMap() { |
| 609 | logger.Debug("delete-all-transaction-id-channel-map") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 610 | kp.lockTransactionIdToChannelMap.Lock() |
| 611 | defer kp.lockTransactionIdToChannelMap.Unlock() |
| 612 | for key, value := range kp.transactionIdToChannelMap { |
| 613 | close(value.ch) |
| 614 | delete(kp.transactionIdToChannelMap, key) |
| 615 | } |
| 616 | } |
| 617 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 618 | func (kp *interContainerProxy) DeleteTopic(topic Topic) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 619 | // If we have any consumers on that topic we need to close them |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 620 | if err := kp.deleteFromTopicResponseChannelMap(topic.Name); err != nil { |
| 621 | logger.Errorw("delete-from-topic-responsechannelmap-failed", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 622 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 623 | if err := kp.deleteFromTopicRequestHandlerChannelMap(topic.Name); err != nil { |
| 624 | logger.Errorw("delete-from-topic-requesthandlerchannelmap-failed", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 625 | } |
| 626 | kp.deleteTopicTransactionIdToChannelMap(topic.Name) |
| 627 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 628 | return kp.kafkaClient.DeleteTopic(&topic) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 631 | func encodeReturnedValue(returnedVal interface{}) (*any.Any, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 632 | // Encode the response argument - needs to be a proto message |
| 633 | if returnedVal == nil { |
| 634 | return nil, nil |
| 635 | } |
| 636 | protoValue, ok := returnedVal.(proto.Message) |
| 637 | if !ok { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 638 | logger.Warnw("response-value-not-proto-message", log.Fields{"error": ok, "returnVal": returnedVal}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 639 | err := errors.New("response-value-not-proto-message") |
| 640 | return nil, err |
| 641 | } |
| 642 | |
| 643 | // Marshal the returned value, if any |
| 644 | var marshalledReturnedVal *any.Any |
| 645 | var err error |
| 646 | if marshalledReturnedVal, err = ptypes.MarshalAny(protoValue); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 647 | logger.Warnw("cannot-marshal-returned-val", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 648 | return nil, err |
| 649 | } |
| 650 | return marshalledReturnedVal, nil |
| 651 | } |
| 652 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 653 | func encodeDefaultFailedResponse(request *ic.InterContainerMessage) *ic.InterContainerMessage { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 654 | responseHeader := &ic.Header{ |
| 655 | Id: request.Header.Id, |
| 656 | Type: ic.MessageType_RESPONSE, |
| 657 | FromTopic: request.Header.ToTopic, |
| 658 | ToTopic: request.Header.FromTopic, |
Scott Baker | 84a55ce | 2020-04-17 10:11:30 -0700 | [diff] [blame] | 659 | Timestamp: ptypes.TimestampNow(), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 660 | } |
| 661 | responseBody := &ic.InterContainerResponseBody{ |
| 662 | Success: false, |
| 663 | Result: nil, |
| 664 | } |
| 665 | var marshalledResponseBody *any.Any |
| 666 | var err error |
| 667 | // Error should never happen here |
| 668 | if marshalledResponseBody, err = ptypes.MarshalAny(responseBody); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 669 | logger.Warnw("cannot-marshal-failed-response-body", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | return &ic.InterContainerMessage{ |
| 673 | Header: responseHeader, |
| 674 | Body: marshalledResponseBody, |
| 675 | } |
| 676 | |
| 677 | } |
| 678 | |
| 679 | //formatRequest formats a request to send over kafka and returns an InterContainerMessage message on success |
| 680 | //or an error on failure |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 681 | func encodeResponse(request *ic.InterContainerMessage, success bool, returnedValues ...interface{}) (*ic.InterContainerMessage, error) { |
| 682 | //logger.Debugw("encodeResponse", log.Fields{"success": success, "returnedValues": returnedValues}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 683 | responseHeader := &ic.Header{ |
| 684 | Id: request.Header.Id, |
| 685 | Type: ic.MessageType_RESPONSE, |
| 686 | FromTopic: request.Header.ToTopic, |
| 687 | ToTopic: request.Header.FromTopic, |
| 688 | KeyTopic: request.Header.KeyTopic, |
Scott Baker | 84a55ce | 2020-04-17 10:11:30 -0700 | [diff] [blame] | 689 | Timestamp: ptypes.TimestampNow(), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | // Go over all returned values |
| 693 | var marshalledReturnedVal *any.Any |
| 694 | var err error |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 695 | |
| 696 | // for now we support only 1 returned value - (excluding the error) |
| 697 | if len(returnedValues) > 0 { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 698 | if marshalledReturnedVal, err = encodeReturnedValue(returnedValues[0]); err != nil { |
| 699 | logger.Warnw("cannot-marshal-response-body", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 700 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | responseBody := &ic.InterContainerResponseBody{ |
| 704 | Success: success, |
| 705 | Result: marshalledReturnedVal, |
| 706 | } |
| 707 | |
| 708 | // Marshal the response body |
| 709 | var marshalledResponseBody *any.Any |
| 710 | if marshalledResponseBody, err = ptypes.MarshalAny(responseBody); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 711 | logger.Warnw("cannot-marshal-response-body", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 712 | return nil, err |
| 713 | } |
| 714 | |
| 715 | return &ic.InterContainerMessage{ |
| 716 | Header: responseHeader, |
| 717 | Body: marshalledResponseBody, |
| 718 | }, nil |
| 719 | } |
| 720 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 721 | func CallFuncByName(myClass interface{}, funcName string, params ...interface{}) (out []reflect.Value, err error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 722 | myClassValue := reflect.ValueOf(myClass) |
| 723 | // Capitalize the first letter in the funcName to workaround the first capital letters required to |
| 724 | // invoke a function from a different package |
| 725 | funcName = strings.Title(funcName) |
| 726 | m := myClassValue.MethodByName(funcName) |
| 727 | if !m.IsValid() { |
| 728 | return make([]reflect.Value, 0), fmt.Errorf("method-not-found \"%s\"", funcName) |
| 729 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 730 | in := make([]reflect.Value, len(params)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 731 | for i, param := range params { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 732 | in[i] = reflect.ValueOf(param) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 733 | } |
| 734 | out = m.Call(in) |
| 735 | return |
| 736 | } |
| 737 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 738 | func (kp *interContainerProxy) addTransactionId(transactionId string, currentArgs []*ic.Argument) []*ic.Argument { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 739 | arg := &KVArg{ |
| 740 | Key: TransactionKey, |
| 741 | Value: &ic.StrType{Val: transactionId}, |
| 742 | } |
| 743 | |
| 744 | var marshalledArg *any.Any |
| 745 | var err error |
| 746 | if marshalledArg, err = ptypes.MarshalAny(&ic.StrType{Val: transactionId}); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 747 | logger.Warnw("cannot-add-transactionId", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 748 | return currentArgs |
| 749 | } |
| 750 | protoArg := &ic.Argument{ |
| 751 | Key: arg.Key, |
| 752 | Value: marshalledArg, |
| 753 | } |
| 754 | return append(currentArgs, protoArg) |
| 755 | } |
| 756 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 757 | func (kp *interContainerProxy) addFromTopic(fromTopic string, currentArgs []*ic.Argument) []*ic.Argument { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 758 | var marshalledArg *any.Any |
| 759 | var err error |
| 760 | if marshalledArg, err = ptypes.MarshalAny(&ic.StrType{Val: fromTopic}); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 761 | logger.Warnw("cannot-add-transactionId", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 762 | return currentArgs |
| 763 | } |
| 764 | protoArg := &ic.Argument{ |
| 765 | Key: FromTopic, |
| 766 | Value: marshalledArg, |
| 767 | } |
| 768 | return append(currentArgs, protoArg) |
| 769 | } |
| 770 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 771 | func (kp *interContainerProxy) handleMessage(msg *ic.InterContainerMessage, targetInterface interface{}) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 772 | |
| 773 | // First extract the header to know whether this is a request - responses are handled by a different handler |
| 774 | if msg.Header.Type == ic.MessageType_REQUEST { |
| 775 | var out []reflect.Value |
| 776 | var err error |
| 777 | |
| 778 | // Get the request body |
| 779 | requestBody := &ic.InterContainerRequestBody{} |
| 780 | if err = ptypes.UnmarshalAny(msg.Body, requestBody); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 781 | logger.Warnw("cannot-unmarshal-request", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 782 | } else { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 783 | logger.Debugw("received-request", log.Fields{"rpc": requestBody.Rpc, "header": msg.Header}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 784 | // let the callee unpack the arguments as its the only one that knows the real proto type |
| 785 | // Augment the requestBody with the message Id as it will be used in scenarios where cores |
| 786 | // are set in pairs and competing |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 787 | requestBody.Args = kp.addTransactionId(msg.Header.Id, requestBody.Args) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 788 | |
| 789 | // Augment the requestBody with the From topic name as it will be used in scenarios where a container |
| 790 | // needs to send an unsollicited message to the currently requested container |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 791 | requestBody.Args = kp.addFromTopic(msg.Header.FromTopic, requestBody.Args) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 792 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 793 | out, err = CallFuncByName(targetInterface, requestBody.Rpc, requestBody.Args) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 794 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 795 | logger.Warn(err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | // Response required? |
| 799 | if requestBody.ResponseRequired { |
| 800 | // If we already have an error before then just return that |
| 801 | var returnError *ic.Error |
| 802 | var returnedValues []interface{} |
| 803 | var success bool |
| 804 | if err != nil { |
| 805 | returnError = &ic.Error{Reason: err.Error()} |
| 806 | returnedValues = make([]interface{}, 1) |
| 807 | returnedValues[0] = returnError |
| 808 | } else { |
| 809 | returnedValues = make([]interface{}, 0) |
| 810 | // Check for errors first |
| 811 | lastIndex := len(out) - 1 |
| 812 | if out[lastIndex].Interface() != nil { // Error |
| 813 | if retError, ok := out[lastIndex].Interface().(error); ok { |
| 814 | if retError.Error() == ErrorTransactionNotAcquired.Error() { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 815 | logger.Debugw("Ignoring request", log.Fields{"error": retError, "txId": msg.Header.Id}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 816 | return // Ignore - process is in competing mode and ignored transaction |
| 817 | } |
| 818 | returnError = &ic.Error{Reason: retError.Error()} |
| 819 | returnedValues = append(returnedValues, returnError) |
| 820 | } else { // Should never happen |
| 821 | returnError = &ic.Error{Reason: "incorrect-error-returns"} |
| 822 | returnedValues = append(returnedValues, returnError) |
| 823 | } |
| 824 | } else if len(out) == 2 && reflect.ValueOf(out[0].Interface()).IsValid() && reflect.ValueOf(out[0].Interface()).IsNil() { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 825 | logger.Warnw("Unexpected response of (nil,nil)", log.Fields{"txId": msg.Header.Id}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 826 | return // Ignore - should not happen |
| 827 | } else { // Non-error case |
| 828 | success = true |
| 829 | for idx, val := range out { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 830 | //logger.Debugw("returned-api-response-loop", log.Fields{"idx": idx, "val": val.Interface()}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 831 | if idx != lastIndex { |
| 832 | returnedValues = append(returnedValues, val.Interface()) |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | var icm *ic.InterContainerMessage |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 839 | if icm, err = encodeResponse(msg, success, returnedValues...); err != nil { |
| 840 | logger.Warnw("error-encoding-response-returning-failure-result", log.Fields{"error": err}) |
| 841 | icm = encodeDefaultFailedResponse(msg) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 842 | } |
| 843 | // To preserve ordering of messages, all messages to a given topic are sent to the same partition |
| 844 | // by providing a message key. The key is encoded in the topic name. If the deviceId is not |
| 845 | // present then the key will be empty, hence all messages for a given topic will be sent to all |
| 846 | // partitions. |
| 847 | replyTopic := &Topic{Name: msg.Header.FromTopic} |
| 848 | key := msg.Header.KeyTopic |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 849 | logger.Debugw("sending-response-to-kafka", log.Fields{"rpc": requestBody.Rpc, "header": icm.Header, "key": key}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 850 | // TODO: handle error response. |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 851 | go func() { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 852 | if err := kp.kafkaClient.Send(icm, replyTopic, key); err != nil { |
| 853 | logger.Errorw("send-reply-failed", log.Fields{ |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 854 | "topic": replyTopic, |
| 855 | "key": key, |
| 856 | "error": err}) |
| 857 | } |
| 858 | }() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 859 | } |
| 860 | } else if msg.Header.Type == ic.MessageType_RESPONSE { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 861 | logger.Debugw("response-received", log.Fields{"msg-header": msg.Header}) |
| 862 | go kp.dispatchResponse(msg) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 863 | } else { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 864 | logger.Warnw("unsupported-message-received", log.Fields{"msg-header": msg.Header}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 868 | func (kp *interContainerProxy) waitForMessages(ch <-chan *ic.InterContainerMessage, topic Topic, targetInterface interface{}) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 869 | // Wait for messages |
| 870 | for msg := range ch { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 871 | //logger.Debugw("request-received", log.Fields{"msg": msg, "topic": topic.Name, "target": targetInterface}) |
| 872 | go kp.handleMessage(msg, targetInterface) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 876 | func (kp *interContainerProxy) dispatchResponse(msg *ic.InterContainerMessage) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 877 | kp.lockTransactionIdToChannelMap.RLock() |
| 878 | defer kp.lockTransactionIdToChannelMap.RUnlock() |
| 879 | if _, exist := kp.transactionIdToChannelMap[msg.Header.Id]; !exist { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 880 | logger.Debugw("no-waiting-channel", log.Fields{"transaction": msg.Header.Id}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 881 | return |
| 882 | } |
| 883 | kp.transactionIdToChannelMap[msg.Header.Id].ch <- msg |
| 884 | } |
| 885 | |
| 886 | // subscribeForResponse allows a caller to subscribe to a given topic when waiting for a response. |
| 887 | // This method is built to prevent all subscribers to receive all messages as is the case of the Subscribe |
| 888 | // API. There is one response channel waiting for kafka messages before dispatching the message to the |
| 889 | // corresponding waiting channel |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 890 | func (kp *interContainerProxy) subscribeForResponse(topic Topic, trnsId string) (chan *ic.InterContainerMessage, error) { |
| 891 | logger.Debugw("subscribeForResponse", log.Fields{"topic": topic.Name, "trnsid": trnsId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 892 | |
| 893 | // Create a specific channel for this consumers. We cannot use the channel from the kafkaclient as it will |
| 894 | // broadcast any message for this topic to all channels waiting on it. |
Scott Baker | ae1d470 | 2020-03-04 14:10:51 -0800 | [diff] [blame] | 895 | // Set channel size to 1 to prevent deadlock, see VOL-2708 |
| 896 | ch := make(chan *ic.InterContainerMessage, 1) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 897 | kp.addToTransactionIdToChannelMap(trnsId, &topic, ch) |
| 898 | |
| 899 | return ch, nil |
| 900 | } |
| 901 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 902 | func (kp *interContainerProxy) unSubscribeForResponse(trnsId string) error { |
| 903 | logger.Debugw("unsubscribe-for-response", log.Fields{"trnsId": trnsId}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 904 | kp.deleteFromTransactionIdToChannelMap(trnsId) |
| 905 | return nil |
| 906 | } |
| 907 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 908 | func (kp *interContainerProxy) EnableLivenessChannel(enable bool) chan bool { |
| 909 | return kp.kafkaClient.EnableLivenessChannel(enable) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 912 | func (kp *interContainerProxy) EnableHealthinessChannel(enable bool) chan bool { |
| 913 | return kp.kafkaClient.EnableHealthinessChannel(enable) |
Scott Baker | 0fef698 | 2019-12-12 09:49:42 -0800 | [diff] [blame] | 914 | } |
| 915 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 916 | func (kp *interContainerProxy) SendLiveness() error { |
| 917 | return kp.kafkaClient.SendLiveness() |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 920 | //formatRequest formats a request to send over kafka and returns an InterContainerMessage message on success |
| 921 | //or an error on failure |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 922 | func encodeRequest(rpc string, toTopic *Topic, replyTopic *Topic, key string, kvArgs ...*KVArg) (*ic.InterContainerMessage, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 923 | requestHeader := &ic.Header{ |
| 924 | Id: uuid.New().String(), |
| 925 | Type: ic.MessageType_REQUEST, |
| 926 | FromTopic: replyTopic.Name, |
| 927 | ToTopic: toTopic.Name, |
| 928 | KeyTopic: key, |
Scott Baker | 84a55ce | 2020-04-17 10:11:30 -0700 | [diff] [blame] | 929 | Timestamp: ptypes.TimestampNow(), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 930 | } |
| 931 | requestBody := &ic.InterContainerRequestBody{ |
| 932 | Rpc: rpc, |
| 933 | ResponseRequired: true, |
| 934 | ReplyToTopic: replyTopic.Name, |
| 935 | } |
| 936 | |
| 937 | for _, arg := range kvArgs { |
| 938 | if arg == nil { |
| 939 | // In case the caller sends an array with empty args |
| 940 | continue |
| 941 | } |
| 942 | var marshalledArg *any.Any |
| 943 | var err error |
| 944 | // ascertain the value interface type is a proto.Message |
| 945 | protoValue, ok := arg.Value.(proto.Message) |
| 946 | if !ok { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 947 | logger.Warnw("argument-value-not-proto-message", log.Fields{"error": ok, "Value": arg.Value}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 948 | err := errors.New("argument-value-not-proto-message") |
| 949 | return nil, err |
| 950 | } |
| 951 | if marshalledArg, err = ptypes.MarshalAny(protoValue); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 952 | logger.Warnw("cannot-marshal-request", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 953 | return nil, err |
| 954 | } |
| 955 | protoArg := &ic.Argument{ |
| 956 | Key: arg.Key, |
| 957 | Value: marshalledArg, |
| 958 | } |
| 959 | requestBody.Args = append(requestBody.Args, protoArg) |
| 960 | } |
| 961 | |
| 962 | var marshalledData *any.Any |
| 963 | var err error |
| 964 | if marshalledData, err = ptypes.MarshalAny(requestBody); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 965 | logger.Warnw("cannot-marshal-request", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 966 | return nil, err |
| 967 | } |
| 968 | request := &ic.InterContainerMessage{ |
| 969 | Header: requestHeader, |
| 970 | Body: marshalledData, |
| 971 | } |
| 972 | return request, nil |
| 973 | } |
| 974 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 975 | func decodeResponse(response *ic.InterContainerMessage) (*ic.InterContainerResponseBody, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 976 | // Extract the message body |
| 977 | responseBody := ic.InterContainerResponseBody{} |
| 978 | if err := ptypes.UnmarshalAny(response.Body, &responseBody); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 979 | logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 980 | return nil, err |
| 981 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame^] | 982 | //logger.Debugw("response-decoded-successfully", log.Fields{"response-status": &responseBody.Success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 983 | |
| 984 | return &responseBody, nil |
| 985 | |
| 986 | } |