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 | package kvstore |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "context" |
| 21 | "errors" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 22 | log "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 23 | "sync" |
| 24 | "time" |
| 25 | //log "ciena.com/coordinator/common" |
| 26 | consulapi "github.com/hashicorp/consul/api" |
| 27 | ) |
| 28 | |
| 29 | type channelContextMap struct { |
| 30 | ctx context.Context |
| 31 | channel chan *Event |
| 32 | cancel context.CancelFunc |
| 33 | } |
| 34 | |
| 35 | // ConsulClient represents the consul KV store client |
| 36 | type ConsulClient struct { |
| 37 | session *consulapi.Session |
| 38 | sessionID string |
| 39 | consul *consulapi.Client |
| 40 | doneCh *chan int |
| 41 | keyReservations map[string]interface{} |
| 42 | watchedChannelsContext map[string][]*channelContextMap |
| 43 | writeLock sync.Mutex |
| 44 | } |
| 45 | |
| 46 | // NewConsulClient returns a new client for the Consul KV store |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 47 | func NewConsulClient(addr string, timeout time.Duration) (*ConsulClient, error) { |
| 48 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 49 | config := consulapi.DefaultConfig() |
| 50 | config.Address = addr |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 51 | config.WaitTime = timeout |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 52 | consul, err := consulapi.NewClient(config) |
| 53 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 54 | logger.Error(err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 55 | return nil, err |
| 56 | } |
| 57 | |
| 58 | doneCh := make(chan int, 1) |
| 59 | wChannelsContext := make(map[string][]*channelContextMap) |
| 60 | reservations := make(map[string]interface{}) |
| 61 | return &ConsulClient{consul: consul, doneCh: &doneCh, watchedChannelsContext: wChannelsContext, keyReservations: reservations}, nil |
| 62 | } |
| 63 | |
| 64 | // IsConnectionUp returns whether the connection to the Consul KV store is up |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 65 | func (c *ConsulClient) IsConnectionUp(ctx context.Context) bool { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 66 | logger.Error("Unimplemented function") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 67 | return false |
| 68 | } |
| 69 | |
| 70 | // List returns an array of key-value pairs with key as a prefix. Timeout defines how long the function will |
| 71 | // wait for a response |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 72 | func (c *ConsulClient) List(ctx context.Context, key string) (map[string]*KVPair, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 73 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 74 | deadline, _ := ctx.Deadline() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 75 | kv := c.consul.KV() |
| 76 | var queryOptions consulapi.QueryOptions |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 77 | // Substract current time from deadline to get the waitTime duration |
| 78 | queryOptions.WaitTime = time.Until(deadline) |
| 79 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 80 | // For now we ignore meta data |
| 81 | kvps, _, err := kv.List(key, &queryOptions) |
| 82 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 83 | logger.Error(err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 84 | return nil, err |
| 85 | } |
| 86 | m := make(map[string]*KVPair) |
| 87 | for _, kvp := range kvps { |
| 88 | m[string(kvp.Key)] = NewKVPair(string(kvp.Key), kvp.Value, string(kvp.Session), 0, -1) |
| 89 | } |
| 90 | return m, nil |
| 91 | } |
| 92 | |
| 93 | // Get returns a key-value pair for a given key. Timeout defines how long the function will |
| 94 | // wait for a response |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 95 | func (c *ConsulClient) Get(ctx context.Context, key string) (*KVPair, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 96 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 97 | deadline, _ := ctx.Deadline() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 98 | kv := c.consul.KV() |
| 99 | var queryOptions consulapi.QueryOptions |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 100 | // Substract current time from deadline to get the waitTime duration |
| 101 | queryOptions.WaitTime = time.Until(deadline) |
| 102 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 103 | // For now we ignore meta data |
| 104 | kvp, _, err := kv.Get(key, &queryOptions) |
| 105 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 106 | logger.Error(err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 107 | return nil, err |
| 108 | } |
| 109 | if kvp != nil { |
| 110 | return NewKVPair(string(kvp.Key), kvp.Value, string(kvp.Session), 0, -1), nil |
| 111 | } |
| 112 | |
| 113 | return nil, nil |
| 114 | } |
| 115 | |
| 116 | // Put writes a key-value pair to the KV store. Value can only be a string or []byte since the consul API |
| 117 | // accepts only a []byte as a value for a put operation. Timeout defines how long the function will |
| 118 | // wait for a response |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 119 | func (c *ConsulClient) Put(ctx context.Context, key string, value interface{}) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 120 | |
| 121 | // Validate that we can create a byte array from the value as consul API expects a byte array |
| 122 | var val []byte |
| 123 | var er error |
| 124 | if val, er = ToByte(value); er != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 125 | logger.Error(er) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 126 | return er |
| 127 | } |
| 128 | |
| 129 | // Create a key value pair |
| 130 | kvp := consulapi.KVPair{Key: key, Value: val} |
| 131 | kv := c.consul.KV() |
| 132 | var writeOptions consulapi.WriteOptions |
| 133 | c.writeLock.Lock() |
| 134 | defer c.writeLock.Unlock() |
| 135 | _, err := kv.Put(&kvp, &writeOptions) |
| 136 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 137 | logger.Error(err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 138 | return err |
| 139 | } |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | // Delete removes a key from the KV store. Timeout defines how long the function will |
| 144 | // wait for a response |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 145 | func (c *ConsulClient) Delete(ctx context.Context, key string) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 146 | kv := c.consul.KV() |
| 147 | var writeOptions consulapi.WriteOptions |
| 148 | c.writeLock.Lock() |
| 149 | defer c.writeLock.Unlock() |
| 150 | _, err := kv.Delete(key, &writeOptions) |
| 151 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 152 | logger.Error(err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 153 | return err |
| 154 | } |
| 155 | return nil |
| 156 | } |
| 157 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 158 | func (c *ConsulClient) deleteSession() { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 159 | if c.sessionID != "" { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 160 | logger.Debug("cleaning-up-session") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 161 | session := c.consul.Session() |
| 162 | _, err := session.Destroy(c.sessionID, nil) |
| 163 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 164 | logger.Errorw("error-cleaning-session", log.Fields{"session": c.sessionID, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | c.sessionID = "" |
| 168 | c.session = nil |
| 169 | } |
| 170 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 171 | func (c *ConsulClient) createSession(ttl time.Duration, retries int) (*consulapi.Session, string, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 172 | session := c.consul.Session() |
| 173 | entry := &consulapi.SessionEntry{ |
| 174 | Behavior: consulapi.SessionBehaviorDelete, |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 175 | TTL: ttl.String(), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | for { |
| 179 | id, meta, err := session.Create(entry, nil) |
| 180 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 181 | logger.Errorw("create-session-error", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 182 | if retries == 0 { |
| 183 | return nil, "", err |
| 184 | } |
| 185 | } else if meta.RequestTime == 0 { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 186 | logger.Errorw("create-session-bad-meta-data", log.Fields{"meta-data": meta}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 187 | if retries == 0 { |
| 188 | return nil, "", errors.New("bad-meta-data") |
| 189 | } |
| 190 | } else if id == "" { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 191 | logger.Error("create-session-nil-id") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 192 | if retries == 0 { |
| 193 | return nil, "", errors.New("ID-nil") |
| 194 | } |
| 195 | } else { |
| 196 | return session, id, nil |
| 197 | } |
| 198 | // If retry param is -1 we will retry indefinitely |
| 199 | if retries > 0 { |
| 200 | retries-- |
| 201 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 202 | logger.Debug("retrying-session-create-after-a-second-delay") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 203 | time.Sleep(time.Duration(1) * time.Second) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Helper function to verify mostly whether the content of two interface types are the same. Focus is []byte and |
| 208 | // string types |
| 209 | func isEqual(val1 interface{}, val2 interface{}) bool { |
| 210 | b1, err := ToByte(val1) |
| 211 | b2, er := ToByte(val2) |
| 212 | if err == nil && er == nil { |
| 213 | return bytes.Equal(b1, b2) |
| 214 | } |
| 215 | return val1 == val2 |
| 216 | } |
| 217 | |
| 218 | // Reserve is invoked to acquire a key and set it to a given value. Value can only be a string or []byte since |
| 219 | // the consul API accepts only a []byte. Timeout defines how long the function will wait for a response. TTL |
| 220 | // defines how long that reservation is valid. When TTL expires the key is unreserved by the KV store itself. |
| 221 | // If the key is acquired then the value returned will be the value passed in. If the key is already acquired |
| 222 | // then the value assigned to that key will be returned. |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 223 | func (c *ConsulClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 224 | |
| 225 | // Validate that we can create a byte array from the value as consul API expects a byte array |
| 226 | var val []byte |
| 227 | var er error |
| 228 | if val, er = ToByte(value); er != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 229 | logger.Error(er) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 230 | return nil, er |
| 231 | } |
| 232 | |
| 233 | // Cleanup any existing session and recreate new ones. A key is reserved against a session |
| 234 | if c.sessionID != "" { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 235 | c.deleteSession() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Clear session if reservation is not successful |
| 239 | reservationSuccessful := false |
| 240 | defer func() { |
| 241 | if !reservationSuccessful { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 242 | logger.Debug("deleting-session") |
| 243 | c.deleteSession() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 244 | } |
| 245 | }() |
| 246 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 247 | session, sessionID, err := c.createSession(ttl, -1) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 248 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 249 | logger.Errorw("no-session-created", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 250 | return "", errors.New("no-session-created") |
| 251 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 252 | logger.Debugw("session-created", log.Fields{"session-id": sessionID}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 253 | c.sessionID = sessionID |
| 254 | c.session = session |
| 255 | |
| 256 | // Try to grap the Key using the session |
| 257 | kv := c.consul.KV() |
| 258 | kvp := consulapi.KVPair{Key: key, Value: val, Session: c.sessionID} |
| 259 | result, _, err := kv.Acquire(&kvp, nil) |
| 260 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 261 | logger.Errorw("error-acquiring-keys", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 262 | return nil, err |
| 263 | } |
| 264 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 265 | logger.Debugw("key-acquired", log.Fields{"key": key, "status": result}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 266 | |
| 267 | // Irrespective whether we were successful in acquiring the key, let's read it back and see if it's us. |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 268 | m, err := c.Get(ctx, key) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 269 | if err != nil { |
| 270 | return nil, err |
| 271 | } |
| 272 | if m != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 273 | logger.Debugw("response-received", log.Fields{"key": m.Key, "m.value": string(m.Value.([]byte)), "value": value}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 274 | if m.Key == key && isEqual(m.Value, value) { |
| 275 | // My reservation is successful - register it. For now, support is only for 1 reservation per key |
| 276 | // per session. |
| 277 | reservationSuccessful = true |
| 278 | c.writeLock.Lock() |
| 279 | c.keyReservations[key] = m.Value |
| 280 | c.writeLock.Unlock() |
| 281 | return m.Value, nil |
| 282 | } |
| 283 | // My reservation has failed. Return the owner of that key |
| 284 | return m.Value, nil |
| 285 | } |
| 286 | return nil, nil |
| 287 | } |
| 288 | |
| 289 | // ReleaseAllReservations releases all key reservations previously made (using Reserve API) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 290 | func (c *ConsulClient) ReleaseAllReservations(ctx context.Context) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 291 | kv := c.consul.KV() |
| 292 | var kvp consulapi.KVPair |
| 293 | var result bool |
| 294 | var err error |
| 295 | |
| 296 | c.writeLock.Lock() |
| 297 | defer c.writeLock.Unlock() |
| 298 | |
| 299 | for key, value := range c.keyReservations { |
| 300 | kvp = consulapi.KVPair{Key: key, Value: value.([]byte), Session: c.sessionID} |
| 301 | result, _, err = kv.Release(&kvp, nil) |
| 302 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 303 | logger.Errorw("cannot-release-reservation", log.Fields{"key": key, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 304 | return err |
| 305 | } |
| 306 | if !result { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 307 | logger.Errorw("cannot-release-reservation", log.Fields{"key": key}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 308 | } |
| 309 | delete(c.keyReservations, key) |
| 310 | } |
| 311 | return nil |
| 312 | } |
| 313 | |
| 314 | // ReleaseReservation releases reservation for a specific key. |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 315 | func (c *ConsulClient) ReleaseReservation(ctx context.Context, key string) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 316 | var ok bool |
| 317 | var reservedValue interface{} |
| 318 | c.writeLock.Lock() |
| 319 | defer c.writeLock.Unlock() |
| 320 | if reservedValue, ok = c.keyReservations[key]; !ok { |
| 321 | return errors.New("key-not-reserved:" + key) |
| 322 | } |
| 323 | // Release the reservation |
| 324 | kv := c.consul.KV() |
| 325 | kvp := consulapi.KVPair{Key: key, Value: reservedValue.([]byte), Session: c.sessionID} |
| 326 | |
| 327 | result, _, er := kv.Release(&kvp, nil) |
| 328 | if er != nil { |
| 329 | return er |
| 330 | } |
| 331 | // Remove that key entry on success |
| 332 | if result { |
| 333 | delete(c.keyReservations, key) |
| 334 | return nil |
| 335 | } |
| 336 | return errors.New("key-cannot-be-unreserved") |
| 337 | } |
| 338 | |
| 339 | // RenewReservation renews a reservation. A reservation will go stale after the specified TTL (Time To Live) |
| 340 | // period specified when reserving the key |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 341 | func (c *ConsulClient) RenewReservation(ctx context.Context, key string) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 342 | // In the case of Consul, renew reservation of a reserve key only require renewing the client session. |
| 343 | |
| 344 | c.writeLock.Lock() |
| 345 | defer c.writeLock.Unlock() |
| 346 | |
| 347 | // Verify the key was reserved |
| 348 | if _, ok := c.keyReservations[key]; !ok { |
| 349 | return errors.New("key-not-reserved") |
| 350 | } |
| 351 | |
| 352 | if c.session == nil { |
| 353 | return errors.New("no-session-exist") |
| 354 | } |
| 355 | |
| 356 | var writeOptions consulapi.WriteOptions |
| 357 | if _, _, err := c.session.Renew(c.sessionID, &writeOptions); err != nil { |
| 358 | return err |
| 359 | } |
| 360 | return nil |
| 361 | } |
| 362 | |
| 363 | // Watch provides the watch capability on a given key. It returns a channel onto which the callee needs to |
| 364 | // listen to receive Events. |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 365 | func (c *ConsulClient) Watch(ctx context.Context, key string, withPrefix bool) chan *Event { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 366 | |
| 367 | // Create a new channel |
| 368 | ch := make(chan *Event, maxClientChannelBufferSize) |
| 369 | |
| 370 | // Create a context to track this request |
| 371 | watchContext, cFunc := context.WithCancel(context.Background()) |
| 372 | |
| 373 | // Save the channel and context reference for later |
| 374 | c.writeLock.Lock() |
| 375 | defer c.writeLock.Unlock() |
| 376 | ccm := channelContextMap{channel: ch, ctx: watchContext, cancel: cFunc} |
| 377 | c.watchedChannelsContext[key] = append(c.watchedChannelsContext[key], &ccm) |
| 378 | |
| 379 | // Launch a go routine to listen for updates |
| 380 | go c.listenForKeyChange(watchContext, key, ch) |
| 381 | |
| 382 | return ch |
| 383 | } |
| 384 | |
| 385 | // CloseWatch closes a specific watch. Both the key and the channel are required when closing a watch as there |
| 386 | // may be multiple listeners on the same key. The previously created channel serves as a key |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 387 | func (c *ConsulClient) CloseWatch(key string, ch chan *Event) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 388 | // First close the context |
| 389 | var ok bool |
| 390 | var watchedChannelsContexts []*channelContextMap |
| 391 | c.writeLock.Lock() |
| 392 | defer c.writeLock.Unlock() |
| 393 | if watchedChannelsContexts, ok = c.watchedChannelsContext[key]; !ok { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 394 | logger.Errorw("key-has-no-watched-context-or-channel", log.Fields{"key": key}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 395 | return |
| 396 | } |
| 397 | // Look for the channels |
| 398 | var pos = -1 |
| 399 | for i, chCtxMap := range watchedChannelsContexts { |
| 400 | if chCtxMap.channel == ch { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 401 | logger.Debug("channel-found") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 402 | chCtxMap.cancel() |
| 403 | //close the channel |
| 404 | close(ch) |
| 405 | pos = i |
| 406 | break |
| 407 | } |
| 408 | } |
| 409 | // Remove that entry if present |
| 410 | if pos >= 0 { |
| 411 | c.watchedChannelsContext[key] = append(c.watchedChannelsContext[key][:pos], c.watchedChannelsContext[key][pos+1:]...) |
| 412 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 413 | logger.Debugw("watched-channel-exiting", log.Fields{"key": key, "channel": c.watchedChannelsContext[key]}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | func (c *ConsulClient) isKVEqual(kv1 *consulapi.KVPair, kv2 *consulapi.KVPair) bool { |
| 417 | if (kv1 == nil) && (kv2 == nil) { |
| 418 | return true |
| 419 | } else if (kv1 == nil) || (kv2 == nil) { |
| 420 | return false |
| 421 | } |
| 422 | // Both the KV should be non-null here |
| 423 | if kv1.Key != kv2.Key || |
| 424 | !bytes.Equal(kv1.Value, kv2.Value) || |
| 425 | kv1.Session != kv2.Session || |
| 426 | kv1.LockIndex != kv2.LockIndex || |
| 427 | kv1.ModifyIndex != kv2.ModifyIndex { |
| 428 | return false |
| 429 | } |
| 430 | return true |
| 431 | } |
| 432 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 433 | func (c *ConsulClient) listenForKeyChange(watchContext context.Context, key string, ch chan *Event) { |
| 434 | logger.Debugw("start-watching-channel", log.Fields{"key": key, "channel": ch}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 435 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 436 | defer c.CloseWatch(key, ch) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 437 | kv := c.consul.KV() |
| 438 | var queryOptions consulapi.QueryOptions |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 439 | queryOptions.WaitTime = defaultKVGetTimeout |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 440 | |
| 441 | // Get the existing value, if any |
| 442 | previousKVPair, meta, err := kv.Get(key, &queryOptions) |
| 443 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 444 | logger.Debug(err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 445 | } |
| 446 | lastIndex := meta.LastIndex |
| 447 | |
| 448 | // Wait for change. Push any change onto the channel and keep waiting for new update |
| 449 | //var waitOptions consulapi.QueryOptions |
| 450 | var pair *consulapi.KVPair |
| 451 | //watchContext, _ := context.WithCancel(context.Background()) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 452 | waitOptions := queryOptions.WithContext(watchContext) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 453 | for { |
| 454 | //waitOptions = consulapi.QueryOptions{WaitIndex: lastIndex} |
| 455 | waitOptions.WaitIndex = lastIndex |
| 456 | pair, meta, err = kv.Get(key, waitOptions) |
| 457 | select { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 458 | case <-watchContext.Done(): |
| 459 | logger.Debug("done-event-received-exiting") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 460 | return |
| 461 | default: |
| 462 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 463 | logger.Warnw("error-from-watch", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 464 | ch <- NewEvent(CONNECTIONDOWN, key, []byte(""), -1) |
| 465 | } else { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 466 | logger.Debugw("index-state", log.Fields{"lastindex": lastIndex, "newindex": meta.LastIndex, "key": key}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 470 | logger.Debug(err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 471 | // On error, block for 10 milliseconds to prevent endless loop |
| 472 | time.Sleep(10 * time.Millisecond) |
| 473 | } else if meta.LastIndex <= lastIndex { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 474 | logger.Info("no-index-change-or-negative") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 475 | } else { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 476 | logger.Debugw("update-received", log.Fields{"pair": pair}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 477 | if pair == nil { |
| 478 | ch <- NewEvent(DELETE, key, []byte(""), -1) |
| 479 | } else if !c.isKVEqual(pair, previousKVPair) { |
| 480 | // Push the change onto the channel if the data has changed |
| 481 | // For now just assume it's a PUT change |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 482 | logger.Debugw("pair-details", log.Fields{"session": pair.Session, "key": pair.Key, "value": pair.Value}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 483 | ch <- NewEvent(PUT, pair.Key, pair.Value, -1) |
| 484 | } |
| 485 | previousKVPair = pair |
| 486 | lastIndex = meta.LastIndex |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // Close closes the KV store client |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 492 | func (c *ConsulClient) Close() { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 493 | var writeOptions consulapi.WriteOptions |
| 494 | // Inform any goroutine it's time to say goodbye. |
| 495 | c.writeLock.Lock() |
| 496 | defer c.writeLock.Unlock() |
| 497 | if c.doneCh != nil { |
| 498 | close(*c.doneCh) |
| 499 | } |
| 500 | |
| 501 | // Clear the sessionID |
| 502 | if _, err := c.consul.Session().Destroy(c.sessionID, &writeOptions); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 503 | logger.Errorw("error-closing-client", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 507 | func (c *ConsulClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 508 | return nil |
| 509 | } |
| 510 | |
| 511 | func (c *ConsulClient) ReleaseLock(lockName string) error { |
| 512 | return nil |
| 513 | } |