cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | f9bffdf | 2022-12-27 07:05:28 -0500 | [diff] [blame] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 3 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 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 |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 7 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 9 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 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. |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 15 | */ |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 16 | |
| 17 | //Package main invokes the application |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "errors" |
| 23 | "fmt" |
nikesh.krishnan | 6dd882b | 2023-03-14 10:02:41 +0530 | [diff] [blame] | 24 | grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry" |
| 25 | codes "google.golang.org/grpc/codes" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 26 | "os" |
| 27 | "os/signal" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 28 | "syscall" |
| 29 | "time" |
| 30 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 31 | conf "github.com/opencord/voltha-lib-go/v7/pkg/config" |
| 32 | "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore" |
| 33 | "github.com/opencord/voltha-lib-go/v7/pkg/events" |
| 34 | "github.com/opencord/voltha-lib-go/v7/pkg/events/eventif" |
| 35 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
| 36 | "github.com/opencord/voltha-lib-go/v7/pkg/kafka" |
| 37 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 38 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
| 39 | "github.com/opencord/voltha-lib-go/v7/pkg/version" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 40 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/config" |
| 41 | ac "github.com/opencord/voltha-openolt-adapter/internal/pkg/core" |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 42 | "github.com/opencord/voltha-protos/v5/go/adapter_service" |
| 43 | ca "github.com/opencord/voltha-protos/v5/go/core_adapter" |
| 44 | "github.com/opencord/voltha-protos/v5/go/core_service" |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 45 | "github.com/opencord/voltha-protos/v5/go/olt_inter_adapter_service" |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 46 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 47 | "google.golang.org/grpc" |
| 48 | ) |
| 49 | |
| 50 | const ( |
| 51 | clusterMessagingService = "cluster-message-service" |
| 52 | oltAdapterService = "olt-adapter-service" |
| 53 | kvService = "kv-service" |
| 54 | coreService = "core-service" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 55 | ) |
| 56 | |
| 57 | type adapter struct { |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 58 | instanceID string |
| 59 | config *config.AdapterFlags |
| 60 | grpcServer *vgrpc.GrpcServer |
| 61 | oltAdapter *ac.OpenOLT |
| 62 | oltInterAdapter *ac.OpenOLTInterAdapter |
| 63 | kafkaClient kafka.Client |
| 64 | kvClient kvstore.Client |
| 65 | coreClient *vgrpc.Client |
| 66 | eventProxy eventif.EventProxy |
| 67 | halted bool |
| 68 | exitChannel chan int |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 69 | } |
| 70 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 71 | func newAdapter(cf *config.AdapterFlags) *adapter { |
| 72 | var a adapter |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 73 | a.instanceID = cf.InstanceID |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 74 | a.config = cf |
| 75 | a.halted = false |
| 76 | a.exitChannel = make(chan int, 1) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 77 | return &a |
| 78 | } |
| 79 | |
| 80 | func (a *adapter) start(ctx context.Context) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 81 | logger.Info(ctx, "Starting Core Adapter components") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 82 | var err error |
| 83 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 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( |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 89 | ctx, |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 90 | clusterMessagingService, |
| 91 | kvService, |
| 92 | oltAdapterService, |
| 93 | coreService, |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 94 | ) |
| 95 | } |
| 96 | } |
| 97 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 98 | // Setup KV Client |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 99 | logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": a.config.KVStoreType}) |
| 100 | if err = a.setKVClient(ctx); err != nil { |
| 101 | logger.Fatalw(ctx, "error-setting-kv-client", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 104 | if p != nil { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 105 | p.UpdateStatus(ctx, kvService, probe.ServiceStatusRunning) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 106 | } |
| 107 | |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 108 | // Setup Log Config |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 109 | cm := conf.NewConfigManager(ctx, a.kvClient, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 110 | |
divyadesai | d26f6b1 | 2020-03-19 06:30:28 +0000 | [diff] [blame] | 111 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
Girish Kumar | 935f7af | 2020-08-18 11:59:42 +0000 | [diff] [blame] | 112 | go conf.StartLogFeaturesConfigProcessing(cm, ctx) |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 113 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 114 | // Setup Kafka Client |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 115 | if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaClusterAddress); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 116 | logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 117 | } |
| 118 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 119 | // Start kafka communication with the broker |
| 120 | if err := kafka.StartAndWaitUntilKafkaConnectionIsUp(ctx, a.kafkaClient, a.config.HeartbeatCheckInterval, clusterMessagingService); err != nil { |
| 121 | logger.Fatal(ctx, "unable-to-connect-to-kafka") |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 124 | // Create the event proxy to post events to KAFKA |
Himani Chawla | cd40780 | 2020-12-10 12:08:59 +0530 | [diff] [blame] | 125 | a.eventProxy = events.NewEventProxy(events.MsgClient(a.kafkaClient), events.MsgTopic(kafka.Topic{Name: a.config.EventTopic})) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 126 | go func() { |
| 127 | if err := a.eventProxy.Start(); err != nil { |
| 128 | logger.Fatalw(ctx, "event-proxy-cannot-start", log.Fields{"error": err}) |
| 129 | } |
| 130 | }() |
| 131 | |
| 132 | // Create the Core client to handle requests to the Core. Note that the coreClient is an interface and needs to be |
| 133 | // cast to the appropriate grpc client by invoking GetCoreGrpcClient on the a.coreClient |
khenaidoo | 27e7ac9 | 2021-12-08 14:43:09 -0500 | [diff] [blame] | 134 | if a.coreClient, err = vgrpc.NewClient( |
| 135 | a.config.AdapterEndpoint, |
| 136 | a.config.CoreEndpoint, |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 137 | "core_service.CoreService", |
khenaidoo | 27e7ac9 | 2021-12-08 14:43:09 -0500 | [diff] [blame] | 138 | a.coreRestarted); err != nil { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 139 | logger.Fatal(ctx, "grpc-client-not-created") |
| 140 | } |
| 141 | // Start the core grpc client |
nikesh.krishnan | 6dd882b | 2023-03-14 10:02:41 +0530 | [diff] [blame] | 142 | retryCodes := []codes.Code{ |
| 143 | codes.Unavailable, // server is currently unavailable |
| 144 | codes.DeadlineExceeded, // deadline for the operation was exceeded |
| 145 | } |
| 146 | grpcRetryOptions := grpc_retry.UnaryClientInterceptor(grpc_retry.WithMax(a.config.MaxRetries), grpc_retry.WithPerRetryTimeout(a.config.PerRPCRetryTimeout), grpc_retry.WithCodes(retryCodes...)) |
| 147 | logger.Debug(ctx, "Configuration values", log.Fields{"RETRY": a.config.MaxRetries, "TIMEOUT": a.config.PerRPCRetryTimeout}) |
| 148 | go a.coreClient.Start(ctx, getCoreServiceClientHandler, grpcRetryOptions) |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 149 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 150 | // Create the open OLT adapter |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 151 | if a.oltAdapter, err = a.startOpenOLT(ctx, a.coreClient, a.eventProxy, a.config, cm); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 152 | logger.Fatalw(ctx, "error-starting-openolt", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 153 | } |
| 154 | |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 155 | // Create the open OLT Inter adapter adapter |
| 156 | if a.oltInterAdapter, err = a.startOpenOLTInterAdapter(ctx, a.oltAdapter); err != nil { |
| 157 | logger.Fatalw(ctx, "error-starting-openolt-inter-adapter", log.Fields{"error": err}) |
| 158 | } |
| 159 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 160 | // Create and start the grpc server |
| 161 | a.grpcServer = vgrpc.NewGrpcServer(a.config.GrpcAddress, nil, false, p) |
| 162 | |
| 163 | //Register the adapter service |
| 164 | a.addAdapterService(ctx, a.grpcServer, a.oltAdapter) |
| 165 | |
| 166 | //Register the olt inter-adapter service |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 167 | a.addOltInterAdapterService(ctx, a.grpcServer, a.oltInterAdapter) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 168 | |
| 169 | // Start the grpc server |
| 170 | go a.startGRPCService(ctx, a.grpcServer, oltAdapterService) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 171 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 172 | // Register this adapter to the Core - retries indefinitely |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 173 | if err = a.registerWithCore(ctx, coreService, -1); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 174 | logger.Fatal(ctx, "error-registering-with-core") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 175 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 176 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 177 | // check the readiness and liveliness and update the probe status |
| 178 | a.checkServicesReadiness(ctx) |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 179 | } |
| 180 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 181 | // TODO: Any action the adapter needs to do following a Core restart? |
| 182 | func (a *adapter) coreRestarted(ctx context.Context, endPoint string) error { |
| 183 | logger.Errorw(ctx, "core-restarted", log.Fields{"endpoint": endPoint}) |
| 184 | return nil |
| 185 | } |
| 186 | |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 187 | // getCoreServiceClientHandler is used to test whether the remote gRPC service is up |
| 188 | func getCoreServiceClientHandler(ctx context.Context, conn *grpc.ClientConn) interface{} { |
| 189 | if conn == nil { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 190 | return nil |
| 191 | } |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 192 | return core_service.NewCoreServiceClient(conn) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 193 | } |
| 194 | |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 195 | /** |
| 196 | This function checks the liveliness and readiness of the kakfa and kv-client services |
| 197 | and update the status in the probe. |
| 198 | */ |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 199 | func (a *adapter) checkServicesReadiness(ctx context.Context) { |
| 200 | // checks the kafka readiness |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 201 | go kafka.MonitorKafkaReadiness(ctx, a.kafkaClient, a.config.LiveProbeInterval, a.config.NotLiveProbeInterval, clusterMessagingService) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 202 | |
| 203 | // checks the kv-store readiness |
| 204 | go a.checkKvStoreReadiness(ctx) |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | This function checks the liveliness and readiness of the kv-store service |
| 209 | and update the status in the probe. |
| 210 | */ |
| 211 | func (a *adapter) checkKvStoreReadiness(ctx context.Context) { |
| 212 | // dividing the live probe interval by 2 to get updated status every 30s |
| 213 | timeout := a.config.LiveProbeInterval / 2 |
| 214 | kvStoreChannel := make(chan bool, 1) |
| 215 | |
Girish Gowdra | 4b48fa4 | 2022-06-01 18:10:08 -0700 | [diff] [blame] | 216 | timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second) |
| 217 | kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx) |
| 218 | cancelFunc() |
| 219 | |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 220 | for { |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 221 | timeoutTimer := time.NewTimer(timeout) |
| 222 | select { |
| 223 | case liveliness := <-kvStoreChannel: |
| 224 | if !liveliness { |
| 225 | // kv-store not reachable or down, updating the status to not ready state |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 226 | probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusNotReady) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 227 | timeout = a.config.NotLiveProbeInterval |
| 228 | } else { |
| 229 | // kv-store is reachable , updating the status to running state |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 230 | probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusRunning) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 231 | timeout = a.config.LiveProbeInterval / 2 |
| 232 | } |
Girish Gowdra | 4b48fa4 | 2022-06-01 18:10:08 -0700 | [diff] [blame] | 233 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 234 | // Check if the timer has expired or not |
| 235 | if !timeoutTimer.Stop() { |
| 236 | <-timeoutTimer.C |
| 237 | } |
Girish Gowdra | 4b48fa4 | 2022-06-01 18:10:08 -0700 | [diff] [blame] | 238 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 239 | case <-timeoutTimer.C: |
Girish Kumar | beadc11 | 2020-02-26 18:41:02 +0000 | [diff] [blame] | 240 | // Check the status of the kv-store. Use timeout of 2 seconds to avoid forever blocking |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 241 | logger.Info(ctx, "kv-store liveliness-recheck") |
Girish Kumar | beadc11 | 2020-02-26 18:41:02 +0000 | [diff] [blame] | 242 | timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second) |
| 243 | |
| 244 | kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx) |
| 245 | // Cleanup cancel func resources |
| 246 | cancelFunc() |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 247 | } |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 251 | func (a *adapter) stop(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 252 | // Stop leadership tracking |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 253 | a.halted = true |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 254 | |
| 255 | // send exit signal |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 256 | a.exitChannel <- 0 |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 257 | |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 258 | // Stop all grpc processing |
| 259 | if err := a.oltAdapter.Stop(ctx); err != nil { |
| 260 | logger.Errorw(ctx, "failure-stopping-olt-adapter-service", log.Fields{"error": err, "adapter": a.config.AdapterName}) |
| 261 | } |
| 262 | if err := a.oltInterAdapter.Stop(ctx); err != nil { |
| 263 | logger.Errorw(ctx, "failure-stopping-olt-inter-adapter-service", log.Fields{"error": err, "adapter": a.config.AdapterName}) |
| 264 | } |
| 265 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 266 | // Cleanup - applies only if we had a kvClient |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 267 | if a.kvClient != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 268 | // Release all reservations |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 269 | if err := a.kvClient.ReleaseAllReservations(ctx); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 270 | logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 271 | } |
| 272 | // Close the DB connection |
Girish Gowdra | 4b48fa4 | 2022-06-01 18:10:08 -0700 | [diff] [blame] | 273 | go a.kvClient.Close(ctx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 274 | } |
| 275 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 276 | if a.eventProxy != nil { |
| 277 | a.eventProxy.Stop() |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 278 | } |
| 279 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 280 | if a.kafkaClient != nil { |
| 281 | a.kafkaClient.Stop(ctx) |
| 282 | } |
| 283 | |
| 284 | // Stop core client |
| 285 | if a.coreClient != nil { |
| 286 | a.coreClient.Stop(ctx) |
| 287 | } |
| 288 | |
Girish Gowdra | 4b48fa4 | 2022-06-01 18:10:08 -0700 | [diff] [blame] | 289 | logger.Info(ctx, "main-stop-processing-complete") |
| 290 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 291 | // TODO: Stop child devices connections |
| 292 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 293 | // TODO: More cleanup |
| 294 | } |
| 295 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 296 | func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 297 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 298 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 299 | switch storeType { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 300 | case "etcd": |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 301 | return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 302 | } |
| 303 | return nil, errors.New("unsupported-kv-store") |
| 304 | } |
| 305 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 306 | func newKafkaClient(ctx context.Context, clientType, address string) (kafka.Client, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 307 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 308 | logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 309 | switch clientType { |
| 310 | case "sarama": |
| 311 | return kafka.NewSaramaClient( |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 312 | kafka.Address(address), |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 313 | kafka.ProducerReturnOnErrors(true), |
| 314 | kafka.ProducerReturnOnSuccess(true), |
| 315 | kafka.ProducerMaxRetries(6), |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 316 | kafka.ProducerRetryBackoff(time.Millisecond*30), |
| 317 | kafka.MetadatMaxRetries(15)), nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 318 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 319 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 320 | return nil, errors.New("unsupported-client-type") |
| 321 | } |
| 322 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 323 | func (a *adapter) setKVClient(ctx context.Context) error { |
| 324 | client, err := newKVClient(ctx, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 325 | if err != nil { |
| 326 | a.kvClient = nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 327 | return err |
| 328 | } |
| 329 | a.kvClient = client |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 330 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 331 | return nil |
| 332 | } |
| 333 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 334 | // startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server |
| 335 | func (a *adapter) startGRPCService(ctx context.Context, server *vgrpc.GrpcServer, serviceName string) { |
| 336 | logger.Infow(ctx, "starting-grpc-service", log.Fields{"service": serviceName}) |
| 337 | |
| 338 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
| 339 | logger.Infow(ctx, "grpc-service-started", log.Fields{"service": serviceName}) |
| 340 | |
| 341 | server.Start(ctx) |
| 342 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusStopped) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 343 | } |
| 344 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 345 | func (a *adapter) addAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_service.AdapterServiceServer) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 346 | logger.Info(ctx, "adding-adapter-service") |
| 347 | |
| 348 | server.AddService(func(gs *grpc.Server) { |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 349 | adapter_service.RegisterAdapterServiceServer(gs, handler) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 350 | }) |
| 351 | } |
| 352 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 353 | func (a *adapter) addOltInterAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler olt_inter_adapter_service.OltInterAdapterServiceServer) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 354 | logger.Info(ctx, "adding-olt-inter-adapter-service") |
| 355 | |
| 356 | server.AddService(func(gs *grpc.Server) { |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 357 | olt_inter_adapter_service.RegisterOltInterAdapterServiceServer(gs, handler) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 358 | }) |
| 359 | } |
| 360 | |
| 361 | func (a *adapter) startOpenOLT(ctx context.Context, cc *vgrpc.Client, ep eventif.EventProxy, |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 362 | cfg *config.AdapterFlags, cm *conf.ConfigManager) (*ac.OpenOLT, error) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 363 | logger.Info(ctx, "starting-open-olt") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 364 | var err error |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 365 | sOLT := ac.NewOpenOLT(ctx, cc, ep, cfg, cm) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 366 | |
| 367 | if err = sOLT.Start(ctx); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 368 | return nil, err |
| 369 | } |
| 370 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 371 | logger.Info(ctx, "open-olt-started") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 372 | return sOLT, nil |
| 373 | } |
| 374 | |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 375 | func (a *adapter) startOpenOLTInterAdapter(ctx context.Context, oo *ac.OpenOLT) (*ac.OpenOLTInterAdapter, error) { |
| 376 | logger.Info(ctx, "starting-open-olt-inter-adapter") |
| 377 | var err error |
| 378 | sOLTInterAdapter := ac.NewOpenOLTInterAdapter(oo) |
| 379 | |
| 380 | if err = sOLTInterAdapter.Start(ctx); err != nil { |
| 381 | return nil, err |
| 382 | } |
| 383 | |
| 384 | logger.Info(ctx, "open-olt-inter-adapter-started") |
| 385 | return sOLTInterAdapter, nil |
| 386 | } |
| 387 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 388 | func (a *adapter) registerWithCore(ctx context.Context, serviceName string, retries int) error { |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 389 | adapterID := fmt.Sprintf("openolt_%d", a.config.CurrentReplica) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 390 | logger.Infow(ctx, "registering-with-core", log.Fields{ |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 391 | "adapterID": adapterID, |
| 392 | "currentReplica": a.config.CurrentReplica, |
| 393 | "totalReplicas": a.config.TotalReplicas, |
| 394 | }) |
| 395 | adapterDescription := &voltha.Adapter{ |
| 396 | Id: adapterID, // Unique name for the device type |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 397 | Vendor: "VOLTHA OpenOLT", |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 398 | Version: version.VersionInfo.Version, |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 399 | // The Endpoint refers to the address this service is listening on. |
| 400 | Endpoint: a.config.AdapterEndpoint, |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 401 | Type: "openolt", |
| 402 | CurrentReplica: int32(a.config.CurrentReplica), |
| 403 | TotalReplicas: int32(a.config.TotalReplicas), |
| 404 | } |
| 405 | types := []*voltha.DeviceType{{ |
| 406 | Id: "openolt", |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 407 | AdapterType: "openolt", // Type of the adapter that handles device type |
| 408 | Adapter: "openolt", // Deprecated attribute |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 409 | AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling |
| 410 | AcceptsAddRemoveFlowUpdates: true}} |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 411 | deviceTypes := &voltha.DeviceTypes{Items: types} |
| 412 | count := 0 |
| 413 | for { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 414 | gClient, err := a.coreClient.GetCoreServiceClient() |
| 415 | if gClient != nil { |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 416 | if _, err = gClient.RegisterAdapter(log.WithSpanFromContext(context.TODO(), ctx), &ca.AdapterRegistration{ |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 417 | Adapter: adapterDescription, |
| 418 | DTypes: deviceTypes}); err == nil { |
| 419 | break |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 420 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 421 | } |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 422 | logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"endpoint": a.config.CoreEndpoint, "error": err, "count": count, "gclient": gClient}) |
| 423 | if retries == count { |
| 424 | return err |
| 425 | } |
| 426 | count++ |
| 427 | // Take a nap before retrying |
| 428 | time.Sleep(2 * time.Second) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 429 | } |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 430 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 431 | logger.Info(ctx, "registered-with-core") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 432 | return nil |
| 433 | } |
| 434 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 435 | func waitForExit(ctx context.Context) int { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 436 | signalChannel := make(chan os.Signal, 1) |
| 437 | signal.Notify(signalChannel, |
| 438 | syscall.SIGHUP, |
| 439 | syscall.SIGINT, |
| 440 | syscall.SIGTERM, |
| 441 | syscall.SIGQUIT) |
| 442 | |
| 443 | exitChannel := make(chan int) |
| 444 | |
| 445 | go func() { |
| 446 | s := <-signalChannel |
| 447 | switch s { |
| 448 | case syscall.SIGHUP, |
| 449 | syscall.SIGINT, |
| 450 | syscall.SIGTERM, |
| 451 | syscall.SIGQUIT: |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 452 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 453 | exitChannel <- 0 |
| 454 | default: |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 455 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 456 | exitChannel <- 1 |
| 457 | } |
| 458 | }() |
| 459 | |
| 460 | code := <-exitChannel |
| 461 | return code |
| 462 | } |
| 463 | |
| 464 | func printBanner() { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 465 | fmt.Println(` ____ ____ _ _______ `) |
| 466 | fmt.Println(` / _ \ / __ \| | |__ __|`) |
| 467 | fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `) |
| 468 | fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `) |
| 469 | fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `) |
| 470 | fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `) |
| 471 | fmt.Println(` | | `) |
| 472 | fmt.Println(` |_| `) |
| 473 | fmt.Println(` `) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 476 | func printVersion() { |
| 477 | fmt.Println("VOLTHA OpenOLT Adapter") |
| 478 | fmt.Println(version.VersionInfo.String(" ")) |
| 479 | } |
| 480 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 481 | func main() { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 482 | ctx := context.Background() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 483 | start := time.Now() |
| 484 | |
| 485 | cf := config.NewAdapterFlags() |
| 486 | cf.ParseCommandArguments() |
| 487 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 488 | // Setup logging |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 489 | |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 490 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
| 491 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 492 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 493 | } |
Rohan Agrawal | 2488f19 | 2020-01-31 09:26:55 +0000 | [diff] [blame] | 494 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 495 | // Setup default logger - applies for packages that do not have specific logger set |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 496 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil { |
Girish Kumar | a1ea2aa | 2020-08-19 18:14:22 +0000 | [diff] [blame] | 497 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // Update all loggers (provisionned via init) with a common field |
Hardik Windlass | b9c869b | 2019-10-10 08:34:32 +0000 | [diff] [blame] | 501 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil { |
Girish Kumar | a1ea2aa | 2020-08-19 18:14:22 +0000 | [diff] [blame] | 502 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 505 | log.SetAllLogLevel(logLevel) |
Rohan Agrawal | 93bced3 | 2020-02-11 10:16:01 +0000 | [diff] [blame] | 506 | |
Matteo Scandolo | 8f2b957 | 2020-02-28 15:35:23 -0800 | [diff] [blame] | 507 | realMain() |
| 508 | |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 509 | defer func() { |
| 510 | err := log.CleanUp() |
| 511 | if err != nil { |
| 512 | logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
| 513 | } |
| 514 | }() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 515 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 516 | // Print version / build information and exit |
| 517 | if cf.DisplayVersionOnly { |
| 518 | printVersion() |
| 519 | return |
| 520 | } |
| 521 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 522 | // Print banner if specified |
| 523 | if cf.Banner { |
| 524 | printBanner() |
| 525 | } |
| 526 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 527 | logger.Infow(ctx, "config", log.Fields{"config": *cf}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 528 | |
| 529 | ctx, cancel := context.WithCancel(context.Background()) |
| 530 | defer cancel() |
| 531 | |
| 532 | ad := newAdapter(cf) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 533 | |
| 534 | p := &probe.Probe{} |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 535 | go p.ListenAndServe(ctx, ad.config.ProbeAddress) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 536 | |
| 537 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 538 | |
Girish Kumar | 935f7af | 2020-08-18 11:59:42 +0000 | [diff] [blame] | 539 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled) |
Girish Kumar | 11e1597 | 2020-06-15 14:51:10 +0000 | [diff] [blame] | 540 | if err != nil { |
| 541 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 542 | } else { |
| 543 | defer log.TerminateTracing(closer) |
| 544 | } |
| 545 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 546 | go ad.start(probeCtx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 547 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 548 | code := waitForExit(ctx) |
| 549 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 550 | |
Girish Gowdra | 4b48fa4 | 2022-06-01 18:10:08 -0700 | [diff] [blame] | 551 | // Use context with cancel as etcd-client stop could take more time sometimes to stop slowing down container shutdown. |
| 552 | ctxWithCancel, cancelFunc := context.WithCancel(ctx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 553 | // Cleanup before leaving |
Girish Gowdra | 4b48fa4 | 2022-06-01 18:10:08 -0700 | [diff] [blame] | 554 | ad.stop(ctxWithCancel) |
| 555 | // Will halt any long-running stop routine gracefully |
| 556 | cancelFunc() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 557 | |
| 558 | elapsed := time.Since(start) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 559 | logger.Infow(ctx, "run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 560 | } |