blob: e8bc92c05ecbbc913a998cc1d435930d6878c8ea [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
khenaidoocfee5f42018-07-19 22:47:38 -040016package kvstore
17
18import (
khenaidoocfee5f42018-07-19 22:47:38 -040019 "context"
20 "errors"
khenaidoocfee5f42018-07-19 22:47:38 -040021 "fmt"
Scott Baker807addd2019-10-24 15:16:21 -070022 "github.com/opencord/voltha-lib-go/v2/pkg/log"
khenaidoob9203542018-09-17 22:56:37 -040023 v3Client "go.etcd.io/etcd/clientv3"
Stephane Barbarie260a5632019-02-26 16:12:49 -050024 v3Concurrency "go.etcd.io/etcd/clientv3/concurrency"
khenaidoob9203542018-09-17 22:56:37 -040025 v3rpcTypes "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
khenaidoo5c11af72018-07-20 17:21:05 -040026 "sync"
khenaidoocfee5f42018-07-19 22:47:38 -040027)
28
29// EtcdClient represents the Etcd KV store client
30type EtcdClient struct {
Stephane Barbariec53a2752019-03-08 17:50:10 -050031 ectdAPI *v3Client.Client
32 leaderRev v3Client.Client
33 keyReservations map[string]*v3Client.LeaseID
34 watchedChannels sync.Map
35 writeLock sync.Mutex
36 lockToMutexMap map[string]*v3Concurrency.Mutex
khenaidoobdcb8e02019-03-06 16:28:56 -050037 lockToSessionMap map[string]*v3Concurrency.Session
Stephane Barbariec53a2752019-03-08 17:50:10 -050038 lockToMutexLock sync.Mutex
khenaidoocfee5f42018-07-19 22:47:38 -040039}
40
41// NewEtcdClient returns a new client for the Etcd KV store
42func NewEtcdClient(addr string, timeout int) (*EtcdClient, error) {
khenaidoocfee5f42018-07-19 22:47:38 -040043 duration := GetDuration(timeout)
44
45 c, err := v3Client.New(v3Client.Config{
46 Endpoints: []string{addr},
47 DialTimeout: duration,
48 })
49 if err != nil {
50 log.Error(err)
51 return nil, err
52 }
Stephane Barbariec53a2752019-03-08 17:50:10 -050053
khenaidoocfee5f42018-07-19 22:47:38 -040054 reservations := make(map[string]*v3Client.LeaseID)
khenaidoobdcb8e02019-03-06 16:28:56 -050055 lockMutexMap := make(map[string]*v3Concurrency.Mutex)
56 lockSessionMap := make(map[string]*v3Concurrency.Session)
Stephane Barbarie260a5632019-02-26 16:12:49 -050057
Stephane Barbariec53a2752019-03-08 17:50:10 -050058 return &EtcdClient{ectdAPI: c, keyReservations: reservations, lockToMutexMap: lockMutexMap,
59 lockToSessionMap: lockSessionMap}, nil
khenaidoocfee5f42018-07-19 22:47:38 -040060}
61
khenaidoob3244212019-08-27 14:32:27 -040062// IsConnectionUp returns whether the connection to the Etcd KV store is up. If a timeout occurs then
63// it is assumed the connection is down or unreachable.
64func (c *EtcdClient) IsConnectionUp(timeout int) bool {
65 // Let's try to get a non existent key. If the connection is up then there will be no error returned.
66 if _, err := c.Get("non-existent-key", timeout); err != nil {
67 return false
68 }
69 return true
70}
71
khenaidoocfee5f42018-07-19 22:47:38 -040072// List returns an array of key-value pairs with key as a prefix. Timeout defines how long the function will
73// wait for a response
sbarbari17d7e222019-11-05 10:02:29 -050074func (c *EtcdClient) List(key string, timeout int) (map[string]*KVPair, error) {
khenaidoocfee5f42018-07-19 22:47:38 -040075 duration := GetDuration(timeout)
76
77 ctx, cancel := context.WithTimeout(context.Background(), duration)
Stephane Barbarie260a5632019-02-26 16:12:49 -050078
khenaidoocfee5f42018-07-19 22:47:38 -040079 resp, err := c.ectdAPI.Get(ctx, key, v3Client.WithPrefix())
80 cancel()
81 if err != nil {
82 log.Error(err)
83 return nil, err
84 }
85 m := make(map[string]*KVPair)
86 for _, ev := range resp.Kvs {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040087 m[string(ev.Key)] = NewKVPair(string(ev.Key), ev.Value, "", ev.Lease, ev.Version)
khenaidoocfee5f42018-07-19 22:47:38 -040088 }
89 return m, nil
90}
91
92// Get returns a key-value pair for a given key. Timeout defines how long the function will
93// wait for a response
sbarbari17d7e222019-11-05 10:02:29 -050094func (c *EtcdClient) Get(key string, timeout int) (*KVPair, error) {
khenaidoocfee5f42018-07-19 22:47:38 -040095 duration := GetDuration(timeout)
96
97 ctx, cancel := context.WithTimeout(context.Background(), duration)
Stephane Barbarie260a5632019-02-26 16:12:49 -050098
khenaidoocfee5f42018-07-19 22:47:38 -040099 resp, err := c.ectdAPI.Get(ctx, key)
100 cancel()
101 if err != nil {
102 log.Error(err)
103 return nil, err
104 }
105 for _, ev := range resp.Kvs {
106 // Only one value is returned
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400107 return NewKVPair(string(ev.Key), ev.Value, "", ev.Lease, ev.Version), nil
khenaidoocfee5f42018-07-19 22:47:38 -0400108 }
109 return nil, nil
110}
111
112// Put writes a key-value pair to the KV store. Value can only be a string or []byte since the etcd API
113// accepts only a string as a value for a put operation. Timeout defines how long the function will
114// wait for a response
sbarbari17d7e222019-11-05 10:02:29 -0500115func (c *EtcdClient) Put(key string, value interface{}, timeout int) error {
khenaidoocfee5f42018-07-19 22:47:38 -0400116
117 // Validate that we can convert value to a string as etcd API expects a string
118 var val string
119 var er error
120 if val, er = ToString(value); er != nil {
121 return fmt.Errorf("unexpected-type-%T", value)
122 }
123
124 duration := GetDuration(timeout)
125
126 ctx, cancel := context.WithTimeout(context.Background(), duration)
Stephane Barbarie260a5632019-02-26 16:12:49 -0500127
khenaidoocfee5f42018-07-19 22:47:38 -0400128 c.writeLock.Lock()
129 defer c.writeLock.Unlock()
khenaidoo09771ef2019-10-11 14:25:02 -0400130
131 var err error
132 // Check if there is already a lease for this key - if there is then use it, otherwise a PUT will make
133 // that KV key permanent instead of automatically removing it after a lease expiration
134 if leaseID, ok := c.keyReservations[key]; ok {
135 _, err = c.ectdAPI.Put(ctx, key, val, v3Client.WithLease(*leaseID))
136 } else {
137 _, err = c.ectdAPI.Put(ctx, key, val)
138 }
khenaidoocfee5f42018-07-19 22:47:38 -0400139 cancel()
140 if err != nil {
141 switch err {
142 case context.Canceled:
khenaidoo5c11af72018-07-20 17:21:05 -0400143 log.Warnw("context-cancelled", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400144 case context.DeadlineExceeded:
khenaidoo5c11af72018-07-20 17:21:05 -0400145 log.Warnw("context-deadline-exceeded", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400146 case v3rpcTypes.ErrEmptyKey:
khenaidoo5c11af72018-07-20 17:21:05 -0400147 log.Warnw("etcd-client-error", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400148 default:
khenaidoo5c11af72018-07-20 17:21:05 -0400149 log.Warnw("bad-endpoints", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400150 }
151 return err
152 }
153 return nil
154}
155
156// Delete removes a key from the KV store. Timeout defines how long the function will
157// wait for a response
sbarbari17d7e222019-11-05 10:02:29 -0500158func (c *EtcdClient) Delete(key string, timeout int) error {
khenaidoocfee5f42018-07-19 22:47:38 -0400159
160 duration := GetDuration(timeout)
161
162 ctx, cancel := context.WithTimeout(context.Background(), duration)
Stephane Barbarie260a5632019-02-26 16:12:49 -0500163
khenaidoocfee5f42018-07-19 22:47:38 -0400164 defer cancel()
165
166 c.writeLock.Lock()
167 defer c.writeLock.Unlock()
168
khenaidoo09771ef2019-10-11 14:25:02 -0400169 // delete the key
170 if _, err := c.ectdAPI.Delete(ctx, key); err != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400171 log.Errorw("failed-to-delete-key", log.Fields{"key": key, "error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400172 return err
173 }
khenaidoo1ce37ad2019-03-24 22:07:24 -0400174 log.Debugw("key(s)-deleted", log.Fields{"key": key})
khenaidoocfee5f42018-07-19 22:47:38 -0400175 return nil
176}
177
178// Reserve is invoked to acquire a key and set it to a given value. Value can only be a string or []byte since
179// the etcd API accepts only a string. Timeout defines how long the function will wait for a response. TTL
180// defines how long that reservation is valid. When TTL expires the key is unreserved by the KV store itself.
181// If the key is acquired then the value returned will be the value passed in. If the key is already acquired
182// then the value assigned to that key will be returned.
183func (c *EtcdClient) Reserve(key string, value interface{}, ttl int64) (interface{}, error) {
184 // Validate that we can convert value to a string as etcd API expects a string
185 var val string
186 var er error
187 if val, er = ToString(value); er != nil {
188 return nil, fmt.Errorf("unexpected-type%T", value)
189 }
190
191 // Create a lease
192 resp, err := c.ectdAPI.Grant(context.Background(), ttl)
193 if err != nil {
194 log.Error(err)
195 return nil, err
196 }
197 // Register the lease id
198 c.writeLock.Lock()
199 c.keyReservations[key] = &resp.ID
200 c.writeLock.Unlock()
201
202 // Revoke lease if reservation is not successful
203 reservationSuccessful := false
204 defer func() {
205 if !reservationSuccessful {
206 if err = c.ReleaseReservation(key); err != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400207 log.Error("cannot-release-lease")
khenaidoocfee5f42018-07-19 22:47:38 -0400208 }
209 }
210 }()
211
212 // Try to grap the Key with the above lease
213 c.ectdAPI.Txn(context.Background())
214 txn := c.ectdAPI.Txn(context.Background())
215 txn = txn.If(v3Client.Compare(v3Client.Version(key), "=", 0))
216 txn = txn.Then(v3Client.OpPut(key, val, v3Client.WithLease(resp.ID)))
217 txn = txn.Else(v3Client.OpGet(key))
218 result, er := txn.Commit()
219 if er != nil {
220 return nil, er
221 }
222
223 if !result.Succeeded {
224 // Verify whether we are already the owner of that Key
225 if len(result.Responses) > 0 &&
226 len(result.Responses[0].GetResponseRange().Kvs) > 0 {
227 kv := result.Responses[0].GetResponseRange().Kvs[0]
228 if string(kv.Value) == val {
229 reservationSuccessful = true
230 return value, nil
231 }
232 return kv.Value, nil
233 }
234 } else {
235 // Read the Key to ensure this is our Key
sbarbari17d7e222019-11-05 10:02:29 -0500236 m, err := c.Get(key, defaultKVGetTimeout)
khenaidoocfee5f42018-07-19 22:47:38 -0400237 if err != nil {
238 return nil, err
239 }
240 if m != nil {
241 if m.Key == key && isEqual(m.Value, value) {
242 // My reservation is successful - register it. For now, support is only for 1 reservation per key
243 // per session.
244 reservationSuccessful = true
245 return value, nil
246 }
247 // My reservation has failed. Return the owner of that key
248 return m.Value, nil
249 }
250 }
251 return nil, nil
252}
253
254// ReleaseAllReservations releases all key reservations previously made (using Reserve API)
255func (c *EtcdClient) ReleaseAllReservations() error {
256 c.writeLock.Lock()
257 defer c.writeLock.Unlock()
258 for key, leaseID := range c.keyReservations {
259 _, err := c.ectdAPI.Revoke(context.Background(), *leaseID)
260 if err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400261 log.Errorw("cannot-release-reservation", log.Fields{"key": key, "error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400262 return err
263 }
264 delete(c.keyReservations, key)
265 }
266 return nil
267}
268
269// ReleaseReservation releases reservation for a specific key.
270func (c *EtcdClient) ReleaseReservation(key string) error {
271 // Get the leaseid using the key
khenaidoo2c6a0992019-04-29 13:46:56 -0400272 log.Debugw("Release-reservation", log.Fields{"key": key})
khenaidoocfee5f42018-07-19 22:47:38 -0400273 var ok bool
274 var leaseID *v3Client.LeaseID
275 c.writeLock.Lock()
276 defer c.writeLock.Unlock()
277 if leaseID, ok = c.keyReservations[key]; !ok {
khenaidoofc1314d2019-03-14 09:34:21 -0400278 return nil
khenaidoocfee5f42018-07-19 22:47:38 -0400279 }
280 if leaseID != nil {
281 _, err := c.ectdAPI.Revoke(context.Background(), *leaseID)
282 if err != nil {
283 log.Error(err)
284 return err
285 }
286 delete(c.keyReservations, key)
287 }
288 return nil
289}
290
291// RenewReservation renews a reservation. A reservation will go stale after the specified TTL (Time To Live)
292// period specified when reserving the key
293func (c *EtcdClient) RenewReservation(key string) error {
294 // Get the leaseid using the key
295 var ok bool
296 var leaseID *v3Client.LeaseID
297 c.writeLock.Lock()
298 defer c.writeLock.Unlock()
299 if leaseID, ok = c.keyReservations[key]; !ok {
300 return errors.New("key-not-reserved")
301 }
302
303 if leaseID != nil {
304 _, err := c.ectdAPI.KeepAliveOnce(context.Background(), *leaseID)
305 if err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400306 log.Errorw("lease-may-have-expired", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400307 return err
308 }
309 } else {
310 return errors.New("lease-expired")
311 }
312 return nil
313}
314
315// Watch provides the watch capability on a given key. It returns a channel onto which the callee needs to
316// listen to receive Events.
317func (c *EtcdClient) Watch(key string) chan *Event {
318 w := v3Client.NewWatcher(c.ectdAPI)
A R Karthick43ba1fb2019-10-03 16:24:21 +0000319 ctx, cancel := context.WithCancel(context.Background())
khenaidoo09771ef2019-10-11 14:25:02 -0400320 channel := w.Watch(ctx, key)
khenaidoocfee5f42018-07-19 22:47:38 -0400321
322 // Create a new channel
323 ch := make(chan *Event, maxClientChannelBufferSize)
324
325 // Keep track of the created channels so they can be closed when required
326 channelMap := make(map[chan *Event]v3Client.Watcher)
327 channelMap[ch] = w
khenaidoocfee5f42018-07-19 22:47:38 -0400328
Stephane Barbariec53a2752019-03-08 17:50:10 -0500329 channelMaps := c.addChannelMap(key, channelMap)
330
khenaidooba6b6c42019-08-02 09:11:56 -0400331 // Changing the log field (from channelMaps) as the underlying logger cannot format the map of channels into a
332 // json format.
333 log.Debugw("watched-channels", log.Fields{"len": len(channelMaps)})
khenaidoocfee5f42018-07-19 22:47:38 -0400334 // Launch a go routine to listen for updates
A R Karthick43ba1fb2019-10-03 16:24:21 +0000335 go c.listenForKeyChange(channel, ch, cancel)
khenaidoocfee5f42018-07-19 22:47:38 -0400336
337 return ch
338
339}
340
Stephane Barbariec53a2752019-03-08 17:50:10 -0500341func (c *EtcdClient) addChannelMap(key string, channelMap map[chan *Event]v3Client.Watcher) []map[chan *Event]v3Client.Watcher {
342 var channels interface{}
343 var exists bool
344
345 if channels, exists = c.watchedChannels.Load(key); exists {
346 channels = append(channels.([]map[chan *Event]v3Client.Watcher), channelMap)
347 } else {
348 channels = []map[chan *Event]v3Client.Watcher{channelMap}
349 }
350 c.watchedChannels.Store(key, channels)
351
352 return channels.([]map[chan *Event]v3Client.Watcher)
353}
354
355func (c *EtcdClient) removeChannelMap(key string, pos int) []map[chan *Event]v3Client.Watcher {
356 var channels interface{}
357 var exists bool
358
359 if channels, exists = c.watchedChannels.Load(key); exists {
360 channels = append(channels.([]map[chan *Event]v3Client.Watcher)[:pos], channels.([]map[chan *Event]v3Client.Watcher)[pos+1:]...)
361 c.watchedChannels.Store(key, channels)
362 }
363
364 return channels.([]map[chan *Event]v3Client.Watcher)
365}
366
367func (c *EtcdClient) getChannelMaps(key string) ([]map[chan *Event]v3Client.Watcher, bool) {
368 var channels interface{}
369 var exists bool
370
371 channels, exists = c.watchedChannels.Load(key)
372
khenaidoodaefa372019-03-15 14:04:25 -0400373 if channels == nil {
374 return nil, exists
375 }
376
Stephane Barbariec53a2752019-03-08 17:50:10 -0500377 return channels.([]map[chan *Event]v3Client.Watcher), exists
378}
379
khenaidoocfee5f42018-07-19 22:47:38 -0400380// CloseWatch closes a specific watch. Both the key and the channel are required when closing a watch as there
381// may be multiple listeners on the same key. The previously created channel serves as a key
382func (c *EtcdClient) CloseWatch(key string, ch chan *Event) {
383 // Get the array of channels mapping
384 var watchedChannels []map[chan *Event]v3Client.Watcher
385 var ok bool
386 c.writeLock.Lock()
387 defer c.writeLock.Unlock()
388
Stephane Barbariec53a2752019-03-08 17:50:10 -0500389 if watchedChannels, ok = c.getChannelMaps(key); !ok {
khenaidoo5c11af72018-07-20 17:21:05 -0400390 log.Warnw("key-has-no-watched-channels", log.Fields{"key": key})
khenaidoocfee5f42018-07-19 22:47:38 -0400391 return
392 }
393 // Look for the channels
394 var pos = -1
395 for i, chMap := range watchedChannels {
396 if t, ok := chMap[ch]; ok {
397 log.Debug("channel-found")
398 // Close the etcd watcher before the client channel. This should close the etcd channel as well
399 if err := t.Close(); err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400400 log.Errorw("watcher-cannot-be-closed", log.Fields{"key": key, "error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400401 }
khenaidoocfee5f42018-07-19 22:47:38 -0400402 pos = i
403 break
404 }
405 }
Stephane Barbariec53a2752019-03-08 17:50:10 -0500406
407 channelMaps, _ := c.getChannelMaps(key)
khenaidoocfee5f42018-07-19 22:47:38 -0400408 // Remove that entry if present
409 if pos >= 0 {
Stephane Barbariec53a2752019-03-08 17:50:10 -0500410 channelMaps = c.removeChannelMap(key, pos)
khenaidoocfee5f42018-07-19 22:47:38 -0400411 }
Stephane Barbariec53a2752019-03-08 17:50:10 -0500412 log.Infow("watcher-channel-exiting", log.Fields{"key": key, "channel": channelMaps})
khenaidoocfee5f42018-07-19 22:47:38 -0400413}
414
A R Karthick43ba1fb2019-10-03 16:24:21 +0000415func (c *EtcdClient) listenForKeyChange(channel v3Client.WatchChan, ch chan<- *Event, cancel context.CancelFunc) {
khenaidoo8f474192019-04-03 17:20:44 -0400416 log.Debug("start-listening-on-channel ...")
A R Karthick43ba1fb2019-10-03 16:24:21 +0000417 defer cancel()
A R Karthickcbae6232019-10-03 21:37:41 +0000418 defer close(ch)
khenaidoocfee5f42018-07-19 22:47:38 -0400419 for resp := range channel {
420 for _, ev := range resp.Events {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400421 ch <- NewEvent(getEventType(ev), ev.Kv.Key, ev.Kv.Value, ev.Kv.Version)
khenaidoocfee5f42018-07-19 22:47:38 -0400422 }
423 }
khenaidoo8f474192019-04-03 17:20:44 -0400424 log.Debug("stop-listening-on-channel ...")
khenaidoocfee5f42018-07-19 22:47:38 -0400425}
426
427func getEventType(event *v3Client.Event) int {
428 switch event.Type {
429 case v3Client.EventTypePut:
430 return PUT
431 case v3Client.EventTypeDelete:
432 return DELETE
433 }
434 return UNKNOWN
435}
436
437// Close closes the KV store client
438func (c *EtcdClient) Close() {
439 c.writeLock.Lock()
440 defer c.writeLock.Unlock()
441 if err := c.ectdAPI.Close(); err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400442 log.Errorw("error-closing-client", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400443 }
444}
khenaidoobdcb8e02019-03-06 16:28:56 -0500445
446func (c *EtcdClient) addLockName(lockName string, lock *v3Concurrency.Mutex, session *v3Concurrency.Session) {
447 c.lockToMutexLock.Lock()
448 defer c.lockToMutexLock.Unlock()
449 c.lockToMutexMap[lockName] = lock
450 c.lockToSessionMap[lockName] = session
451}
452
453func (c *EtcdClient) deleteLockName(lockName string) {
454 c.lockToMutexLock.Lock()
455 defer c.lockToMutexLock.Unlock()
456 delete(c.lockToMutexMap, lockName)
457 delete(c.lockToSessionMap, lockName)
458}
459
460func (c *EtcdClient) getLock(lockName string) (*v3Concurrency.Mutex, *v3Concurrency.Session) {
461 c.lockToMutexLock.Lock()
462 defer c.lockToMutexLock.Unlock()
463 var lock *v3Concurrency.Mutex
464 var session *v3Concurrency.Session
465 if l, exist := c.lockToMutexMap[lockName]; exist {
466 lock = l
467 }
468 if s, exist := c.lockToSessionMap[lockName]; exist {
469 session = s
470 }
471 return lock, session
472}
473
Stephane Barbariec53a2752019-03-08 17:50:10 -0500474func (c *EtcdClient) AcquireLock(lockName string, timeout int) error {
khenaidoobdcb8e02019-03-06 16:28:56 -0500475 duration := GetDuration(timeout)
476 ctx, cancel := context.WithTimeout(context.Background(), duration)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400477 defer cancel()
khenaidoobdcb8e02019-03-06 16:28:56 -0500478 session, _ := v3Concurrency.NewSession(c.ectdAPI, v3Concurrency.WithContext(ctx))
Stephane Barbariec53a2752019-03-08 17:50:10 -0500479 mu := v3Concurrency.NewMutex(session, "/devicelock_"+lockName)
khenaidoobdcb8e02019-03-06 16:28:56 -0500480 if err := mu.Lock(context.Background()); err != nil {
khenaidoo2c6a0992019-04-29 13:46:56 -0400481 cancel()
khenaidoobdcb8e02019-03-06 16:28:56 -0500482 return err
483 }
484 c.addLockName(lockName, mu, session)
khenaidoobdcb8e02019-03-06 16:28:56 -0500485 return nil
486}
487
Stephane Barbariec53a2752019-03-08 17:50:10 -0500488func (c *EtcdClient) ReleaseLock(lockName string) error {
khenaidoobdcb8e02019-03-06 16:28:56 -0500489 lock, session := c.getLock(lockName)
490 var err error
491 if lock != nil {
492 if e := lock.Unlock(context.Background()); e != nil {
493 err = e
494 }
495 }
496 if session != nil {
497 if e := session.Close(); e != nil {
498 err = e
499 }
500 }
501 c.deleteLockName(lockName)
502
503 return err
Stephane Barbariec53a2752019-03-08 17:50:10 -0500504}