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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 21 | "time" |
| 22 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/rw_core/config" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v2/pkg/db" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 27 | 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 Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 31 | "github.com/opencord/voltha-protos/v2/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 32 | "google.golang.org/grpc" |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 33 | "google.golang.org/grpc/codes" |
| 34 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 35 | ) |
| 36 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 37 | // Core represent read,write core attributes |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 38 | type Core struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 39 | instanceID string |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 40 | deviceMgr *DeviceManager |
| 41 | logicalDeviceMgr *LogicalDeviceManager |
| 42 | grpcServer *grpcserver.GrpcServer |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 43 | grpcNBIAPIHandler *APIHandler |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 44 | adapterMgr *AdapterManager |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 45 | config *config.RWCoreFlags |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 46 | kmp *kafka.InterContainerProxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 47 | clusterDataRoot model.Root |
| 48 | localDataRoot model.Root |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 49 | clusterDataProxy *model.Proxy |
| 50 | localDataProxy *model.Proxy |
| 51 | exitChannel chan int |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 52 | kvClient kvstore.Client |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 53 | backend db.Backend |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 54 | kafkaClient kafka.Client |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 55 | deviceOwnership *DeviceOwnership |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func init() { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 59 | _, 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 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 63 | } |
| 64 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 65 | // NewCore creates instance of rw core |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 66 | 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] | 67 | var core Core |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 68 | core.instanceID = id |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 69 | core.exitChannel = make(chan int, 1) |
| 70 | core.config = cf |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 71 | core.kvClient = kvClient |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 72 | core.kafkaClient = kafkaClient |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 73 | |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 74 | // 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 Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 78 | // Setup the KV store |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 79 | 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 Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 89 | core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false) |
| 90 | core.localDataProxy = core.localDataRoot.CreateProxy(context.Background(), "/", false) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 91 | return &core |
| 92 | } |
| 93 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 94 | // Start brings up core services |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 95 | func (core *Core) Start(ctx context.Context) { |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 96 | |
| 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 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 113 | log.Info("starting-core-services", log.Fields{"coreId": core.instanceID}) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 114 | |
| 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. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 119 | if p != nil { |
| 120 | p.UpdateStatus("kv-store", probe.ServiceStatusRunning) |
| 121 | } |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 122 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 123 | // 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. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 127 | } |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 128 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 129 | log.Debugw("values", log.Fields{"kmp": core.kmp}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 130 | core.deviceMgr = newDeviceManager(core) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 131 | core.adapterMgr = newAdapterManager(core.clusterDataProxy, core.instanceID, core.deviceMgr) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 132 | core.deviceMgr.adapterMgr = core.adapterMgr |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 133 | core.logicalDeviceMgr = newLogicalDeviceManager(core, core.deviceMgr, core.kmp, core.clusterDataProxy, core.config.DefaultCoreTimeout) |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 134 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 135 | // 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) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 143 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 144 | go core.startDeviceManager(ctx) |
| 145 | go core.startLogicalDeviceManager(ctx) |
| 146 | go core.startGRPCService(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 147 | go core.startAdapterManager(ctx) |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 148 | go core.monitorKvstoreLiveness(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 149 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 150 | // Setup device ownership context |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 151 | core.deviceOwnership = NewDeviceOwnership(core.instanceID, core.kvClient, core.deviceMgr, core.logicalDeviceMgr, |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 152 | "service/voltha/owns_device", 10) |
| 153 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 154 | log.Info("core-services-started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 155 | } |
| 156 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 157 | // Stop brings down core services |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 158 | func (core *Core) Stop(ctx context.Context) { |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 159 | log.Info("stopping-adaptercore") |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 160 | if core.exitChannel != nil { |
| 161 | core.exitChannel <- 1 |
| 162 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 163 | // Stop all the started services |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 164 | 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 | } |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 176 | log.Info("adaptercore-stopped") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 177 | } |
| 178 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 179 | //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] | 180 | func (core *Core) startGRPCService(ctx context.Context) { |
| 181 | // create an insecure gserver server |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 182 | 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] | 183 | log.Info("grpc-server-created") |
| 184 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 185 | core.grpcNBIAPIHandler = NewAPIHandler(core) |
Richard Jankowski | 46464e9 | 2019-03-05 11:53:55 -0500 | [diff] [blame] | 186 | log.Infow("grpc-handler", log.Fields{"core_binding_key": core.config.CoreBindingKey}) |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 187 | core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 188 | // 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 Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 192 | core.grpcNBIAPIHandler, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 193 | ) |
| 194 | } |
| 195 | |
| 196 | core.grpcServer.AddService(f) |
| 197 | log.Info("grpc-service-added") |
| 198 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 199 | /* |
| 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) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 211 | log.Info("grpc-server-started") |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 212 | core.grpcServer.Start(context.Background()) |
| 213 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 216 | // Initialize the kafka manager, but we will start it later |
| 217 | func (core *Core) initKafkaManager(ctx context.Context) error { |
| 218 | log.Infow("initialize-kafka-manager", log.Fields{"host": core.config.KafkaAdapterHost, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 219 | "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic}) |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 220 | |
| 221 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPreparing) |
| 222 | |
| 223 | // create the proxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 224 | var err error |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 225 | if core.kmp, err = kafka.NewInterContainerProxy( |
| 226 | kafka.InterContainerHost(core.config.KafkaAdapterHost), |
| 227 | kafka.InterContainerPort(core.config.KafkaAdapterPort), |
| 228 | kafka.MsgClient(core.kafkaClient), |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 229 | kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}), |
| 230 | kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic})); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 231 | log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err}) |
| 232 | return err |
| 233 | } |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 234 | |
| 235 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPrepared) |
| 236 | |
| 237 | return nil |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * KafkaMonitorThread |
| 242 | * |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 243 | * Responsible for starting the Kafka Interadapter Proxy and monitoring its liveness |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 244 | * 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 |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 249 | * channel. Group receivers also update liveness state, and a receiver will typically |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 250 | * 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 Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 273 | 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] | 274 | 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) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 285 | log.Infow("error-starting-kafka-messaging-proxy", log.Fields{"error": err}) |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 286 | time.Sleep(startupRetryInterval) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 287 | } else { |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 288 | // 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 |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 293 | if err := core.registerAdapterRequestHandlers(ctx, core.instanceID, core.deviceMgr, core.logicalDeviceMgr, core.adapterMgr, core.clusterDataProxy, core.localDataProxy); err != nil { |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 294 | log.Fatal("Failure-registering-adapterRequestHandler") |
| 295 | } |
| 296 | |
| 297 | started = true |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 298 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 299 | } |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 300 | |
| 301 | log.Info("started-kafka-message-proxy") |
| 302 | |
| 303 | livenessChannel := core.kmp.EnableLivenessChannel(true) |
| 304 | |
| 305 | log.Info("enabled-kafka-liveness-channel") |
| 306 | |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 307 | timeout := liveProbeInterval |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 308 | 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 Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 322 | timeout = notLiveProbeInterval |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 323 | } 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 Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 331 | timeout = liveProbeInterval |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 332 | } |
| 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 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 349 | } |
| 350 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 351 | // 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] | 352 | func (core *Core) waitUntilKVStoreReachableOrMaxTries(ctx context.Context, maxRetries int, retryInterval time.Duration) error { |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 353 | 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 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 369 | count++ |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 370 | // Take a nap before retrying |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 371 | time.Sleep(retryInterval) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 372 | 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 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 382 | func (core *Core) registerAdapterRequestHandlers(ctx context.Context, coreInstanceID string, dMgr *DeviceManager, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 383 | ldMgr *LogicalDeviceManager, aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy, |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 384 | ) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 385 | requestProxy := NewAdapterRequestHandlerProxy(core, coreInstanceID, dMgr, ldMgr, aMgr, cdProxy, ldProxy, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 386 | core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 387 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 388 | // 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 Hagerman | a6d0c36 | 2019-07-30 12:50:21 -0400 | [diff] [blame] | 394 | // 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 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 400 | log.Info("request-handler-registered") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 401 | return nil |
| 402 | } |
| 403 | |
| 404 | func (core *Core) startDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 405 | log.Info("DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 406 | core.deviceMgr.start(ctx, core.logicalDeviceMgr) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 407 | log.Info("DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | func (core *Core) startLogicalDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 411 | log.Info("Logical-DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 412 | core.logicalDeviceMgr.start(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 413 | log.Info("Logical-DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 414 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 415 | |
| 416 | func (core *Core) startAdapterManager(ctx context.Context) { |
| 417 | log.Info("Adapter-Manager-Starting...") |
| 418 | core.adapterMgr.start(ctx) |
| 419 | log.Info("Adapter-Manager-Started") |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 420 | } |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 421 | |
| 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 | */ |
| 436 | func (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 | } |