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