cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1 | /* |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
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" |
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 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 29 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 30 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 31 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters" |
| 32 | com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common" |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 33 | conf "github.com/opencord/voltha-lib-go/v3/pkg/config" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 34 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 35 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 36 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 37 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 38 | "github.com/opencord/voltha-lib-go/v3/pkg/version" |
| 39 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/config" |
| 40 | ac "github.com/opencord/voltha-openolt-adapter/internal/pkg/core" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 41 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 42 | "github.com/opencord/voltha-protos/v3/go/voltha" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | type adapter struct { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 46 | instanceID string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 47 | config *config.AdapterFlags |
| 48 | iAdapter adapters.IAdapter |
| 49 | kafkaClient kafka.Client |
| 50 | kvClient kvstore.Client |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 51 | kip kafka.InterContainerProxy |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 52 | coreProxy adapterif.CoreProxy |
| 53 | adapterProxy adapterif.AdapterProxy |
| 54 | eventProxy adapterif.EventProxy |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 55 | halted bool |
| 56 | exitChannel chan int |
| 57 | receiverChannels []<-chan *ic.InterContainerMessage |
| 58 | } |
| 59 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 60 | func newAdapter(cf *config.AdapterFlags) *adapter { |
| 61 | var a adapter |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 62 | a.instanceID = cf.InstanceID |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 63 | a.config = cf |
| 64 | a.halted = false |
| 65 | a.exitChannel = make(chan int, 1) |
| 66 | a.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0) |
| 67 | return &a |
| 68 | } |
| 69 | |
| 70 | func (a *adapter) start(ctx context.Context) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 71 | logger.Info(ctx, "Starting Core Adapter components") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 72 | var err error |
| 73 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 74 | var p *probe.Probe |
| 75 | if value := ctx.Value(probe.ProbeContextKey); value != nil { |
| 76 | if _, ok := value.(*probe.Probe); ok { |
| 77 | p = value.(*probe.Probe) |
| 78 | p.RegisterService( |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 79 | ctx, |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 80 | "message-bus", |
| 81 | "kv-store", |
| 82 | "container-proxy", |
| 83 | "core-request-handler", |
| 84 | "register-with-core", |
| 85 | ) |
| 86 | } |
| 87 | } |
| 88 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 89 | // Setup KV Client |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 90 | logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": a.config.KVStoreType}) |
| 91 | if err = a.setKVClient(ctx); err != nil { |
| 92 | logger.Fatalw(ctx, "error-setting-kv-client", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 95 | if p != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 96 | p.UpdateStatus(ctx, "kv-store", probe.ServiceStatusRunning) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 97 | } |
| 98 | |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 99 | // Setup Log Config |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 100 | cm := conf.NewConfigManager(ctx, a.kvClient, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
divyadesai | d26f6b1 | 2020-03-19 06:30:28 +0000 | [diff] [blame] | 101 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 102 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 103 | // Setup Kafka Client |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 104 | if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaAdapterAddress); err != nil { |
| 105 | logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 108 | if p != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 109 | p.UpdateStatus(ctx, "message-bus", probe.ServiceStatusRunning) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 112 | // setup endpointManager |
| 113 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 114 | // Start the common InterContainer Proxy - retries indefinitely |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 115 | if a.kip, err = a.startInterContainerProxy(ctx, -1); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 116 | logger.Fatal(ctx, "error-starting-inter-container-proxy") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Create the core proxy to handle requests to the Core |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 120 | a.coreProxy = com.NewCoreProxy(ctx, a.kip, a.config.Topic, a.config.CoreTopic) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 121 | |
| 122 | // Create the adaptor proxy to handle request between olt and onu |
Matteo Scandolo | 4665468 | 2020-08-05 11:46:37 -0700 | [diff] [blame] | 123 | a.adapterProxy = com.NewAdapterProxy(ctx, a.kip, a.config.CoreTopic, cm.Backend) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 124 | |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 125 | // Create the event proxy to post events to KAFKA |
| 126 | a.eventProxy = com.NewEventProxy(com.MsgClient(a.kafkaClient), com.MsgTopic(kafka.Topic{Name: a.config.EventTopic})) |
| 127 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 128 | // Create the open OLT adapter |
Girish Kumar | f26e488 | 2020-03-05 06:49:10 +0000 | [diff] [blame] | 129 | if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy, a.config); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 130 | logger.Fatalw(ctx, "error-starting-openolt", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Register the core request handler |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 134 | if err = a.setupRequestHandler(ctx, a.instanceID, a.iAdapter); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 135 | logger.Fatalw(ctx, "error-setting-core-request-handler", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 138 | // Register this adapter to the Core - retries indefinitely |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 139 | if err = a.registerWithCore(ctx, -1); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 140 | logger.Fatal(ctx, "error-registering-with-core") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 141 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 142 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 143 | // check the readiness and liveliness and update the probe status |
| 144 | a.checkServicesReadiness(ctx) |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | This function checks the liveliness and readiness of the kakfa and kv-client services |
| 149 | and update the status in the probe. |
| 150 | */ |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 151 | func (a *adapter) checkServicesReadiness(ctx context.Context) { |
| 152 | // checks the kafka readiness |
| 153 | go a.checkKafkaReadiness(ctx) |
| 154 | |
| 155 | // checks the kv-store readiness |
| 156 | go a.checkKvStoreReadiness(ctx) |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | This function checks the liveliness and readiness of the kv-store service |
| 161 | and update the status in the probe. |
| 162 | */ |
| 163 | func (a *adapter) checkKvStoreReadiness(ctx context.Context) { |
| 164 | // dividing the live probe interval by 2 to get updated status every 30s |
| 165 | timeout := a.config.LiveProbeInterval / 2 |
| 166 | kvStoreChannel := make(chan bool, 1) |
| 167 | |
| 168 | // Default false to check the liveliness. |
| 169 | kvStoreChannel <- false |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 170 | for { |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 171 | timeoutTimer := time.NewTimer(timeout) |
| 172 | select { |
| 173 | case liveliness := <-kvStoreChannel: |
| 174 | if !liveliness { |
| 175 | // kv-store not reachable or down, updating the status to not ready state |
| 176 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady) |
| 177 | timeout = a.config.NotLiveProbeInterval |
| 178 | } else { |
| 179 | // kv-store is reachable , updating the status to running state |
| 180 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning) |
| 181 | timeout = a.config.LiveProbeInterval / 2 |
| 182 | } |
| 183 | // Check if the timer has expired or not |
| 184 | if !timeoutTimer.Stop() { |
| 185 | <-timeoutTimer.C |
| 186 | } |
| 187 | case <-timeoutTimer.C: |
Girish Kumar | beadc11 | 2020-02-26 18:41:02 +0000 | [diff] [blame] | 188 | // 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] | 189 | logger.Info(ctx, "kv-store liveliness-recheck") |
Girish Kumar | beadc11 | 2020-02-26 18:41:02 +0000 | [diff] [blame] | 190 | timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second) |
| 191 | |
| 192 | kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx) |
| 193 | // Cleanup cancel func resources |
| 194 | cancelFunc() |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 195 | } |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | This function checks the liveliness and readiness of the kafka service |
| 201 | and update the status in the probe. |
| 202 | */ |
| 203 | func (a *adapter) checkKafkaReadiness(ctx context.Context) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 204 | livelinessChannel := a.kafkaClient.EnableLivenessChannel(ctx, true) |
| 205 | healthinessChannel := a.kafkaClient.EnableHealthinessChannel(ctx, true) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 206 | timeout := a.config.LiveProbeInterval |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 207 | failed := false |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 208 | for { |
| 209 | timeoutTimer := time.NewTimer(timeout) |
| 210 | |
| 211 | select { |
Scott Baker | 86fce9a | 2019-12-12 09:47:17 -0800 | [diff] [blame] | 212 | case healthiness := <-healthinessChannel: |
| 213 | if !healthiness { |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 214 | // This will eventually cause K8s to restart the container, and will do |
| 215 | // so in a way that allows cleanup to continue, rather than an immediate |
| 216 | // panic and exit here. |
| 217 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusFailed) |
| 218 | failed = true |
| 219 | } |
| 220 | // Check if the timer has expired or not |
| 221 | if !timeoutTimer.Stop() { |
| 222 | <-timeoutTimer.C |
Scott Baker | 86fce9a | 2019-12-12 09:47:17 -0800 | [diff] [blame] | 223 | } |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 224 | case liveliness := <-livelinessChannel: |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 225 | if failed { |
| 226 | // Failures of the message bus are permanent and can't ever be recovered from, |
| 227 | // so make sure we never inadvertently reset a failed state back to unready. |
| 228 | } else if !liveliness { |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 229 | // kafka not reachable or down, updating the status to not ready state |
| 230 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady) |
| 231 | timeout = a.config.NotLiveProbeInterval |
| 232 | } else { |
| 233 | // kafka is reachable , updating the status to running state |
| 234 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning) |
| 235 | timeout = a.config.LiveProbeInterval |
| 236 | } |
| 237 | // Check if the timer has expired or not |
| 238 | if !timeoutTimer.Stop() { |
| 239 | <-timeoutTimer.C |
| 240 | } |
| 241 | case <-timeoutTimer.C: |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 242 | logger.Info(ctx, "kafka-proxy-liveness-recheck") |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 243 | // send the liveness probe in a goroutine; we don't want to deadlock ourselves as |
| 244 | // the liveness probe may wait (and block) writing to our channel. |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 245 | err := a.kafkaClient.SendLiveness(ctx) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 246 | if err != nil { |
| 247 | // Catch possible error case if sending liveness after Sarama has been stopped. |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 248 | logger.Warnw(ctx, "error-kafka-send-liveness", log.Fields{"error": err}) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 249 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 250 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 251 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 252 | } |
| 253 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 254 | func (a *adapter) stop(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 255 | // Stop leadership tracking |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 256 | a.halted = true |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 257 | |
| 258 | // send exit signal |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 259 | a.exitChannel <- 0 |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 260 | |
| 261 | // Cleanup - applies only if we had a kvClient |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 262 | if a.kvClient != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 263 | // Release all reservations |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 264 | if err := a.kvClient.ReleaseAllReservations(ctx); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 265 | logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 266 | } |
| 267 | // Close the DB connection |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 268 | a.kvClient.Close(ctx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 271 | if a.kip != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 272 | a.kip.Stop(ctx) |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 273 | } |
| 274 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 275 | // TODO: More cleanup |
| 276 | } |
| 277 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 278 | 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] | 279 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 280 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 281 | switch storeType { |
| 282 | case "consul": |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 283 | return kvstore.NewConsulClient(ctx, address, timeout) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 284 | case "etcd": |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 285 | return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 286 | } |
| 287 | return nil, errors.New("unsupported-kv-store") |
| 288 | } |
| 289 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 290 | func newKafkaClient(ctx context.Context, clientType, address string) (kafka.Client, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 291 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 292 | logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 293 | switch clientType { |
| 294 | case "sarama": |
| 295 | return kafka.NewSaramaClient( |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 296 | kafka.Address(address), |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 297 | kafka.ProducerReturnOnErrors(true), |
| 298 | kafka.ProducerReturnOnSuccess(true), |
| 299 | kafka.ProducerMaxRetries(6), |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 300 | kafka.ProducerRetryBackoff(time.Millisecond*30), |
| 301 | kafka.MetadatMaxRetries(15)), nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 302 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 303 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 304 | return nil, errors.New("unsupported-client-type") |
| 305 | } |
| 306 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 307 | func (a *adapter) setKVClient(ctx context.Context) error { |
| 308 | client, err := newKVClient(ctx, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 309 | if err != nil { |
| 310 | a.kvClient = nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 311 | return err |
| 312 | } |
| 313 | a.kvClient = client |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 314 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 315 | return nil |
| 316 | } |
| 317 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 318 | func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (kafka.InterContainerProxy, error) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 319 | logger.Infow(ctx, "starting-intercontainer-messaging-proxy", log.Fields{"address": a.config.KafkaAdapterAddress, |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 320 | "topic": a.config.Topic}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 321 | var err error |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 322 | kip := kafka.NewInterContainerProxy( |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 323 | kafka.InterContainerAddress(a.config.KafkaAdapterAddress), |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 324 | kafka.MsgClient(a.kafkaClient), |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 325 | kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic})) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 326 | count := 0 |
| 327 | for { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 328 | if err = kip.Start(ctx); err != nil { |
| 329 | logger.Warnw(ctx, "error-starting-messaging-proxy", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 330 | if retries == count { |
| 331 | return nil, err |
| 332 | } |
| 333 | count = +1 |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 334 | // Take a nap before retrying |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 335 | time.Sleep(2 * time.Second) |
| 336 | } else { |
| 337 | break |
| 338 | } |
| 339 | } |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 340 | probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 341 | logger.Info(ctx, "common-messaging-proxy-created") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 342 | return kip, nil |
| 343 | } |
| 344 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 345 | func (a *adapter) startOpenOLT(ctx context.Context, kip kafka.InterContainerProxy, |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 346 | cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy, |
| 347 | cfg *config.AdapterFlags) (*ac.OpenOLT, error) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 348 | logger.Info(ctx, "starting-open-olt") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 349 | var err error |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 350 | sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, cfg) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 351 | |
| 352 | if err = sOLT.Start(ctx); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 353 | return nil, err |
| 354 | } |
| 355 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 356 | logger.Info(ctx, "open-olt-started") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 357 | return sOLT, nil |
| 358 | } |
| 359 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 360 | func (a *adapter) setupRequestHandler(ctx context.Context, coreInstanceID string, iadapter adapters.IAdapter) error { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 361 | logger.Info(ctx, "setting-request-handler") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 362 | requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 363 | if err := a.kip.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 364 | return err |
| 365 | |
| 366 | } |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 367 | probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 368 | logger.Info(ctx, "request-handler-setup-done") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 369 | return nil |
| 370 | } |
| 371 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 372 | func (a *adapter) registerWithCore(ctx context.Context, retries int) error { |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 373 | adapterID := fmt.Sprintf("openolt_%d", a.config.CurrentReplica) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 374 | logger.Infow(ctx, "registering-with-core", log.Fields{ |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 375 | "adapterID": adapterID, |
| 376 | "currentReplica": a.config.CurrentReplica, |
| 377 | "totalReplicas": a.config.TotalReplicas, |
| 378 | }) |
| 379 | adapterDescription := &voltha.Adapter{ |
| 380 | Id: adapterID, // Unique name for the device type |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 381 | Vendor: "VOLTHA OpenOLT", |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 382 | Version: version.VersionInfo.Version, |
| 383 | // TODO once we'll be ready to support multiple versions of the OpenOLT adapter |
| 384 | // the Endpoint will have to change to `openolt_<currentReplica`> |
serkant.uluderya | 5e3528d | 2020-05-22 19:31:07 -0700 | [diff] [blame] | 385 | Endpoint: a.config.Topic, |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 386 | Type: "openolt", |
| 387 | CurrentReplica: int32(a.config.CurrentReplica), |
| 388 | TotalReplicas: int32(a.config.TotalReplicas), |
| 389 | } |
| 390 | types := []*voltha.DeviceType{{ |
| 391 | Id: "openolt", |
| 392 | Adapter: "openolt", // Type of the adapter that handles device type |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 393 | AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling |
| 394 | AcceptsAddRemoveFlowUpdates: true}} |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 395 | deviceTypes := &voltha.DeviceTypes{Items: types} |
| 396 | count := 0 |
| 397 | for { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 398 | if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 399 | logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 400 | if retries == count { |
| 401 | return err |
| 402 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 403 | count++ |
| 404 | // Take a nap before retrying |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 405 | time.Sleep(2 * time.Second) |
| 406 | } else { |
| 407 | break |
| 408 | } |
| 409 | } |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 410 | probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 411 | logger.Info(ctx, "registered-with-core") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 412 | return nil |
| 413 | } |
| 414 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 415 | func waitForExit(ctx context.Context) int { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 416 | signalChannel := make(chan os.Signal, 1) |
| 417 | signal.Notify(signalChannel, |
| 418 | syscall.SIGHUP, |
| 419 | syscall.SIGINT, |
| 420 | syscall.SIGTERM, |
| 421 | syscall.SIGQUIT) |
| 422 | |
| 423 | exitChannel := make(chan int) |
| 424 | |
| 425 | go func() { |
| 426 | s := <-signalChannel |
| 427 | switch s { |
| 428 | case syscall.SIGHUP, |
| 429 | syscall.SIGINT, |
| 430 | syscall.SIGTERM, |
| 431 | syscall.SIGQUIT: |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 432 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 433 | exitChannel <- 0 |
| 434 | default: |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 435 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 436 | exitChannel <- 1 |
| 437 | } |
| 438 | }() |
| 439 | |
| 440 | code := <-exitChannel |
| 441 | return code |
| 442 | } |
| 443 | |
| 444 | func printBanner() { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 445 | fmt.Println(` ____ ____ _ _______ `) |
| 446 | fmt.Println(` / _ \ / __ \| | |__ __|`) |
| 447 | fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `) |
| 448 | fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `) |
| 449 | fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `) |
| 450 | fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `) |
| 451 | fmt.Println(` | | `) |
| 452 | fmt.Println(` |_| `) |
| 453 | fmt.Println(` `) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 456 | func printVersion() { |
| 457 | fmt.Println("VOLTHA OpenOLT Adapter") |
| 458 | fmt.Println(version.VersionInfo.String(" ")) |
| 459 | } |
| 460 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 461 | func main() { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 462 | ctx := context.Background() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 463 | start := time.Now() |
| 464 | |
| 465 | cf := config.NewAdapterFlags() |
| 466 | cf.ParseCommandArguments() |
| 467 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 468 | // Setup logging |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 469 | |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 470 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
| 471 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 472 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 473 | } |
Rohan Agrawal | 2488f19 | 2020-01-31 09:26:55 +0000 | [diff] [blame] | 474 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 475 | // 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] | 476 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 477 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 478 | } |
| 479 | |
| 480 | // Update all loggers (provisionned via init) with a common field |
Hardik Windlass | b9c869b | 2019-10-10 08:34:32 +0000 | [diff] [blame] | 481 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 482 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 483 | } |
| 484 | |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 485 | log.SetAllLogLevel(logLevel) |
Rohan Agrawal | 93bced3 | 2020-02-11 10:16:01 +0000 | [diff] [blame] | 486 | |
Matteo Scandolo | 8f2b957 | 2020-02-28 15:35:23 -0800 | [diff] [blame] | 487 | realMain() |
| 488 | |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 489 | defer func() { |
| 490 | err := log.CleanUp() |
| 491 | if err != nil { |
| 492 | logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
| 493 | } |
| 494 | }() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 495 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 496 | // Print version / build information and exit |
| 497 | if cf.DisplayVersionOnly { |
| 498 | printVersion() |
| 499 | return |
| 500 | } |
| 501 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 502 | // Print banner if specified |
| 503 | if cf.Banner { |
| 504 | printBanner() |
| 505 | } |
| 506 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 507 | logger.Infow(ctx, "config", log.Fields{"config": *cf}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 508 | |
| 509 | ctx, cancel := context.WithCancel(context.Background()) |
| 510 | defer cancel() |
| 511 | |
| 512 | ad := newAdapter(cf) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 513 | |
| 514 | p := &probe.Probe{} |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 515 | go p.ListenAndServe(ctx, ad.config.ProbeAddress) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 516 | |
| 517 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 518 | |
Girish Kumar | 11e1597 | 2020-06-15 14:51:10 +0000 | [diff] [blame] | 519 | closer, err := log.InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled) |
| 520 | if err != nil { |
| 521 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 522 | } else { |
| 523 | defer log.TerminateTracing(closer) |
| 524 | } |
| 525 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 526 | go ad.start(probeCtx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 527 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 528 | code := waitForExit(ctx) |
| 529 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 530 | |
| 531 | // Cleanup before leaving |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 532 | ad.stop(ctx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 533 | |
| 534 | elapsed := time.Since(start) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 535 | 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] | 536 | } |