khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 4 | "context" |
| 5 | "errors" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 6 | "fmt" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 7 | "github.com/opencord/voltha-go/common/log" |
| 8 | "github.com/opencord/voltha-go/db/kvstore" |
| 9 | "github.com/opencord/voltha-go/rw_core/config" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 10 | "os" |
| 11 | "os/signal" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 12 | "strconv" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 13 | "syscall" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 14 | "time" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | type rwCore struct { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 18 | kvClient kvstore.Client |
| 19 | config *config.RWCoreFlags |
| 20 | halted bool |
| 21 | exitChannel chan int |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) { |
| 25 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 26 | log.Infow("kv-store-type", log.Fields{"store": storeType}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 27 | switch storeType { |
| 28 | case "consul": |
| 29 | return kvstore.NewConsulClient(address, timeout) |
| 30 | case "etcd": |
| 31 | return kvstore.NewEtcdClient(address, timeout) |
| 32 | } |
| 33 | return nil, errors.New("unsupported-kv-store") |
| 34 | } |
| 35 | |
| 36 | func newRWCore(cf *config.RWCoreFlags) *rwCore { |
| 37 | var rwCore rwCore |
| 38 | rwCore.config = cf |
| 39 | rwCore.halted = false |
| 40 | rwCore.exitChannel = make(chan int, 1) |
| 41 | return &rwCore |
| 42 | } |
| 43 | |
| 44 | func (core *rwCore) setKVClient() error { |
| 45 | addr := core.config.KVStoreHost + ":" + strconv.Itoa(core.config.KVStorePort) |
| 46 | client, err := newKVClient(core.config.KVStoreType, addr, core.config.KVStoreTimeout) |
| 47 | if err != nil { |
| 48 | log.Error(err) |
| 49 | return err |
| 50 | } |
| 51 | core.kvClient = client |
| 52 | return nil |
| 53 | } |
| 54 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 55 | func toString(value interface{}) (string, error) { |
| 56 | switch t := value.(type) { |
| 57 | case []byte: |
| 58 | return string(value.([]byte)), nil |
| 59 | case string: |
| 60 | return value.(string), nil |
| 61 | default: |
| 62 | return "", fmt.Errorf("unexpected-type-%T", t) |
| 63 | } |
| 64 | } |
| 65 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 66 | func (core *rwCore) start(ctx context.Context) { |
| 67 | log.Info("Starting RW Core components") |
| 68 | // Setup GRPC Server |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 69 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 70 | // Setup KV Client |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 71 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 72 | // Setup Kafka messaging services |
| 73 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 74 | } |
| 75 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 76 | func (core *rwCore) stop() { |
| 77 | // Stop leadership tracking |
| 78 | core.halted = true |
| 79 | |
| 80 | // send exit signal |
| 81 | core.exitChannel <- 0 |
| 82 | |
| 83 | // Cleanup - applies only if we had a kvClient |
| 84 | if core.kvClient != nil { |
| 85 | // Release all reservations |
| 86 | if err := core.kvClient.ReleaseAllReservations(); err != nil { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 87 | log.Infow("fail-to-release-all-reservations", log.Fields{"error": err}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 88 | } |
| 89 | // Close the DB connection |
| 90 | core.kvClient.Close() |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func waitForExit() int { |
| 95 | signalChannel := make(chan os.Signal, 1) |
| 96 | signal.Notify(signalChannel, |
| 97 | syscall.SIGHUP, |
| 98 | syscall.SIGINT, |
| 99 | syscall.SIGTERM, |
| 100 | syscall.SIGQUIT) |
| 101 | |
| 102 | exitChannel := make(chan int) |
| 103 | |
| 104 | go func() { |
| 105 | s := <-signalChannel |
| 106 | switch s { |
| 107 | case syscall.SIGHUP, |
| 108 | syscall.SIGINT, |
| 109 | syscall.SIGTERM, |
| 110 | syscall.SIGQUIT: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 111 | log.Infow("closing-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 112 | exitChannel <- 0 |
| 113 | default: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 114 | log.Infow("unexpected-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 115 | exitChannel <- 1 |
| 116 | } |
| 117 | }() |
| 118 | |
| 119 | code := <-exitChannel |
| 120 | return code |
| 121 | } |
| 122 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 123 | func printBanner() { |
| 124 | fmt.Println(" ") |
| 125 | fmt.Println(" ______ ______ ") |
| 126 | fmt.Println("| _ \\ \\ / / ___|___ _ __ ___ ") |
| 127 | fmt.Println("| |_) \\ \\ /\\ / / | / _ \\| '__/ _ \\ ") |
| 128 | fmt.Println("| _ < \\ V V /| |__| (_) | | | __/ ") |
| 129 | fmt.Println("|_| \\_\\ \\_/\\_/ \\____\\___/|_| \\___| ") |
| 130 | fmt.Println(" ") |
| 131 | } |
| 132 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 133 | func main() { |
| 134 | start := time.Now() |
| 135 | |
| 136 | cf := config.NewRWCoreFlags() |
| 137 | cf.ParseCommandArguments() |
| 138 | |
| 139 | // Setup logging |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 140 | if _, err := log.SetLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 141 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 142 | } |
| 143 | defer log.CleanUp() |
| 144 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 145 | // Print banner if specified |
| 146 | if cf.Banner { |
| 147 | printBanner() |
| 148 | } |
| 149 | |
| 150 | log.Infow("rw-core-config", log.Fields{"config": *cf}) |
| 151 | |
| 152 | ctx, cancel := context.WithCancel(context.Background()) |
| 153 | defer cancel() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 154 | |
| 155 | core := newRWCore(cf) |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 156 | go core.start(ctx) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 157 | |
| 158 | code := waitForExit() |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 159 | log.Infow("received-a-closing-signal", log.Fields{"code": code}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 160 | |
| 161 | // Cleanup before leaving |
| 162 | core.stop() |
| 163 | |
| 164 | elapsed := time.Since(start) |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 165 | log.Infow("rw-core-run-time", log.Fields{"core": core.config.InstanceID, "time": elapsed / time.Second}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 166 | } |