khenaidoo | b920354 | 2018-09-17 22:56:37 -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 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 20 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 21 | "github.com/opencord/voltha-go/rw_core/config" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v2/pkg/db" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 24 | 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 Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v2/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 29 | "google.golang.org/grpc" |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 30 | "google.golang.org/grpc/codes" |
| 31 | "google.golang.org/grpc/status" |
| 32 | "time" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | type Core struct { |
| 36 | instanceId string |
| 37 | deviceMgr *DeviceManager |
| 38 | logicalDeviceMgr *LogicalDeviceManager |
| 39 | grpcServer *grpcserver.GrpcServer |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 40 | grpcNBIAPIHandler *APIHandler |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 41 | adapterMgr *AdapterManager |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 42 | config *config.RWCoreFlags |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 43 | kmp *kafka.InterContainerProxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 44 | clusterDataRoot model.Root |
| 45 | localDataRoot model.Root |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 46 | clusterDataProxy *model.Proxy |
| 47 | localDataProxy *model.Proxy |
| 48 | exitChannel chan int |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 49 | kvClient kvstore.Client |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 50 | backend db.Backend |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 51 | kafkaClient kafka.Client |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 52 | deviceOwnership *DeviceOwnership |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | func init() { |
| 56 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 57 | } |
| 58 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 59 | func NewCore(id string, cf *config.RWCoreFlags, kvClient kvstore.Client, kafkaClient kafka.Client) *Core { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 60 | var core Core |
| 61 | core.instanceId = id |
| 62 | core.exitChannel = make(chan int, 1) |
| 63 | core.config = cf |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 64 | core.kvClient = kvClient |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 65 | core.kafkaClient = kafkaClient |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 66 | |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 67 | // 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 Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 71 | // Setup the KV store |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 72 | 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 Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 82 | core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false) |
| 83 | core.localDataProxy = core.localDataRoot.CreateProxy(context.Background(), "/", false) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 84 | return &core |
| 85 | } |
| 86 | |
| 87 | func (core *Core) Start(ctx context.Context) { |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 88 | |
| 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 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 105 | 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. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 111 | if p != nil { |
| 112 | p.UpdateStatus("kv-store", probe.ServiceStatusRunning) |
| 113 | } |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 114 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 115 | // 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. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 119 | } |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 120 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 121 | log.Debugw("values", log.Fields{"kmp": core.kmp}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 122 | core.deviceMgr = newDeviceManager(core) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 123 | core.adapterMgr = newAdapterManager(core.clusterDataProxy, core.instanceId, core.deviceMgr) |
| 124 | core.deviceMgr.adapterMgr = core.adapterMgr |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 125 | core.logicalDeviceMgr = newLogicalDeviceManager(core, core.deviceMgr, core.kmp, core.clusterDataProxy, core.config.DefaultCoreTimeout) |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 126 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 127 | // 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) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 135 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 136 | go core.startDeviceManager(ctx) |
| 137 | go core.startLogicalDeviceManager(ctx) |
| 138 | go core.startGRPCService(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 139 | go core.startAdapterManager(ctx) |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 140 | go core.monitorKvstoreLiveness(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 141 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 142 | // Setup device ownership context |
| 143 | core.deviceOwnership = NewDeviceOwnership(core.instanceId, core.kvClient, core.deviceMgr, core.logicalDeviceMgr, |
| 144 | "service/voltha/owns_device", 10) |
| 145 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 146 | log.Info("core-services-started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | func (core *Core) Stop(ctx context.Context) { |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 150 | log.Info("stopping-adaptercore") |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 151 | if core.exitChannel != nil { |
| 152 | core.exitChannel <- 1 |
| 153 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 154 | // Stop all the started services |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 155 | 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 | } |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 167 | log.Info("adaptercore-stopped") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 168 | } |
| 169 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 170 | //startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 171 | func (core *Core) startGRPCService(ctx context.Context) { |
| 172 | // create an insecure gserver server |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 173 | core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false, probe.GetProbeFromContext(ctx)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 174 | log.Info("grpc-server-created") |
| 175 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 176 | core.grpcNBIAPIHandler = NewAPIHandler(core) |
Richard Jankowski | 46464e9 | 2019-03-05 11:53:55 -0500 | [diff] [blame] | 177 | log.Infow("grpc-handler", log.Fields{"core_binding_key": core.config.CoreBindingKey}) |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 178 | core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 179 | // 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 Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 183 | core.grpcNBIAPIHandler, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 184 | ) |
| 185 | } |
| 186 | |
| 187 | core.grpcServer.AddService(f) |
| 188 | log.Info("grpc-service-added") |
| 189 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 190 | /* |
| 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) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 202 | log.Info("grpc-server-started") |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 203 | core.grpcServer.Start(context.Background()) |
| 204 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 207 | // Initialize the kafka manager, but we will start it later |
| 208 | func (core *Core) initKafkaManager(ctx context.Context) error { |
| 209 | log.Infow("initialize-kafka-manager", log.Fields{"host": core.config.KafkaAdapterHost, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 210 | "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic}) |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 211 | |
| 212 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPreparing) |
| 213 | |
| 214 | // create the proxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 215 | var err error |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 216 | if core.kmp, err = kafka.NewInterContainerProxy( |
| 217 | kafka.InterContainerHost(core.config.KafkaAdapterHost), |
| 218 | kafka.InterContainerPort(core.config.KafkaAdapterPort), |
| 219 | kafka.MsgClient(core.kafkaClient), |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 220 | kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}), |
| 221 | kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic})); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 222 | log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err}) |
| 223 | return err |
| 224 | } |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 225 | |
| 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 Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 264 | func (core *Core) startKafkaManager(ctx context.Context, startupRetryInterval time.Duration, liveProbeInterval time.Duration, notLiveProbeInterval time.Duration) { |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 265 | 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) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 276 | log.Infow("error-starting-kafka-messaging-proxy", log.Fields{"error": err}) |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 277 | time.Sleep(startupRetryInterval) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 278 | } else { |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 279 | // 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 |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 289 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 290 | } |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 291 | |
| 292 | log.Info("started-kafka-message-proxy") |
| 293 | |
| 294 | livenessChannel := core.kmp.EnableLivenessChannel(true) |
| 295 | |
| 296 | log.Info("enabled-kafka-liveness-channel") |
| 297 | |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 298 | timeout := liveProbeInterval |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 299 | 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 Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 313 | timeout = notLiveProbeInterval |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 314 | } 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 Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 322 | timeout = liveProbeInterval |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 323 | } |
| 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 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 340 | } |
| 341 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 342 | // waitUntilKVStoreReachableOrMaxTries will wait until it can connect to a KV store or until maxtries has been reached |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 343 | func (core *Core) waitUntilKVStoreReachableOrMaxTries(ctx context.Context, maxRetries int, retryInterval time.Duration) error { |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 344 | 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 Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 362 | time.Sleep(retryInterval) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 363 | 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 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 373 | func (core *Core) registerAdapterRequestHandlers(ctx context.Context, coreInstanceId string, dMgr *DeviceManager, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 374 | ldMgr *LogicalDeviceManager, aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy, |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 375 | ) error { |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 376 | requestProxy := NewAdapterRequestHandlerProxy(core, coreInstanceId, dMgr, ldMgr, aMgr, cdProxy, ldProxy, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 377 | core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 378 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 379 | // 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 Hagerman | a6d0c36 | 2019-07-30 12:50:21 -0400 | [diff] [blame] | 385 | // 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 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 391 | log.Info("request-handler-registered") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 392 | return nil |
| 393 | } |
| 394 | |
| 395 | func (core *Core) startDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 396 | log.Info("DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 397 | core.deviceMgr.start(ctx, core.logicalDeviceMgr) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 398 | log.Info("DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | func (core *Core) startLogicalDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 402 | log.Info("Logical-DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 403 | core.logicalDeviceMgr.start(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 404 | log.Info("Logical-DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 405 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 406 | |
| 407 | func (core *Core) startAdapterManager(ctx context.Context) { |
| 408 | log.Info("Adapter-Manager-Starting...") |
| 409 | core.adapterMgr.start(ctx) |
| 410 | log.Info("Adapter-Manager-Started") |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 411 | } |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 412 | |
| 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 | */ |
| 427 | func (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 | } |