Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 | */ |
| 16 | |
sbarbari | 1e3e29c | 2019-11-05 10:06:50 -0500 | [diff] [blame] | 17 | package db |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 18 | |
| 19 | import ( |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 20 | "context" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 21 | "errors" |
| 22 | "fmt" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 23 | "strconv" |
| 24 | "sync" |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 25 | "time" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 26 | |
| 27 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 28 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 29 | "google.golang.org/grpc/codes" |
| 30 | "google.golang.org/grpc/status" |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | const ( |
| 34 | // Default Minimal Interval for posting alive state of backend kvstore on Liveness Channel |
| 35 | DefaultLivenessChannelInterval = time.Second * 30 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 36 | ) |
| 37 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 38 | // Backend structure holds details for accessing the kv store |
| 39 | type Backend struct { |
| 40 | sync.RWMutex |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 41 | Client kvstore.Client |
| 42 | StoreType string |
| 43 | Host string |
| 44 | Port int |
| 45 | Timeout int |
| 46 | PathPrefix string |
| 47 | alive bool // Is this backend connection alive? |
| 48 | liveness chan bool // channel to post alive state |
| 49 | LivenessChannelInterval time.Duration // regularly push alive state beyond this interval |
| 50 | lastLivenessTime time.Time // Instant of last alive state push |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // NewBackend creates a new instance of a Backend structure |
| 54 | func NewBackend(storeType string, host string, port int, timeout int, pathPrefix string) *Backend { |
| 55 | var err error |
| 56 | |
| 57 | b := &Backend{ |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 58 | StoreType: storeType, |
| 59 | Host: host, |
| 60 | Port: port, |
| 61 | Timeout: timeout, |
| 62 | LivenessChannelInterval: DefaultLivenessChannelInterval, |
| 63 | PathPrefix: pathPrefix, |
| 64 | alive: false, // connection considered down at start |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | address := host + ":" + strconv.Itoa(port) |
| 68 | if b.Client, err = b.newClient(address, timeout); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 69 | logger.Errorw("failed-to-create-kv-client", |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 70 | log.Fields{ |
| 71 | "type": storeType, "host": host, "port": port, |
| 72 | "timeout": timeout, "prefix": pathPrefix, |
| 73 | "error": err.Error(), |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | return b |
| 78 | } |
| 79 | |
| 80 | func (b *Backend) newClient(address string, timeout int) (kvstore.Client, error) { |
| 81 | switch b.StoreType { |
| 82 | case "consul": |
| 83 | return kvstore.NewConsulClient(address, timeout) |
| 84 | case "etcd": |
| 85 | return kvstore.NewEtcdClient(address, timeout) |
| 86 | } |
| 87 | return nil, errors.New("unsupported-kv-store") |
| 88 | } |
| 89 | |
| 90 | func (b *Backend) makePath(key string) string { |
| 91 | path := fmt.Sprintf("%s/%s", b.PathPrefix, key) |
| 92 | return path |
| 93 | } |
| 94 | |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 95 | func (b *Backend) updateLiveness(alive bool) { |
| 96 | // Periodically push stream of liveness data to the channel, |
| 97 | // so that in a live state, the core does not timeout and |
| 98 | // send a forced liveness message. Push alive state if the |
| 99 | // last push to channel was beyond livenessChannelInterval |
| 100 | if b.liveness != nil { |
| 101 | |
| 102 | if b.alive != alive { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 103 | logger.Debug("update-liveness-channel-reason-change") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 104 | b.liveness <- alive |
| 105 | b.lastLivenessTime = time.Now() |
| 106 | } else if time.Now().Sub(b.lastLivenessTime) > b.LivenessChannelInterval { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 107 | logger.Debug("update-liveness-channel-reason-interval") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 108 | b.liveness <- alive |
| 109 | b.lastLivenessTime = time.Now() |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Emit log message only for alive state change |
| 114 | if b.alive != alive { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 115 | logger.Debugw("change-kvstore-alive-status", log.Fields{"alive": alive}) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 116 | b.alive = alive |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Perform a dummy Key Lookup on kvstore to test Connection Liveness and |
| 121 | // post on Liveness channel |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 122 | func (b *Backend) PerformLivenessCheck(ctx context.Context) bool { |
| 123 | alive := b.Client.IsConnectionUp(ctx) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 124 | logger.Debugw("kvstore-liveness-check-result", log.Fields{"alive": alive}) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 125 | |
| 126 | b.updateLiveness(alive) |
| 127 | return alive |
| 128 | } |
| 129 | |
| 130 | // Enable the liveness monitor channel. This channel will report |
| 131 | // a "true" or "false" on every kvstore operation which indicates whether |
| 132 | // or not the connection is still Live. This channel is then picked up |
| 133 | // by the service (i.e. rw_core / ro_core) to update readiness status |
| 134 | // and/or take other actions. |
| 135 | func (b *Backend) EnableLivenessChannel() chan bool { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 136 | logger.Debug("enable-kvstore-liveness-channel") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 137 | |
| 138 | if b.liveness == nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 139 | logger.Debug("create-kvstore-liveness-channel") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 140 | |
| 141 | // Channel size of 10 to avoid any possibility of blocking in Load conditions |
| 142 | b.liveness = make(chan bool, 10) |
| 143 | |
| 144 | // Post initial alive state |
| 145 | b.liveness <- b.alive |
| 146 | b.lastLivenessTime = time.Now() |
| 147 | } |
| 148 | |
| 149 | return b.liveness |
| 150 | } |
| 151 | |
| 152 | // Extract Alive status of Kvstore based on type of error |
| 153 | func (b *Backend) isErrorIndicatingAliveKvstore(err error) bool { |
| 154 | // Alive unless observed an error indicating so |
| 155 | alive := true |
| 156 | |
| 157 | if err != nil { |
| 158 | |
| 159 | // timeout indicates kvstore not reachable/alive |
| 160 | if err == context.DeadlineExceeded { |
| 161 | alive = false |
| 162 | } |
| 163 | |
| 164 | // Need to analyze client-specific errors based on backend type |
| 165 | if b.StoreType == "etcd" { |
| 166 | |
| 167 | // For etcd backend, consider not-alive only for errors indicating |
| 168 | // timedout request or unavailable/corrupted cluster. For all remaining |
| 169 | // error codes listed in https://godoc.org/google.golang.org/grpc/codes#Code, |
| 170 | // we would not infer a not-alive backend because such a error may also |
| 171 | // occur due to bad client requests or sequence of operations |
| 172 | switch status.Code(err) { |
| 173 | case codes.DeadlineExceeded: |
| 174 | fallthrough |
| 175 | case codes.Unavailable: |
| 176 | fallthrough |
| 177 | case codes.DataLoss: |
| 178 | alive = false |
| 179 | } |
| 180 | |
| 181 | //} else { |
| 182 | // TODO: Implement for consul backend; would it be needed ever? |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return alive |
| 187 | } |
| 188 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 189 | // List retrieves one or more items that match the specified key |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 190 | func (b *Backend) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 191 | b.Lock() |
| 192 | defer b.Unlock() |
| 193 | |
| 194 | formattedPath := b.makePath(key) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 195 | logger.Debugw("listing-key", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 196 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 197 | pair, err := b.Client.List(ctx, formattedPath) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 198 | |
| 199 | b.updateLiveness(b.isErrorIndicatingAliveKvstore(err)) |
| 200 | |
| 201 | return pair, err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Get retrieves an item that matches the specified key |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 205 | func (b *Backend) Get(ctx context.Context, key string) (*kvstore.KVPair, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 206 | b.Lock() |
| 207 | defer b.Unlock() |
| 208 | |
| 209 | formattedPath := b.makePath(key) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 210 | logger.Debugw("getting-key", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 211 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 212 | pair, err := b.Client.Get(ctx, formattedPath) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 213 | |
| 214 | b.updateLiveness(b.isErrorIndicatingAliveKvstore(err)) |
| 215 | |
| 216 | return pair, err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Put stores an item value under the specifed key |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 220 | func (b *Backend) Put(ctx context.Context, key string, value interface{}) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 221 | b.Lock() |
| 222 | defer b.Unlock() |
| 223 | |
| 224 | formattedPath := b.makePath(key) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 225 | logger.Debugw("putting-key", log.Fields{"key": key, "value": string(value.([]byte)), "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 226 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 227 | err := b.Client.Put(ctx, formattedPath, value) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 228 | |
| 229 | b.updateLiveness(b.isErrorIndicatingAliveKvstore(err)) |
| 230 | |
| 231 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Delete removes an item under the specified key |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 235 | func (b *Backend) Delete(ctx context.Context, key string) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 236 | b.Lock() |
| 237 | defer b.Unlock() |
| 238 | |
| 239 | formattedPath := b.makePath(key) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 240 | logger.Debugw("deleting-key", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 241 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 242 | err := b.Client.Delete(ctx, formattedPath) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 243 | |
| 244 | b.updateLiveness(b.isErrorIndicatingAliveKvstore(err)) |
| 245 | |
| 246 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // CreateWatch starts watching events for the specified key |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 250 | func (b *Backend) CreateWatch(ctx context.Context, key string) chan *kvstore.Event { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 251 | b.Lock() |
| 252 | defer b.Unlock() |
| 253 | |
| 254 | formattedPath := b.makePath(key) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 255 | logger.Debugw("creating-key-watch", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 256 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 257 | return b.Client.Watch(ctx, formattedPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // DeleteWatch stops watching events for the specified key |
| 261 | func (b *Backend) DeleteWatch(key string, ch chan *kvstore.Event) { |
| 262 | b.Lock() |
| 263 | defer b.Unlock() |
| 264 | |
| 265 | formattedPath := b.makePath(key) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 266 | logger.Debugw("deleting-key-watch", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 267 | |
| 268 | b.Client.CloseWatch(formattedPath, ch) |
| 269 | } |