blob: 047ef4a96848d0e79b32e00b0bbec35c2a95939b [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 */
16package core
17
18import (
19 "context"
sbarbari17d7e222019-11-05 10:02:29 -050020 "github.com/opencord/voltha-go/db/model"
khenaidoob9203542018-09-17 22:56:37 -040021 "github.com/opencord/voltha-go/rw_core/config"
sbarbari17d7e222019-11-05 10:02:29 -050022 "github.com/opencord/voltha-lib-go/v2/pkg/db"
Scott Baker807addd2019-10-24 15:16:21 -070023 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
Scott Baker807addd2019-10-24 15:16:21 -070024 grpcserver "github.com/opencord/voltha-lib-go/v2/pkg/grpc"
25 "github.com/opencord/voltha-lib-go/v2/pkg/kafka"
26 "github.com/opencord/voltha-lib-go/v2/pkg/log"
27 "github.com/opencord/voltha-lib-go/v2/pkg/probe"
Scott Baker555307d2019-11-04 08:58:01 -080028 "github.com/opencord/voltha-protos/v2/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040029 "google.golang.org/grpc"
khenaidoob3244212019-08-27 14:32:27 -040030 "google.golang.org/grpc/codes"
31 "google.golang.org/grpc/status"
32 "time"
khenaidoob9203542018-09-17 22:56:37 -040033)
34
35type Core struct {
36 instanceId string
37 deviceMgr *DeviceManager
38 logicalDeviceMgr *LogicalDeviceManager
39 grpcServer *grpcserver.GrpcServer
Richard Jankowskidbab94a2018-12-06 16:20:25 -050040 grpcNBIAPIHandler *APIHandler
khenaidoo2c6a0992019-04-29 13:46:56 -040041 adapterMgr *AdapterManager
khenaidoob9203542018-09-17 22:56:37 -040042 config *config.RWCoreFlags
khenaidoo43c82122018-11-22 18:38:28 -050043 kmp *kafka.InterContainerProxy
khenaidoo92e62c52018-10-03 14:02:54 -040044 clusterDataRoot model.Root
45 localDataRoot model.Root
khenaidoob9203542018-09-17 22:56:37 -040046 clusterDataProxy *model.Proxy
47 localDataProxy *model.Proxy
48 exitChannel chan int
Richard Jankowskie4d77662018-10-17 13:53:21 -040049 kvClient kvstore.Client
Girish Kumar4d3887d2019-11-22 14:22:05 +000050 backend db.Backend
khenaidoo43c82122018-11-22 18:38:28 -050051 kafkaClient kafka.Client
khenaidoo2c6a0992019-04-29 13:46:56 -040052 deviceOwnership *DeviceOwnership
khenaidoob9203542018-09-17 22:56:37 -040053}
54
55func init() {
56 log.AddPackage(log.JSON, log.WarnLevel, nil)
57}
58
khenaidoo43c82122018-11-22 18:38:28 -050059func NewCore(id string, cf *config.RWCoreFlags, kvClient kvstore.Client, kafkaClient kafka.Client) *Core {
khenaidoob9203542018-09-17 22:56:37 -040060 var core Core
61 core.instanceId = id
62 core.exitChannel = make(chan int, 1)
63 core.config = cf
Richard Jankowskie4d77662018-10-17 13:53:21 -040064 core.kvClient = kvClient
khenaidoo43c82122018-11-22 18:38:28 -050065 core.kafkaClient = kafkaClient
Richard Jankowskie4d77662018-10-17 13:53:21 -040066
Girish Kumar4d3887d2019-11-22 14:22:05 +000067 // Configure backend to push Liveness Status at least every (cf.LiveProbeInterval / 2) seconds
68 // so as to avoid trigger of Liveness check (due to Liveness timeout) when backend is alive
69 livenessChannelInterval := cf.LiveProbeInterval / 2
70
Richard Jankowskie4d77662018-10-17 13:53:21 -040071 // Setup the KV store
Girish Kumar4d3887d2019-11-22 14:22:05 +000072 core.backend = db.Backend{
73 Client: kvClient,
74 StoreType: cf.KVStoreType,
75 Host: cf.KVStoreHost,
76 Port: cf.KVStorePort,
77 Timeout: cf.KVStoreTimeout,
78 LivenessChannelInterval: livenessChannelInterval,
79 PathPrefix: cf.KVStoreDataPrefix}
80 core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &core.backend)
81 core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &core.backend)
Stephane Barbarieef6650d2019-07-18 12:15:09 -040082 core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false)
83 core.localDataProxy = core.localDataRoot.CreateProxy(context.Background(), "/", false)
khenaidoob9203542018-09-17 22:56:37 -040084 return &core
85}
86
87func (core *Core) Start(ctx context.Context) {
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -070088
89 // If the context has a probe then fetch it and register our services
90 var p *probe.Probe
91 if value := ctx.Value(probe.ProbeContextKey); value != nil {
92 if _, ok := value.(*probe.Probe); ok {
93 p = value.(*probe.Probe)
94 p.RegisterService(
95 "message-bus",
96 "kv-store",
97 "device-manager",
98 "logical-device-manager",
99 "adapter-manager",
100 "grpc-service",
101 )
102 }
103 }
104
khenaidoob3244212019-08-27 14:32:27 -0400105 log.Info("starting-core-services", log.Fields{"coreId": core.instanceId})
106
107 // Wait until connection to KV Store is up
108 if err := core.waitUntilKVStoreReachableOrMaxTries(ctx, core.config.MaxConnectionRetries, core.config.ConnectionRetryInterval); err != nil {
109 log.Fatal("Unable-to-connect-to-KV-store")
110 }
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700111 if p != nil {
112 p.UpdateStatus("kv-store", probe.ServiceStatusRunning)
113 }
khenaidoob3244212019-08-27 14:32:27 -0400114
Scott Bakeree6a0872019-10-29 15:59:52 -0700115 // core.kmp must be created before deviceMgr and adapterMgr, as they will make
116 // private copies of the poiner to core.kmp.
117 if err := core.initKafkaManager(ctx); err != nil {
118 log.Fatal("Failed-to-init-kafka-manager")
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700119 }
khenaidoob3244212019-08-27 14:32:27 -0400120
khenaidoo631fe542019-05-31 15:44:43 -0400121 log.Debugw("values", log.Fields{"kmp": core.kmp})
Richard Jankowski199fd862019-03-18 14:49:51 -0400122 core.deviceMgr = newDeviceManager(core)
khenaidooba6b6c42019-08-02 09:11:56 -0400123 core.adapterMgr = newAdapterManager(core.clusterDataProxy, core.instanceId, core.deviceMgr)
124 core.deviceMgr.adapterMgr = core.adapterMgr
khenaidoo2c6a0992019-04-29 13:46:56 -0400125 core.logicalDeviceMgr = newLogicalDeviceManager(core, core.deviceMgr, core.kmp, core.clusterDataProxy, core.config.DefaultCoreTimeout)
khenaidoo54e0ddf2019-02-27 16:21:33 -0500126
Scott Bakeree6a0872019-10-29 15:59:52 -0700127 // Start the KafkaManager. This must be done after the deviceMgr, adapterMgr, and
128 // logicalDeviceMgr have been created, as once the kmp is started, it will register
129 // the above with the kmp.
130
131 go core.startKafkaManager(ctx,
132 core.config.ConnectionRetryInterval,
133 core.config.LiveProbeInterval,
134 core.config.NotLiveProbeInterval)
khenaidoob3244212019-08-27 14:32:27 -0400135
khenaidoob9203542018-09-17 22:56:37 -0400136 go core.startDeviceManager(ctx)
137 go core.startLogicalDeviceManager(ctx)
138 go core.startGRPCService(ctx)
khenaidoo21d51152019-02-01 13:48:37 -0500139 go core.startAdapterManager(ctx)
Girish Kumar4d3887d2019-11-22 14:22:05 +0000140 go core.monitorKvstoreLiveness(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400141
khenaidoo1ce37ad2019-03-24 22:07:24 -0400142 // Setup device ownership context
143 core.deviceOwnership = NewDeviceOwnership(core.instanceId, core.kvClient, core.deviceMgr, core.logicalDeviceMgr,
144 "service/voltha/owns_device", 10)
145
khenaidoob3244212019-08-27 14:32:27 -0400146 log.Info("core-services-started")
khenaidoob9203542018-09-17 22:56:37 -0400147}
148
149func (core *Core) Stop(ctx context.Context) {
khenaidoo19374072018-12-11 11:05:15 -0500150 log.Info("stopping-adaptercore")
David Bainbridgef794fc52019-10-03 22:37:12 +0000151 if core.exitChannel != nil {
152 core.exitChannel <- 1
153 }
khenaidoo43c82122018-11-22 18:38:28 -0500154 // Stop all the started services
David Bainbridgef794fc52019-10-03 22:37:12 +0000155 if core.grpcServer != nil {
156 core.grpcServer.Stop()
157 }
158 if core.logicalDeviceMgr != nil {
159 core.logicalDeviceMgr.stop(ctx)
160 }
161 if core.deviceMgr != nil {
162 core.deviceMgr.stop(ctx)
163 }
164 if core.kmp != nil {
165 core.kmp.Stop()
166 }
khenaidoo19374072018-12-11 11:05:15 -0500167 log.Info("adaptercore-stopped")
khenaidoob9203542018-09-17 22:56:37 -0400168}
169
khenaidoo631fe542019-05-31 15:44:43 -0400170//startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server
khenaidoob9203542018-09-17 22:56:37 -0400171func (core *Core) startGRPCService(ctx context.Context) {
172 // create an insecure gserver server
Scott Bakeree6a0872019-10-29 15:59:52 -0700173 core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false, probe.GetProbeFromContext(ctx))
khenaidoob9203542018-09-17 22:56:37 -0400174 log.Info("grpc-server-created")
175
khenaidoo54e0ddf2019-02-27 16:21:33 -0500176 core.grpcNBIAPIHandler = NewAPIHandler(core)
Richard Jankowski46464e92019-03-05 11:53:55 -0500177 log.Infow("grpc-handler", log.Fields{"core_binding_key": core.config.CoreBindingKey})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500178 core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler)
khenaidoob9203542018-09-17 22:56:37 -0400179 // Create a function to register the core GRPC service with the GRPC server
180 f := func(gs *grpc.Server) {
181 voltha.RegisterVolthaServiceServer(
182 gs,
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500183 core.grpcNBIAPIHandler,
khenaidoob9203542018-09-17 22:56:37 -0400184 )
185 }
186
187 core.grpcServer.AddService(f)
188 log.Info("grpc-service-added")
189
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700190 /*
191 * Start the GRPC server
192 *
193 * This is a bit sub-optimal here as the grpcServer.Start call does not return (blocks)
194 * until something fails, but we want to send a "start" status update. As written this
195 * means that we are actually sending the "start" status update before the server is
196 * started, which means it is possible that the status is "running" before it actually is.
197 *
198 * This means that there is a small window in which the core could return its status as
199 * ready, when it really isn't.
200 */
201 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning)
khenaidoob9203542018-09-17 22:56:37 -0400202 log.Info("grpc-server-started")
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700203 core.grpcServer.Start(context.Background())
204 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped)
khenaidoob9203542018-09-17 22:56:37 -0400205}
206
Scott Bakeree6a0872019-10-29 15:59:52 -0700207// Initialize the kafka manager, but we will start it later
208func (core *Core) initKafkaManager(ctx context.Context) error {
209 log.Infow("initialize-kafka-manager", log.Fields{"host": core.config.KafkaAdapterHost,
khenaidoob9203542018-09-17 22:56:37 -0400210 "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic})
Scott Bakeree6a0872019-10-29 15:59:52 -0700211
212 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPreparing)
213
214 // create the proxy
khenaidoob9203542018-09-17 22:56:37 -0400215 var err error
khenaidoo43c82122018-11-22 18:38:28 -0500216 if core.kmp, err = kafka.NewInterContainerProxy(
217 kafka.InterContainerHost(core.config.KafkaAdapterHost),
218 kafka.InterContainerPort(core.config.KafkaAdapterPort),
219 kafka.MsgClient(core.kafkaClient),
khenaidoo79232702018-12-04 11:00:41 -0500220 kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}),
221 kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic})); err != nil {
khenaidoob9203542018-09-17 22:56:37 -0400222 log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err})
223 return err
224 }
Scott Bakeree6a0872019-10-29 15:59:52 -0700225
226 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPrepared)
227
228 return nil
229}
230
231/*
232 * KafkaMonitorThread
233 *
234 * Repsonsible for starting the Kafka Interadapter Proxy and monitoring its liveness
235 * state.
236 *
237 * Any producer that fails to send will cause KafkaInterContainerProxy to
238 * post a false event on its liveness channel. Any producer that succeeds in sending
239 * will cause KafkaInterContainerProxy to post a true event on its liveness
240 * channel. Group recievers also update liveness state, and a receiver will typically
241 * indicate a loss of liveness within 3-5 seconds of Kafka going down. Receivers
242 * only indicate restoration of liveness if a message is received. During normal
243 * operation, messages will be routinely produced and received, automatically
244 * indicating liveness state. These routine liveness indications are rate-limited
245 * inside sarama_client.
246 *
247 * This thread monitors the status of KafkaInterContainerProxy's liveness and pushes
248 * that state to the core's readiness probes. If no liveness event has been seen
249 * within a timeout, then the thread will make an attempt to produce a "liveness"
250 * message, which will in turn trigger a liveness event on the liveness channel, true
251 * or false depending on whether the attempt succeeded.
252 *
253 * The gRPC server in turn monitors the state of the readiness probe and will
254 * start issuing UNAVAILABLE response while the probe is not ready.
255 *
256 * startupRetryInterval -- interval between attempts to start
257 * liveProbeInterval -- interval between liveness checks when in a live state
258 * notLiveProbeInterval -- interval between liveness checks when in a notLive state
259 *
260 * liveProbeInterval and notLiveProbeInterval can be configured separately,
261 * though the current default is that both are set to 60 seconds.
262 */
263
Girish Kumar4d3887d2019-11-22 14:22:05 +0000264func (core *Core) startKafkaManager(ctx context.Context, startupRetryInterval time.Duration, liveProbeInterval time.Duration, notLiveProbeInterval time.Duration) {
Scott Bakeree6a0872019-10-29 15:59:52 -0700265 log.Infow("starting-kafka-manager-thread", log.Fields{"host": core.config.KafkaAdapterHost,
266 "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic})
267
268 started := false
269 for !started {
270 // If we haven't started yet, then try to start
271 log.Infow("starting-kafka-proxy", log.Fields{})
272 if err := core.kmp.Start(); err != nil {
273 // We failed to start. Delay and then try again later.
274 // Don't worry about liveness, as we can't be live until we've started.
275 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
khenaidoob3244212019-08-27 14:32:27 -0400276 log.Infow("error-starting-kafka-messaging-proxy", log.Fields{"error": err})
Girish Kumar4d3887d2019-11-22 14:22:05 +0000277 time.Sleep(startupRetryInterval)
khenaidoob3244212019-08-27 14:32:27 -0400278 } else {
Scott Bakeree6a0872019-10-29 15:59:52 -0700279 // We started. We only need to do this once.
280 // Next we'll fall through and start checking liveness.
281 log.Infow("started-kafka-proxy", log.Fields{})
282
283 // cannot do this until after the kmp is started
284 if err := core.registerAdapterRequestHandlers(ctx, core.instanceId, core.deviceMgr, core.logicalDeviceMgr, core.adapterMgr, core.clusterDataProxy, core.localDataProxy); err != nil {
285 log.Fatal("Failure-registering-adapterRequestHandler")
286 }
287
288 started = true
khenaidoob3244212019-08-27 14:32:27 -0400289 }
khenaidoob9203542018-09-17 22:56:37 -0400290 }
Scott Bakeree6a0872019-10-29 15:59:52 -0700291
292 log.Info("started-kafka-message-proxy")
293
294 livenessChannel := core.kmp.EnableLivenessChannel(true)
295
296 log.Info("enabled-kafka-liveness-channel")
297
Girish Kumar4d3887d2019-11-22 14:22:05 +0000298 timeout := liveProbeInterval
Scott Bakeree6a0872019-10-29 15:59:52 -0700299 for {
300 timeoutTimer := time.NewTimer(timeout)
301 select {
302 case liveness := <-livenessChannel:
303 log.Infow("kafka-manager-thread-liveness-event", log.Fields{"liveness": liveness})
304 // there was a state change in Kafka liveness
305 if !liveness {
306 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
307
308 if core.grpcServer != nil {
309 log.Info("kafka-manager-thread-set-server-notready")
310 }
311
312 // retry frequently while life is bad
Girish Kumar4d3887d2019-11-22 14:22:05 +0000313 timeout = notLiveProbeInterval
Scott Bakeree6a0872019-10-29 15:59:52 -0700314 } else {
315 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning)
316
317 if core.grpcServer != nil {
318 log.Info("kafka-manager-thread-set-server-ready")
319 }
320
321 // retry infrequently while life is good
Girish Kumar4d3887d2019-11-22 14:22:05 +0000322 timeout = liveProbeInterval
Scott Bakeree6a0872019-10-29 15:59:52 -0700323 }
324 if !timeoutTimer.Stop() {
325 <-timeoutTimer.C
326 }
327 case <-timeoutTimer.C:
328 log.Info("kafka-proxy-liveness-recheck")
329 // send the liveness probe in a goroutine; we don't want to deadlock ourselves as
330 // the liveness probe may wait (and block) writing to our channel.
331 go func() {
332 err := core.kmp.SendLiveness()
333 if err != nil {
334 // Catch possible error case if sending liveness after Sarama has been stopped.
335 log.Warnw("error-kafka-send-liveness", log.Fields{"error": err})
336 }
337 }()
338 }
339 }
khenaidoob9203542018-09-17 22:56:37 -0400340}
341
khenaidoob3244212019-08-27 14:32:27 -0400342// waitUntilKVStoreReachableOrMaxTries will wait until it can connect to a KV store or until maxtries has been reached
Girish Kumar4d3887d2019-11-22 14:22:05 +0000343func (core *Core) waitUntilKVStoreReachableOrMaxTries(ctx context.Context, maxRetries int, retryInterval time.Duration) error {
khenaidoob3244212019-08-27 14:32:27 -0400344 log.Infow("verifying-KV-store-connectivity", log.Fields{"host": core.config.KVStoreHost,
345 "port": core.config.KVStorePort, "retries": maxRetries, "retryInterval": retryInterval})
346 // Get timeout in seconds with 1 second set as minimum
347 timeout := int(core.config.DefaultCoreTimeout / 1000)
348 if timeout < 1 {
349 timeout = 1
350 }
351 count := 0
352 for {
353 if !core.kvClient.IsConnectionUp(timeout) {
354 log.Info("KV-store-unreachable")
355 if maxRetries != -1 {
356 if count >= maxRetries {
357 return status.Error(codes.Unavailable, "kv store unreachable")
358 }
359 }
360 count += 1
361 // Take a nap before retrying
Girish Kumar4d3887d2019-11-22 14:22:05 +0000362 time.Sleep(retryInterval)
khenaidoob3244212019-08-27 14:32:27 -0400363 log.Infow("retry-KV-store-connectivity", log.Fields{"retryCount": count, "maxRetries": maxRetries, "retryInterval": retryInterval})
364
365 } else {
366 break
367 }
368 }
369 log.Info("KV-store-reachable")
370 return nil
371}
372
khenaidoo54e0ddf2019-02-27 16:21:33 -0500373func (core *Core) registerAdapterRequestHandlers(ctx context.Context, coreInstanceId string, dMgr *DeviceManager,
khenaidoo297cd252019-02-07 22:10:23 -0500374 ldMgr *LogicalDeviceManager, aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy,
khenaidoo54e0ddf2019-02-27 16:21:33 -0500375) error {
Richard Jankowski199fd862019-03-18 14:49:51 -0400376 requestProxy := NewAdapterRequestHandlerProxy(core, coreInstanceId, dMgr, ldMgr, aMgr, cdProxy, ldProxy,
khenaidoo297cd252019-02-07 22:10:23 -0500377 core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout)
khenaidoob9203542018-09-17 22:56:37 -0400378
khenaidoo54e0ddf2019-02-27 16:21:33 -0500379 // Register the broadcast topic to handle any core-bound broadcast requests
380 if err := core.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: core.config.CoreTopic}, requestProxy); err != nil {
381 log.Fatalw("Failed-registering-broadcast-handler", log.Fields{"topic": core.config.CoreTopic})
382 return err
383 }
384
Kent Hagermana6d0c362019-07-30 12:50:21 -0400385 // Register the core-pair topic to handle core-bound requests destined to the core pair
386 if err := core.kmp.SubscribeWithDefaultRequestHandler(kafka.Topic{Name: core.config.CorePairTopic}, kafka.OffsetNewest); err != nil {
387 log.Fatalw("Failed-registering-pair-handler", log.Fields{"topic": core.config.CorePairTopic})
388 return err
389 }
390
khenaidoo54e0ddf2019-02-27 16:21:33 -0500391 log.Info("request-handler-registered")
khenaidoob9203542018-09-17 22:56:37 -0400392 return nil
393}
394
395func (core *Core) startDeviceManager(ctx context.Context) {
khenaidoo21d51152019-02-01 13:48:37 -0500396 log.Info("DeviceManager-Starting...")
khenaidoo4d4802d2018-10-04 21:59:49 -0400397 core.deviceMgr.start(ctx, core.logicalDeviceMgr)
khenaidoo21d51152019-02-01 13:48:37 -0500398 log.Info("DeviceManager-Started")
khenaidoob9203542018-09-17 22:56:37 -0400399}
400
401func (core *Core) startLogicalDeviceManager(ctx context.Context) {
khenaidoo21d51152019-02-01 13:48:37 -0500402 log.Info("Logical-DeviceManager-Starting...")
khenaidoo4d4802d2018-10-04 21:59:49 -0400403 core.logicalDeviceMgr.start(ctx)
khenaidoo21d51152019-02-01 13:48:37 -0500404 log.Info("Logical-DeviceManager-Started")
khenaidoob9203542018-09-17 22:56:37 -0400405}
khenaidoo21d51152019-02-01 13:48:37 -0500406
407func (core *Core) startAdapterManager(ctx context.Context) {
408 log.Info("Adapter-Manager-Starting...")
409 core.adapterMgr.start(ctx)
410 log.Info("Adapter-Manager-Started")
William Kurkiandaa6bb22019-03-07 12:26:28 -0500411}
Girish Kumar4d3887d2019-11-22 14:22:05 +0000412
413/*
414* Thread to monitor kvstore Liveness (connection status)
415*
416* This function constantly monitors Liveness State of kvstore as reported
417* periodically by backend and updates the Status of kv-store service registered
418* with rw_core probe.
419*
420* If no liveness event has been seen within a timeout, then the thread will
421* perform a "liveness" check attempt, which will in turn trigger a liveness event on
422* the liveness channel, true or false depending on whether the attempt succeeded.
423*
424* The gRPC server in turn monitors the state of the readiness probe and will
425* start issuing UNAVAILABLE response while the probe is not ready.
426 */
427func (core *Core) monitorKvstoreLiveness(ctx context.Context) {
428 log.Info("start-monitoring-kvstore-liveness")
429
430 // Instruct backend to create Liveness channel for transporting state updates
431 livenessChannel := core.backend.EnableLivenessChannel()
432
433 log.Debug("enabled-kvstore-liveness-channel")
434
435 // Default state for kvstore is alive for rw_core
436 timeout := core.config.LiveProbeInterval
437 for {
438 timeoutTimer := time.NewTimer(timeout)
439 select {
440
441 case liveness := <-livenessChannel:
442 log.Debugw("received-liveness-change-notification", log.Fields{"liveness": liveness})
443
444 if !liveness {
445 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady)
446
447 if core.grpcServer != nil {
448 log.Info("kvstore-set-server-notready")
449 }
450
451 timeout = core.config.NotLiveProbeInterval
452
453 } else {
454 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning)
455
456 if core.grpcServer != nil {
457 log.Info("kvstore-set-server-ready")
458 }
459
460 timeout = core.config.LiveProbeInterval
461 }
462
463 if !timeoutTimer.Stop() {
464 <-timeoutTimer.C
465 }
466
467 case <-timeoutTimer.C:
468 log.Info("kvstore-perform-liveness-check-on-timeout")
469
470 // Trigger Liveness check if no liveness update received within the timeout period.
471 // The Liveness check will push Live state to same channel which this routine is
472 // reading and processing. This, do it asynchronously to avoid blocking for
473 // backend response and avoid any possibility of deadlock
474 go core.backend.PerformLivenessCheck(core.config.KVStoreTimeout)
475 }
476 }
477}