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" |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 21 | "fmt" |
Scott Baker | 2d87ee3 | 2020-03-03 13:04:01 -0800 | [diff] [blame] | 22 | "sync" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 23 | "time" |
| 24 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/rw_core/config" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
| 28 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 29 | grpcserver "github.com/opencord/voltha-lib-go/v3/pkg/grpc" |
| 30 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 31 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 32 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
| 33 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 34 | "google.golang.org/grpc" |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 35 | "google.golang.org/grpc/codes" |
| 36 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 37 | ) |
| 38 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 39 | // Core represent read,write core attributes |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 40 | type Core struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 41 | instanceID string |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 42 | deviceMgr *DeviceManager |
| 43 | logicalDeviceMgr *LogicalDeviceManager |
| 44 | grpcServer *grpcserver.GrpcServer |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 45 | grpcNBIAPIHandler *APIHandler |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 46 | adapterMgr *AdapterManager |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 47 | config *config.RWCoreFlags |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 48 | kmp kafka.InterContainerProxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 49 | clusterDataRoot model.Root |
| 50 | localDataRoot model.Root |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 51 | clusterDataProxy *model.Proxy |
| 52 | localDataProxy *model.Proxy |
Scott Baker | 2d87ee3 | 2020-03-03 13:04:01 -0800 | [diff] [blame] | 53 | exitChannel chan struct{} |
| 54 | stopOnce sync.Once |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 55 | kvClient kvstore.Client |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 56 | backend db.Backend |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 57 | kafkaClient kafka.Client |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 58 | deviceOwnership *DeviceOwnership |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func init() { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 62 | _, err := log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 63 | if err != nil { |
| 64 | log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err}) |
| 65 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 66 | } |
| 67 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 68 | // NewCore creates instance of rw core |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 69 | func NewCore(ctx context.Context, id string, cf *config.RWCoreFlags, kvClient kvstore.Client, kafkaClient kafka.Client) *Core { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 70 | var core Core |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 71 | core.instanceID = id |
Scott Baker | 2d87ee3 | 2020-03-03 13:04:01 -0800 | [diff] [blame] | 72 | core.exitChannel = make(chan struct{}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 73 | core.config = cf |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 74 | core.kvClient = kvClient |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 75 | core.kafkaClient = kafkaClient |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 76 | |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 77 | // Configure backend to push Liveness Status at least every (cf.LiveProbeInterval / 2) seconds |
| 78 | // so as to avoid trigger of Liveness check (due to Liveness timeout) when backend is alive |
| 79 | livenessChannelInterval := cf.LiveProbeInterval / 2 |
| 80 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 81 | // Setup the KV store |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 82 | core.backend = db.Backend{ |
| 83 | Client: kvClient, |
| 84 | StoreType: cf.KVStoreType, |
| 85 | Host: cf.KVStoreHost, |
| 86 | Port: cf.KVStorePort, |
| 87 | Timeout: cf.KVStoreTimeout, |
| 88 | LivenessChannelInterval: livenessChannelInterval, |
| 89 | PathPrefix: cf.KVStoreDataPrefix} |
| 90 | core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &core.backend) |
| 91 | core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &core.backend) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 92 | return &core |
| 93 | } |
| 94 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 95 | // Start brings up core services |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 96 | func (core *Core) Start(ctx context.Context) error { |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 97 | |
| 98 | // If the context has a probe then fetch it and register our services |
| 99 | var p *probe.Probe |
| 100 | if value := ctx.Value(probe.ProbeContextKey); value != nil { |
| 101 | if _, ok := value.(*probe.Probe); ok { |
| 102 | p = value.(*probe.Probe) |
| 103 | p.RegisterService( |
| 104 | "message-bus", |
| 105 | "kv-store", |
| 106 | "device-manager", |
| 107 | "logical-device-manager", |
| 108 | "adapter-manager", |
| 109 | "grpc-service", |
| 110 | ) |
| 111 | } |
| 112 | } |
| 113 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 114 | log.Info("starting-core-services", log.Fields{"coreId": core.instanceID}) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 115 | |
| 116 | // Wait until connection to KV Store is up |
| 117 | if err := core.waitUntilKVStoreReachableOrMaxTries(ctx, core.config.MaxConnectionRetries, core.config.ConnectionRetryInterval); err != nil { |
| 118 | log.Fatal("Unable-to-connect-to-KV-store") |
| 119 | } |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 120 | if p != nil { |
| 121 | p.UpdateStatus("kv-store", probe.ServiceStatusRunning) |
| 122 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 123 | var err error |
| 124 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 125 | core.clusterDataProxy, err = core.clusterDataRoot.CreateProxy(ctx, "/", false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 126 | if err != nil { |
| 127 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady) |
| 128 | return fmt.Errorf("Failed to create cluster data proxy") |
| 129 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 130 | core.localDataProxy, err = core.localDataRoot.CreateProxy(ctx, "/", false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 131 | if err != nil { |
| 132 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady) |
| 133 | return fmt.Errorf("Failed to create local data proxy") |
| 134 | } |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 135 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 136 | // core.kmp must be created before deviceMgr and adapterMgr, as they will make |
| 137 | // private copies of the poiner to core.kmp. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 138 | core.initKafkaManager(ctx) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 139 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 140 | log.Debugw("values", log.Fields{"kmp": core.kmp}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 141 | core.deviceMgr = newDeviceManager(core) |
Kent Hagerman | 16ce36a | 2019-12-17 13:40:53 -0500 | [diff] [blame] | 142 | core.adapterMgr = newAdapterManager(core.clusterDataProxy, core.instanceID, core.kafkaClient, core.deviceMgr) |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 143 | core.deviceMgr.adapterMgr = core.adapterMgr |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 144 | core.logicalDeviceMgr = newLogicalDeviceManager(core, core.deviceMgr, core.kmp, core.clusterDataProxy, core.config.DefaultCoreTimeout) |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 145 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 146 | // Start the KafkaManager. This must be done after the deviceMgr, adapterMgr, and |
| 147 | // logicalDeviceMgr have been created, as once the kmp is started, it will register |
| 148 | // the above with the kmp. |
| 149 | |
| 150 | go core.startKafkaManager(ctx, |
| 151 | core.config.ConnectionRetryInterval, |
| 152 | core.config.LiveProbeInterval, |
| 153 | core.config.NotLiveProbeInterval) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 154 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 155 | go core.startDeviceManager(ctx) |
| 156 | go core.startLogicalDeviceManager(ctx) |
| 157 | go core.startGRPCService(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 158 | go core.startAdapterManager(ctx) |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 159 | go core.monitorKvstoreLiveness(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 160 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 161 | // Setup device ownership context |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 162 | core.deviceOwnership = NewDeviceOwnership(core.instanceID, core.kvClient, core.deviceMgr, core.logicalDeviceMgr, |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 163 | "service/voltha/owns_device", 10) |
| 164 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 165 | log.Info("core-services-started") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 166 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 167 | } |
| 168 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 169 | // Stop brings down core services |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 170 | func (core *Core) Stop(ctx context.Context) { |
Scott Baker | 2d87ee3 | 2020-03-03 13:04:01 -0800 | [diff] [blame] | 171 | core.stopOnce.Do(func() { |
| 172 | log.Info("stopping-adaptercore") |
| 173 | // Signal to the KVStoreMonitor that we are stopping. |
| 174 | close(core.exitChannel) |
| 175 | // Stop all the started services |
| 176 | if core.grpcServer != nil { |
| 177 | core.grpcServer.Stop() |
| 178 | } |
| 179 | if core.logicalDeviceMgr != nil { |
| 180 | core.logicalDeviceMgr.stop(ctx) |
| 181 | } |
| 182 | if core.deviceMgr != nil { |
| 183 | core.deviceMgr.stop(ctx) |
| 184 | } |
| 185 | if core.kmp != nil { |
| 186 | core.kmp.Stop() |
| 187 | } |
| 188 | log.Info("adaptercore-stopped") |
| 189 | }) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 190 | } |
| 191 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 192 | //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] | 193 | func (core *Core) startGRPCService(ctx context.Context) { |
| 194 | // create an insecure gserver server |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 195 | 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] | 196 | log.Info("grpc-server-created") |
| 197 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 198 | core.grpcNBIAPIHandler = NewAPIHandler(core) |
Richard Jankowski | 46464e9 | 2019-03-05 11:53:55 -0500 | [diff] [blame] | 199 | log.Infow("grpc-handler", log.Fields{"core_binding_key": core.config.CoreBindingKey}) |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 200 | core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 201 | // Create a function to register the core GRPC service with the GRPC server |
| 202 | f := func(gs *grpc.Server) { |
| 203 | voltha.RegisterVolthaServiceServer( |
| 204 | gs, |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 205 | core.grpcNBIAPIHandler, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 206 | ) |
| 207 | } |
| 208 | |
| 209 | core.grpcServer.AddService(f) |
| 210 | log.Info("grpc-service-added") |
| 211 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 212 | /* |
| 213 | * Start the GRPC server |
| 214 | * |
| 215 | * This is a bit sub-optimal here as the grpcServer.Start call does not return (blocks) |
| 216 | * until something fails, but we want to send a "start" status update. As written this |
| 217 | * means that we are actually sending the "start" status update before the server is |
| 218 | * started, which means it is possible that the status is "running" before it actually is. |
| 219 | * |
| 220 | * This means that there is a small window in which the core could return its status as |
| 221 | * ready, when it really isn't. |
| 222 | */ |
| 223 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 224 | log.Info("grpc-server-started") |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 225 | core.grpcServer.Start(ctx) |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 226 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 229 | // Initialize the kafka manager, but we will start it later |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 230 | func (core *Core) initKafkaManager(ctx context.Context) { |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 231 | log.Infow("initialize-kafka-manager", log.Fields{"host": core.config.KafkaAdapterHost, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 232 | "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic}) |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 233 | |
| 234 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPreparing) |
| 235 | |
| 236 | // create the proxy |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 237 | core.kmp = kafka.NewInterContainerProxy( |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 238 | kafka.InterContainerHost(core.config.KafkaAdapterHost), |
| 239 | kafka.InterContainerPort(core.config.KafkaAdapterPort), |
| 240 | kafka.MsgClient(core.kafkaClient), |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 241 | kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}), |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 242 | kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic})) |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 243 | |
| 244 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusPrepared) |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
| 248 | * KafkaMonitorThread |
| 249 | * |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 250 | * Responsible for starting the Kafka Interadapter Proxy and monitoring its liveness |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 251 | * state. |
| 252 | * |
| 253 | * Any producer that fails to send will cause KafkaInterContainerProxy to |
| 254 | * post a false event on its liveness channel. Any producer that succeeds in sending |
| 255 | * will cause KafkaInterContainerProxy to post a true event on its liveness |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 256 | * channel. Group receivers also update liveness state, and a receiver will typically |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 257 | * indicate a loss of liveness within 3-5 seconds of Kafka going down. Receivers |
| 258 | * only indicate restoration of liveness if a message is received. During normal |
| 259 | * operation, messages will be routinely produced and received, automatically |
| 260 | * indicating liveness state. These routine liveness indications are rate-limited |
| 261 | * inside sarama_client. |
| 262 | * |
| 263 | * This thread monitors the status of KafkaInterContainerProxy's liveness and pushes |
| 264 | * that state to the core's readiness probes. If no liveness event has been seen |
| 265 | * within a timeout, then the thread will make an attempt to produce a "liveness" |
| 266 | * message, which will in turn trigger a liveness event on the liveness channel, true |
| 267 | * or false depending on whether the attempt succeeded. |
| 268 | * |
| 269 | * The gRPC server in turn monitors the state of the readiness probe and will |
| 270 | * start issuing UNAVAILABLE response while the probe is not ready. |
| 271 | * |
| 272 | * startupRetryInterval -- interval between attempts to start |
| 273 | * liveProbeInterval -- interval between liveness checks when in a live state |
| 274 | * notLiveProbeInterval -- interval between liveness checks when in a notLive state |
| 275 | * |
| 276 | * liveProbeInterval and notLiveProbeInterval can be configured separately, |
| 277 | * though the current default is that both are set to 60 seconds. |
| 278 | */ |
| 279 | |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 280 | 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] | 281 | log.Infow("starting-kafka-manager-thread", log.Fields{"host": core.config.KafkaAdapterHost, |
| 282 | "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic}) |
| 283 | |
| 284 | started := false |
| 285 | for !started { |
| 286 | // If we haven't started yet, then try to start |
| 287 | log.Infow("starting-kafka-proxy", log.Fields{}) |
| 288 | if err := core.kmp.Start(); err != nil { |
| 289 | // We failed to start. Delay and then try again later. |
| 290 | // Don't worry about liveness, as we can't be live until we've started. |
| 291 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 292 | log.Infow("error-starting-kafka-messaging-proxy", log.Fields{"error": err}) |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 293 | time.Sleep(startupRetryInterval) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 294 | } else { |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 295 | // We started. We only need to do this once. |
| 296 | // Next we'll fall through and start checking liveness. |
| 297 | log.Infow("started-kafka-proxy", log.Fields{}) |
| 298 | |
| 299 | // cannot do this until after the kmp is started |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 300 | 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] | 301 | log.Fatal("Failure-registering-adapterRequestHandler") |
| 302 | } |
| 303 | |
| 304 | started = true |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 305 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 306 | } |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 307 | |
| 308 | log.Info("started-kafka-message-proxy") |
| 309 | |
| 310 | livenessChannel := core.kmp.EnableLivenessChannel(true) |
| 311 | |
| 312 | log.Info("enabled-kafka-liveness-channel") |
| 313 | |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 314 | timeout := liveProbeInterval |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 315 | for { |
| 316 | timeoutTimer := time.NewTimer(timeout) |
| 317 | select { |
| 318 | case liveness := <-livenessChannel: |
| 319 | log.Infow("kafka-manager-thread-liveness-event", log.Fields{"liveness": liveness}) |
| 320 | // there was a state change in Kafka liveness |
| 321 | if !liveness { |
| 322 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady) |
| 323 | |
| 324 | if core.grpcServer != nil { |
| 325 | log.Info("kafka-manager-thread-set-server-notready") |
| 326 | } |
| 327 | |
| 328 | // retry frequently while life is bad |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 329 | timeout = notLiveProbeInterval |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 330 | } else { |
| 331 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning) |
| 332 | |
| 333 | if core.grpcServer != nil { |
| 334 | log.Info("kafka-manager-thread-set-server-ready") |
| 335 | } |
| 336 | |
| 337 | // retry infrequently while life is good |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 338 | timeout = liveProbeInterval |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 339 | } |
| 340 | if !timeoutTimer.Stop() { |
| 341 | <-timeoutTimer.C |
| 342 | } |
| 343 | case <-timeoutTimer.C: |
| 344 | log.Info("kafka-proxy-liveness-recheck") |
| 345 | // send the liveness probe in a goroutine; we don't want to deadlock ourselves as |
| 346 | // the liveness probe may wait (and block) writing to our channel. |
| 347 | go func() { |
| 348 | err := core.kmp.SendLiveness() |
| 349 | if err != nil { |
| 350 | // Catch possible error case if sending liveness after Sarama has been stopped. |
| 351 | log.Warnw("error-kafka-send-liveness", log.Fields{"error": err}) |
| 352 | } |
| 353 | }() |
| 354 | } |
| 355 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 356 | } |
| 357 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 358 | // 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] | 359 | func (core *Core) waitUntilKVStoreReachableOrMaxTries(ctx context.Context, maxRetries int, retryInterval time.Duration) error { |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 360 | log.Infow("verifying-KV-store-connectivity", log.Fields{"host": core.config.KVStoreHost, |
| 361 | "port": core.config.KVStorePort, "retries": maxRetries, "retryInterval": retryInterval}) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 362 | count := 0 |
| 363 | for { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 364 | if !core.kvClient.IsConnectionUp(ctx) { |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 365 | log.Info("KV-store-unreachable") |
| 366 | if maxRetries != -1 { |
| 367 | if count >= maxRetries { |
| 368 | return status.Error(codes.Unavailable, "kv store unreachable") |
| 369 | } |
| 370 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 371 | count++ |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 372 | // Take a nap before retrying |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 373 | time.Sleep(retryInterval) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 374 | log.Infow("retry-KV-store-connectivity", log.Fields{"retryCount": count, "maxRetries": maxRetries, "retryInterval": retryInterval}) |
| 375 | |
| 376 | } else { |
| 377 | break |
| 378 | } |
| 379 | } |
| 380 | log.Info("KV-store-reachable") |
| 381 | return nil |
| 382 | } |
| 383 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 384 | func (core *Core) registerAdapterRequestHandlers(ctx context.Context, coreInstanceID string, dMgr *DeviceManager, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 385 | ldMgr *LogicalDeviceManager, aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy, |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 386 | ) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 387 | requestProxy := NewAdapterRequestHandlerProxy(core, coreInstanceID, dMgr, ldMgr, aMgr, cdProxy, ldProxy, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 388 | core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 389 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 390 | // Register the broadcast topic to handle any core-bound broadcast requests |
| 391 | if err := core.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: core.config.CoreTopic}, requestProxy); err != nil { |
| 392 | log.Fatalw("Failed-registering-broadcast-handler", log.Fields{"topic": core.config.CoreTopic}) |
| 393 | return err |
| 394 | } |
| 395 | |
Kent Hagerman | a6d0c36 | 2019-07-30 12:50:21 -0400 | [diff] [blame] | 396 | // Register the core-pair topic to handle core-bound requests destined to the core pair |
| 397 | if err := core.kmp.SubscribeWithDefaultRequestHandler(kafka.Topic{Name: core.config.CorePairTopic}, kafka.OffsetNewest); err != nil { |
| 398 | log.Fatalw("Failed-registering-pair-handler", log.Fields{"topic": core.config.CorePairTopic}) |
| 399 | return err |
| 400 | } |
| 401 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 402 | log.Info("request-handler-registered") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 403 | return nil |
| 404 | } |
| 405 | |
| 406 | func (core *Core) startDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 407 | log.Info("DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 408 | core.deviceMgr.start(ctx, core.logicalDeviceMgr) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 409 | log.Info("DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | func (core *Core) startLogicalDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 413 | log.Info("Logical-DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 414 | core.logicalDeviceMgr.start(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 415 | log.Info("Logical-DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 416 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 417 | |
| 418 | func (core *Core) startAdapterManager(ctx context.Context) { |
| 419 | log.Info("Adapter-Manager-Starting...") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 420 | err := core.adapterMgr.start(ctx) |
| 421 | if err != nil { |
| 422 | log.Fatalf("failed-to-start-adapter-manager: error %v ", err) |
| 423 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 424 | log.Info("Adapter-Manager-Started") |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 425 | } |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 426 | |
| 427 | /* |
| 428 | * Thread to monitor kvstore Liveness (connection status) |
| 429 | * |
| 430 | * This function constantly monitors Liveness State of kvstore as reported |
| 431 | * periodically by backend and updates the Status of kv-store service registered |
| 432 | * with rw_core probe. |
| 433 | * |
| 434 | * If no liveness event has been seen within a timeout, then the thread will |
| 435 | * perform a "liveness" check attempt, which will in turn trigger a liveness event on |
| 436 | * the liveness channel, true or false depending on whether the attempt succeeded. |
| 437 | * |
| 438 | * The gRPC server in turn monitors the state of the readiness probe and will |
| 439 | * start issuing UNAVAILABLE response while the probe is not ready. |
| 440 | */ |
| 441 | func (core *Core) monitorKvstoreLiveness(ctx context.Context) { |
| 442 | log.Info("start-monitoring-kvstore-liveness") |
| 443 | |
| 444 | // Instruct backend to create Liveness channel for transporting state updates |
| 445 | livenessChannel := core.backend.EnableLivenessChannel() |
| 446 | |
| 447 | log.Debug("enabled-kvstore-liveness-channel") |
| 448 | |
| 449 | // Default state for kvstore is alive for rw_core |
| 450 | timeout := core.config.LiveProbeInterval |
Scott Baker | 2d87ee3 | 2020-03-03 13:04:01 -0800 | [diff] [blame] | 451 | loop: |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 452 | for { |
| 453 | timeoutTimer := time.NewTimer(timeout) |
| 454 | select { |
| 455 | |
| 456 | case liveness := <-livenessChannel: |
| 457 | log.Debugw("received-liveness-change-notification", log.Fields{"liveness": liveness}) |
| 458 | |
| 459 | if !liveness { |
| 460 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady) |
| 461 | |
| 462 | if core.grpcServer != nil { |
| 463 | log.Info("kvstore-set-server-notready") |
| 464 | } |
| 465 | |
| 466 | timeout = core.config.NotLiveProbeInterval |
| 467 | |
| 468 | } else { |
| 469 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning) |
| 470 | |
| 471 | if core.grpcServer != nil { |
| 472 | log.Info("kvstore-set-server-ready") |
| 473 | } |
| 474 | |
| 475 | timeout = core.config.LiveProbeInterval |
| 476 | } |
| 477 | |
| 478 | if !timeoutTimer.Stop() { |
| 479 | <-timeoutTimer.C |
| 480 | } |
| 481 | |
Scott Baker | 2d87ee3 | 2020-03-03 13:04:01 -0800 | [diff] [blame] | 482 | case <-core.exitChannel: |
| 483 | break loop |
| 484 | |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 485 | case <-timeoutTimer.C: |
| 486 | log.Info("kvstore-perform-liveness-check-on-timeout") |
| 487 | |
| 488 | // Trigger Liveness check if no liveness update received within the timeout period. |
| 489 | // The Liveness check will push Live state to same channel which this routine is |
| 490 | // reading and processing. This, do it asynchronously to avoid blocking for |
| 491 | // backend response and avoid any possibility of deadlock |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 492 | go core.backend.PerformLivenessCheck(ctx) |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |