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