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" |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 23 | "sync" |
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 | |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore" |
| 27 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 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 |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 41 | Timeout time.Duration |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 42 | Address string |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 43 | PathPrefix string |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 44 | alive bool // Is this backend connection alive? |
| 45 | livenessMutex sync.Mutex |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 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 | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 52 | func NewBackend(ctx context.Context, storeType string, address string, 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, |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 57 | Address: address, |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 58 | Timeout: timeout, |
| 59 | LivenessChannelInterval: DefaultLivenessChannelInterval, |
| 60 | PathPrefix: pathPrefix, |
| 61 | alive: false, // connection considered down at start |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 64 | if b.Client, err = b.newClient(ctx, address, timeout); err != nil { |
| 65 | logger.Errorw(ctx, "failed-to-create-kv-client", |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 66 | log.Fields{ |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 67 | "type": storeType, "address": address, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 68 | "timeout": timeout, "prefix": pathPrefix, |
| 69 | "error": err.Error(), |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | return b |
| 74 | } |
| 75 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 76 | func (b *Backend) newClient(ctx context.Context, address string, timeout time.Duration) (kvstore.Client, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 77 | switch b.StoreType { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 78 | case "etcd": |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 79 | return kvstore.NewEtcdClient(ctx, 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 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 84 | func (b *Backend) makePath(ctx context.Context, 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 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 89 | func (b *Backend) updateLiveness(ctx context.Context, 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 |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 94 | b.livenessMutex.Lock() |
| 95 | defer b.livenessMutex.Unlock() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 96 | if b.liveness != nil { |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 97 | if b.alive != alive { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 98 | logger.Debug(ctx, "update-liveness-channel-reason-change") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 99 | b.liveness <- alive |
| 100 | b.lastLivenessTime = time.Now() |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 101 | } else if time.Since(b.lastLivenessTime) > b.LivenessChannelInterval { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 102 | logger.Debug(ctx, "update-liveness-channel-reason-interval") |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 103 | b.liveness <- alive |
| 104 | b.lastLivenessTime = time.Now() |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Emit log message only for alive state change |
| 109 | if b.alive != alive { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 110 | logger.Debugw(ctx, "change-kvstore-alive-status", log.Fields{"alive": alive}) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 111 | b.alive = alive |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Perform a dummy Key Lookup on kvstore to test Connection Liveness and |
| 116 | // post on Liveness channel |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 117 | func (b *Backend) PerformLivenessCheck(ctx context.Context) bool { |
| 118 | alive := b.Client.IsConnectionUp(ctx) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 119 | logger.Debugw(ctx, "kvstore-liveness-check-result", log.Fields{"alive": alive}) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 120 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 121 | b.updateLiveness(ctx, alive) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 122 | return alive |
| 123 | } |
| 124 | |
| 125 | // Enable the liveness monitor channel. This channel will report |
| 126 | // a "true" or "false" on every kvstore operation which indicates whether |
| 127 | // or not the connection is still Live. This channel is then picked up |
| 128 | // by the service (i.e. rw_core / ro_core) to update readiness status |
| 129 | // and/or take other actions. |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 130 | func (b *Backend) EnableLivenessChannel(ctx context.Context) chan bool { |
| 131 | logger.Debug(ctx, "enable-kvstore-liveness-channel") |
David K. Bainbridge | 5edd7fb | 2020-07-29 19:30:48 -0700 | [diff] [blame] | 132 | b.livenessMutex.Lock() |
| 133 | defer b.livenessMutex.Unlock() |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 134 | if b.liveness == nil { |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 135 | b.liveness = make(chan bool, 10) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 136 | b.liveness <- b.alive |
| 137 | b.lastLivenessTime = time.Now() |
| 138 | } |
| 139 | |
| 140 | return b.liveness |
| 141 | } |
| 142 | |
| 143 | // Extract Alive status of Kvstore based on type of error |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 144 | func (b *Backend) isErrorIndicatingAliveKvstore(ctx context.Context, err error) bool { |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 145 | // Alive unless observed an error indicating so |
| 146 | alive := true |
| 147 | |
| 148 | if err != nil { |
| 149 | |
| 150 | // timeout indicates kvstore not reachable/alive |
| 151 | if err == context.DeadlineExceeded { |
| 152 | alive = false |
| 153 | } |
| 154 | |
| 155 | // Need to analyze client-specific errors based on backend type |
| 156 | if b.StoreType == "etcd" { |
| 157 | |
| 158 | // For etcd backend, consider not-alive only for errors indicating |
| 159 | // timedout request or unavailable/corrupted cluster. For all remaining |
| 160 | // error codes listed in https://godoc.org/google.golang.org/grpc/codes#Code, |
| 161 | // we would not infer a not-alive backend because such a error may also |
| 162 | // occur due to bad client requests or sequence of operations |
| 163 | switch status.Code(err) { |
| 164 | case codes.DeadlineExceeded: |
| 165 | fallthrough |
| 166 | case codes.Unavailable: |
| 167 | fallthrough |
| 168 | case codes.DataLoss: |
| 169 | alive = false |
| 170 | } |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | return alive |
| 175 | } |
| 176 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 177 | // List retrieves one or more items that match the specified key |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 178 | func (b *Backend) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) { |
Rohan Agrawal | fb1cb35 | 2020-08-03 04:59:32 +0000 | [diff] [blame] | 179 | span, ctx := log.CreateChildSpan(ctx, "etcd-list") |
| 180 | defer span.Finish() |
| 181 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 182 | formattedPath := b.makePath(ctx, key) |
| 183 | logger.Debugw(ctx, "listing-key", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 184 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 185 | pair, err := b.Client.List(ctx, formattedPath) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 186 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 187 | b.updateLiveness(ctx, b.isErrorIndicatingAliveKvstore(ctx, err)) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 188 | |
| 189 | return pair, err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Get retrieves an item that matches the specified key |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 193 | func (b *Backend) Get(ctx context.Context, key string) (*kvstore.KVPair, error) { |
Rohan Agrawal | fb1cb35 | 2020-08-03 04:59:32 +0000 | [diff] [blame] | 194 | span, ctx := log.CreateChildSpan(ctx, "etcd-get") |
| 195 | defer span.Finish() |
| 196 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 197 | formattedPath := b.makePath(ctx, key) |
| 198 | logger.Debugw(ctx, "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 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 202 | b.updateLiveness(ctx, b.isErrorIndicatingAliveKvstore(ctx, 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 { |
Rohan Agrawal | fb1cb35 | 2020-08-03 04:59:32 +0000 | [diff] [blame] | 209 | span, ctx := log.CreateChildSpan(ctx, "etcd-put") |
| 210 | defer span.Finish() |
| 211 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 212 | formattedPath := b.makePath(ctx, key) |
| 213 | logger.Debugw(ctx, "putting-key", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 214 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 215 | err := b.Client.Put(ctx, formattedPath, value) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 216 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 217 | b.updateLiveness(ctx, b.isErrorIndicatingAliveKvstore(ctx, err)) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 218 | |
| 219 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // Delete removes an item under the specified key |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 223 | func (b *Backend) Delete(ctx context.Context, key string) error { |
Rohan Agrawal | fb1cb35 | 2020-08-03 04:59:32 +0000 | [diff] [blame] | 224 | span, ctx := log.CreateChildSpan(ctx, "etcd-delete") |
| 225 | defer span.Finish() |
| 226 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 227 | formattedPath := b.makePath(ctx, key) |
| 228 | logger.Debugw(ctx, "deleting-key", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 229 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 230 | err := b.Client.Delete(ctx, formattedPath) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 231 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 232 | b.updateLiveness(ctx, b.isErrorIndicatingAliveKvstore(ctx, err)) |
Girish Kumar | ca52210 | 2019-11-08 11:26:35 +0000 | [diff] [blame] | 233 | |
| 234 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Serkant Uluderya | 198de90 | 2020-11-16 20:29:17 +0300 | [diff] [blame] | 237 | // DeleteWithPrefix removes items having prefix key |
| 238 | func (b *Backend) DeleteWithPrefix(ctx context.Context, prefixKey string) error { |
| 239 | span, ctx := log.CreateChildSpan(ctx, "etcd-delete-with-prefix") |
| 240 | defer span.Finish() |
| 241 | |
| 242 | formattedPath := b.makePath(ctx, prefixKey) |
| 243 | logger.Debugw(ctx, "deleting-prefix-key", log.Fields{"key": prefixKey, "path": formattedPath}) |
| 244 | |
| 245 | err := b.Client.DeleteWithPrefix(ctx, formattedPath) |
| 246 | |
| 247 | b.updateLiveness(ctx, b.isErrorIndicatingAliveKvstore(ctx, err)) |
| 248 | |
| 249 | return err |
| 250 | } |
| 251 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 252 | // CreateWatch starts watching events for the specified key |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 253 | func (b *Backend) CreateWatch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event { |
Rohan Agrawal | fb1cb35 | 2020-08-03 04:59:32 +0000 | [diff] [blame] | 254 | span, ctx := log.CreateChildSpan(ctx, "etcd-create-watch") |
| 255 | defer span.Finish() |
| 256 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 257 | formattedPath := b.makePath(ctx, key) |
| 258 | logger.Debugw(ctx, "creating-key-watch", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 259 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 260 | return b.Client.Watch(ctx, formattedPath, withPrefix) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // DeleteWatch stops watching events for the specified key |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 264 | func (b *Backend) DeleteWatch(ctx context.Context, key string, ch chan *kvstore.Event) { |
Rohan Agrawal | fb1cb35 | 2020-08-03 04:59:32 +0000 | [diff] [blame] | 265 | span, ctx := log.CreateChildSpan(ctx, "etcd-delete-watch") |
| 266 | defer span.Finish() |
| 267 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 268 | formattedPath := b.makePath(ctx, key) |
| 269 | logger.Debugw(ctx, "deleting-key-watch", log.Fields{"key": key, "path": formattedPath}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 270 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 271 | b.Client.CloseWatch(ctx, formattedPath, ch) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 272 | } |