blob: 639ea752c53be9b09179985f6202fbd3b3c99807 [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"
Stephane Barbarie260a5632019-02-26 16:12:49 -050022 "github.com/opencord/voltha-go/common/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
62// List returns an array of key-value pairs with key as a prefix. Timeout defines how long the function will
63// wait for a response
Stephane Barbarie260a5632019-02-26 16:12:49 -050064func (c *EtcdClient) List(key string, timeout int, lock ...bool) (map[string]*KVPair, error) {
khenaidoocfee5f42018-07-19 22:47:38 -040065 duration := GetDuration(timeout)
66
67 ctx, cancel := context.WithTimeout(context.Background(), duration)
Stephane Barbarie260a5632019-02-26 16:12:49 -050068
69 // DO NOT lock by default; otherwise lock per instructed value
70 if len(lock) > 0 && lock[0] {
71 session, _ := v3Concurrency.NewSession(c.ectdAPI, v3Concurrency.WithContext(ctx))
Stephane Barbariec53a2752019-03-08 17:50:10 -050072 mu := v3Concurrency.NewMutex(session, "/lock"+key)
Stephane Barbarie260a5632019-02-26 16:12:49 -050073 mu.Lock(context.Background())
74 defer mu.Unlock(context.Background())
75 defer session.Close()
76 }
77
khenaidoocfee5f42018-07-19 22:47:38 -040078 resp, err := c.ectdAPI.Get(ctx, key, v3Client.WithPrefix())
79 cancel()
80 if err != nil {
81 log.Error(err)
82 return nil, err
83 }
84 m := make(map[string]*KVPair)
85 for _, ev := range resp.Kvs {
86 m[string(ev.Key)] = NewKVPair(string(ev.Key), ev.Value, "", ev.Lease)
87 }
88 return m, nil
89}
90
91// Get returns a key-value pair for a given key. Timeout defines how long the function will
92// wait for a response
Stephane Barbarie260a5632019-02-26 16:12:49 -050093func (c *EtcdClient) Get(key string, timeout int, lock ...bool) (*KVPair, error) {
khenaidoocfee5f42018-07-19 22:47:38 -040094 duration := GetDuration(timeout)
95
96 ctx, cancel := context.WithTimeout(context.Background(), duration)
Stephane Barbarie260a5632019-02-26 16:12:49 -050097
98 // Lock by default; otherwise lock per instructed value
Stephane Barbariec53a2752019-03-08 17:50:10 -050099 if len(lock) > 0 && lock[0] {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500100 session, _ := v3Concurrency.NewSession(c.ectdAPI, v3Concurrency.WithContext(ctx))
Stephane Barbariec53a2752019-03-08 17:50:10 -0500101 mu := v3Concurrency.NewMutex(session, "/lock"+key)
Stephane Barbarie260a5632019-02-26 16:12:49 -0500102 mu.Lock(context.Background())
103 defer mu.Unlock(context.Background())
104 defer session.Close()
105 }
106
khenaidoocfee5f42018-07-19 22:47:38 -0400107 resp, err := c.ectdAPI.Get(ctx, key)
108 cancel()
109 if err != nil {
110 log.Error(err)
111 return nil, err
112 }
113 for _, ev := range resp.Kvs {
114 // Only one value is returned
115 return NewKVPair(string(ev.Key), ev.Value, "", ev.Lease), nil
116 }
117 return nil, nil
118}
119
120// Put writes a key-value pair to the KV store. Value can only be a string or []byte since the etcd API
121// accepts only a string as a value for a put operation. Timeout defines how long the function will
122// wait for a response
Stephane Barbarie260a5632019-02-26 16:12:49 -0500123func (c *EtcdClient) Put(key string, value interface{}, timeout int, lock ...bool) error {
khenaidoocfee5f42018-07-19 22:47:38 -0400124
125 // Validate that we can convert value to a string as etcd API expects a string
126 var val string
127 var er error
128 if val, er = ToString(value); er != nil {
129 return fmt.Errorf("unexpected-type-%T", value)
130 }
131
132 duration := GetDuration(timeout)
133
134 ctx, cancel := context.WithTimeout(context.Background(), duration)
Stephane Barbarie260a5632019-02-26 16:12:49 -0500135
136 // Lock by default; otherwise lock per instructed value
137 if len(lock) == 0 || lock[0] {
138 session, _ := v3Concurrency.NewSession(c.ectdAPI, v3Concurrency.WithContext(ctx))
Stephane Barbariec53a2752019-03-08 17:50:10 -0500139 mu := v3Concurrency.NewMutex(session, "/lock"+key)
Stephane Barbarie260a5632019-02-26 16:12:49 -0500140 mu.Lock(context.Background())
141 defer mu.Unlock(context.Background())
142 defer session.Close()
143 }
144
khenaidoocfee5f42018-07-19 22:47:38 -0400145 c.writeLock.Lock()
146 defer c.writeLock.Unlock()
147 _, err := c.ectdAPI.Put(ctx, key, val)
148 cancel()
149 if err != nil {
150 switch err {
151 case context.Canceled:
khenaidoo5c11af72018-07-20 17:21:05 -0400152 log.Warnw("context-cancelled", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400153 case context.DeadlineExceeded:
khenaidoo5c11af72018-07-20 17:21:05 -0400154 log.Warnw("context-deadline-exceeded", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400155 case v3rpcTypes.ErrEmptyKey:
khenaidoo5c11af72018-07-20 17:21:05 -0400156 log.Warnw("etcd-client-error", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400157 default:
khenaidoo5c11af72018-07-20 17:21:05 -0400158 log.Warnw("bad-endpoints", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400159 }
160 return err
161 }
162 return nil
163}
164
165// Delete removes a key from the KV store. Timeout defines how long the function will
166// wait for a response
Stephane Barbarie260a5632019-02-26 16:12:49 -0500167func (c *EtcdClient) Delete(key string, timeout int, lock ...bool) error {
khenaidoocfee5f42018-07-19 22:47:38 -0400168
169 duration := GetDuration(timeout)
170
171 ctx, cancel := context.WithTimeout(context.Background(), duration)
Stephane Barbarie260a5632019-02-26 16:12:49 -0500172
173 // Lock by default; otherwise lock per instructed value
174 if len(lock) == 0 || lock[0] {
175 session, _ := v3Concurrency.NewSession(c.ectdAPI, v3Concurrency.WithContext(ctx))
Stephane Barbariec53a2752019-03-08 17:50:10 -0500176 mu := v3Concurrency.NewMutex(session, "/lock"+key)
Stephane Barbarie260a5632019-02-26 16:12:49 -0500177 mu.Lock(context.Background())
178 defer mu.Unlock(context.Background())
179 defer session.Close()
180 }
181
khenaidoocfee5f42018-07-19 22:47:38 -0400182 defer cancel()
183
184 c.writeLock.Lock()
185 defer c.writeLock.Unlock()
186
187 // count keys about to be deleted
188 gresp, err := c.ectdAPI.Get(ctx, key, v3Client.WithPrefix())
189 if err != nil {
190 log.Error(err)
191 return err
192 }
193
194 // delete the keys
195 dresp, err := c.ectdAPI.Delete(ctx, key, v3Client.WithPrefix())
196 if err != nil {
197 log.Error(err)
198 return err
199 }
200
201 if dresp == nil || gresp == nil {
202 log.Debug("nothing-to-delete")
203 return nil
204 }
205
khenaidoo5c11af72018-07-20 17:21:05 -0400206 log.Debugw("delete-keys", log.Fields{"all-keys-deleted": int64(len(gresp.Kvs)) == dresp.Deleted})
khenaidoocfee5f42018-07-19 22:47:38 -0400207 if int64(len(gresp.Kvs)) == dresp.Deleted {
208 log.Debug("All-keys-deleted")
209 } else {
210 log.Error("not-all-keys-deleted")
211 err := errors.New("not-all-keys-deleted")
212 return err
213 }
214 return nil
215}
216
217// Reserve is invoked to acquire a key and set it to a given value. Value can only be a string or []byte since
218// the etcd API accepts only a string. Timeout defines how long the function will wait for a response. TTL
219// defines how long that reservation is valid. When TTL expires the key is unreserved by the KV store itself.
220// If the key is acquired then the value returned will be the value passed in. If the key is already acquired
221// then the value assigned to that key will be returned.
222func (c *EtcdClient) Reserve(key string, value interface{}, ttl int64) (interface{}, error) {
223 // Validate that we can convert value to a string as etcd API expects a string
224 var val string
225 var er error
226 if val, er = ToString(value); er != nil {
227 return nil, fmt.Errorf("unexpected-type%T", value)
228 }
229
230 // Create a lease
231 resp, err := c.ectdAPI.Grant(context.Background(), ttl)
232 if err != nil {
233 log.Error(err)
234 return nil, err
235 }
236 // Register the lease id
237 c.writeLock.Lock()
238 c.keyReservations[key] = &resp.ID
239 c.writeLock.Unlock()
240
241 // Revoke lease if reservation is not successful
242 reservationSuccessful := false
243 defer func() {
244 if !reservationSuccessful {
245 if err = c.ReleaseReservation(key); err != nil {
246 log.Errorf("cannot-release-lease")
247 }
248 }
249 }()
250
251 // Try to grap the Key with the above lease
252 c.ectdAPI.Txn(context.Background())
253 txn := c.ectdAPI.Txn(context.Background())
254 txn = txn.If(v3Client.Compare(v3Client.Version(key), "=", 0))
255 txn = txn.Then(v3Client.OpPut(key, val, v3Client.WithLease(resp.ID)))
256 txn = txn.Else(v3Client.OpGet(key))
257 result, er := txn.Commit()
258 if er != nil {
259 return nil, er
260 }
261
262 if !result.Succeeded {
263 // Verify whether we are already the owner of that Key
264 if len(result.Responses) > 0 &&
265 len(result.Responses[0].GetResponseRange().Kvs) > 0 {
266 kv := result.Responses[0].GetResponseRange().Kvs[0]
267 if string(kv.Value) == val {
268 reservationSuccessful = true
269 return value, nil
270 }
271 return kv.Value, nil
272 }
273 } else {
274 // Read the Key to ensure this is our Key
Stephane Barbarie260a5632019-02-26 16:12:49 -0500275 m, err := c.Get(key, defaultKVGetTimeout, false)
khenaidoocfee5f42018-07-19 22:47:38 -0400276 if err != nil {
277 return nil, err
278 }
279 if m != nil {
280 if m.Key == key && isEqual(m.Value, value) {
281 // My reservation is successful - register it. For now, support is only for 1 reservation per key
282 // per session.
283 reservationSuccessful = true
284 return value, nil
285 }
286 // My reservation has failed. Return the owner of that key
287 return m.Value, nil
288 }
289 }
290 return nil, nil
291}
292
293// ReleaseAllReservations releases all key reservations previously made (using Reserve API)
294func (c *EtcdClient) ReleaseAllReservations() error {
295 c.writeLock.Lock()
296 defer c.writeLock.Unlock()
297 for key, leaseID := range c.keyReservations {
298 _, err := c.ectdAPI.Revoke(context.Background(), *leaseID)
299 if err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400300 log.Errorw("cannot-release-reservation", log.Fields{"key": key, "error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400301 return err
302 }
303 delete(c.keyReservations, key)
304 }
305 return nil
306}
307
308// ReleaseReservation releases reservation for a specific key.
309func (c *EtcdClient) ReleaseReservation(key string) error {
310 // Get the leaseid using the key
311 var ok bool
312 var leaseID *v3Client.LeaseID
313 c.writeLock.Lock()
314 defer c.writeLock.Unlock()
315 if leaseID, ok = c.keyReservations[key]; !ok {
316 return errors.New("key-not-reserved")
317 }
318 if leaseID != nil {
319 _, err := c.ectdAPI.Revoke(context.Background(), *leaseID)
320 if err != nil {
321 log.Error(err)
322 return err
323 }
324 delete(c.keyReservations, key)
325 }
326 return nil
327}
328
329// RenewReservation renews a reservation. A reservation will go stale after the specified TTL (Time To Live)
330// period specified when reserving the key
331func (c *EtcdClient) RenewReservation(key string) error {
332 // Get the leaseid using the key
333 var ok bool
334 var leaseID *v3Client.LeaseID
335 c.writeLock.Lock()
336 defer c.writeLock.Unlock()
337 if leaseID, ok = c.keyReservations[key]; !ok {
338 return errors.New("key-not-reserved")
339 }
340
341 if leaseID != nil {
342 _, err := c.ectdAPI.KeepAliveOnce(context.Background(), *leaseID)
343 if err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400344 log.Errorw("lease-may-have-expired", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400345 return err
346 }
347 } else {
348 return errors.New("lease-expired")
349 }
350 return nil
351}
352
353// Watch provides the watch capability on a given key. It returns a channel onto which the callee needs to
354// listen to receive Events.
355func (c *EtcdClient) Watch(key string) chan *Event {
356 w := v3Client.NewWatcher(c.ectdAPI)
357 channel := w.Watch(context.Background(), key, v3Client.WithPrefix())
358
359 // Create a new channel
360 ch := make(chan *Event, maxClientChannelBufferSize)
361
362 // Keep track of the created channels so they can be closed when required
363 channelMap := make(map[chan *Event]v3Client.Watcher)
364 channelMap[ch] = w
365 //c.writeLock.Lock()
366 //defer c.writeLock.Unlock()
khenaidoocfee5f42018-07-19 22:47:38 -0400367
Stephane Barbariec53a2752019-03-08 17:50:10 -0500368 channelMaps := c.addChannelMap(key, channelMap)
369
370 log.Debugw("watched-channels", log.Fields{"channels": channelMaps})
khenaidoocfee5f42018-07-19 22:47:38 -0400371 // Launch a go routine to listen for updates
372 go c.listenForKeyChange(channel, ch)
373
374 return ch
375
376}
377
Stephane Barbariec53a2752019-03-08 17:50:10 -0500378func (c *EtcdClient) addChannelMap(key string, channelMap map[chan *Event]v3Client.Watcher) []map[chan *Event]v3Client.Watcher {
379 var channels interface{}
380 var exists bool
381
382 if channels, exists = c.watchedChannels.Load(key); exists {
383 channels = append(channels.([]map[chan *Event]v3Client.Watcher), channelMap)
384 } else {
385 channels = []map[chan *Event]v3Client.Watcher{channelMap}
386 }
387 c.watchedChannels.Store(key, channels)
388
389 return channels.([]map[chan *Event]v3Client.Watcher)
390}
391
392func (c *EtcdClient) removeChannelMap(key string, pos int) []map[chan *Event]v3Client.Watcher {
393 var channels interface{}
394 var exists bool
395
396 if channels, exists = c.watchedChannels.Load(key); exists {
397 channels = append(channels.([]map[chan *Event]v3Client.Watcher)[:pos], channels.([]map[chan *Event]v3Client.Watcher)[pos+1:]...)
398 c.watchedChannels.Store(key, channels)
399 }
400
401 return channels.([]map[chan *Event]v3Client.Watcher)
402}
403
404func (c *EtcdClient) getChannelMaps(key string) ([]map[chan *Event]v3Client.Watcher, bool) {
405 var channels interface{}
406 var exists bool
407
408 channels, exists = c.watchedChannels.Load(key)
409
410 return channels.([]map[chan *Event]v3Client.Watcher), exists
411}
412
khenaidoocfee5f42018-07-19 22:47:38 -0400413// CloseWatch closes a specific watch. Both the key and the channel are required when closing a watch as there
414// may be multiple listeners on the same key. The previously created channel serves as a key
415func (c *EtcdClient) CloseWatch(key string, ch chan *Event) {
416 // Get the array of channels mapping
417 var watchedChannels []map[chan *Event]v3Client.Watcher
418 var ok bool
419 c.writeLock.Lock()
420 defer c.writeLock.Unlock()
421
Stephane Barbariec53a2752019-03-08 17:50:10 -0500422 if watchedChannels, ok = c.getChannelMaps(key); !ok {
khenaidoo5c11af72018-07-20 17:21:05 -0400423 log.Warnw("key-has-no-watched-channels", log.Fields{"key": key})
khenaidoocfee5f42018-07-19 22:47:38 -0400424 return
425 }
426 // Look for the channels
427 var pos = -1
428 for i, chMap := range watchedChannels {
429 if t, ok := chMap[ch]; ok {
430 log.Debug("channel-found")
431 // Close the etcd watcher before the client channel. This should close the etcd channel as well
432 if err := t.Close(); err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400433 log.Errorw("watcher-cannot-be-closed", log.Fields{"key": key, "error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400434 }
435 close(ch)
436 pos = i
437 break
438 }
439 }
Stephane Barbariec53a2752019-03-08 17:50:10 -0500440
441 channelMaps, _ := c.getChannelMaps(key)
khenaidoocfee5f42018-07-19 22:47:38 -0400442 // Remove that entry if present
443 if pos >= 0 {
Stephane Barbariec53a2752019-03-08 17:50:10 -0500444 channelMaps = c.removeChannelMap(key, pos)
khenaidoocfee5f42018-07-19 22:47:38 -0400445 }
Stephane Barbariec53a2752019-03-08 17:50:10 -0500446 log.Infow("watcher-channel-exiting", log.Fields{"key": key, "channel": channelMaps})
khenaidoocfee5f42018-07-19 22:47:38 -0400447}
448
449func (c *EtcdClient) listenForKeyChange(channel v3Client.WatchChan, ch chan<- *Event) {
khenaidoo5c11af72018-07-20 17:21:05 -0400450 log.Infow("start-listening-on-channel", log.Fields{"channel": ch})
khenaidoocfee5f42018-07-19 22:47:38 -0400451 for resp := range channel {
452 for _, ev := range resp.Events {
453 //log.Debugf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
454 ch <- NewEvent(getEventType(ev), ev.Kv.Key, ev.Kv.Value)
455 }
456 }
457 log.Info("stop-listening-on-channel")
458}
459
460func getEventType(event *v3Client.Event) int {
461 switch event.Type {
462 case v3Client.EventTypePut:
463 return PUT
464 case v3Client.EventTypeDelete:
465 return DELETE
466 }
467 return UNKNOWN
468}
469
470// Close closes the KV store client
471func (c *EtcdClient) Close() {
472 c.writeLock.Lock()
473 defer c.writeLock.Unlock()
474 if err := c.ectdAPI.Close(); err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400475 log.Errorw("error-closing-client", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400476 }
477}
khenaidoobdcb8e02019-03-06 16:28:56 -0500478
479func (c *EtcdClient) addLockName(lockName string, lock *v3Concurrency.Mutex, session *v3Concurrency.Session) {
480 c.lockToMutexLock.Lock()
481 defer c.lockToMutexLock.Unlock()
482 c.lockToMutexMap[lockName] = lock
483 c.lockToSessionMap[lockName] = session
484}
485
486func (c *EtcdClient) deleteLockName(lockName string) {
487 c.lockToMutexLock.Lock()
488 defer c.lockToMutexLock.Unlock()
489 delete(c.lockToMutexMap, lockName)
490 delete(c.lockToSessionMap, lockName)
491}
492
493func (c *EtcdClient) getLock(lockName string) (*v3Concurrency.Mutex, *v3Concurrency.Session) {
494 c.lockToMutexLock.Lock()
495 defer c.lockToMutexLock.Unlock()
496 var lock *v3Concurrency.Mutex
497 var session *v3Concurrency.Session
498 if l, exist := c.lockToMutexMap[lockName]; exist {
499 lock = l
500 }
501 if s, exist := c.lockToSessionMap[lockName]; exist {
502 session = s
503 }
504 return lock, session
505}
506
Stephane Barbariec53a2752019-03-08 17:50:10 -0500507func (c *EtcdClient) AcquireLock(lockName string, timeout int) error {
khenaidoobdcb8e02019-03-06 16:28:56 -0500508 duration := GetDuration(timeout)
509 ctx, cancel := context.WithTimeout(context.Background(), duration)
510 session, _ := v3Concurrency.NewSession(c.ectdAPI, v3Concurrency.WithContext(ctx))
Stephane Barbariec53a2752019-03-08 17:50:10 -0500511 mu := v3Concurrency.NewMutex(session, "/devicelock_"+lockName)
khenaidoobdcb8e02019-03-06 16:28:56 -0500512 if err := mu.Lock(context.Background()); err != nil {
513 return err
514 }
515 c.addLockName(lockName, mu, session)
516 cancel()
517 return nil
518}
519
Stephane Barbariec53a2752019-03-08 17:50:10 -0500520func (c *EtcdClient) ReleaseLock(lockName string) error {
khenaidoobdcb8e02019-03-06 16:28:56 -0500521 lock, session := c.getLock(lockName)
522 var err error
523 if lock != nil {
524 if e := lock.Unlock(context.Background()); e != nil {
525 err = e
526 }
527 }
528 if session != nil {
529 if e := session.Close(); e != nil {
530 err = e
531 }
532 }
533 c.deleteLockName(lockName)
534
535 return err
Stephane Barbariec53a2752019-03-08 17:50:10 -0500536}