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