blob: faddd57dfcfc764d4bb5bb2021f8c8f4b4f83ba2 [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"
khenaidoo5c11af72018-07-20 17:21:05 -040022 "github.com/opencord/voltha-go/rw_core/config"
khenaidoob9203542018-09-17 22:56:37 -040023 c "github.com/opencord/voltha-go/rw_core/core"
khenaidoo631fe542019-05-31 15:44:43 -040024 "github.com/opencord/voltha-go/rw_core/utils"
Scott Baker807addd2019-10-24 15:16:21 -070025 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
26 grpcserver "github.com/opencord/voltha-lib-go/v2/pkg/grpc"
27 "github.com/opencord/voltha-lib-go/v2/pkg/kafka"
28 "github.com/opencord/voltha-lib-go/v2/pkg/log"
29 "github.com/opencord/voltha-lib-go/v2/pkg/probe"
30 "github.com/opencord/voltha-lib-go/v2/pkg/version"
Scott Baker555307d2019-11-04 08:58:01 -080031 ic "github.com/opencord/voltha-protos/v2/go/inter_container"
khenaidoocfee5f42018-07-19 22:47:38 -040032 "os"
33 "os/signal"
khenaidoocfee5f42018-07-19 22:47:38 -040034 "strconv"
khenaidoocfee5f42018-07-19 22:47:38 -040035 "syscall"
khenaidoo5c11af72018-07-20 17:21:05 -040036 "time"
khenaidoocfee5f42018-07-19 22:47:38 -040037)
38
39type rwCore struct {
khenaidoo5c11af72018-07-20 17:21:05 -040040 kvClient kvstore.Client
41 config *config.RWCoreFlags
42 halted bool
43 exitChannel chan int
khenaidoob9203542018-09-17 22:56:37 -040044 //kmp *kafka.KafkaMessagingProxy
khenaidoo43c82122018-11-22 18:38:28 -050045 grpcServer *grpcserver.GrpcServer
46 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() {
khenaidoo2c6f1672018-09-20 23:14:41 -040053 log.AddPackage(log.JSON, log.DebugLevel, nil)
khenaidoob9203542018-09-17 22:56:37 -040054}
55
khenaidoocfee5f42018-07-19 22:47:38 -040056func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
57
khenaidoo5c11af72018-07-20 17:21:05 -040058 log.Infow("kv-store-type", log.Fields{"store": storeType})
khenaidoocfee5f42018-07-19 22:47:38 -040059 switch storeType {
60 case "consul":
61 return kvstore.NewConsulClient(address, timeout)
62 case "etcd":
63 return kvstore.NewEtcdClient(address, timeout)
64 }
65 return nil, errors.New("unsupported-kv-store")
66}
67
Scott Bakeree6a0872019-10-29 15:59:52 -070068func newKafkaClient(clientType string, host string, port int, instanceID string, livenessChannelInterval time.Duration) (kafka.Client, error) {
khenaidoo43c82122018-11-22 18:38:28 -050069
70 log.Infow("kafka-client-type", log.Fields{"client": clientType})
71 switch clientType {
72 case "sarama":
73 return kafka.NewSaramaClient(
74 kafka.Host(host),
khenaidoo90847922018-12-03 14:47:51 -050075 kafka.Port(port),
khenaidooca301322019-01-09 23:06:32 -050076 kafka.ConsumerType(kafka.GroupCustomer),
khenaidoo90847922018-12-03 14:47:51 -050077 kafka.ProducerReturnOnErrors(true),
78 kafka.ProducerReturnOnSuccess(true),
79 kafka.ProducerMaxRetries(6),
khenaidooca301322019-01-09 23:06:32 -050080 kafka.NumPartitions(3),
81 kafka.ConsumerGroupName(instanceID),
82 kafka.ConsumerGroupPrefix(instanceID),
khenaidoo54e0ddf2019-02-27 16:21:33 -050083 kafka.AutoCreateTopic(true),
khenaidooca301322019-01-09 23:06:32 -050084 kafka.ProducerFlushFrequency(5),
Scott Bakeree6a0872019-10-29 15:59:52 -070085 kafka.ProducerRetryBackoff(time.Millisecond*30),
86 kafka.LivenessChannelInterval(livenessChannelInterval),
87 ), nil
khenaidoo43c82122018-11-22 18:38:28 -050088 }
89 return nil, errors.New("unsupported-client-type")
90}
91
khenaidoocfee5f42018-07-19 22:47:38 -040092func newRWCore(cf *config.RWCoreFlags) *rwCore {
93 var rwCore rwCore
94 rwCore.config = cf
95 rwCore.halted = false
96 rwCore.exitChannel = make(chan int, 1)
khenaidoo79232702018-12-04 11:00:41 -050097 rwCore.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
khenaidoocfee5f42018-07-19 22:47:38 -040098 return &rwCore
99}
100
khenaidoob9203542018-09-17 22:56:37 -0400101func (rw *rwCore) setKVClient() error {
102 addr := rw.config.KVStoreHost + ":" + strconv.Itoa(rw.config.KVStorePort)
103 client, err := newKVClient(rw.config.KVStoreType, addr, rw.config.KVStoreTimeout)
khenaidoocfee5f42018-07-19 22:47:38 -0400104 if err != nil {
Richard Jankowskie4d77662018-10-17 13:53:21 -0400105 rw.kvClient = nil
khenaidoocfee5f42018-07-19 22:47:38 -0400106 log.Error(err)
107 return err
108 }
khenaidoob9203542018-09-17 22:56:37 -0400109 rw.kvClient = client
khenaidoocfee5f42018-07-19 22:47:38 -0400110 return nil
111}
112
khenaidoocfee5f42018-07-19 22:47:38 -0400113func toString(value interface{}) (string, error) {
114 switch t := value.(type) {
115 case []byte:
116 return string(value.([]byte)), nil
117 case string:
118 return value.(string), nil
119 default:
120 return "", fmt.Errorf("unexpected-type-%T", t)
121 }
122}
123
khenaidoo631fe542019-05-31 15:44:43 -0400124func (rw *rwCore) start(ctx context.Context, instanceId string) {
khenaidoo5c11af72018-07-20 17:21:05 -0400125 log.Info("Starting RW Core components")
khenaidoob9203542018-09-17 22:56:37 -0400126
khenaidoo90847922018-12-03 14:47:51 -0500127 // Setup KV Client
Richard Jankowskie4d77662018-10-17 13:53:21 -0400128 log.Debugw("create-kv-client", log.Fields{"kvstore": rw.config.KVStoreType})
129 err := rw.setKVClient()
130 if err == nil {
131 // Setup KV transaction context
khenaidoo9cdc1a62019-01-24 21:57:40 -0500132 txnPrefix := rw.config.KVStoreDataPrefix + "/transactions/"
khenaidoo631fe542019-05-31 15:44:43 -0400133 if err = c.SetTransactionContext(instanceId,
khenaidoo9cdc1a62019-01-24 21:57:40 -0500134 txnPrefix,
Richard Jankowskie4d77662018-10-17 13:53:21 -0400135 rw.kvClient,
khenaidoo09771ef2019-10-11 14:25:02 -0400136 rw.config.KVStoreTimeout); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500137 log.Fatal("creating-transaction-context-failed")
138 }
Richard Jankowskie4d77662018-10-17 13:53:21 -0400139 }
140
khenaidoo90847922018-12-03 14:47:51 -0500141 // Setup Kafka Client
Scott Bakeree6a0872019-10-29 15:59:52 -0700142 if rw.kafkaClient, err = newKafkaClient("sarama",
143 rw.config.KafkaAdapterHost,
144 rw.config.KafkaAdapterPort,
145 instanceId,
146 time.Duration(rw.config.LiveProbeInterval)*time.Second/2); err != nil {
khenaidoo43c82122018-11-22 18:38:28 -0500147 log.Fatal("Unsupported-kafka-client")
148 }
149
khenaidoob9203542018-09-17 22:56:37 -0400150 // Create the core service
khenaidoo631fe542019-05-31 15:44:43 -0400151 rw.core = c.NewCore(instanceId, rw.config, rw.kvClient, rw.kafkaClient)
khenaidoob9203542018-09-17 22:56:37 -0400152
153 // start the core
154 rw.core.Start(ctx)
khenaidoocfee5f42018-07-19 22:47:38 -0400155}
156
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700157func (rw *rwCore) stop(ctx context.Context) {
khenaidoocfee5f42018-07-19 22:47:38 -0400158 // Stop leadership tracking
khenaidoob9203542018-09-17 22:56:37 -0400159 rw.halted = true
khenaidoocfee5f42018-07-19 22:47:38 -0400160
161 // send exit signal
khenaidoob9203542018-09-17 22:56:37 -0400162 rw.exitChannel <- 0
khenaidoocfee5f42018-07-19 22:47:38 -0400163
164 // Cleanup - applies only if we had a kvClient
khenaidoob9203542018-09-17 22:56:37 -0400165 if rw.kvClient != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400166 // Release all reservations
khenaidoob9203542018-09-17 22:56:37 -0400167 if err := rw.kvClient.ReleaseAllReservations(); err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400168 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400169 }
170 // Close the DB connection
khenaidoob9203542018-09-17 22:56:37 -0400171 rw.kvClient.Close()
khenaidoocfee5f42018-07-19 22:47:38 -0400172 }
khenaidoo43c82122018-11-22 18:38:28 -0500173
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700174 rw.core.Stop(ctx)
khenaidoo43c82122018-11-22 18:38:28 -0500175
176 //if rw.kafkaClient != nil {
177 // rw.kafkaClient.Stop()
178 //}
khenaidoocfee5f42018-07-19 22:47:38 -0400179}
180
181func waitForExit() int {
182 signalChannel := make(chan os.Signal, 1)
183 signal.Notify(signalChannel,
184 syscall.SIGHUP,
185 syscall.SIGINT,
186 syscall.SIGTERM,
187 syscall.SIGQUIT)
188
189 exitChannel := make(chan int)
190
191 go func() {
192 s := <-signalChannel
193 switch s {
194 case syscall.SIGHUP,
195 syscall.SIGINT,
196 syscall.SIGTERM,
197 syscall.SIGQUIT:
khenaidoo5c11af72018-07-20 17:21:05 -0400198 log.Infow("closing-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400199 exitChannel <- 0
200 default:
khenaidoo5c11af72018-07-20 17:21:05 -0400201 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400202 exitChannel <- 1
203 }
204 }()
205
206 code := <-exitChannel
207 return code
208}
209
khenaidoo5c11af72018-07-20 17:21:05 -0400210func printBanner() {
211 fmt.Println(" ")
212 fmt.Println(" ______ ______ ")
213 fmt.Println("| _ \\ \\ / / ___|___ _ __ ___ ")
214 fmt.Println("| |_) \\ \\ /\\ / / | / _ \\| '__/ _ \\ ")
215 fmt.Println("| _ < \\ V V /| |__| (_) | | | __/ ")
216 fmt.Println("|_| \\_\\ \\_/\\_/ \\____\\___/|_| \\___| ")
217 fmt.Println(" ")
218}
219
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700220func printVersion() {
221 fmt.Println("VOLTHA Read-Write Core")
222 fmt.Println(version.VersionInfo.String(" "))
223}
224
khenaidoocfee5f42018-07-19 22:47:38 -0400225func main() {
226 start := time.Now()
227
228 cf := config.NewRWCoreFlags()
229 cf.ParseCommandArguments()
230
khenaidoo631fe542019-05-31 15:44:43 -0400231 // Set the instance ID as the hostname
232 var instanceId string
233 hostName := utils.GetHostName()
234 if len(hostName) > 0 {
235 instanceId = hostName
236 } else {
237 log.Fatal("HOSTNAME not set")
238 }
khenaidoob9203542018-09-17 22:56:37 -0400239
240 //Setup default logger - applies for packages that do not have specific logger set
khenaidoo631fe542019-05-31 15:44:43 -0400241 if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": instanceId}); err != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400242 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
243 }
khenaidoob9203542018-09-17 22:56:37 -0400244
khenaidoo631fe542019-05-31 15:44:43 -0400245 // Update all loggers (provisioned via init) with a common field
246 if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceId}); err != nil {
khenaidoob9203542018-09-17 22:56:37 -0400247 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
248 }
249
khenaidoo631fe542019-05-31 15:44:43 -0400250 // Update all loggers to log level specified as input parameter
251 log.SetAllLogLevel(cf.LogLevel)
khenaidoo2c6a0992019-04-29 13:46:56 -0400252
khenaidoo631fe542019-05-31 15:44:43 -0400253 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
khenaidoob9203542018-09-17 22:56:37 -0400254
khenaidoocfee5f42018-07-19 22:47:38 -0400255 defer log.CleanUp()
256
khenaidoo631fe542019-05-31 15:44:43 -0400257 // Print version / build information and exit
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700258 if cf.DisplayVersionOnly {
259 printVersion()
260 return
261 }
262
khenaidoo5c11af72018-07-20 17:21:05 -0400263 // Print banner if specified
264 if cf.Banner {
265 printBanner()
266 }
267
268 log.Infow("rw-core-config", log.Fields{"config": *cf})
269
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700270 // Create the core
271 rw := newRWCore(cf)
272
273 // Create a context adding the status update channel
khenaidoo5c11af72018-07-20 17:21:05 -0400274 ctx, cancel := context.WithCancel(context.Background())
275 defer cancel()
khenaidoocfee5f42018-07-19 22:47:38 -0400276
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700277 /*
278 * Create and start the liveness and readiness container management probes. This
279 * is done in the main function so just in case the main starts multiple other
280 * objects there can be a single probe end point for the process.
281 */
282 p := &probe.Probe{}
Kent Hagermanc4618832019-10-07 12:24:36 -0400283 go p.ListenAndServe(fmt.Sprintf("%s:%d", rw.config.ProbeHost, rw.config.ProbePort))
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700284
285 // Add the probe to the context to pass to all the services started
286 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
287
288 // Start the core
289 go rw.start(probeCtx, instanceId)
khenaidoocfee5f42018-07-19 22:47:38 -0400290
291 code := waitForExit()
khenaidoo5c11af72018-07-20 17:21:05 -0400292 log.Infow("received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400293
294 // Cleanup before leaving
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700295 rw.stop(probeCtx)
khenaidoocfee5f42018-07-19 22:47:38 -0400296
297 elapsed := time.Since(start)
khenaidoo631fe542019-05-31 15:44:43 -0400298 log.Infow("rw-core-run-time", log.Fields{"core": instanceId, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400299}