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