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" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
| 26 | "github.com/opencord/voltha-go/rw_core/core/api" |
| 27 | "github.com/opencord/voltha-go/rw_core/core/device" |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 28 | conf "github.com/opencord/voltha-lib-go/v3/pkg/config" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 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 | 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 { |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 39 | shutdown context.CancelFunc |
| 40 | stopped chan struct{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 41 | } |
| 42 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 43 | // NewCore creates instance of rw core |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 44 | func NewCore(ctx context.Context, id string, cf *config.RWCoreFlags) *Core { |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 45 | // If the context has a probe then fetch it and register our services |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 46 | if p := probe.GetProbeFromContext(ctx); p != nil { |
| 47 | p.RegisterService( |
| 48 | "message-bus", |
| 49 | "kv-store", |
| 50 | "adapter-manager", |
| 51 | "grpc-service", |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 52 | ) |
| 53 | } |
| 54 | |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 55 | // new threads will be given a new cancelable context, so that they can be aborted later when Stop() is called |
| 56 | shutdownCtx, cancelCtx := context.WithCancel(ctx) |
| 57 | |
| 58 | core := &Core{shutdown: cancelCtx, stopped: make(chan struct{})} |
| 59 | go core.start(shutdownCtx, id, cf) |
| 60 | return core |
| 61 | } |
| 62 | |
| 63 | func (core *Core) start(ctx context.Context, id string, cf *config.RWCoreFlags) { |
| 64 | logger.Info("starting-core-services", log.Fields{"coreId": id}) |
| 65 | |
| 66 | // deferred functions are used to run cleanup |
| 67 | // failing partway will stop anything that's been started |
| 68 | defer close(core.stopped) |
| 69 | defer core.shutdown() |
| 70 | |
| 71 | logger.Info("Starting RW Core components") |
| 72 | |
| 73 | // setup kv client |
| 74 | logger.Debugw("create-kv-client", log.Fields{"kvstore": cf.KVStoreType}) |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 75 | kvClient, err := newKVClient(cf.KVStoreType, cf.KVStoreAddress, cf.KVStoreTimeout) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 76 | if err != nil { |
| 77 | logger.Fatal(err) |
| 78 | } |
| 79 | defer stopKVClient(context.Background(), kvClient) |
| 80 | |
| 81 | // sync logging config with kv store |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 82 | cm := conf.NewConfigManager(kvClient, cf.KVStoreType, cf.KVStoreAddress, cf.KVStoreTimeout) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 83 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
| 84 | |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 85 | backend := cm.Backend |
| 86 | backend.LivenessChannelInterval = cf.LiveProbeInterval / 2 |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 87 | |
| 88 | // wait until connection to KV Store is up |
| 89 | if err := waitUntilKVStoreReachableOrMaxTries(ctx, kvClient, cf.MaxConnectionRetries, cf.ConnectionRetryInterval); err != nil { |
| 90 | logger.Fatal("Unable-to-connect-to-KV-store") |
| 91 | } |
| 92 | go monitorKVStoreLiveness(ctx, backend, cf.LiveProbeInterval, cf.NotLiveProbeInterval) |
| 93 | |
| 94 | // create kafka client |
| 95 | kafkaClient := kafka.NewSaramaClient( |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 96 | kafka.Address(cf.KafkaAdapterAddress), |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 97 | kafka.ConsumerType(kafka.GroupCustomer), |
| 98 | kafka.ProducerReturnOnErrors(true), |
| 99 | kafka.ProducerReturnOnSuccess(true), |
| 100 | kafka.ProducerMaxRetries(6), |
| 101 | kafka.NumPartitions(3), |
| 102 | kafka.ConsumerGroupName(id), |
| 103 | kafka.ConsumerGroupPrefix(id), |
| 104 | kafka.AutoCreateTopic(true), |
| 105 | kafka.ProducerFlushFrequency(5), |
| 106 | kafka.ProducerRetryBackoff(time.Millisecond*30), |
| 107 | kafka.LivenessChannelInterval(cf.LiveProbeInterval/2), |
| 108 | ) |
| 109 | // defer kafkaClient.Stop() |
| 110 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 111 | // create kv path |
| 112 | dbPath := model.NewDBPath(backend) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 113 | |
| 114 | // load adapters & device types while other things are starting |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 115 | adapterMgr := adapter.NewAdapterManager(dbPath, id, kafkaClient) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 116 | go adapterMgr.Start(ctx) |
| 117 | |
| 118 | // connect to kafka, then wait until reachable and publisher/consumer created |
| 119 | // core.kmp must be created before deviceMgr and adapterMgr |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 120 | kmp, err := startKafkInterContainerProxy(ctx, kafkaClient, cf.KafkaAdapterAddress, cf.CoreTopic, cf.ConnectionRetryInterval) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 121 | if err != nil { |
| 122 | logger.Warn("Failed to setup kafka connection") |
| 123 | return |
| 124 | } |
| 125 | defer kmp.Stop() |
| 126 | go monitorKafkaLiveness(ctx, kmp, cf.LiveProbeInterval, cf.NotLiveProbeInterval) |
| 127 | |
| 128 | // create the core of the system, the device managers |
| 129 | endpointMgr := kafka.NewEndpointManager(backend) |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 130 | deviceMgr, logicalDeviceMgr := device.NewManagers(dbPath, adapterMgr, kmp, endpointMgr, cf.CoreTopic, id, cf.DefaultCoreTimeout) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 131 | |
| 132 | // register kafka RPC handler |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 133 | registerAdapterRequestHandlers(kmp, deviceMgr, adapterMgr, cf.CoreTopic) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 134 | |
| 135 | // start gRPC handler |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 136 | grpcServer := grpcserver.NewGrpcServer(cf.GrpcAddress, nil, false, probe.GetProbeFromContext(ctx)) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 137 | go startGRPCService(ctx, grpcServer, api.NewNBIHandler(deviceMgr, logicalDeviceMgr, adapterMgr)) |
| 138 | defer grpcServer.Stop() |
| 139 | |
| 140 | // wait for core to be stopped, via Stop() or context cancellation, before running deferred functions |
| 141 | <-ctx.Done() |
| 142 | } |
| 143 | |
| 144 | // Stop brings down core services |
| 145 | func (core *Core) Stop() { |
| 146 | core.shutdown() |
| 147 | <-core.stopped |
| 148 | } |
| 149 | |
| 150 | // startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server |
| 151 | func startGRPCService(ctx context.Context, server *grpcserver.GrpcServer, handler voltha.VolthaServiceServer) { |
| 152 | logger.Info("grpc-server-created") |
| 153 | |
| 154 | server.AddService(func(gs *grpc.Server) { voltha.RegisterVolthaServiceServer(gs, handler) }) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 155 | logger.Info("grpc-service-added") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 156 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 157 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 158 | logger.Info("grpc-server-started") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 159 | // Note that there is a small window here in which the core could return its status as ready, |
| 160 | // when it really isn't. This is unlikely to cause issues, as the delay is incredibly short. |
| 161 | server.Start(ctx) |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 162 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 163 | } |