khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 17 | package main |
| 18 | |
| 19 | import ( |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 20 | "context" |
| 21 | "errors" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 22 | "fmt" |
| 23 | "os" |
| 24 | "os/signal" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 25 | "strconv" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 26 | "syscall" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 27 | "time" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 28 | |
| 29 | "github.com/opencord/voltha-go/rw_core/config" |
| 30 | c "github.com/opencord/voltha-go/rw_core/core" |
| 31 | "github.com/opencord/voltha-go/rw_core/utils" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 32 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 33 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 34 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 35 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
| 36 | "github.com/opencord/voltha-lib-go/v3/pkg/version" |
| 37 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 38 | ) |
| 39 | |
| 40 | type rwCore struct { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 41 | kvClient kvstore.Client |
| 42 | config *config.RWCoreFlags |
| 43 | halted bool |
| 44 | exitChannel chan int |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 45 | //kmp *kafka.KafkaMessagingProxy |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 46 | kafkaClient kafka.Client |
| 47 | core *c.Core |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 48 | //For test |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 49 | receiverChannels []<-chan *ic.InterContainerMessage |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 50 | } |
| 51 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 52 | func init() { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 53 | _, err := log.AddPackage(log.JSON, log.DebugLevel, nil) |
| 54 | if err != nil { |
| 55 | log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err}) |
| 56 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 57 | } |
| 58 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 59 | func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) { |
| 60 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 61 | log.Infow("kv-store-type", log.Fields{"store": storeType}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 62 | switch storeType { |
| 63 | case "consul": |
| 64 | return kvstore.NewConsulClient(address, timeout) |
| 65 | case "etcd": |
| 66 | return kvstore.NewEtcdClient(address, timeout) |
| 67 | } |
| 68 | return nil, errors.New("unsupported-kv-store") |
| 69 | } |
| 70 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 71 | func newKafkaClient(clientType string, host string, port int, instanceID string, livenessChannelInterval time.Duration) (kafka.Client, error) { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 72 | |
| 73 | log.Infow("kafka-client-type", log.Fields{"client": clientType}) |
| 74 | switch clientType { |
| 75 | case "sarama": |
| 76 | return kafka.NewSaramaClient( |
| 77 | kafka.Host(host), |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 78 | kafka.Port(port), |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 79 | kafka.ConsumerType(kafka.GroupCustomer), |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 80 | kafka.ProducerReturnOnErrors(true), |
| 81 | kafka.ProducerReturnOnSuccess(true), |
| 82 | kafka.ProducerMaxRetries(6), |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 83 | kafka.NumPartitions(3), |
| 84 | kafka.ConsumerGroupName(instanceID), |
| 85 | kafka.ConsumerGroupPrefix(instanceID), |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 86 | kafka.AutoCreateTopic(true), |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 87 | kafka.ProducerFlushFrequency(5), |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 88 | kafka.ProducerRetryBackoff(time.Millisecond*30), |
| 89 | kafka.LivenessChannelInterval(livenessChannelInterval), |
| 90 | ), nil |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 91 | } |
| 92 | return nil, errors.New("unsupported-client-type") |
| 93 | } |
| 94 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 95 | func newRWCore(cf *config.RWCoreFlags) *rwCore { |
| 96 | var rwCore rwCore |
| 97 | rwCore.config = cf |
| 98 | rwCore.halted = false |
| 99 | rwCore.exitChannel = make(chan int, 1) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 100 | rwCore.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 101 | return &rwCore |
| 102 | } |
| 103 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 104 | func (rw *rwCore) start(ctx context.Context, instanceID string) { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 105 | log.Info("Starting RW Core components") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 106 | |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 107 | // Setup KV Client |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 108 | log.Debugw("create-kv-client", log.Fields{"kvstore": rw.config.KVStoreType}) |
Kent Hagerman | 16ce36a | 2019-12-17 13:40:53 -0500 | [diff] [blame] | 109 | var err error |
| 110 | if rw.kvClient, err = newKVClient( |
| 111 | rw.config.KVStoreType, |
| 112 | rw.config.KVStoreHost+":"+strconv.Itoa(rw.config.KVStorePort), |
| 113 | rw.config.KVStoreTimeout); err != nil { |
| 114 | log.Fatal(err) |
| 115 | } |
| 116 | |
| 117 | // Setup KV transaction context |
| 118 | if err := c.SetTransactionContext(instanceID, |
| 119 | rw.config.KVStoreDataPrefix+"/transactions/", |
| 120 | rw.kvClient, |
| 121 | rw.config.KVStoreTimeout); err != nil { |
| 122 | log.Fatal("creating-transaction-context-failed") |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 123 | } |
| 124 | |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 125 | // Setup Kafka Client |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 126 | if rw.kafkaClient, err = newKafkaClient("sarama", |
| 127 | rw.config.KafkaAdapterHost, |
| 128 | rw.config.KafkaAdapterPort, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 129 | instanceID, |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 130 | rw.config.LiveProbeInterval/2); err != nil { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 131 | log.Fatal("Unsupported-kafka-client") |
| 132 | } |
| 133 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 134 | // Create the core service |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 135 | rw.core = c.NewCore(ctx, instanceID, rw.config, rw.kvClient, rw.kafkaClient) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 136 | |
| 137 | // start the core |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 138 | err = rw.core.Start(ctx) |
| 139 | if err != nil { |
| 140 | log.Fatalf("failed-to-start-rwcore", log.Fields{"error": err}) |
| 141 | } |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 142 | } |
| 143 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 144 | func (rw *rwCore) stop(ctx context.Context) { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 145 | // Stop leadership tracking |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 146 | rw.halted = true |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 147 | |
| 148 | // send exit signal |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 149 | rw.exitChannel <- 0 |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 150 | |
| 151 | // Cleanup - applies only if we had a kvClient |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 152 | if rw.kvClient != nil { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 153 | // Release all reservations |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 154 | if err := rw.kvClient.ReleaseAllReservations(ctx); err != nil { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 155 | log.Infow("fail-to-release-all-reservations", log.Fields{"error": err}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 156 | } |
| 157 | // Close the DB connection |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 158 | rw.kvClient.Close() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 159 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 160 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 161 | rw.core.Stop(ctx) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 162 | |
| 163 | //if rw.kafkaClient != nil { |
| 164 | // rw.kafkaClient.Stop() |
| 165 | //} |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | func waitForExit() int { |
| 169 | signalChannel := make(chan os.Signal, 1) |
| 170 | signal.Notify(signalChannel, |
| 171 | syscall.SIGHUP, |
| 172 | syscall.SIGINT, |
| 173 | syscall.SIGTERM, |
| 174 | syscall.SIGQUIT) |
| 175 | |
| 176 | exitChannel := make(chan int) |
| 177 | |
| 178 | go func() { |
| 179 | s := <-signalChannel |
| 180 | switch s { |
| 181 | case syscall.SIGHUP, |
| 182 | syscall.SIGINT, |
| 183 | syscall.SIGTERM, |
| 184 | syscall.SIGQUIT: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 185 | log.Infow("closing-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 186 | exitChannel <- 0 |
| 187 | default: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 188 | log.Infow("unexpected-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 189 | exitChannel <- 1 |
| 190 | } |
| 191 | }() |
| 192 | |
| 193 | code := <-exitChannel |
| 194 | return code |
| 195 | } |
| 196 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 197 | func printBanner() { |
| 198 | fmt.Println(" ") |
| 199 | fmt.Println(" ______ ______ ") |
| 200 | fmt.Println("| _ \\ \\ / / ___|___ _ __ ___ ") |
| 201 | fmt.Println("| |_) \\ \\ /\\ / / | / _ \\| '__/ _ \\ ") |
| 202 | fmt.Println("| _ < \\ V V /| |__| (_) | | | __/ ") |
| 203 | fmt.Println("|_| \\_\\ \\_/\\_/ \\____\\___/|_| \\___| ") |
| 204 | fmt.Println(" ") |
| 205 | } |
| 206 | |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 207 | func printVersion() { |
| 208 | fmt.Println("VOLTHA Read-Write Core") |
| 209 | fmt.Println(version.VersionInfo.String(" ")) |
| 210 | } |
| 211 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 212 | func main() { |
| 213 | start := time.Now() |
| 214 | |
| 215 | cf := config.NewRWCoreFlags() |
| 216 | cf.ParseCommandArguments() |
| 217 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 218 | // Set the instance ID as the hostname |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 219 | var instanceID string |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 220 | hostName := utils.GetHostName() |
| 221 | if len(hostName) > 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 222 | instanceID = hostName |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 223 | } else { |
| 224 | log.Fatal("HOSTNAME not set") |
| 225 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 226 | |
| 227 | //Setup default logger - applies for packages that do not have specific logger set |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 228 | if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": instanceID}); err != nil { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 229 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 230 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 231 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 232 | // Update all loggers (provisioned via init) with a common field |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 233 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceID}); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 234 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 235 | } |
| 236 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 237 | // Update all loggers to log level specified as input parameter |
| 238 | log.SetAllLogLevel(cf.LogLevel) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 239 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 240 | //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 241 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 242 | defer func() { |
| 243 | err := log.CleanUp() |
| 244 | if err != nil { |
| 245 | log.Errorw("unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
| 246 | } |
| 247 | }() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 248 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 249 | // Print version / build information and exit |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 250 | if cf.DisplayVersionOnly { |
| 251 | printVersion() |
| 252 | return |
| 253 | } |
| 254 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 255 | // Print banner if specified |
| 256 | if cf.Banner { |
| 257 | printBanner() |
| 258 | } |
| 259 | |
| 260 | log.Infow("rw-core-config", log.Fields{"config": *cf}) |
| 261 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 262 | // Create the core |
| 263 | rw := newRWCore(cf) |
| 264 | |
| 265 | // Create a context adding the status update channel |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 266 | ctx, cancel := context.WithCancel(context.Background()) |
| 267 | defer cancel() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 268 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 269 | /* |
| 270 | * Create and start the liveness and readiness container management probes. This |
| 271 | * is done in the main function so just in case the main starts multiple other |
| 272 | * objects there can be a single probe end point for the process. |
| 273 | */ |
| 274 | p := &probe.Probe{} |
Kent Hagerman | c461883 | 2019-10-07 12:24:36 -0400 | [diff] [blame] | 275 | go p.ListenAndServe(fmt.Sprintf("%s:%d", rw.config.ProbeHost, rw.config.ProbePort)) |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 276 | |
| 277 | // Add the probe to the context to pass to all the services started |
| 278 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 279 | |
| 280 | // Start the core |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 281 | go rw.start(probeCtx, instanceID) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 282 | |
| 283 | code := waitForExit() |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 284 | log.Infow("received-a-closing-signal", log.Fields{"code": code}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 285 | |
| 286 | // Cleanup before leaving |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 287 | rw.stop(probeCtx) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 288 | |
| 289 | elapsed := time.Since(start) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 290 | log.Infow("rw-core-run-time", log.Fields{"core": instanceID, "time": elapsed / time.Second}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 291 | } |