Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "time" |
| 22 | |
| 23 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
| 24 | "github.com/opencord/voltha-go/rw_core/core/api" |
| 25 | "github.com/opencord/voltha-go/rw_core/core/device" |
| 26 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 27 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 28 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
| 29 | ) |
| 30 | |
| 31 | // startKafkInterContainerProxy is responsible for starting the Kafka Interadapter Proxy |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 32 | func startKafkInterContainerProxy(ctx context.Context, kafkaClient kafka.Client, address string, coreTopic string, connectionRetryInterval time.Duration) (kafka.InterContainerProxy, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 33 | logger.Infow(ctx, "initialize-kafka-manager", log.Fields{"address": address, "topic": coreTopic}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 34 | |
| 35 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPreparing) |
| 36 | |
| 37 | // create the kafka RPC proxy |
| 38 | kmp := kafka.NewInterContainerProxy( |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 39 | kafka.InterContainerAddress(address), |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 40 | kafka.MsgClient(kafkaClient), |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 41 | kafka.DefaultTopic(&kafka.Topic{Name: coreTopic})) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 42 | |
| 43 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPrepared) |
| 44 | |
| 45 | // wait for connectivity |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 46 | logger.Infow(ctx, "starting-kafka-manager", log.Fields{"address": address, "topic": coreTopic}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 47 | |
| 48 | for { |
| 49 | // If we haven't started yet, then try to start |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 50 | logger.Infow(ctx, "starting-kafka-proxy", log.Fields{}) |
| 51 | if err := kmp.Start(ctx); err != nil { |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 52 | // We failed to start. Delay and then try again later. |
| 53 | // Don't worry about liveness, as we can't be live until we've started. |
| 54 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 55 | logger.Infow(ctx, "error-starting-kafka-messaging-proxy", log.Fields{"error": err}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 56 | select { |
| 57 | case <-time.After(connectionRetryInterval): |
| 58 | case <-ctx.Done(): |
| 59 | return nil, ctx.Err() |
| 60 | } |
| 61 | continue |
| 62 | } |
| 63 | // We started. We only need to do this once. |
| 64 | // Next we'll fall through and start checking liveness. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 65 | logger.Infow(ctx, "started-kafka-proxy", log.Fields{}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 66 | break |
| 67 | } |
| 68 | return kmp, nil |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * monitorKafkaLiveness is responsible for monitoring the Kafka Interadapter Proxy connectivity state |
| 73 | * |
| 74 | * Any producer that fails to send will cause KafkaInterContainerProxy to |
| 75 | * post a false event on its liveness channel. Any producer that succeeds in sending |
| 76 | * will cause KafkaInterContainerProxy to post a true event on its liveness |
| 77 | * channel. Group receivers also update liveness state, and a receiver will typically |
| 78 | * indicate a loss of liveness within 3-5 seconds of Kafka going down. Receivers |
| 79 | * only indicate restoration of liveness if a message is received. During normal |
| 80 | * operation, messages will be routinely produced and received, automatically |
| 81 | * indicating liveness state. These routine liveness indications are rate-limited |
| 82 | * inside sarama_client. |
| 83 | * |
| 84 | * This thread monitors the status of KafkaInterContainerProxy's liveness and pushes |
| 85 | * that state to the core's readiness probes. If no liveness event has been seen |
| 86 | * within a timeout, then the thread will make an attempt to produce a "liveness" |
| 87 | * message, which will in turn trigger a liveness event on the liveness channel, true |
| 88 | * or false depending on whether the attempt succeeded. |
| 89 | * |
| 90 | * The gRPC server in turn monitors the state of the readiness probe and will |
| 91 | * start issuing UNAVAILABLE response while the probe is not ready. |
| 92 | * |
| 93 | * startupRetryInterval -- interval between attempts to start |
| 94 | * liveProbeInterval -- interval between liveness checks when in a live state |
| 95 | * notLiveProbeInterval -- interval between liveness checks when in a notLive state |
| 96 | * |
| 97 | * liveProbeInterval and notLiveProbeInterval can be configured separately, |
| 98 | * though the current default is that both are set to 60 seconds. |
| 99 | */ |
| 100 | func monitorKafkaLiveness(ctx context.Context, kmp kafka.InterContainerProxy, liveProbeInterval time.Duration, notLiveProbeInterval time.Duration) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 101 | logger.Info(ctx, "started-kafka-message-proxy") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 102 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 103 | livenessChannel := kmp.EnableLivenessChannel(ctx, true) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 104 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 105 | logger.Info(ctx, "enabled-kafka-liveness-channel") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 106 | |
| 107 | timeout := liveProbeInterval |
| 108 | for { |
| 109 | timeoutTimer := time.NewTimer(timeout) |
| 110 | select { |
| 111 | case liveness := <-livenessChannel: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 112 | logger.Infow(ctx, "kafka-manager-thread-liveness-event", log.Fields{"liveness": liveness}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 113 | // there was a state change in Kafka liveness |
| 114 | if !liveness { |
| 115 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 116 | logger.Info(ctx, "kafka-manager-thread-set-server-notready") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 117 | |
| 118 | // retry frequently while life is bad |
| 119 | timeout = notLiveProbeInterval |
| 120 | } else { |
| 121 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 122 | logger.Info(ctx, "kafka-manager-thread-set-server-ready") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 123 | |
| 124 | // retry infrequently while life is good |
| 125 | timeout = liveProbeInterval |
| 126 | } |
| 127 | if !timeoutTimer.Stop() { |
| 128 | <-timeoutTimer.C |
| 129 | } |
| 130 | case <-timeoutTimer.C: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 131 | logger.Info(ctx, "kafka-proxy-liveness-recheck") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 132 | // send the liveness probe in a goroutine; we don't want to deadlock ourselves as |
| 133 | // the liveness probe may wait (and block) writing to our channel. |
| 134 | go func() { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 135 | err := kmp.SendLiveness(ctx) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 136 | if err != nil { |
| 137 | // Catch possible error case if sending liveness after Sarama has been stopped. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 138 | logger.Warnw(ctx, "error-kafka-send-liveness", log.Fields{"error": err}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 139 | } |
| 140 | }() |
| 141 | case <-ctx.Done(): |
| 142 | return // just exit |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 147 | func registerAdapterRequestHandlers(ctx context.Context, kmp kafka.InterContainerProxy, dMgr *device.Manager, aMgr *adapter.Manager, coreTopic string) { |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 148 | requestProxy := api.NewAdapterRequestHandlerProxy(dMgr, aMgr) |
| 149 | |
| 150 | // Register the broadcast topic to handle any core-bound broadcast requests |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 151 | if err := kmp.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: coreTopic}, requestProxy); err != nil { |
| 152 | logger.Fatalw(ctx, "Failed-registering-broadcast-handler", log.Fields{"topic": coreTopic}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 153 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 154 | |
| 155 | logger.Info(ctx, "request-handler-registered") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 156 | } |