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