blob: e8a7ea66179cc3c0a19dd786a66a8b9dc384d700 [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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoocfee5f42018-07-19 22:47:38 -040017package main
18
19import (
khenaidoo5c11af72018-07-20 17:21:05 -040020 "context"
21 "errors"
khenaidoocfee5f42018-07-19 22:47:38 -040022 "fmt"
23 "os"
24 "os/signal"
khenaidoocfee5f42018-07-19 22:47:38 -040025 "strconv"
khenaidoocfee5f42018-07-19 22:47:38 -040026 "syscall"
khenaidoo5c11af72018-07-20 17:21:05 -040027 "time"
npujar1d86a522019-11-14 17:11:16 +053028
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.uluderya2ae470f2020-01-21 11:13:09 -080032 "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"
khenaidoocfee5f42018-07-19 22:47:38 -040038)
39
40type rwCore struct {
khenaidoo5c11af72018-07-20 17:21:05 -040041 kvClient kvstore.Client
42 config *config.RWCoreFlags
43 halted bool
44 exitChannel chan int
khenaidoob9203542018-09-17 22:56:37 -040045 //kmp *kafka.KafkaMessagingProxy
khenaidoo43c82122018-11-22 18:38:28 -050046 kafkaClient kafka.Client
47 core *c.Core
khenaidooabad44c2018-08-03 16:58:35 -040048 //For test
khenaidoo79232702018-12-04 11:00:41 -050049 receiverChannels []<-chan *ic.InterContainerMessage
khenaidoocfee5f42018-07-19 22:47:38 -040050}
51
khenaidoob9203542018-09-17 22:56:37 -040052func init() {
npujar1d86a522019-11-14 17:11:16 +053053 _, 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 }
khenaidoob9203542018-09-17 22:56:37 -040057}
58
khenaidoocfee5f42018-07-19 22:47:38 -040059func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
60
khenaidoo5c11af72018-07-20 17:21:05 -040061 log.Infow("kv-store-type", log.Fields{"store": storeType})
khenaidoocfee5f42018-07-19 22:47:38 -040062 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 Bakeree6a0872019-10-29 15:59:52 -070071func newKafkaClient(clientType string, host string, port int, instanceID string, livenessChannelInterval time.Duration) (kafka.Client, error) {
khenaidoo43c82122018-11-22 18:38:28 -050072
73 log.Infow("kafka-client-type", log.Fields{"client": clientType})
74 switch clientType {
75 case "sarama":
76 return kafka.NewSaramaClient(
77 kafka.Host(host),
khenaidoo90847922018-12-03 14:47:51 -050078 kafka.Port(port),
khenaidooca301322019-01-09 23:06:32 -050079 kafka.ConsumerType(kafka.GroupCustomer),
khenaidoo90847922018-12-03 14:47:51 -050080 kafka.ProducerReturnOnErrors(true),
81 kafka.ProducerReturnOnSuccess(true),
82 kafka.ProducerMaxRetries(6),
khenaidooca301322019-01-09 23:06:32 -050083 kafka.NumPartitions(3),
84 kafka.ConsumerGroupName(instanceID),
85 kafka.ConsumerGroupPrefix(instanceID),
khenaidoo54e0ddf2019-02-27 16:21:33 -050086 kafka.AutoCreateTopic(true),
khenaidooca301322019-01-09 23:06:32 -050087 kafka.ProducerFlushFrequency(5),
Scott Bakeree6a0872019-10-29 15:59:52 -070088 kafka.ProducerRetryBackoff(time.Millisecond*30),
89 kafka.LivenessChannelInterval(livenessChannelInterval),
90 ), nil
khenaidoo43c82122018-11-22 18:38:28 -050091 }
92 return nil, errors.New("unsupported-client-type")
93}
94
khenaidoocfee5f42018-07-19 22:47:38 -040095func newRWCore(cf *config.RWCoreFlags) *rwCore {
96 var rwCore rwCore
97 rwCore.config = cf
98 rwCore.halted = false
99 rwCore.exitChannel = make(chan int, 1)
khenaidoo79232702018-12-04 11:00:41 -0500100 rwCore.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
khenaidoocfee5f42018-07-19 22:47:38 -0400101 return &rwCore
102}
103
npujar1d86a522019-11-14 17:11:16 +0530104func (rw *rwCore) start(ctx context.Context, instanceID string) {
khenaidoo5c11af72018-07-20 17:21:05 -0400105 log.Info("Starting RW Core components")
khenaidoob9203542018-09-17 22:56:37 -0400106
khenaidoo90847922018-12-03 14:47:51 -0500107 // Setup KV Client
Richard Jankowskie4d77662018-10-17 13:53:21 -0400108 log.Debugw("create-kv-client", log.Fields{"kvstore": rw.config.KVStoreType})
Kent Hagerman16ce36a2019-12-17 13:40:53 -0500109 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 Jankowskie4d77662018-10-17 13:53:21 -0400123 }
124
khenaidoo90847922018-12-03 14:47:51 -0500125 // Setup Kafka Client
Scott Bakeree6a0872019-10-29 15:59:52 -0700126 if rw.kafkaClient, err = newKafkaClient("sarama",
127 rw.config.KafkaAdapterHost,
128 rw.config.KafkaAdapterPort,
npujar1d86a522019-11-14 17:11:16 +0530129 instanceID,
Girish Kumar4d3887d2019-11-22 14:22:05 +0000130 rw.config.LiveProbeInterval/2); err != nil {
khenaidoo43c82122018-11-22 18:38:28 -0500131 log.Fatal("Unsupported-kafka-client")
132 }
133
khenaidoob9203542018-09-17 22:56:37 -0400134 // Create the core service
Thomas Lee Se5a44012019-11-07 20:32:24 +0530135 rw.core = c.NewCore(ctx, instanceID, rw.config, rw.kvClient, rw.kafkaClient)
khenaidoob9203542018-09-17 22:56:37 -0400136
137 // start the core
Thomas Lee Se5a44012019-11-07 20:32:24 +0530138 err = rw.core.Start(ctx)
139 if err != nil {
140 log.Fatalf("failed-to-start-rwcore", log.Fields{"error": err})
141 }
khenaidoocfee5f42018-07-19 22:47:38 -0400142}
143
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700144func (rw *rwCore) stop(ctx context.Context) {
khenaidoocfee5f42018-07-19 22:47:38 -0400145 // Stop leadership tracking
khenaidoob9203542018-09-17 22:56:37 -0400146 rw.halted = true
khenaidoocfee5f42018-07-19 22:47:38 -0400147
148 // send exit signal
khenaidoob9203542018-09-17 22:56:37 -0400149 rw.exitChannel <- 0
khenaidoocfee5f42018-07-19 22:47:38 -0400150
151 // Cleanup - applies only if we had a kvClient
khenaidoob9203542018-09-17 22:56:37 -0400152 if rw.kvClient != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400153 // Release all reservations
npujar467fe752020-01-16 20:17:45 +0530154 if err := rw.kvClient.ReleaseAllReservations(ctx); err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400155 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400156 }
157 // Close the DB connection
khenaidoob9203542018-09-17 22:56:37 -0400158 rw.kvClient.Close()
khenaidoocfee5f42018-07-19 22:47:38 -0400159 }
khenaidoo43c82122018-11-22 18:38:28 -0500160
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700161 rw.core.Stop(ctx)
khenaidoo43c82122018-11-22 18:38:28 -0500162
163 //if rw.kafkaClient != nil {
164 // rw.kafkaClient.Stop()
165 //}
khenaidoocfee5f42018-07-19 22:47:38 -0400166}
167
168func 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:
khenaidoo5c11af72018-07-20 17:21:05 -0400185 log.Infow("closing-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400186 exitChannel <- 0
187 default:
khenaidoo5c11af72018-07-20 17:21:05 -0400188 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400189 exitChannel <- 1
190 }
191 }()
192
193 code := <-exitChannel
194 return code
195}
196
khenaidoo5c11af72018-07-20 17:21:05 -0400197func 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. Bainbridgef430cd52019-05-28 15:00:35 -0700207func printVersion() {
208 fmt.Println("VOLTHA Read-Write Core")
209 fmt.Println(version.VersionInfo.String(" "))
210}
211
khenaidoocfee5f42018-07-19 22:47:38 -0400212func main() {
213 start := time.Now()
214
215 cf := config.NewRWCoreFlags()
216 cf.ParseCommandArguments()
217
khenaidoo631fe542019-05-31 15:44:43 -0400218 // Set the instance ID as the hostname
npujar1d86a522019-11-14 17:11:16 +0530219 var instanceID string
khenaidoo631fe542019-05-31 15:44:43 -0400220 hostName := utils.GetHostName()
221 if len(hostName) > 0 {
npujar1d86a522019-11-14 17:11:16 +0530222 instanceID = hostName
khenaidoo631fe542019-05-31 15:44:43 -0400223 } else {
224 log.Fatal("HOSTNAME not set")
225 }
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +0000226
Don Newton8eca4622020-02-10 16:44:48 -0500227 realMain()
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +0000228
229 logLevel, err := log.StringToLogLevel(cf.LogLevel)
230 if err != nil {
231 panic(err)
232 }
233
khenaidoob9203542018-09-17 22:56:37 -0400234 //Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +0000235 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": instanceID}); err != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400236 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
237 }
khenaidoob9203542018-09-17 22:56:37 -0400238
khenaidoo631fe542019-05-31 15:44:43 -0400239 // Update all loggers (provisioned via init) with a common field
npujar1d86a522019-11-14 17:11:16 +0530240 if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceID}); err != nil {
khenaidoob9203542018-09-17 22:56:37 -0400241 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
242 }
243
khenaidoo631fe542019-05-31 15:44:43 -0400244 // Update all loggers to log level specified as input parameter
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +0000245 log.SetAllLogLevel(logLevel)
khenaidoo2c6a0992019-04-29 13:46:56 -0400246
khenaidoo631fe542019-05-31 15:44:43 -0400247 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
khenaidoob9203542018-09-17 22:56:37 -0400248
npujar1d86a522019-11-14 17:11:16 +0530249 defer func() {
250 err := log.CleanUp()
251 if err != nil {
252 log.Errorw("unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
253 }
254 }()
khenaidoocfee5f42018-07-19 22:47:38 -0400255
khenaidoo631fe542019-05-31 15:44:43 -0400256 // Print version / build information and exit
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700257 if cf.DisplayVersionOnly {
258 printVersion()
259 return
260 }
261
khenaidoo5c11af72018-07-20 17:21:05 -0400262 // Print banner if specified
263 if cf.Banner {
264 printBanner()
265 }
266
267 log.Infow("rw-core-config", log.Fields{"config": *cf})
268
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700269 // Create the core
270 rw := newRWCore(cf)
271
272 // Create a context adding the status update channel
khenaidoo5c11af72018-07-20 17:21:05 -0400273 ctx, cancel := context.WithCancel(context.Background())
274 defer cancel()
khenaidoocfee5f42018-07-19 22:47:38 -0400275
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700276 /*
277 * Create and start the liveness and readiness container management probes. This
278 * is done in the main function so just in case the main starts multiple other
279 * objects there can be a single probe end point for the process.
280 */
281 p := &probe.Probe{}
Kent Hagermanc4618832019-10-07 12:24:36 -0400282 go p.ListenAndServe(fmt.Sprintf("%s:%d", rw.config.ProbeHost, rw.config.ProbePort))
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700283
284 // Add the probe to the context to pass to all the services started
285 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
286
287 // Start the core
npujar1d86a522019-11-14 17:11:16 +0530288 go rw.start(probeCtx, instanceID)
khenaidoocfee5f42018-07-19 22:47:38 -0400289
290 code := waitForExit()
khenaidoo5c11af72018-07-20 17:21:05 -0400291 log.Infow("received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400292
293 // Cleanup before leaving
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700294 rw.stop(probeCtx)
khenaidoocfee5f42018-07-19 22:47:38 -0400295
296 elapsed := time.Since(start)
npujar1d86a522019-11-14 17:11:16 +0530297 log.Infow("rw-core-run-time", log.Fields{"core": instanceID, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400298}