blob: 9ace2fb977071902a797ff747c7253800c4c5e8d [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
khenaidoocfee5f42018-07-19 22:47:38 -040016package main
17
18import (
khenaidoo5c11af72018-07-20 17:21:05 -040019 "context"
20 "errors"
khenaidoocfee5f42018-07-19 22:47:38 -040021 "fmt"
khenaidoobf6e7bb2018-08-14 22:27:29 -040022 grpcserver "github.com/opencord/voltha-go/common/grpc"
khenaidoo5c11af72018-07-20 17:21:05 -040023 "github.com/opencord/voltha-go/common/log"
David K. Bainbridgef430cd52019-05-28 15:00:35 -070024 "github.com/opencord/voltha-go/common/version"
khenaidoo5c11af72018-07-20 17:21:05 -040025 "github.com/opencord/voltha-go/db/kvstore"
khenaidoo43c82122018-11-22 18:38:28 -050026 "github.com/opencord/voltha-go/kafka"
khenaidoo5c11af72018-07-20 17:21:05 -040027 "github.com/opencord/voltha-go/rw_core/config"
khenaidoob9203542018-09-17 22:56:37 -040028 c "github.com/opencord/voltha-go/rw_core/core"
khenaidoo631fe542019-05-31 15:44:43 -040029 "github.com/opencord/voltha-go/rw_core/utils"
khenaidoo2c6a0992019-04-29 13:46:56 -040030 ic "github.com/opencord/voltha-protos/go/inter_container"
khenaidoocfee5f42018-07-19 22:47:38 -040031 "os"
32 "os/signal"
khenaidoocfee5f42018-07-19 22:47:38 -040033 "strconv"
khenaidoocfee5f42018-07-19 22:47:38 -040034 "syscall"
khenaidoo5c11af72018-07-20 17:21:05 -040035 "time"
khenaidoocfee5f42018-07-19 22:47:38 -040036)
37
38type rwCore struct {
khenaidoo5c11af72018-07-20 17:21:05 -040039 kvClient kvstore.Client
40 config *config.RWCoreFlags
41 halted bool
42 exitChannel chan int
khenaidoob9203542018-09-17 22:56:37 -040043 //kmp *kafka.KafkaMessagingProxy
khenaidoo43c82122018-11-22 18:38:28 -050044 grpcServer *grpcserver.GrpcServer
45 kafkaClient kafka.Client
46 core *c.Core
khenaidooabad44c2018-08-03 16:58:35 -040047 //For test
khenaidoo79232702018-12-04 11:00:41 -050048 receiverChannels []<-chan *ic.InterContainerMessage
khenaidoocfee5f42018-07-19 22:47:38 -040049}
50
khenaidoob9203542018-09-17 22:56:37 -040051func init() {
khenaidoo2c6f1672018-09-20 23:14:41 -040052 log.AddPackage(log.JSON, log.DebugLevel, nil)
khenaidoob9203542018-09-17 22:56:37 -040053}
54
khenaidoocfee5f42018-07-19 22:47:38 -040055func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
56
khenaidoo5c11af72018-07-20 17:21:05 -040057 log.Infow("kv-store-type", log.Fields{"store": storeType})
khenaidoocfee5f42018-07-19 22:47:38 -040058 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
khenaidooca301322019-01-09 23:06:32 -050067func newKafkaClient(clientType string, host string, port int, instanceID string) (kafka.Client, error) {
khenaidoo43c82122018-11-22 18:38:28 -050068
69 log.Infow("kafka-client-type", log.Fields{"client": clientType})
70 switch clientType {
71 case "sarama":
72 return kafka.NewSaramaClient(
73 kafka.Host(host),
khenaidoo90847922018-12-03 14:47:51 -050074 kafka.Port(port),
khenaidooca301322019-01-09 23:06:32 -050075 kafka.ConsumerType(kafka.GroupCustomer),
khenaidoo90847922018-12-03 14:47:51 -050076 kafka.ProducerReturnOnErrors(true),
77 kafka.ProducerReturnOnSuccess(true),
78 kafka.ProducerMaxRetries(6),
khenaidooca301322019-01-09 23:06:32 -050079 kafka.NumPartitions(3),
80 kafka.ConsumerGroupName(instanceID),
81 kafka.ConsumerGroupPrefix(instanceID),
khenaidoo54e0ddf2019-02-27 16:21:33 -050082 kafka.AutoCreateTopic(true),
khenaidooca301322019-01-09 23:06:32 -050083 kafka.ProducerFlushFrequency(5),
khenaidoo79232702018-12-04 11:00:41 -050084 kafka.ProducerRetryBackoff(time.Millisecond*30)), nil
khenaidoo43c82122018-11-22 18:38:28 -050085 }
86 return nil, errors.New("unsupported-client-type")
87}
88
khenaidoocfee5f42018-07-19 22:47:38 -040089func newRWCore(cf *config.RWCoreFlags) *rwCore {
90 var rwCore rwCore
91 rwCore.config = cf
92 rwCore.halted = false
93 rwCore.exitChannel = make(chan int, 1)
khenaidoo79232702018-12-04 11:00:41 -050094 rwCore.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
khenaidoocfee5f42018-07-19 22:47:38 -040095 return &rwCore
96}
97
khenaidoob9203542018-09-17 22:56:37 -040098func (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)
khenaidoocfee5f42018-07-19 22:47:38 -0400101 if err != nil {
Richard Jankowskie4d77662018-10-17 13:53:21 -0400102 rw.kvClient = nil
khenaidoocfee5f42018-07-19 22:47:38 -0400103 log.Error(err)
104 return err
105 }
khenaidoob9203542018-09-17 22:56:37 -0400106 rw.kvClient = client
khenaidoocfee5f42018-07-19 22:47:38 -0400107 return nil
108}
109
khenaidoocfee5f42018-07-19 22:47:38 -0400110func 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
khenaidoo631fe542019-05-31 15:44:43 -0400121func (rw *rwCore) start(ctx context.Context, instanceId string) {
khenaidoo5c11af72018-07-20 17:21:05 -0400122 log.Info("Starting RW Core components")
khenaidoob9203542018-09-17 22:56:37 -0400123
khenaidoo90847922018-12-03 14:47:51 -0500124 // Setup KV Client
Richard Jankowskie4d77662018-10-17 13:53:21 -0400125 log.Debugw("create-kv-client", log.Fields{"kvstore": rw.config.KVStoreType})
126 err := rw.setKVClient()
127 if err == nil {
128 // Setup KV transaction context
khenaidoo9cdc1a62019-01-24 21:57:40 -0500129 txnPrefix := rw.config.KVStoreDataPrefix + "/transactions/"
khenaidoo631fe542019-05-31 15:44:43 -0400130 if err = c.SetTransactionContext(instanceId,
khenaidoo9cdc1a62019-01-24 21:57:40 -0500131 txnPrefix,
Richard Jankowskie4d77662018-10-17 13:53:21 -0400132 rw.kvClient,
133 rw.config.KVStoreTimeout,
Richard Jankowski199fd862019-03-18 14:49:51 -0400134 rw.config.KVTxnKeyDelTime,
khenaidoo1ce37ad2019-03-24 22:07:24 -0400135 1); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500136 log.Fatal("creating-transaction-context-failed")
137 }
Richard Jankowskie4d77662018-10-17 13:53:21 -0400138 }
139
khenaidoo90847922018-12-03 14:47:51 -0500140 // Setup Kafka Client
khenaidoo631fe542019-05-31 15:44:43 -0400141 if rw.kafkaClient, err = newKafkaClient("sarama", rw.config.KafkaAdapterHost, rw.config.KafkaAdapterPort, instanceId); err != nil {
khenaidoo43c82122018-11-22 18:38:28 -0500142 log.Fatal("Unsupported-kafka-client")
143 }
144
khenaidoob9203542018-09-17 22:56:37 -0400145 // Create the core service
khenaidoo631fe542019-05-31 15:44:43 -0400146 rw.core = c.NewCore(instanceId, rw.config, rw.kvClient, rw.kafkaClient)
khenaidoob9203542018-09-17 22:56:37 -0400147
148 // start the core
149 rw.core.Start(ctx)
khenaidoocfee5f42018-07-19 22:47:38 -0400150}
151
khenaidoob9203542018-09-17 22:56:37 -0400152func (rw *rwCore) stop() {
khenaidoocfee5f42018-07-19 22:47:38 -0400153 // Stop leadership tracking
khenaidoob9203542018-09-17 22:56:37 -0400154 rw.halted = true
khenaidoocfee5f42018-07-19 22:47:38 -0400155
156 // send exit signal
khenaidoob9203542018-09-17 22:56:37 -0400157 rw.exitChannel <- 0
khenaidoocfee5f42018-07-19 22:47:38 -0400158
159 // Cleanup - applies only if we had a kvClient
khenaidoob9203542018-09-17 22:56:37 -0400160 if rw.kvClient != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400161 // Release all reservations
khenaidoob9203542018-09-17 22:56:37 -0400162 if err := rw.kvClient.ReleaseAllReservations(); err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400163 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400164 }
165 // Close the DB connection
khenaidoob9203542018-09-17 22:56:37 -0400166 rw.kvClient.Close()
khenaidoocfee5f42018-07-19 22:47:38 -0400167 }
khenaidoo43c82122018-11-22 18:38:28 -0500168
169 rw.core.Stop(nil)
170
171 //if rw.kafkaClient != nil {
172 // rw.kafkaClient.Stop()
173 //}
khenaidoocfee5f42018-07-19 22:47:38 -0400174}
175
176func 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:
khenaidoo5c11af72018-07-20 17:21:05 -0400193 log.Infow("closing-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400194 exitChannel <- 0
195 default:
khenaidoo5c11af72018-07-20 17:21:05 -0400196 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400197 exitChannel <- 1
198 }
199 }()
200
201 code := <-exitChannel
202 return code
203}
204
khenaidoo5c11af72018-07-20 17:21:05 -0400205func 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. Bainbridgef430cd52019-05-28 15:00:35 -0700215func printVersion() {
216 fmt.Println("VOLTHA Read-Write Core")
217 fmt.Println(version.VersionInfo.String(" "))
218}
219
khenaidoocfee5f42018-07-19 22:47:38 -0400220func main() {
221 start := time.Now()
222
223 cf := config.NewRWCoreFlags()
224 cf.ParseCommandArguments()
225
khenaidoo631fe542019-05-31 15:44:43 -0400226 // 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 }
khenaidoob9203542018-09-17 22:56:37 -0400234
235 //Setup default logger - applies for packages that do not have specific logger set
khenaidoo631fe542019-05-31 15:44:43 -0400236 if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": instanceId}); err != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400237 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
238 }
khenaidoob9203542018-09-17 22:56:37 -0400239
khenaidoo631fe542019-05-31 15:44:43 -0400240 // Update all loggers (provisioned via init) with a common field
241 if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceId}); err != nil {
khenaidoob9203542018-09-17 22:56:37 -0400242 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
243 }
244
khenaidoo631fe542019-05-31 15:44:43 -0400245 // Update all loggers to log level specified as input parameter
246 log.SetAllLogLevel(cf.LogLevel)
khenaidoo2c6a0992019-04-29 13:46:56 -0400247
khenaidoo631fe542019-05-31 15:44:43 -0400248 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
khenaidoob9203542018-09-17 22:56:37 -0400249
khenaidoocfee5f42018-07-19 22:47:38 -0400250 defer log.CleanUp()
251
khenaidoo631fe542019-05-31 15:44:43 -0400252 // Print version / build information and exit
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700253 if cf.DisplayVersionOnly {
254 printVersion()
255 return
256 }
257
khenaidoo5c11af72018-07-20 17:21:05 -0400258 // 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()
khenaidoocfee5f42018-07-19 22:47:38 -0400267
khenaidoob9203542018-09-17 22:56:37 -0400268 rw := newRWCore(cf)
khenaidoo631fe542019-05-31 15:44:43 -0400269 go rw.start(ctx, instanceId)
khenaidoocfee5f42018-07-19 22:47:38 -0400270
271 code := waitForExit()
khenaidoo5c11af72018-07-20 17:21:05 -0400272 log.Infow("received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400273
274 // Cleanup before leaving
khenaidoob9203542018-09-17 22:56:37 -0400275 rw.stop()
khenaidoocfee5f42018-07-19 22:47:38 -0400276
277 elapsed := time.Since(start)
khenaidoo631fe542019-05-31 15:44:43 -0400278 log.Infow("rw-core-run-time", log.Fields{"core": instanceId, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400279}