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 | */ |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 16 | package main |
| 17 | |
| 18 | import ( |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 19 | "context" |
| 20 | "errors" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 21 | "fmt" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 22 | grpcserver "github.com/opencord/voltha-go/common/grpc" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/common/log" |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/common/version" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/db/kvstore" |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/kafka" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 27 | "github.com/opencord/voltha-go/rw_core/config" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 28 | c "github.com/opencord/voltha-go/rw_core/core" |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 29 | "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 30 | ic "github.com/opencord/voltha-protos/go/inter_container" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 31 | "os" |
| 32 | "os/signal" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 33 | "strconv" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 34 | "syscall" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 35 | "time" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | type rwCore struct { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 39 | kvClient kvstore.Client |
| 40 | config *config.RWCoreFlags |
| 41 | halted bool |
| 42 | exitChannel chan int |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 43 | //kmp *kafka.KafkaMessagingProxy |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 44 | grpcServer *grpcserver.GrpcServer |
| 45 | kafkaClient kafka.Client |
| 46 | core *c.Core |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 47 | //For test |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 48 | receiverChannels []<-chan *ic.InterContainerMessage |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 49 | } |
| 50 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 51 | func init() { |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 52 | log.AddPackage(log.JSON, log.DebugLevel, nil) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 53 | } |
| 54 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 55 | func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) { |
| 56 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 57 | log.Infow("kv-store-type", log.Fields{"store": storeType}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 58 | switch storeType { |
| 59 | case "consul": |
| 60 | return kvstore.NewConsulClient(address, timeout) |
| 61 | case "etcd": |
| 62 | return kvstore.NewEtcdClient(address, timeout) |
| 63 | } |
| 64 | return nil, errors.New("unsupported-kv-store") |
| 65 | } |
| 66 | |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 67 | func newKafkaClient(clientType string, host string, port int, instanceID string) (kafka.Client, error) { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 68 | |
| 69 | log.Infow("kafka-client-type", log.Fields{"client": clientType}) |
| 70 | switch clientType { |
| 71 | case "sarama": |
| 72 | return kafka.NewSaramaClient( |
| 73 | kafka.Host(host), |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 74 | kafka.Port(port), |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 75 | kafka.ConsumerType(kafka.GroupCustomer), |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 76 | kafka.ProducerReturnOnErrors(true), |
| 77 | kafka.ProducerReturnOnSuccess(true), |
| 78 | kafka.ProducerMaxRetries(6), |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 79 | kafka.NumPartitions(3), |
| 80 | kafka.ConsumerGroupName(instanceID), |
| 81 | kafka.ConsumerGroupPrefix(instanceID), |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 82 | kafka.AutoCreateTopic(true), |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 83 | kafka.ProducerFlushFrequency(5), |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 84 | kafka.ProducerRetryBackoff(time.Millisecond*30)), nil |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 85 | } |
| 86 | return nil, errors.New("unsupported-client-type") |
| 87 | } |
| 88 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 89 | func newRWCore(cf *config.RWCoreFlags) *rwCore { |
| 90 | var rwCore rwCore |
| 91 | rwCore.config = cf |
| 92 | rwCore.halted = false |
| 93 | rwCore.exitChannel = make(chan int, 1) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 94 | rwCore.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 95 | return &rwCore |
| 96 | } |
| 97 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 98 | func (rw *rwCore) setKVClient() error { |
| 99 | addr := rw.config.KVStoreHost + ":" + strconv.Itoa(rw.config.KVStorePort) |
| 100 | client, err := newKVClient(rw.config.KVStoreType, addr, rw.config.KVStoreTimeout) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 101 | if err != nil { |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 102 | rw.kvClient = nil |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 103 | log.Error(err) |
| 104 | return err |
| 105 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 106 | rw.kvClient = client |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 107 | return nil |
| 108 | } |
| 109 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 110 | func toString(value interface{}) (string, error) { |
| 111 | switch t := value.(type) { |
| 112 | case []byte: |
| 113 | return string(value.([]byte)), nil |
| 114 | case string: |
| 115 | return value.(string), nil |
| 116 | default: |
| 117 | return "", fmt.Errorf("unexpected-type-%T", t) |
| 118 | } |
| 119 | } |
| 120 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 121 | func (rw *rwCore) start(ctx context.Context, instanceId string) { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 122 | log.Info("Starting RW Core components") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 123 | |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 124 | // Setup KV Client |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 125 | log.Debugw("create-kv-client", log.Fields{"kvstore": rw.config.KVStoreType}) |
| 126 | err := rw.setKVClient() |
| 127 | if err == nil { |
| 128 | // Setup KV transaction context |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 129 | txnPrefix := rw.config.KVStoreDataPrefix + "/transactions/" |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 130 | if err = c.SetTransactionContext(instanceId, |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 131 | txnPrefix, |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 132 | rw.kvClient, |
| 133 | rw.config.KVStoreTimeout, |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 134 | rw.config.KVTxnKeyDelTime, |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 135 | 1); err != nil { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 136 | log.Fatal("creating-transaction-context-failed") |
| 137 | } |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 138 | } |
| 139 | |
khenaidoo | 9084792 | 2018-12-03 14:47:51 -0500 | [diff] [blame] | 140 | // Setup Kafka Client |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 141 | if rw.kafkaClient, err = newKafkaClient("sarama", rw.config.KafkaAdapterHost, rw.config.KafkaAdapterPort, instanceId); err != nil { |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 142 | log.Fatal("Unsupported-kafka-client") |
| 143 | } |
| 144 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 145 | // Create the core service |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 146 | rw.core = c.NewCore(instanceId, rw.config, rw.kvClient, rw.kafkaClient) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 147 | |
| 148 | // start the core |
| 149 | rw.core.Start(ctx) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 150 | } |
| 151 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 152 | func (rw *rwCore) stop() { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 153 | // Stop leadership tracking |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 154 | rw.halted = true |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 155 | |
| 156 | // send exit signal |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 157 | rw.exitChannel <- 0 |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 158 | |
| 159 | // Cleanup - applies only if we had a kvClient |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 160 | if rw.kvClient != nil { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 161 | // Release all reservations |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 162 | if err := rw.kvClient.ReleaseAllReservations(); err != nil { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 163 | log.Infow("fail-to-release-all-reservations", log.Fields{"error": err}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 164 | } |
| 165 | // Close the DB connection |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 166 | rw.kvClient.Close() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 167 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 168 | |
| 169 | rw.core.Stop(nil) |
| 170 | |
| 171 | //if rw.kafkaClient != nil { |
| 172 | // rw.kafkaClient.Stop() |
| 173 | //} |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | func waitForExit() int { |
| 177 | signalChannel := make(chan os.Signal, 1) |
| 178 | signal.Notify(signalChannel, |
| 179 | syscall.SIGHUP, |
| 180 | syscall.SIGINT, |
| 181 | syscall.SIGTERM, |
| 182 | syscall.SIGQUIT) |
| 183 | |
| 184 | exitChannel := make(chan int) |
| 185 | |
| 186 | go func() { |
| 187 | s := <-signalChannel |
| 188 | switch s { |
| 189 | case syscall.SIGHUP, |
| 190 | syscall.SIGINT, |
| 191 | syscall.SIGTERM, |
| 192 | syscall.SIGQUIT: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 193 | log.Infow("closing-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 194 | exitChannel <- 0 |
| 195 | default: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 196 | log.Infow("unexpected-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 197 | exitChannel <- 1 |
| 198 | } |
| 199 | }() |
| 200 | |
| 201 | code := <-exitChannel |
| 202 | return code |
| 203 | } |
| 204 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 205 | func printBanner() { |
| 206 | fmt.Println(" ") |
| 207 | fmt.Println(" ______ ______ ") |
| 208 | fmt.Println("| _ \\ \\ / / ___|___ _ __ ___ ") |
| 209 | fmt.Println("| |_) \\ \\ /\\ / / | / _ \\| '__/ _ \\ ") |
| 210 | fmt.Println("| _ < \\ V V /| |__| (_) | | | __/ ") |
| 211 | fmt.Println("|_| \\_\\ \\_/\\_/ \\____\\___/|_| \\___| ") |
| 212 | fmt.Println(" ") |
| 213 | } |
| 214 | |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 215 | func printVersion() { |
| 216 | fmt.Println("VOLTHA Read-Write Core") |
| 217 | fmt.Println(version.VersionInfo.String(" ")) |
| 218 | } |
| 219 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 220 | func main() { |
| 221 | start := time.Now() |
| 222 | |
| 223 | cf := config.NewRWCoreFlags() |
| 224 | cf.ParseCommandArguments() |
| 225 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 226 | // Set the instance ID as the hostname |
| 227 | var instanceId string |
| 228 | hostName := utils.GetHostName() |
| 229 | if len(hostName) > 0 { |
| 230 | instanceId = hostName |
| 231 | } else { |
| 232 | log.Fatal("HOSTNAME not set") |
| 233 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 234 | |
| 235 | //Setup default logger - applies for packages that do not have specific logger set |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 236 | 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] | 237 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 238 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 239 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 240 | // Update all loggers (provisioned via init) with a common field |
| 241 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceId}); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 242 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 243 | } |
| 244 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 245 | // Update all loggers to log level specified as input parameter |
| 246 | log.SetAllLogLevel(cf.LogLevel) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 247 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 248 | //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 249 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 250 | defer log.CleanUp() |
| 251 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 252 | // Print version / build information and exit |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 253 | if cf.DisplayVersionOnly { |
| 254 | printVersion() |
| 255 | return |
| 256 | } |
| 257 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 258 | // Print banner if specified |
| 259 | if cf.Banner { |
| 260 | printBanner() |
| 261 | } |
| 262 | |
| 263 | log.Infow("rw-core-config", log.Fields{"config": *cf}) |
| 264 | |
| 265 | ctx, cancel := context.WithCancel(context.Background()) |
| 266 | defer cancel() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 267 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 268 | rw := newRWCore(cf) |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 269 | go rw.start(ctx, instanceId) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 270 | |
| 271 | code := waitForExit() |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 272 | log.Infow("received-a-closing-signal", log.Fields{"code": code}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 273 | |
| 274 | // Cleanup before leaving |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 275 | rw.stop() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 276 | |
| 277 | elapsed := time.Since(start) |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 278 | 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] | 279 | } |