blob: 23f4bb2a98f7dbf1e859bfb3b0d6291e573b0f40 [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"
Thomas Lee Se5a44012019-11-07 20:32:24 +053021 "fmt"
npujar1d86a522019-11-14 17:11:16 +053022 "time"
23
sbarbari17d7e222019-11-05 10:02:29 -050024 "github.com/opencord/voltha-go/db/model"
khenaidoob9203542018-09-17 22:56:37 -040025 "github.com/opencord/voltha-go/rw_core/config"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080026 "github.com/opencord/voltha-lib-go/v3/pkg/db"
27 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
28 grpcserver "github.com/opencord/voltha-lib-go/v3/pkg/grpc"
29 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
30 "github.com/opencord/voltha-lib-go/v3/pkg/log"
31 "github.com/opencord/voltha-lib-go/v3/pkg/probe"
32 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040033 "google.golang.org/grpc"
khenaidoob3244212019-08-27 14:32:27 -040034 "google.golang.org/grpc/codes"
35 "google.golang.org/grpc/status"
khenaidoob9203542018-09-17 22:56:37 -040036)
37
npujar1d86a522019-11-14 17:11:16 +053038// Core represent read,write core attributes
khenaidoob9203542018-09-17 22:56:37 -040039type Core struct {
npujar1d86a522019-11-14 17:11:16 +053040 instanceID string
khenaidoob9203542018-09-17 22:56:37 -040041 deviceMgr *DeviceManager
42 logicalDeviceMgr *LogicalDeviceManager
43 grpcServer *grpcserver.GrpcServer
Richard Jankowskidbab94a2018-12-06 16:20:25 -050044 grpcNBIAPIHandler *APIHandler
khenaidoo2c6a0992019-04-29 13:46:56 -040045 adapterMgr *AdapterManager
khenaidoob9203542018-09-17 22:56:37 -040046 config *config.RWCoreFlags
npujar467fe752020-01-16 20:17:45 +053047 kmp kafka.InterContainerProxy
khenaidoo92e62c52018-10-03 14:02:54 -040048 clusterDataRoot model.Root
49 localDataRoot model.Root
khenaidoob9203542018-09-17 22:56:37 -040050 clusterDataProxy *model.Proxy
51 localDataProxy *model.Proxy
52 exitChannel chan int
Richard Jankowskie4d77662018-10-17 13:53:21 -040053 kvClient kvstore.Client
Girish Kumar4d3887d2019-11-22 14:22:05 +000054 backend db.Backend
khenaidoo43c82122018-11-22 18:38:28 -050055 kafkaClient kafka.Client
khenaidoo2c6a0992019-04-29 13:46:56 -040056 deviceOwnership *DeviceOwnership
khenaidoob9203542018-09-17 22:56:37 -040057}
58
59func init() {
npujar1d86a522019-11-14 17:11:16 +053060 _, err := log.AddPackage(log.JSON, log.WarnLevel, nil)
61 if err != nil {
62 log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err})
63 }
khenaidoob9203542018-09-17 22:56:37 -040064}
65
npujar1d86a522019-11-14 17:11:16 +053066// NewCore creates instance of rw core
Thomas Lee Se5a44012019-11-07 20:32:24 +053067func NewCore(ctx context.Context, id string, cf *config.RWCoreFlags, kvClient kvstore.Client, kafkaClient kafka.Client) *Core {
khenaidoob9203542018-09-17 22:56:37 -040068 var core Core
npujar1d86a522019-11-14 17:11:16 +053069 core.instanceID = id
khenaidoob9203542018-09-17 22:56:37 -040070 core.exitChannel = make(chan int, 1)
71 core.config = cf
Richard Jankowskie4d77662018-10-17 13:53:21 -040072 core.kvClient = kvClient
khenaidoo43c82122018-11-22 18:38:28 -050073 core.kafkaClient = kafkaClient
Richard Jankowskie4d77662018-10-17 13:53:21 -040074
Girish Kumar4d3887d2019-11-22 14:22:05 +000075 // Configure backend to push Liveness Status at least every (cf.LiveProbeInterval / 2) seconds
76 // so as to avoid trigger of Liveness check (due to Liveness timeout) when backend is alive
77 livenessChannelInterval := cf.LiveProbeInterval / 2
78
Richard Jankowskie4d77662018-10-17 13:53:21 -040079 // Setup the KV store
Girish Kumar4d3887d2019-11-22 14:22:05 +000080 core.backend = db.Backend{
81 Client: kvClient,
82 StoreType: cf.KVStoreType,
83 Host: cf.KVStoreHost,
84 Port: cf.KVStorePort,
85 Timeout: cf.KVStoreTimeout,
86 LivenessChannelInterval: livenessChannelInterval,
87 PathPrefix: cf.KVStoreDataPrefix}
88 core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &core.backend)
89 core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &core.backend)
khenaidoob9203542018-09-17 22:56:37 -040090 return &core
91}
92
npujar1d86a522019-11-14 17:11:16 +053093// Start brings up core services
Thomas Lee Se5a44012019-11-07 20:32:24 +053094func (core *Core) Start(ctx context.Context) error {
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -070095
96 // If the context has a probe then fetch it and register our services
97 var p *probe.Probe
98 if value := ctx.Value(probe.ProbeContextKey); value != nil {
99 if _, ok := value.(*probe.Probe); ok {
100 p = value.(*probe.Probe)
101 p.RegisterService(
102 "message-bus",
103 "kv-store",
104 "device-manager",
105 "logical-device-manager",
106 "adapter-manager",
107 "grpc-service",
108 )
109 }
110 }
111
npujar1d86a522019-11-14 17:11:16 +0530112 log.Info("starting-core-services", log.Fields{"coreId": core.instanceID})
khenaidoob3244212019-08-27 14:32:27 -0400113
114 // Wait until connection to KV Store is up
115 if err := core.waitUntilKVStoreReachableOrMaxTries(ctx, core.config.MaxConnectionRetries, core.config.ConnectionRetryInterval); err != nil {
116 log.Fatal("Unable-to-connect-to-KV-store")
117 }
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700118 if p != nil {
119 p.UpdateStatus("kv-store", probe.ServiceStatusRunning)
120 }
Thomas Lee Se5a44012019-11-07 20:32:24 +0530121 var err error
122
npujar467fe752020-01-16 20:17:45 +0530123 core.clusterDataProxy, err = core.clusterDataRoot.CreateProxy(ctx, "/", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530124 if err != nil {
125 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady)
126 return fmt.Errorf("Failed to create cluster data proxy")
127 }
npujar467fe752020-01-16 20:17:45 +0530128 core.localDataProxy, err = core.localDataRoot.CreateProxy(ctx, "/", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530129 if err != nil {
130 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady)
131 return fmt.Errorf("Failed to create local data proxy")
132 }
khenaidoob3244212019-08-27 14:32:27 -0400133
Scott Bakeree6a0872019-10-29 15:59:52 -0700134 // core.kmp must be created before deviceMgr and adapterMgr, as they will make
135 // private copies of the poiner to core.kmp.
npujar467fe752020-01-16 20:17:45 +0530136 core.initKafkaManager(ctx)
khenaidoob3244212019-08-27 14:32:27 -0400137
khenaidoo631fe542019-05-31 15:44:43 -0400138 log.Debugw("values", log.Fields{"kmp": core.kmp})
Richard Jankowski199fd862019-03-18 14:49:51 -0400139 core.deviceMgr = newDeviceManager(core)
npujar1d86a522019-11-14 17:11:16 +0530140 core.adapterMgr = newAdapterManager(core.clusterDataProxy, core.instanceID, core.deviceMgr)
khenaidooba6b6c42019-08-02 09:11:56 -0400141 core.deviceMgr.adapterMgr = core.adapterMgr
khenaidoo2c6a0992019-04-29 13:46:56 -0400142 core.logicalDeviceMgr = newLogicalDeviceManager(core, core.deviceMgr, core.kmp, core.clusterDataProxy, core.config.DefaultCoreTimeout)
khenaidoo54e0ddf2019-02-27 16:21:33 -0500143
Scott Bakeree6a0872019-10-29 15:59:52 -0700144 // Start the KafkaManager. This must be done after the deviceMgr, adapterMgr, and
145 // logicalDeviceMgr have been created, as once the kmp is started, it will register
146 // the above with the kmp.
147
148 go core.startKafkaManager(ctx,
149 core.config.ConnectionRetryInterval,
150 core.config.LiveProbeInterval,
151 core.config.NotLiveProbeInterval)
khenaidoob3244212019-08-27 14:32:27 -0400152
khenaidoob9203542018-09-17 22:56:37 -0400153 go core.startDeviceManager(ctx)
154 go core.startLogicalDeviceManager(ctx)
155 go core.startGRPCService(ctx)
khenaidoo21d51152019-02-01 13:48:37 -0500156 go core.startAdapterManager(ctx)
Girish Kumar4d3887d2019-11-22 14:22:05 +0000157 go core.monitorKvstoreLiveness(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400158
khenaidoo1ce37ad2019-03-24 22:07:24 -0400159 // Setup device ownership context
npujar1d86a522019-11-14 17:11:16 +0530160 core.deviceOwnership = NewDeviceOwnership(core.instanceID, core.kvClient, core.deviceMgr, core.logicalDeviceMgr,
khenaidoo1ce37ad2019-03-24 22:07:24 -0400161 "service/voltha/owns_device", 10)
162
khenaidoob3244212019-08-27 14:32:27 -0400163 log.Info("core-services-started")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530164 return nil
khenaidoob9203542018-09-17 22:56:37 -0400165}
166
npujar1d86a522019-11-14 17:11:16 +0530167// Stop brings down core services
khenaidoob9203542018-09-17 22:56:37 -0400168func (core *Core) Stop(ctx context.Context) {
khenaidoo19374072018-12-11 11:05:15 -0500169 log.Info("stopping-adaptercore")
David Bainbridgef794fc52019-10-03 22:37:12 +0000170 if core.exitChannel != nil {
171 core.exitChannel <- 1
172 }
khenaidoo43c82122018-11-22 18:38:28 -0500173 // Stop all the started services
David Bainbridgef794fc52019-10-03 22:37:12 +0000174 if core.grpcServer != nil {
175 core.grpcServer.Stop()
176 }
177 if core.logicalDeviceMgr != nil {
178 core.logicalDeviceMgr.stop(ctx)
179 }
180 if core.deviceMgr != nil {
181 core.deviceMgr.stop(ctx)
182 }
183 if core.kmp != nil {
184 core.kmp.Stop()
185 }
khenaidoo19374072018-12-11 11:05:15 -0500186 log.Info("adaptercore-stopped")
khenaidoob9203542018-09-17 22:56:37 -0400187}
188
khenaidoo631fe542019-05-31 15:44:43 -0400189//startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server
khenaidoob9203542018-09-17 22:56:37 -0400190func (core *Core) startGRPCService(ctx context.Context) {
191 // create an insecure gserver server
Scott Bakeree6a0872019-10-29 15:59:52 -0700192 core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false, probe.GetProbeFromContext(ctx))
khenaidoob9203542018-09-17 22:56:37 -0400193 log.Info("grpc-server-created")
194
khenaidoo54e0ddf2019-02-27 16:21:33 -0500195 core.grpcNBIAPIHandler = NewAPIHandler(core)
Richard Jankowski46464e92019-03-05 11:53:55 -0500196 log.Infow("grpc-handler", log.Fields{"core_binding_key": core.config.CoreBindingKey})
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500197 core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler)
khenaidoob9203542018-09-17 22:56:37 -0400198 // Create a function to register the core GRPC service with the GRPC server
199 f := func(gs *grpc.Server) {
200 voltha.RegisterVolthaServiceServer(
201 gs,
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500202 core.grpcNBIAPIHandler,
khenaidoob9203542018-09-17 22:56:37 -0400203 )
204 }
205
206 core.grpcServer.AddService(f)
207 log.Info("grpc-service-added")
208
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700209 /*
210 * Start the GRPC server
211 *
212 * This is a bit sub-optimal here as the grpcServer.Start call does not return (blocks)
213 * until something fails, but we want to send a "start" status update. As written this
214 * means that we are actually sending the "start" status update before the server is
215 * started, which means it is possible that the status is "running" before it actually is.
216 *
217 * This means that there is a small window in which the core could return its status as
218 * ready, when it really isn't.
219 */
220 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning)
khenaidoob9203542018-09-17 22:56:37 -0400221 log.Info("grpc-server-started")
npujar467fe752020-01-16 20:17:45 +0530222 core.grpcServer.Start(ctx)
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700223 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped)
khenaidoob9203542018-09-17 22:56:37 -0400224}
225
Scott Bakeree6a0872019-10-29 15:59:52 -0700226// Initialize the kafka manager, but we will start it later
npujar467fe752020-01-16 20:17:45 +0530227func (core *Core) initKafkaManager(ctx context.Context) {
Scott Bakeree6a0872019-10-29 15:59:52 -0700228 log.Infow("initialize-kafka-manager", log.Fields{"host": core.config.KafkaAdapterHost,
khenaidoob9203542018-09-17 22:56:37 -0400229 "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic})
Scott Bakeree6a0872019-10-29 15:59:52 -0700230
231 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPreparing)
232
233 // create the proxy
npujar467fe752020-01-16 20:17:45 +0530234 core.kmp = kafka.NewInterContainerProxy(
khenaidoo43c82122018-11-22 18:38:28 -0500235 kafka.InterContainerHost(core.config.KafkaAdapterHost),
236 kafka.InterContainerPort(core.config.KafkaAdapterPort),
237 kafka.MsgClient(core.kafkaClient),
khenaidoo79232702018-12-04 11:00:41 -0500238 kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}),
npujar467fe752020-01-16 20:17:45 +0530239 kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic}))
Scott Bakeree6a0872019-10-29 15:59:52 -0700240
241 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPrepared)
Scott Bakeree6a0872019-10-29 15:59:52 -0700242}
243
244/*
245 * KafkaMonitorThread
246 *
npujar1d86a522019-11-14 17:11:16 +0530247 * Responsible for starting the Kafka Interadapter Proxy and monitoring its liveness
Scott Bakeree6a0872019-10-29 15:59:52 -0700248 * state.
249 *
250 * Any producer that fails to send will cause KafkaInterContainerProxy to
251 * post a false event on its liveness channel. Any producer that succeeds in sending
252 * will cause KafkaInterContainerProxy to post a true event on its liveness
npujar1d86a522019-11-14 17:11:16 +0530253 * channel. Group receivers also update liveness state, and a receiver will typically
Scott Bakeree6a0872019-10-29 15:59:52 -0700254 * indicate a loss of liveness within 3-5 seconds of Kafka going down. Receivers
255 * only indicate restoration of liveness if a message is received. During normal
256 * operation, messages will be routinely produced and received, automatically
257 * indicating liveness state. These routine liveness indications are rate-limited
258 * inside sarama_client.
259 *
260 * This thread monitors the status of KafkaInterContainerProxy's liveness and pushes
261 * that state to the core's readiness probes. If no liveness event has been seen
262 * within a timeout, then the thread will make an attempt to produce a "liveness"
263 * message, which will in turn trigger a liveness event on the liveness channel, true
264 * or false depending on whether the attempt succeeded.
265 *
266 * The gRPC server in turn monitors the state of the readiness probe and will
267 * start issuing UNAVAILABLE response while the probe is not ready.
268 *
269 * startupRetryInterval -- interval between attempts to start
270 * liveProbeInterval -- interval between liveness checks when in a live state
271 * notLiveProbeInterval -- interval between liveness checks when in a notLive state
272 *
273 * liveProbeInterval and notLiveProbeInterval can be configured separately,
274 * though the current default is that both are set to 60 seconds.
275 */
276
Girish Kumar4d3887d2019-11-22 14:22:05 +0000277func (core *Core) startKafkaManager(ctx context.Context, startupRetryInterval time.Duration, liveProbeInterval time.Duration, notLiveProbeInterval time.Duration) {
Scott Bakeree6a0872019-10-29 15:59:52 -0700278 log.Infow("starting-kafka-manager-thread", log.Fields{"host": core.config.KafkaAdapterHost,
279 "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic})
280
281 started := false
282 for !started {
283 // If we haven't started yet, then try to start
284 log.Infow("starting-kafka-proxy", log.Fields{})
285 if err := core.kmp.Start(); err != nil {
286 // We failed to start. Delay and then try again later.
287 // Don't worry about liveness, as we can't be live until we've started.
288 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
khenaidoob3244212019-08-27 14:32:27 -0400289 log.Infow("error-starting-kafka-messaging-proxy", log.Fields{"error": err})
Girish Kumar4d3887d2019-11-22 14:22:05 +0000290 time.Sleep(startupRetryInterval)
khenaidoob3244212019-08-27 14:32:27 -0400291 } else {
Scott Bakeree6a0872019-10-29 15:59:52 -0700292 // We started. We only need to do this once.
293 // Next we'll fall through and start checking liveness.
294 log.Infow("started-kafka-proxy", log.Fields{})
295
296 // cannot do this until after the kmp is started
npujar1d86a522019-11-14 17:11:16 +0530297 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 -0700298 log.Fatal("Failure-registering-adapterRequestHandler")
299 }
300
301 started = true
khenaidoob3244212019-08-27 14:32:27 -0400302 }
khenaidoob9203542018-09-17 22:56:37 -0400303 }
Scott Bakeree6a0872019-10-29 15:59:52 -0700304
305 log.Info("started-kafka-message-proxy")
306
307 livenessChannel := core.kmp.EnableLivenessChannel(true)
308
309 log.Info("enabled-kafka-liveness-channel")
310
Girish Kumar4d3887d2019-11-22 14:22:05 +0000311 timeout := liveProbeInterval
Scott Bakeree6a0872019-10-29 15:59:52 -0700312 for {
313 timeoutTimer := time.NewTimer(timeout)
314 select {
315 case liveness := <-livenessChannel:
316 log.Infow("kafka-manager-thread-liveness-event", log.Fields{"liveness": liveness})
317 // there was a state change in Kafka liveness
318 if !liveness {
319 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
320
321 if core.grpcServer != nil {
322 log.Info("kafka-manager-thread-set-server-notready")
323 }
324
325 // retry frequently while life is bad
Girish Kumar4d3887d2019-11-22 14:22:05 +0000326 timeout = notLiveProbeInterval
Scott Bakeree6a0872019-10-29 15:59:52 -0700327 } else {
328 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning)
329
330 if core.grpcServer != nil {
331 log.Info("kafka-manager-thread-set-server-ready")
332 }
333
334 // retry infrequently while life is good
Girish Kumar4d3887d2019-11-22 14:22:05 +0000335 timeout = liveProbeInterval
Scott Bakeree6a0872019-10-29 15:59:52 -0700336 }
337 if !timeoutTimer.Stop() {
338 <-timeoutTimer.C
339 }
340 case <-timeoutTimer.C:
341 log.Info("kafka-proxy-liveness-recheck")
342 // send the liveness probe in a goroutine; we don't want to deadlock ourselves as
343 // the liveness probe may wait (and block) writing to our channel.
344 go func() {
345 err := core.kmp.SendLiveness()
346 if err != nil {
347 // Catch possible error case if sending liveness after Sarama has been stopped.
348 log.Warnw("error-kafka-send-liveness", log.Fields{"error": err})
349 }
350 }()
351 }
352 }
khenaidoob9203542018-09-17 22:56:37 -0400353}
354
khenaidoob3244212019-08-27 14:32:27 -0400355// waitUntilKVStoreReachableOrMaxTries will wait until it can connect to a KV store or until maxtries has been reached
Girish Kumar4d3887d2019-11-22 14:22:05 +0000356func (core *Core) waitUntilKVStoreReachableOrMaxTries(ctx context.Context, maxRetries int, retryInterval time.Duration) error {
khenaidoob3244212019-08-27 14:32:27 -0400357 log.Infow("verifying-KV-store-connectivity", log.Fields{"host": core.config.KVStoreHost,
358 "port": core.config.KVStorePort, "retries": maxRetries, "retryInterval": retryInterval})
khenaidoob3244212019-08-27 14:32:27 -0400359 count := 0
360 for {
npujar467fe752020-01-16 20:17:45 +0530361 if !core.kvClient.IsConnectionUp(ctx) {
khenaidoob3244212019-08-27 14:32:27 -0400362 log.Info("KV-store-unreachable")
363 if maxRetries != -1 {
364 if count >= maxRetries {
365 return status.Error(codes.Unavailable, "kv store unreachable")
366 }
367 }
npujar1d86a522019-11-14 17:11:16 +0530368 count++
khenaidoob3244212019-08-27 14:32:27 -0400369 // Take a nap before retrying
Girish Kumar4d3887d2019-11-22 14:22:05 +0000370 time.Sleep(retryInterval)
khenaidoob3244212019-08-27 14:32:27 -0400371 log.Infow("retry-KV-store-connectivity", log.Fields{"retryCount": count, "maxRetries": maxRetries, "retryInterval": retryInterval})
372
373 } else {
374 break
375 }
376 }
377 log.Info("KV-store-reachable")
378 return nil
379}
380
npujar1d86a522019-11-14 17:11:16 +0530381func (core *Core) registerAdapterRequestHandlers(ctx context.Context, coreInstanceID string, dMgr *DeviceManager,
khenaidoo297cd252019-02-07 22:10:23 -0500382 ldMgr *LogicalDeviceManager, aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy,
khenaidoo54e0ddf2019-02-27 16:21:33 -0500383) error {
npujar1d86a522019-11-14 17:11:16 +0530384 requestProxy := NewAdapterRequestHandlerProxy(core, coreInstanceID, dMgr, ldMgr, aMgr, cdProxy, ldProxy,
khenaidoo297cd252019-02-07 22:10:23 -0500385 core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout)
khenaidoob9203542018-09-17 22:56:37 -0400386
khenaidoo54e0ddf2019-02-27 16:21:33 -0500387 // Register the broadcast topic to handle any core-bound broadcast requests
388 if err := core.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: core.config.CoreTopic}, requestProxy); err != nil {
389 log.Fatalw("Failed-registering-broadcast-handler", log.Fields{"topic": core.config.CoreTopic})
390 return err
391 }
392
Kent Hagermana6d0c362019-07-30 12:50:21 -0400393 // Register the core-pair topic to handle core-bound requests destined to the core pair
394 if err := core.kmp.SubscribeWithDefaultRequestHandler(kafka.Topic{Name: core.config.CorePairTopic}, kafka.OffsetNewest); err != nil {
395 log.Fatalw("Failed-registering-pair-handler", log.Fields{"topic": core.config.CorePairTopic})
396 return err
397 }
398
khenaidoo54e0ddf2019-02-27 16:21:33 -0500399 log.Info("request-handler-registered")
khenaidoob9203542018-09-17 22:56:37 -0400400 return nil
401}
402
403func (core *Core) startDeviceManager(ctx context.Context) {
khenaidoo21d51152019-02-01 13:48:37 -0500404 log.Info("DeviceManager-Starting...")
khenaidoo4d4802d2018-10-04 21:59:49 -0400405 core.deviceMgr.start(ctx, core.logicalDeviceMgr)
khenaidoo21d51152019-02-01 13:48:37 -0500406 log.Info("DeviceManager-Started")
khenaidoob9203542018-09-17 22:56:37 -0400407}
408
409func (core *Core) startLogicalDeviceManager(ctx context.Context) {
khenaidoo21d51152019-02-01 13:48:37 -0500410 log.Info("Logical-DeviceManager-Starting...")
khenaidoo4d4802d2018-10-04 21:59:49 -0400411 core.logicalDeviceMgr.start(ctx)
khenaidoo21d51152019-02-01 13:48:37 -0500412 log.Info("Logical-DeviceManager-Started")
khenaidoob9203542018-09-17 22:56:37 -0400413}
khenaidoo21d51152019-02-01 13:48:37 -0500414
415func (core *Core) startAdapterManager(ctx context.Context) {
416 log.Info("Adapter-Manager-Starting...")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530417 err := core.adapterMgr.start(ctx)
418 if err != nil {
419 log.Fatalf("failed-to-start-adapter-manager: error %v ", err)
420 }
khenaidoo21d51152019-02-01 13:48:37 -0500421 log.Info("Adapter-Manager-Started")
William Kurkiandaa6bb22019-03-07 12:26:28 -0500422}
Girish Kumar4d3887d2019-11-22 14:22:05 +0000423
424/*
425* Thread to monitor kvstore Liveness (connection status)
426*
427* This function constantly monitors Liveness State of kvstore as reported
428* periodically by backend and updates the Status of kv-store service registered
429* with rw_core probe.
430*
431* If no liveness event has been seen within a timeout, then the thread will
432* perform a "liveness" check attempt, which will in turn trigger a liveness event on
433* the liveness channel, true or false depending on whether the attempt succeeded.
434*
435* The gRPC server in turn monitors the state of the readiness probe and will
436* start issuing UNAVAILABLE response while the probe is not ready.
437 */
438func (core *Core) monitorKvstoreLiveness(ctx context.Context) {
439 log.Info("start-monitoring-kvstore-liveness")
440
441 // Instruct backend to create Liveness channel for transporting state updates
442 livenessChannel := core.backend.EnableLivenessChannel()
443
444 log.Debug("enabled-kvstore-liveness-channel")
445
446 // Default state for kvstore is alive for rw_core
447 timeout := core.config.LiveProbeInterval
448 for {
449 timeoutTimer := time.NewTimer(timeout)
450 select {
451
452 case liveness := <-livenessChannel:
453 log.Debugw("received-liveness-change-notification", log.Fields{"liveness": liveness})
454
455 if !liveness {
456 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady)
457
458 if core.grpcServer != nil {
459 log.Info("kvstore-set-server-notready")
460 }
461
462 timeout = core.config.NotLiveProbeInterval
463
464 } else {
465 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning)
466
467 if core.grpcServer != nil {
468 log.Info("kvstore-set-server-ready")
469 }
470
471 timeout = core.config.LiveProbeInterval
472 }
473
474 if !timeoutTimer.Stop() {
475 <-timeoutTimer.C
476 }
477
478 case <-timeoutTimer.C:
479 log.Info("kvstore-perform-liveness-check-on-timeout")
480
481 // Trigger Liveness check if no liveness update received within the timeout period.
482 // The Liveness check will push Live state to same channel which this routine is
483 // reading and processing. This, do it asynchronously to avoid blocking for
484 // backend response and avoid any possibility of deadlock
npujar467fe752020-01-16 20:17:45 +0530485 go core.backend.PerformLivenessCheck(ctx)
Girish Kumar4d3887d2019-11-22 14:22:05 +0000486 }
487 }
488}