blob: ef0840254948a38c105a6069afe5860e0eb2cdb6 [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoob9203542018-09-17 22:56:37 -040017package core
18
19import (
20 "context"
npujar1d86a522019-11-14 17:11:16 +053021 "time"
22
sbarbari17d7e222019-11-05 10:02:29 -050023 "github.com/opencord/voltha-go/db/model"
khenaidoob9203542018-09-17 22:56:37 -040024 "github.com/opencord/voltha-go/rw_core/config"
sbarbari17d7e222019-11-05 10:02:29 -050025 "github.com/opencord/voltha-lib-go/v2/pkg/db"
Scott Baker807addd2019-10-24 15:16:21 -070026 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
Scott Baker807addd2019-10-24 15:16:21 -070027 grpcserver "github.com/opencord/voltha-lib-go/v2/pkg/grpc"
28 "github.com/opencord/voltha-lib-go/v2/pkg/kafka"
29 "github.com/opencord/voltha-lib-go/v2/pkg/log"
30 "github.com/opencord/voltha-lib-go/v2/pkg/probe"
Scott Baker555307d2019-11-04 08:58:01 -080031 "github.com/opencord/voltha-protos/v2/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040032 "google.golang.org/grpc"
khenaidoob3244212019-08-27 14:32:27 -040033 "google.golang.org/grpc/codes"
34 "google.golang.org/grpc/status"
khenaidoob9203542018-09-17 22:56:37 -040035)
36
npujar1d86a522019-11-14 17:11:16 +053037// Core represent read,write core attributes
khenaidoob9203542018-09-17 22:56:37 -040038type Core struct {
npujar1d86a522019-11-14 17:11:16 +053039 instanceID string
khenaidoob9203542018-09-17 22:56:37 -040040 deviceMgr *DeviceManager
41 logicalDeviceMgr *LogicalDeviceManager
42 grpcServer *grpcserver.GrpcServer
Richard Jankowskidbab94a2018-12-06 16:20:25 -050043 grpcNBIAPIHandler *APIHandler
khenaidoo2c6a0992019-04-29 13:46:56 -040044 adapterMgr *AdapterManager
khenaidoob9203542018-09-17 22:56:37 -040045 config *config.RWCoreFlags
khenaidoo43c82122018-11-22 18:38:28 -050046 kmp *kafka.InterContainerProxy
khenaidoo92e62c52018-10-03 14:02:54 -040047 clusterDataRoot model.Root
48 localDataRoot model.Root
khenaidoob9203542018-09-17 22:56:37 -040049 clusterDataProxy *model.Proxy
50 localDataProxy *model.Proxy
51 exitChannel chan int
Richard Jankowskie4d77662018-10-17 13:53:21 -040052 kvClient kvstore.Client
Girish Kumar4d3887d2019-11-22 14:22:05 +000053 backend db.Backend
khenaidoo43c82122018-11-22 18:38:28 -050054 kafkaClient kafka.Client
khenaidoo2c6a0992019-04-29 13:46:56 -040055 deviceOwnership *DeviceOwnership
khenaidoob9203542018-09-17 22:56:37 -040056}
57
58func init() {
npujar1d86a522019-11-14 17:11:16 +053059 _, err := log.AddPackage(log.JSON, log.WarnLevel, nil)
60 if err != nil {
61 log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err})
62 }
khenaidoob9203542018-09-17 22:56:37 -040063}
64
npujar1d86a522019-11-14 17:11:16 +053065// NewCore creates instance of rw core
khenaidoo43c82122018-11-22 18:38:28 -050066func NewCore(id string, cf *config.RWCoreFlags, kvClient kvstore.Client, kafkaClient kafka.Client) *Core {
khenaidoob9203542018-09-17 22:56:37 -040067 var core Core
npujar1d86a522019-11-14 17:11:16 +053068 core.instanceID = id
khenaidoob9203542018-09-17 22:56:37 -040069 core.exitChannel = make(chan int, 1)
70 core.config = cf
Richard Jankowskie4d77662018-10-17 13:53:21 -040071 core.kvClient = kvClient
khenaidoo43c82122018-11-22 18:38:28 -050072 core.kafkaClient = kafkaClient
Richard Jankowskie4d77662018-10-17 13:53:21 -040073
Girish Kumar4d3887d2019-11-22 14:22:05 +000074 // Configure backend to push Liveness Status at least every (cf.LiveProbeInterval / 2) seconds
75 // so as to avoid trigger of Liveness check (due to Liveness timeout) when backend is alive
76 livenessChannelInterval := cf.LiveProbeInterval / 2
77
Richard Jankowskie4d77662018-10-17 13:53:21 -040078 // Setup the KV store
Girish Kumar4d3887d2019-11-22 14:22:05 +000079 core.backend = db.Backend{
80 Client: kvClient,
81 StoreType: cf.KVStoreType,
82 Host: cf.KVStoreHost,
83 Port: cf.KVStorePort,
84 Timeout: cf.KVStoreTimeout,
85 LivenessChannelInterval: livenessChannelInterval,
86 PathPrefix: cf.KVStoreDataPrefix}
87 core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &core.backend)
88 core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &core.backend)
Stephane Barbarieef6650d2019-07-18 12:15:09 -040089 core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false)
90 core.localDataProxy = core.localDataRoot.CreateProxy(context.Background(), "/", false)
khenaidoob9203542018-09-17 22:56:37 -040091 return &core
92}
93
npujar1d86a522019-11-14 17:11:16 +053094// Start brings up core services
khenaidoob9203542018-09-17 22:56:37 -040095func (core *Core) Start(ctx context.Context) {
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -070096
97 // If the context has a probe then fetch it and register our services
98 var p *probe.Probe
99 if value := ctx.Value(probe.ProbeContextKey); value != nil {
100 if _, ok := value.(*probe.Probe); ok {
101 p = value.(*probe.Probe)
102 p.RegisterService(
103 "message-bus",
104 "kv-store",
105 "device-manager",
106 "logical-device-manager",
107 "adapter-manager",
108 "grpc-service",
109 )
110 }
111 }
112
npujar1d86a522019-11-14 17:11:16 +0530113 log.Info("starting-core-services", log.Fields{"coreId": core.instanceID})
khenaidoob3244212019-08-27 14:32:27 -0400114
115 // Wait until connection to KV Store is up
116 if err := core.waitUntilKVStoreReachableOrMaxTries(ctx, core.config.MaxConnectionRetries, core.config.ConnectionRetryInterval); err != nil {
117 log.Fatal("Unable-to-connect-to-KV-store")
118 }
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700119 if p != nil {
120 p.UpdateStatus("kv-store", probe.ServiceStatusRunning)
121 }
khenaidoob3244212019-08-27 14:32:27 -0400122
Scott Bakeree6a0872019-10-29 15:59:52 -0700123 // core.kmp must be created before deviceMgr and adapterMgr, as they will make
124 // private copies of the poiner to core.kmp.
125 if err := core.initKafkaManager(ctx); err != nil {
126 log.Fatal("Failed-to-init-kafka-manager")
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700127 }
khenaidoob3244212019-08-27 14:32:27 -0400128
khenaidoo631fe542019-05-31 15:44:43 -0400129 log.Debugw("values", log.Fields{"kmp": core.kmp})
Richard Jankowski199fd862019-03-18 14:49:51 -0400130 core.deviceMgr = newDeviceManager(core)
npujar1d86a522019-11-14 17:11:16 +0530131 core.adapterMgr = newAdapterManager(core.clusterDataProxy, core.instanceID, core.deviceMgr)
khenaidooba6b6c42019-08-02 09:11:56 -0400132 core.deviceMgr.adapterMgr = core.adapterMgr
khenaidoo2c6a0992019-04-29 13:46:56 -0400133 core.logicalDeviceMgr = newLogicalDeviceManager(core, core.deviceMgr, core.kmp, core.clusterDataProxy, core.config.DefaultCoreTimeout)
khenaidoo54e0ddf2019-02-27 16:21:33 -0500134
Scott Bakeree6a0872019-10-29 15:59:52 -0700135 // Start the KafkaManager. This must be done after the deviceMgr, adapterMgr, and
136 // logicalDeviceMgr have been created, as once the kmp is started, it will register
137 // the above with the kmp.
138
139 go core.startKafkaManager(ctx,
140 core.config.ConnectionRetryInterval,
141 core.config.LiveProbeInterval,
142 core.config.NotLiveProbeInterval)
khenaidoob3244212019-08-27 14:32:27 -0400143
khenaidoob9203542018-09-17 22:56:37 -0400144 go core.startDeviceManager(ctx)
145 go core.startLogicalDeviceManager(ctx)
146 go core.startGRPCService(ctx)
khenaidoo21d51152019-02-01 13:48:37 -0500147 go core.startAdapterManager(ctx)
Girish Kumar4d3887d2019-11-22 14:22:05 +0000148 go core.monitorKvstoreLiveness(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400149
khenaidoo1ce37ad2019-03-24 22:07:24 -0400150 // Setup device ownership context
npujar1d86a522019-11-14 17:11:16 +0530151 core.deviceOwnership = NewDeviceOwnership(core.instanceID, core.kvClient, core.deviceMgr, core.logicalDeviceMgr,
khenaidoo1ce37ad2019-03-24 22:07:24 -0400152 "service/voltha/owns_device", 10)
153
khenaidoob3244212019-08-27 14:32:27 -0400154 log.Info("core-services-started")
khenaidoob9203542018-09-17 22:56:37 -0400155}
156
npujar1d86a522019-11-14 17:11:16 +0530157// Stop brings down core services
khenaidoob9203542018-09-17 22:56:37 -0400158func (core *Core) Stop(ctx context.Context) {
khenaidoo19374072018-12-11 11:05:15 -0500159 log.Info("stopping-adaptercore")
David Bainbridgef794fc52019-10-03 22:37:12 +0000160 if core.exitChannel != nil {
161 core.exitChannel <- 1
162 }
khenaidoo43c82122018-11-22 18:38:28 -0500163 // Stop all the started services
David Bainbridgef794fc52019-10-03 22:37:12 +0000164 if core.grpcServer != nil {
165 core.grpcServer.Stop()
166 }
167 if core.logicalDeviceMgr != nil {
168 core.logicalDeviceMgr.stop(ctx)
169 }
170 if core.deviceMgr != nil {
171 core.deviceMgr.stop(ctx)
172 }
173 if core.kmp != nil {
174 core.kmp.Stop()
175 }
khenaidoo19374072018-12-11 11:05:15 -0500176 log.Info("adaptercore-stopped")
khenaidoob9203542018-09-17 22:56:37 -0400177}
178
khenaidoo631fe542019-05-31 15:44:43 -0400179//startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server
khenaidoob9203542018-09-17 22:56:37 -0400180func (core *Core) startGRPCService(ctx context.Context) {
181 // create an insecure gserver server
Scott Bakeree6a0872019-10-29 15:59:52 -0700182 core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false, probe.GetProbeFromContext(ctx))
khenaidoob9203542018-09-17 22:56:37 -0400183 log.Info("grpc-server-created")
184
khenaidoo54e0ddf2019-02-27 16:21:33 -0500185 core.grpcNBIAPIHandler = NewAPIHandler(core)
Richard Jankowski46464e92019-03-05 11:53:55 -0500186 log.Infow("grpc-handler", log.Fields{"core_binding_key": core.config.CoreBindingKey})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500187 core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler)
khenaidoob9203542018-09-17 22:56:37 -0400188 // Create a function to register the core GRPC service with the GRPC server
189 f := func(gs *grpc.Server) {
190 voltha.RegisterVolthaServiceServer(
191 gs,
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500192 core.grpcNBIAPIHandler,
khenaidoob9203542018-09-17 22:56:37 -0400193 )
194 }
195
196 core.grpcServer.AddService(f)
197 log.Info("grpc-service-added")
198
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700199 /*
200 * Start the GRPC server
201 *
202 * This is a bit sub-optimal here as the grpcServer.Start call does not return (blocks)
203 * until something fails, but we want to send a "start" status update. As written this
204 * means that we are actually sending the "start" status update before the server is
205 * started, which means it is possible that the status is "running" before it actually is.
206 *
207 * This means that there is a small window in which the core could return its status as
208 * ready, when it really isn't.
209 */
210 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning)
khenaidoob9203542018-09-17 22:56:37 -0400211 log.Info("grpc-server-started")
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700212 core.grpcServer.Start(context.Background())
213 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped)
khenaidoob9203542018-09-17 22:56:37 -0400214}
215
Scott Bakeree6a0872019-10-29 15:59:52 -0700216// Initialize the kafka manager, but we will start it later
217func (core *Core) initKafkaManager(ctx context.Context) error {
218 log.Infow("initialize-kafka-manager", log.Fields{"host": core.config.KafkaAdapterHost,
khenaidoob9203542018-09-17 22:56:37 -0400219 "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic})
Scott Bakeree6a0872019-10-29 15:59:52 -0700220
221 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPreparing)
222
223 // create the proxy
khenaidoob9203542018-09-17 22:56:37 -0400224 var err error
khenaidoo43c82122018-11-22 18:38:28 -0500225 if core.kmp, err = kafka.NewInterContainerProxy(
226 kafka.InterContainerHost(core.config.KafkaAdapterHost),
227 kafka.InterContainerPort(core.config.KafkaAdapterPort),
228 kafka.MsgClient(core.kafkaClient),
khenaidoo79232702018-12-04 11:00:41 -0500229 kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}),
230 kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic})); err != nil {
khenaidoob9203542018-09-17 22:56:37 -0400231 log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err})
232 return err
233 }
Scott Bakeree6a0872019-10-29 15:59:52 -0700234
235 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPrepared)
236
237 return nil
238}
239
240/*
241 * KafkaMonitorThread
242 *
npujar1d86a522019-11-14 17:11:16 +0530243 * Responsible for starting the Kafka Interadapter Proxy and monitoring its liveness
Scott Bakeree6a0872019-10-29 15:59:52 -0700244 * state.
245 *
246 * Any producer that fails to send will cause KafkaInterContainerProxy to
247 * post a false event on its liveness channel. Any producer that succeeds in sending
248 * will cause KafkaInterContainerProxy to post a true event on its liveness
npujar1d86a522019-11-14 17:11:16 +0530249 * channel. Group receivers also update liveness state, and a receiver will typically
Scott Bakeree6a0872019-10-29 15:59:52 -0700250 * indicate a loss of liveness within 3-5 seconds of Kafka going down. Receivers
251 * only indicate restoration of liveness if a message is received. During normal
252 * operation, messages will be routinely produced and received, automatically
253 * indicating liveness state. These routine liveness indications are rate-limited
254 * inside sarama_client.
255 *
256 * This thread monitors the status of KafkaInterContainerProxy's liveness and pushes
257 * that state to the core's readiness probes. If no liveness event has been seen
258 * within a timeout, then the thread will make an attempt to produce a "liveness"
259 * message, which will in turn trigger a liveness event on the liveness channel, true
260 * or false depending on whether the attempt succeeded.
261 *
262 * The gRPC server in turn monitors the state of the readiness probe and will
263 * start issuing UNAVAILABLE response while the probe is not ready.
264 *
265 * startupRetryInterval -- interval between attempts to start
266 * liveProbeInterval -- interval between liveness checks when in a live state
267 * notLiveProbeInterval -- interval between liveness checks when in a notLive state
268 *
269 * liveProbeInterval and notLiveProbeInterval can be configured separately,
270 * though the current default is that both are set to 60 seconds.
271 */
272
Girish Kumar4d3887d2019-11-22 14:22:05 +0000273func (core *Core) startKafkaManager(ctx context.Context, startupRetryInterval time.Duration, liveProbeInterval time.Duration, notLiveProbeInterval time.Duration) {
Scott Bakeree6a0872019-10-29 15:59:52 -0700274 log.Infow("starting-kafka-manager-thread", log.Fields{"host": core.config.KafkaAdapterHost,
275 "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic})
276
277 started := false
278 for !started {
279 // If we haven't started yet, then try to start
280 log.Infow("starting-kafka-proxy", log.Fields{})
281 if err := core.kmp.Start(); err != nil {
282 // We failed to start. Delay and then try again later.
283 // Don't worry about liveness, as we can't be live until we've started.
284 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
khenaidoob3244212019-08-27 14:32:27 -0400285 log.Infow("error-starting-kafka-messaging-proxy", log.Fields{"error": err})
Girish Kumar4d3887d2019-11-22 14:22:05 +0000286 time.Sleep(startupRetryInterval)
khenaidoob3244212019-08-27 14:32:27 -0400287 } else {
Scott Bakeree6a0872019-10-29 15:59:52 -0700288 // We started. We only need to do this once.
289 // Next we'll fall through and start checking liveness.
290 log.Infow("started-kafka-proxy", log.Fields{})
291
292 // cannot do this until after the kmp is started
npujar1d86a522019-11-14 17:11:16 +0530293 if err := core.registerAdapterRequestHandlers(ctx, core.instanceID, core.deviceMgr, core.logicalDeviceMgr, core.adapterMgr, core.clusterDataProxy, core.localDataProxy); err != nil {
Scott Bakeree6a0872019-10-29 15:59:52 -0700294 log.Fatal("Failure-registering-adapterRequestHandler")
295 }
296
297 started = true
khenaidoob3244212019-08-27 14:32:27 -0400298 }
khenaidoob9203542018-09-17 22:56:37 -0400299 }
Scott Bakeree6a0872019-10-29 15:59:52 -0700300
301 log.Info("started-kafka-message-proxy")
302
303 livenessChannel := core.kmp.EnableLivenessChannel(true)
304
305 log.Info("enabled-kafka-liveness-channel")
306
Girish Kumar4d3887d2019-11-22 14:22:05 +0000307 timeout := liveProbeInterval
Scott Bakeree6a0872019-10-29 15:59:52 -0700308 for {
309 timeoutTimer := time.NewTimer(timeout)
310 select {
311 case liveness := <-livenessChannel:
312 log.Infow("kafka-manager-thread-liveness-event", log.Fields{"liveness": liveness})
313 // there was a state change in Kafka liveness
314 if !liveness {
315 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
316
317 if core.grpcServer != nil {
318 log.Info("kafka-manager-thread-set-server-notready")
319 }
320
321 // retry frequently while life is bad
Girish Kumar4d3887d2019-11-22 14:22:05 +0000322 timeout = notLiveProbeInterval
Scott Bakeree6a0872019-10-29 15:59:52 -0700323 } else {
324 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning)
325
326 if core.grpcServer != nil {
327 log.Info("kafka-manager-thread-set-server-ready")
328 }
329
330 // retry infrequently while life is good
Girish Kumar4d3887d2019-11-22 14:22:05 +0000331 timeout = liveProbeInterval
Scott Bakeree6a0872019-10-29 15:59:52 -0700332 }
333 if !timeoutTimer.Stop() {
334 <-timeoutTimer.C
335 }
336 case <-timeoutTimer.C:
337 log.Info("kafka-proxy-liveness-recheck")
338 // send the liveness probe in a goroutine; we don't want to deadlock ourselves as
339 // the liveness probe may wait (and block) writing to our channel.
340 go func() {
341 err := core.kmp.SendLiveness()
342 if err != nil {
343 // Catch possible error case if sending liveness after Sarama has been stopped.
344 log.Warnw("error-kafka-send-liveness", log.Fields{"error": err})
345 }
346 }()
347 }
348 }
khenaidoob9203542018-09-17 22:56:37 -0400349}
350
khenaidoob3244212019-08-27 14:32:27 -0400351// waitUntilKVStoreReachableOrMaxTries will wait until it can connect to a KV store or until maxtries has been reached
Girish Kumar4d3887d2019-11-22 14:22:05 +0000352func (core *Core) waitUntilKVStoreReachableOrMaxTries(ctx context.Context, maxRetries int, retryInterval time.Duration) error {
khenaidoob3244212019-08-27 14:32:27 -0400353 log.Infow("verifying-KV-store-connectivity", log.Fields{"host": core.config.KVStoreHost,
354 "port": core.config.KVStorePort, "retries": maxRetries, "retryInterval": retryInterval})
355 // Get timeout in seconds with 1 second set as minimum
356 timeout := int(core.config.DefaultCoreTimeout / 1000)
357 if timeout < 1 {
358 timeout = 1
359 }
360 count := 0
361 for {
362 if !core.kvClient.IsConnectionUp(timeout) {
363 log.Info("KV-store-unreachable")
364 if maxRetries != -1 {
365 if count >= maxRetries {
366 return status.Error(codes.Unavailable, "kv store unreachable")
367 }
368 }
npujar1d86a522019-11-14 17:11:16 +0530369 count++
khenaidoob3244212019-08-27 14:32:27 -0400370 // Take a nap before retrying
Girish Kumar4d3887d2019-11-22 14:22:05 +0000371 time.Sleep(retryInterval)
khenaidoob3244212019-08-27 14:32:27 -0400372 log.Infow("retry-KV-store-connectivity", log.Fields{"retryCount": count, "maxRetries": maxRetries, "retryInterval": retryInterval})
373
374 } else {
375 break
376 }
377 }
378 log.Info("KV-store-reachable")
379 return nil
380}
381
npujar1d86a522019-11-14 17:11:16 +0530382func (core *Core) registerAdapterRequestHandlers(ctx context.Context, coreInstanceID string, dMgr *DeviceManager,
khenaidoo297cd252019-02-07 22:10:23 -0500383 ldMgr *LogicalDeviceManager, aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy,
khenaidoo54e0ddf2019-02-27 16:21:33 -0500384) error {
npujar1d86a522019-11-14 17:11:16 +0530385 requestProxy := NewAdapterRequestHandlerProxy(core, coreInstanceID, dMgr, ldMgr, aMgr, cdProxy, ldProxy,
khenaidoo297cd252019-02-07 22:10:23 -0500386 core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout)
khenaidoob9203542018-09-17 22:56:37 -0400387
khenaidoo54e0ddf2019-02-27 16:21:33 -0500388 // Register the broadcast topic to handle any core-bound broadcast requests
389 if err := core.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: core.config.CoreTopic}, requestProxy); err != nil {
390 log.Fatalw("Failed-registering-broadcast-handler", log.Fields{"topic": core.config.CoreTopic})
391 return err
392 }
393
Kent Hagermana6d0c362019-07-30 12:50:21 -0400394 // Register the core-pair topic to handle core-bound requests destined to the core pair
395 if err := core.kmp.SubscribeWithDefaultRequestHandler(kafka.Topic{Name: core.config.CorePairTopic}, kafka.OffsetNewest); err != nil {
396 log.Fatalw("Failed-registering-pair-handler", log.Fields{"topic": core.config.CorePairTopic})
397 return err
398 }
399
khenaidoo54e0ddf2019-02-27 16:21:33 -0500400 log.Info("request-handler-registered")
khenaidoob9203542018-09-17 22:56:37 -0400401 return nil
402}
403
404func (core *Core) startDeviceManager(ctx context.Context) {
khenaidoo21d51152019-02-01 13:48:37 -0500405 log.Info("DeviceManager-Starting...")
khenaidoo4d4802d2018-10-04 21:59:49 -0400406 core.deviceMgr.start(ctx, core.logicalDeviceMgr)
khenaidoo21d51152019-02-01 13:48:37 -0500407 log.Info("DeviceManager-Started")
khenaidoob9203542018-09-17 22:56:37 -0400408}
409
410func (core *Core) startLogicalDeviceManager(ctx context.Context) {
khenaidoo21d51152019-02-01 13:48:37 -0500411 log.Info("Logical-DeviceManager-Starting...")
khenaidoo4d4802d2018-10-04 21:59:49 -0400412 core.logicalDeviceMgr.start(ctx)
khenaidoo21d51152019-02-01 13:48:37 -0500413 log.Info("Logical-DeviceManager-Started")
khenaidoob9203542018-09-17 22:56:37 -0400414}
khenaidoo21d51152019-02-01 13:48:37 -0500415
416func (core *Core) startAdapterManager(ctx context.Context) {
417 log.Info("Adapter-Manager-Starting...")
418 core.adapterMgr.start(ctx)
419 log.Info("Adapter-Manager-Started")
William Kurkiandaa6bb22019-03-07 12:26:28 -0500420}
Girish Kumar4d3887d2019-11-22 14:22:05 +0000421
422/*
423* Thread to monitor kvstore Liveness (connection status)
424*
425* This function constantly monitors Liveness State of kvstore as reported
426* periodically by backend and updates the Status of kv-store service registered
427* with rw_core probe.
428*
429* If no liveness event has been seen within a timeout, then the thread will
430* perform a "liveness" check attempt, which will in turn trigger a liveness event on
431* the liveness channel, true or false depending on whether the attempt succeeded.
432*
433* The gRPC server in turn monitors the state of the readiness probe and will
434* start issuing UNAVAILABLE response while the probe is not ready.
435 */
436func (core *Core) monitorKvstoreLiveness(ctx context.Context) {
437 log.Info("start-monitoring-kvstore-liveness")
438
439 // Instruct backend to create Liveness channel for transporting state updates
440 livenessChannel := core.backend.EnableLivenessChannel()
441
442 log.Debug("enabled-kvstore-liveness-channel")
443
444 // Default state for kvstore is alive for rw_core
445 timeout := core.config.LiveProbeInterval
446 for {
447 timeoutTimer := time.NewTimer(timeout)
448 select {
449
450 case liveness := <-livenessChannel:
451 log.Debugw("received-liveness-change-notification", log.Fields{"liveness": liveness})
452
453 if !liveness {
454 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady)
455
456 if core.grpcServer != nil {
457 log.Info("kvstore-set-server-notready")
458 }
459
460 timeout = core.config.NotLiveProbeInterval
461
462 } else {
463 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning)
464
465 if core.grpcServer != nil {
466 log.Info("kvstore-set-server-ready")
467 }
468
469 timeout = core.config.LiveProbeInterval
470 }
471
472 if !timeoutTimer.Stop() {
473 <-timeoutTimer.C
474 }
475
476 case <-timeoutTimer.C:
477 log.Info("kvstore-perform-liveness-check-on-timeout")
478
479 // Trigger Liveness check if no liveness update received within the timeout period.
480 // The Liveness check will push Live state to same channel which this routine is
481 // reading and processing. This, do it asynchronously to avoid blocking for
482 // backend response and avoid any possibility of deadlock
483 go core.backend.PerformLivenessCheck(core.config.KVStoreTimeout)
484 }
485 }
486}