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 |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 50 | kafkaClient kafka.Client |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 51 | deviceOwnership *DeviceOwnership |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func init() { |
| 55 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 56 | } |
| 57 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 58 | 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] | 59 | var core Core |
| 60 | core.instanceId = id |
| 61 | core.exitChannel = make(chan int, 1) |
| 62 | core.config = cf |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 63 | core.kvClient = kvClient |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 64 | core.kafkaClient = kafkaClient |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 65 | |
| 66 | // Setup the KV store |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 67 | backend := db.Backend{ |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 68 | Client: kvClient, |
| 69 | StoreType: cf.KVStoreType, |
| 70 | Host: cf.KVStoreHost, |
| 71 | Port: cf.KVStorePort, |
| 72 | Timeout: cf.KVStoreTimeout, |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 73 | PathPrefix: cf.KVStoreDataPrefix} |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 74 | core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &backend) |
| 75 | core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &backend) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 76 | core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false) |
| 77 | core.localDataProxy = core.localDataRoot.CreateProxy(context.Background(), "/", false) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 78 | return &core |
| 79 | } |
| 80 | |
| 81 | func (core *Core) Start(ctx context.Context) { |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 82 | |
| 83 | // If the context has a probe then fetch it and register our services |
| 84 | var p *probe.Probe |
| 85 | if value := ctx.Value(probe.ProbeContextKey); value != nil { |
| 86 | if _, ok := value.(*probe.Probe); ok { |
| 87 | p = value.(*probe.Probe) |
| 88 | p.RegisterService( |
| 89 | "message-bus", |
| 90 | "kv-store", |
| 91 | "device-manager", |
| 92 | "logical-device-manager", |
| 93 | "adapter-manager", |
| 94 | "grpc-service", |
| 95 | ) |
| 96 | } |
| 97 | } |
| 98 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 99 | log.Info("starting-core-services", log.Fields{"coreId": core.instanceId}) |
| 100 | |
| 101 | // Wait until connection to KV Store is up |
| 102 | if err := core.waitUntilKVStoreReachableOrMaxTries(ctx, core.config.MaxConnectionRetries, core.config.ConnectionRetryInterval); err != nil { |
| 103 | log.Fatal("Unable-to-connect-to-KV-store") |
| 104 | } |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 105 | if p != nil { |
| 106 | p.UpdateStatus("kv-store", probe.ServiceStatusRunning) |
| 107 | } |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 108 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 109 | // core.kmp must be created before deviceMgr and adapterMgr, as they will make |
| 110 | // private copies of the poiner to core.kmp. |
| 111 | if err := core.initKafkaManager(ctx); err != nil { |
| 112 | log.Fatal("Failed-to-init-kafka-manager") |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 113 | } |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 114 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 115 | log.Debugw("values", log.Fields{"kmp": core.kmp}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 116 | core.deviceMgr = newDeviceManager(core) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 117 | core.adapterMgr = newAdapterManager(core.clusterDataProxy, core.instanceId, core.deviceMgr) |
| 118 | core.deviceMgr.adapterMgr = core.adapterMgr |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 119 | core.logicalDeviceMgr = newLogicalDeviceManager(core, core.deviceMgr, core.kmp, core.clusterDataProxy, core.config.DefaultCoreTimeout) |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 120 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 121 | // Start the KafkaManager. This must be done after the deviceMgr, adapterMgr, and |
| 122 | // logicalDeviceMgr have been created, as once the kmp is started, it will register |
| 123 | // the above with the kmp. |
| 124 | |
| 125 | go core.startKafkaManager(ctx, |
| 126 | core.config.ConnectionRetryInterval, |
| 127 | core.config.LiveProbeInterval, |
| 128 | core.config.NotLiveProbeInterval) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 129 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 130 | go core.startDeviceManager(ctx) |
| 131 | go core.startLogicalDeviceManager(ctx) |
| 132 | go core.startGRPCService(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 133 | go core.startAdapterManager(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 134 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 135 | // Setup device ownership context |
| 136 | core.deviceOwnership = NewDeviceOwnership(core.instanceId, core.kvClient, core.deviceMgr, core.logicalDeviceMgr, |
| 137 | "service/voltha/owns_device", 10) |
| 138 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 139 | log.Info("core-services-started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | func (core *Core) Stop(ctx context.Context) { |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 143 | log.Info("stopping-adaptercore") |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 144 | if core.exitChannel != nil { |
| 145 | core.exitChannel <- 1 |
| 146 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 147 | // Stop all the started services |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 148 | if core.grpcServer != nil { |
| 149 | core.grpcServer.Stop() |
| 150 | } |
| 151 | if core.logicalDeviceMgr != nil { |
| 152 | core.logicalDeviceMgr.stop(ctx) |
| 153 | } |
| 154 | if core.deviceMgr != nil { |
| 155 | core.deviceMgr.stop(ctx) |
| 156 | } |
| 157 | if core.kmp != nil { |
| 158 | core.kmp.Stop() |
| 159 | } |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 160 | log.Info("adaptercore-stopped") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 161 | } |
| 162 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 163 | //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] | 164 | func (core *Core) startGRPCService(ctx context.Context) { |
| 165 | // create an insecure gserver server |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 166 | 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] | 167 | log.Info("grpc-server-created") |
| 168 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 169 | core.grpcNBIAPIHandler = NewAPIHandler(core) |
Richard Jankowski | 46464e9 | 2019-03-05 11:53:55 -0500 | [diff] [blame] | 170 | log.Infow("grpc-handler", log.Fields{"core_binding_key": core.config.CoreBindingKey}) |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 171 | core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 172 | // Create a function to register the core GRPC service with the GRPC server |
| 173 | f := func(gs *grpc.Server) { |
| 174 | voltha.RegisterVolthaServiceServer( |
| 175 | gs, |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 176 | core.grpcNBIAPIHandler, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 177 | ) |
| 178 | } |
| 179 | |
| 180 | core.grpcServer.AddService(f) |
| 181 | log.Info("grpc-service-added") |
| 182 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 183 | /* |
| 184 | * Start the GRPC server |
| 185 | * |
| 186 | * This is a bit sub-optimal here as the grpcServer.Start call does not return (blocks) |
| 187 | * until something fails, but we want to send a "start" status update. As written this |
| 188 | * means that we are actually sending the "start" status update before the server is |
| 189 | * started, which means it is possible that the status is "running" before it actually is. |
| 190 | * |
| 191 | * This means that there is a small window in which the core could return its status as |
| 192 | * ready, when it really isn't. |
| 193 | */ |
| 194 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 195 | log.Info("grpc-server-started") |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 196 | core.grpcServer.Start(context.Background()) |
| 197 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 200 | // Initialize the kafka manager, but we will start it later |
| 201 | func (core *Core) initKafkaManager(ctx context.Context) error { |
| 202 | log.Infow("initialize-kafka-manager", log.Fields{"host": core.config.KafkaAdapterHost, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 203 | "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic}) |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 204 | |
| 205 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPreparing) |
| 206 | |
| 207 | // create the proxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 208 | var err error |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 209 | if core.kmp, err = kafka.NewInterContainerProxy( |
| 210 | kafka.InterContainerHost(core.config.KafkaAdapterHost), |
| 211 | kafka.InterContainerPort(core.config.KafkaAdapterPort), |
| 212 | kafka.MsgClient(core.kafkaClient), |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 213 | kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}), |
| 214 | kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic})); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 215 | log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err}) |
| 216 | return err |
| 217 | } |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 218 | |
| 219 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPrepared) |
| 220 | |
| 221 | return nil |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * KafkaMonitorThread |
| 226 | * |
| 227 | * Repsonsible for starting the Kafka Interadapter Proxy and monitoring its liveness |
| 228 | * state. |
| 229 | * |
| 230 | * Any producer that fails to send will cause KafkaInterContainerProxy to |
| 231 | * post a false event on its liveness channel. Any producer that succeeds in sending |
| 232 | * will cause KafkaInterContainerProxy to post a true event on its liveness |
| 233 | * channel. Group recievers also update liveness state, and a receiver will typically |
| 234 | * indicate a loss of liveness within 3-5 seconds of Kafka going down. Receivers |
| 235 | * only indicate restoration of liveness if a message is received. During normal |
| 236 | * operation, messages will be routinely produced and received, automatically |
| 237 | * indicating liveness state. These routine liveness indications are rate-limited |
| 238 | * inside sarama_client. |
| 239 | * |
| 240 | * This thread monitors the status of KafkaInterContainerProxy's liveness and pushes |
| 241 | * that state to the core's readiness probes. If no liveness event has been seen |
| 242 | * within a timeout, then the thread will make an attempt to produce a "liveness" |
| 243 | * message, which will in turn trigger a liveness event on the liveness channel, true |
| 244 | * or false depending on whether the attempt succeeded. |
| 245 | * |
| 246 | * The gRPC server in turn monitors the state of the readiness probe and will |
| 247 | * start issuing UNAVAILABLE response while the probe is not ready. |
| 248 | * |
| 249 | * startupRetryInterval -- interval between attempts to start |
| 250 | * liveProbeInterval -- interval between liveness checks when in a live state |
| 251 | * notLiveProbeInterval -- interval between liveness checks when in a notLive state |
| 252 | * |
| 253 | * liveProbeInterval and notLiveProbeInterval can be configured separately, |
| 254 | * though the current default is that both are set to 60 seconds. |
| 255 | */ |
| 256 | |
| 257 | func (core *Core) startKafkaManager(ctx context.Context, startupRetryInterval int, liveProbeInterval int, notLiveProbeInterval int) { |
| 258 | log.Infow("starting-kafka-manager-thread", log.Fields{"host": core.config.KafkaAdapterHost, |
| 259 | "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic}) |
| 260 | |
| 261 | started := false |
| 262 | for !started { |
| 263 | // If we haven't started yet, then try to start |
| 264 | log.Infow("starting-kafka-proxy", log.Fields{}) |
| 265 | if err := core.kmp.Start(); err != nil { |
| 266 | // We failed to start. Delay and then try again later. |
| 267 | // Don't worry about liveness, as we can't be live until we've started. |
| 268 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 269 | log.Infow("error-starting-kafka-messaging-proxy", log.Fields{"error": err}) |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 270 | time.Sleep(time.Duration(startupRetryInterval) * time.Second) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 271 | } else { |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 272 | // We started. We only need to do this once. |
| 273 | // Next we'll fall through and start checking liveness. |
| 274 | log.Infow("started-kafka-proxy", log.Fields{}) |
| 275 | |
| 276 | // cannot do this until after the kmp is started |
| 277 | if err := core.registerAdapterRequestHandlers(ctx, core.instanceId, core.deviceMgr, core.logicalDeviceMgr, core.adapterMgr, core.clusterDataProxy, core.localDataProxy); err != nil { |
| 278 | log.Fatal("Failure-registering-adapterRequestHandler") |
| 279 | } |
| 280 | |
| 281 | started = true |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 282 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 283 | } |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 284 | |
| 285 | log.Info("started-kafka-message-proxy") |
| 286 | |
| 287 | livenessChannel := core.kmp.EnableLivenessChannel(true) |
| 288 | |
| 289 | log.Info("enabled-kafka-liveness-channel") |
| 290 | |
| 291 | timeout := time.Duration(liveProbeInterval) * time.Second |
| 292 | for { |
| 293 | timeoutTimer := time.NewTimer(timeout) |
| 294 | select { |
| 295 | case liveness := <-livenessChannel: |
| 296 | log.Infow("kafka-manager-thread-liveness-event", log.Fields{"liveness": liveness}) |
| 297 | // there was a state change in Kafka liveness |
| 298 | if !liveness { |
| 299 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady) |
| 300 | |
| 301 | if core.grpcServer != nil { |
| 302 | log.Info("kafka-manager-thread-set-server-notready") |
| 303 | } |
| 304 | |
| 305 | // retry frequently while life is bad |
| 306 | timeout = time.Duration(notLiveProbeInterval) * time.Second |
| 307 | } else { |
| 308 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning) |
| 309 | |
| 310 | if core.grpcServer != nil { |
| 311 | log.Info("kafka-manager-thread-set-server-ready") |
| 312 | } |
| 313 | |
| 314 | // retry infrequently while life is good |
| 315 | timeout = time.Duration(liveProbeInterval) * time.Second |
| 316 | } |
| 317 | if !timeoutTimer.Stop() { |
| 318 | <-timeoutTimer.C |
| 319 | } |
| 320 | case <-timeoutTimer.C: |
| 321 | log.Info("kafka-proxy-liveness-recheck") |
| 322 | // send the liveness probe in a goroutine; we don't want to deadlock ourselves as |
| 323 | // the liveness probe may wait (and block) writing to our channel. |
| 324 | go func() { |
| 325 | err := core.kmp.SendLiveness() |
| 326 | if err != nil { |
| 327 | // Catch possible error case if sending liveness after Sarama has been stopped. |
| 328 | log.Warnw("error-kafka-send-liveness", log.Fields{"error": err}) |
| 329 | } |
| 330 | }() |
| 331 | } |
| 332 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 333 | } |
| 334 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 335 | // waitUntilKVStoreReachableOrMaxTries will wait until it can connect to a KV store or until maxtries has been reached |
| 336 | func (core *Core) waitUntilKVStoreReachableOrMaxTries(ctx context.Context, maxRetries int, retryInterval int) error { |
| 337 | log.Infow("verifying-KV-store-connectivity", log.Fields{"host": core.config.KVStoreHost, |
| 338 | "port": core.config.KVStorePort, "retries": maxRetries, "retryInterval": retryInterval}) |
| 339 | // Get timeout in seconds with 1 second set as minimum |
| 340 | timeout := int(core.config.DefaultCoreTimeout / 1000) |
| 341 | if timeout < 1 { |
| 342 | timeout = 1 |
| 343 | } |
| 344 | count := 0 |
| 345 | for { |
| 346 | if !core.kvClient.IsConnectionUp(timeout) { |
| 347 | log.Info("KV-store-unreachable") |
| 348 | if maxRetries != -1 { |
| 349 | if count >= maxRetries { |
| 350 | return status.Error(codes.Unavailable, "kv store unreachable") |
| 351 | } |
| 352 | } |
| 353 | count += 1 |
| 354 | // Take a nap before retrying |
| 355 | time.Sleep(time.Duration(retryInterval) * time.Second) |
| 356 | log.Infow("retry-KV-store-connectivity", log.Fields{"retryCount": count, "maxRetries": maxRetries, "retryInterval": retryInterval}) |
| 357 | |
| 358 | } else { |
| 359 | break |
| 360 | } |
| 361 | } |
| 362 | log.Info("KV-store-reachable") |
| 363 | return nil |
| 364 | } |
| 365 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 366 | func (core *Core) registerAdapterRequestHandlers(ctx context.Context, coreInstanceId string, dMgr *DeviceManager, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 367 | ldMgr *LogicalDeviceManager, aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy, |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 368 | ) error { |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 369 | requestProxy := NewAdapterRequestHandlerProxy(core, coreInstanceId, dMgr, ldMgr, aMgr, cdProxy, ldProxy, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 370 | core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 371 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 372 | // Register the broadcast topic to handle any core-bound broadcast requests |
| 373 | if err := core.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: core.config.CoreTopic}, requestProxy); err != nil { |
| 374 | log.Fatalw("Failed-registering-broadcast-handler", log.Fields{"topic": core.config.CoreTopic}) |
| 375 | return err |
| 376 | } |
| 377 | |
Kent Hagerman | a6d0c36 | 2019-07-30 12:50:21 -0400 | [diff] [blame] | 378 | // Register the core-pair topic to handle core-bound requests destined to the core pair |
| 379 | if err := core.kmp.SubscribeWithDefaultRequestHandler(kafka.Topic{Name: core.config.CorePairTopic}, kafka.OffsetNewest); err != nil { |
| 380 | log.Fatalw("Failed-registering-pair-handler", log.Fields{"topic": core.config.CorePairTopic}) |
| 381 | return err |
| 382 | } |
| 383 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 384 | log.Info("request-handler-registered") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 385 | return nil |
| 386 | } |
| 387 | |
| 388 | func (core *Core) startDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 389 | log.Info("DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 390 | core.deviceMgr.start(ctx, core.logicalDeviceMgr) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 391 | log.Info("DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | func (core *Core) startLogicalDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 395 | log.Info("Logical-DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 396 | core.logicalDeviceMgr.start(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 397 | log.Info("Logical-DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 398 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 399 | |
| 400 | func (core *Core) startAdapterManager(ctx context.Context) { |
| 401 | log.Info("Adapter-Manager-Starting...") |
| 402 | core.adapterMgr.start(ctx) |
| 403 | log.Info("Adapter-Manager-Started") |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 404 | } |