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