blob: 2d126f7547afc99b841d803bba5a0e72e6aee38c [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -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 */
16package kvstore
17
18import (
19 "context"
20 "errors"
21 "fmt"
npujarec5762e2020-01-01 14:08:48 +053022 "sync"
23
Esin Karamanccb714b2019-11-29 15:02:06 +000024 "github.com/opencord/voltha-lib-go/v3/pkg/log"
William Kurkianea869482019-04-09 15:16:11 -040025 v3Client "go.etcd.io/etcd/clientv3"
26 v3Concurrency "go.etcd.io/etcd/clientv3/concurrency"
27 v3rpcTypes "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
William Kurkianea869482019-04-09 15:16:11 -040028)
29
30// EtcdClient represents the Etcd KV store client
31type EtcdClient struct {
32 ectdAPI *v3Client.Client
William Kurkianea869482019-04-09 15:16:11 -040033 keyReservations map[string]*v3Client.LeaseID
34 watchedChannels sync.Map
35 writeLock sync.Mutex
36 lockToMutexMap map[string]*v3Concurrency.Mutex
37 lockToSessionMap map[string]*v3Concurrency.Session
38 lockToMutexLock sync.Mutex
39}
40
41// NewEtcdClient returns a new client for the Etcd KV store
42func NewEtcdClient(addr string, timeout int) (*EtcdClient, error) {
43 duration := GetDuration(timeout)
44
45 c, err := v3Client.New(v3Client.Config{
46 Endpoints: []string{addr},
47 DialTimeout: duration,
48 })
49 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +000050 logger.Error(err)
William Kurkianea869482019-04-09 15:16:11 -040051 return nil, err
52 }
53
54 reservations := make(map[string]*v3Client.LeaseID)
55 lockMutexMap := make(map[string]*v3Concurrency.Mutex)
56 lockSessionMap := make(map[string]*v3Concurrency.Session)
57
58 return &EtcdClient{ectdAPI: c, keyReservations: reservations, lockToMutexMap: lockMutexMap,
59 lockToSessionMap: lockSessionMap}, nil
60}
61
Devmalya Paul495b94a2019-08-27 19:42:00 -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.
npujarec5762e2020-01-01 14:08:48 +053064func (c *EtcdClient) IsConnectionUp(ctx context.Context) bool {
Devmalya Paul495b94a2019-08-27 19:42:00 -040065 // Let's try to get a non existent key. If the connection is up then there will be no error returned.
npujarec5762e2020-01-01 14:08:48 +053066 if _, err := c.Get(ctx, "non-existent-key"); err != nil {
Devmalya Paul495b94a2019-08-27 19:42:00 -040067 return false
68 }
npujarec5762e2020-01-01 14:08:48 +053069 //cancel()
Devmalya Paul495b94a2019-08-27 19:42:00 -040070 return true
71}
72
William Kurkianea869482019-04-09 15:16:11 -040073// List returns an array of key-value pairs with key as a prefix. Timeout defines how long the function will
74// wait for a response
npujarec5762e2020-01-01 14:08:48 +053075func (c *EtcdClient) List(ctx context.Context, key string) (map[string]*KVPair, error) {
William Kurkianea869482019-04-09 15:16:11 -040076 resp, err := c.ectdAPI.Get(ctx, key, v3Client.WithPrefix())
William Kurkianea869482019-04-09 15:16:11 -040077 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +000078 logger.Error(err)
William Kurkianea869482019-04-09 15:16:11 -040079 return nil, err
80 }
81 m := make(map[string]*KVPair)
82 for _, ev := range resp.Kvs {
Manikkaraj kb1d51442019-07-23 10:41:02 -040083 m[string(ev.Key)] = NewKVPair(string(ev.Key), ev.Value, "", ev.Lease, ev.Version)
William Kurkianea869482019-04-09 15:16:11 -040084 }
85 return m, nil
86}
87
88// Get returns a key-value pair for a given key. Timeout defines how long the function will
89// wait for a response
npujarec5762e2020-01-01 14:08:48 +053090func (c *EtcdClient) Get(ctx context.Context, key string) (*KVPair, error) {
William Kurkianea869482019-04-09 15:16:11 -040091
William Kurkianea869482019-04-09 15:16:11 -040092 resp, err := c.ectdAPI.Get(ctx, key)
npujarec5762e2020-01-01 14:08:48 +053093
William Kurkianea869482019-04-09 15:16:11 -040094 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +000095 logger.Error(err)
William Kurkianea869482019-04-09 15:16:11 -040096 return nil, err
97 }
98 for _, ev := range resp.Kvs {
99 // Only one value is returned
Manikkaraj kb1d51442019-07-23 10:41:02 -0400100 return NewKVPair(string(ev.Key), ev.Value, "", ev.Lease, ev.Version), nil
William Kurkianea869482019-04-09 15:16:11 -0400101 }
102 return nil, nil
103}
104
105// Put writes a key-value pair to the KV store. Value can only be a string or []byte since the etcd API
106// accepts only a string as a value for a put operation. Timeout defines how long the function will
107// wait for a response
npujarec5762e2020-01-01 14:08:48 +0530108func (c *EtcdClient) Put(ctx context.Context, key string, value interface{}) error {
William Kurkianea869482019-04-09 15:16:11 -0400109
110 // Validate that we can convert value to a string as etcd API expects a string
111 var val string
112 var er error
113 if val, er = ToString(value); er != nil {
114 return fmt.Errorf("unexpected-type-%T", value)
115 }
116
William Kurkianea869482019-04-09 15:16:11 -0400117 c.writeLock.Lock()
118 defer c.writeLock.Unlock()
kdarapub26b4502019-10-05 03:02:33 +0530119
120 var err error
121 // Check if there is already a lease for this key - if there is then use it, otherwise a PUT will make
122 // that KV key permanent instead of automatically removing it after a lease expiration
123 if leaseID, ok := c.keyReservations[key]; ok {
124 _, err = c.ectdAPI.Put(ctx, key, val, v3Client.WithLease(*leaseID))
125 } else {
126 _, err = c.ectdAPI.Put(ctx, key, val)
127 }
npujarec5762e2020-01-01 14:08:48 +0530128
William Kurkianea869482019-04-09 15:16:11 -0400129 if err != nil {
130 switch err {
131 case context.Canceled:
Esin Karamanccb714b2019-11-29 15:02:06 +0000132 logger.Warnw("context-cancelled", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400133 case context.DeadlineExceeded:
Esin Karamanccb714b2019-11-29 15:02:06 +0000134 logger.Warnw("context-deadline-exceeded", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400135 case v3rpcTypes.ErrEmptyKey:
Esin Karamanccb714b2019-11-29 15:02:06 +0000136 logger.Warnw("etcd-client-error", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400137 default:
Esin Karamanccb714b2019-11-29 15:02:06 +0000138 logger.Warnw("bad-endpoints", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400139 }
140 return err
141 }
142 return nil
143}
144
145// Delete removes a key from the KV store. Timeout defines how long the function will
146// wait for a response
npujarec5762e2020-01-01 14:08:48 +0530147func (c *EtcdClient) Delete(ctx context.Context, key string) error {
William Kurkianea869482019-04-09 15:16:11 -0400148
149 c.writeLock.Lock()
150 defer c.writeLock.Unlock()
151
kdarapub26b4502019-10-05 03:02:33 +0530152 // delete the key
153 if _, err := c.ectdAPI.Delete(ctx, key); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000154 logger.Errorw("failed-to-delete-key", log.Fields{"key": key, "error": err})
William Kurkianea869482019-04-09 15:16:11 -0400155 return err
156 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000157 logger.Debugw("key(s)-deleted", log.Fields{"key": key})
William Kurkianea869482019-04-09 15:16:11 -0400158 return nil
159}
160
161// Reserve is invoked to acquire a key and set it to a given value. Value can only be a string or []byte since
162// the etcd API accepts only a string. Timeout defines how long the function will wait for a response. TTL
163// defines how long that reservation is valid. When TTL expires the key is unreserved by the KV store itself.
164// If the key is acquired then the value returned will be the value passed in. If the key is already acquired
165// then the value assigned to that key will be returned.
npujarec5762e2020-01-01 14:08:48 +0530166func (c *EtcdClient) Reserve(ctx context.Context, key string, value interface{}, ttl int64) (interface{}, error) {
William Kurkianea869482019-04-09 15:16:11 -0400167 // Validate that we can convert value to a string as etcd API expects a string
168 var val string
169 var er error
170 if val, er = ToString(value); er != nil {
171 return nil, fmt.Errorf("unexpected-type%T", value)
172 }
173
cbabu95f21522019-11-13 14:25:18 +0100174 resp, err := c.ectdAPI.Grant(ctx, ttl)
William Kurkianea869482019-04-09 15:16:11 -0400175 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000176 logger.Error(err)
William Kurkianea869482019-04-09 15:16:11 -0400177 return nil, err
178 }
179 // Register the lease id
180 c.writeLock.Lock()
181 c.keyReservations[key] = &resp.ID
182 c.writeLock.Unlock()
183
184 // Revoke lease if reservation is not successful
185 reservationSuccessful := false
186 defer func() {
187 if !reservationSuccessful {
npujarec5762e2020-01-01 14:08:48 +0530188 if err = c.ReleaseReservation(context.Background(), key); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000189 logger.Error("cannot-release-lease")
William Kurkianea869482019-04-09 15:16:11 -0400190 }
191 }
192 }()
193
194 // Try to grap the Key with the above lease
195 c.ectdAPI.Txn(context.Background())
196 txn := c.ectdAPI.Txn(context.Background())
197 txn = txn.If(v3Client.Compare(v3Client.Version(key), "=", 0))
198 txn = txn.Then(v3Client.OpPut(key, val, v3Client.WithLease(resp.ID)))
199 txn = txn.Else(v3Client.OpGet(key))
200 result, er := txn.Commit()
201 if er != nil {
202 return nil, er
203 }
204
205 if !result.Succeeded {
206 // Verify whether we are already the owner of that Key
207 if len(result.Responses) > 0 &&
208 len(result.Responses[0].GetResponseRange().Kvs) > 0 {
209 kv := result.Responses[0].GetResponseRange().Kvs[0]
210 if string(kv.Value) == val {
211 reservationSuccessful = true
212 return value, nil
213 }
214 return kv.Value, nil
215 }
216 } else {
217 // Read the Key to ensure this is our Key
npujarec5762e2020-01-01 14:08:48 +0530218 m, err := c.Get(ctx, key)
William Kurkianea869482019-04-09 15:16:11 -0400219 if err != nil {
220 return nil, err
221 }
222 if m != nil {
223 if m.Key == key && isEqual(m.Value, value) {
224 // My reservation is successful - register it. For now, support is only for 1 reservation per key
225 // per session.
226 reservationSuccessful = true
227 return value, nil
228 }
229 // My reservation has failed. Return the owner of that key
230 return m.Value, nil
231 }
232 }
233 return nil, nil
234}
235
236// ReleaseAllReservations releases all key reservations previously made (using Reserve API)
npujarec5762e2020-01-01 14:08:48 +0530237func (c *EtcdClient) ReleaseAllReservations(ctx context.Context) error {
William Kurkianea869482019-04-09 15:16:11 -0400238 c.writeLock.Lock()
239 defer c.writeLock.Unlock()
cbabu95f21522019-11-13 14:25:18 +0100240
William Kurkianea869482019-04-09 15:16:11 -0400241 for key, leaseID := range c.keyReservations {
cbabu95f21522019-11-13 14:25:18 +0100242 _, err := c.ectdAPI.Revoke(ctx, *leaseID)
William Kurkianea869482019-04-09 15:16:11 -0400243 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000244 logger.Errorw("cannot-release-reservation", log.Fields{"key": key, "error": err})
William Kurkianea869482019-04-09 15:16:11 -0400245 return err
246 }
247 delete(c.keyReservations, key)
248 }
249 return nil
250}
251
252// ReleaseReservation releases reservation for a specific key.
npujarec5762e2020-01-01 14:08:48 +0530253func (c *EtcdClient) ReleaseReservation(ctx context.Context, key string) error {
William Kurkianea869482019-04-09 15:16:11 -0400254 // Get the leaseid using the key
Esin Karamanccb714b2019-11-29 15:02:06 +0000255 logger.Debugw("Release-reservation", log.Fields{"key": key})
William Kurkianea869482019-04-09 15:16:11 -0400256 var ok bool
257 var leaseID *v3Client.LeaseID
258 c.writeLock.Lock()
259 defer c.writeLock.Unlock()
260 if leaseID, ok = c.keyReservations[key]; !ok {
261 return nil
262 }
cbabu95f21522019-11-13 14:25:18 +0100263
William Kurkianea869482019-04-09 15:16:11 -0400264 if leaseID != nil {
cbabu95f21522019-11-13 14:25:18 +0100265 _, err := c.ectdAPI.Revoke(ctx, *leaseID)
William Kurkianea869482019-04-09 15:16:11 -0400266 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000267 logger.Error(err)
William Kurkianea869482019-04-09 15:16:11 -0400268 return err
269 }
270 delete(c.keyReservations, key)
271 }
272 return nil
273}
274
275// RenewReservation renews a reservation. A reservation will go stale after the specified TTL (Time To Live)
276// period specified when reserving the key
npujarec5762e2020-01-01 14:08:48 +0530277func (c *EtcdClient) RenewReservation(ctx context.Context, key string) error {
William Kurkianea869482019-04-09 15:16:11 -0400278 // Get the leaseid using the key
279 var ok bool
280 var leaseID *v3Client.LeaseID
281 c.writeLock.Lock()
282 defer c.writeLock.Unlock()
283 if leaseID, ok = c.keyReservations[key]; !ok {
284 return errors.New("key-not-reserved")
285 }
286
287 if leaseID != nil {
cbabu95f21522019-11-13 14:25:18 +0100288 _, err := c.ectdAPI.KeepAliveOnce(ctx, *leaseID)
William Kurkianea869482019-04-09 15:16:11 -0400289 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000290 logger.Errorw("lease-may-have-expired", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400291 return err
292 }
293 } else {
294 return errors.New("lease-expired")
295 }
296 return nil
297}
298
299// Watch provides the watch capability on a given key. It returns a channel onto which the callee needs to
300// listen to receive Events.
npujarec5762e2020-01-01 14:08:48 +0530301func (c *EtcdClient) Watch(ctx context.Context, key string) chan *Event {
William Kurkianea869482019-04-09 15:16:11 -0400302 w := v3Client.NewWatcher(c.ectdAPI)
npujarec5762e2020-01-01 14:08:48 +0530303 ctx, cancel := context.WithCancel(ctx)
kdarapub26b4502019-10-05 03:02:33 +0530304 channel := w.Watch(ctx, key)
William Kurkianea869482019-04-09 15:16:11 -0400305
306 // Create a new channel
307 ch := make(chan *Event, maxClientChannelBufferSize)
308
309 // Keep track of the created channels so they can be closed when required
310 channelMap := make(map[chan *Event]v3Client.Watcher)
311 channelMap[ch] = w
William Kurkianea869482019-04-09 15:16:11 -0400312
313 channelMaps := c.addChannelMap(key, channelMap)
314
Manikkaraj kb1d51442019-07-23 10:41:02 -0400315 // Changing the log field (from channelMaps) as the underlying logger cannot format the map of channels into a
316 // json format.
Esin Karamanccb714b2019-11-29 15:02:06 +0000317 logger.Debugw("watched-channels", log.Fields{"len": len(channelMaps)})
William Kurkianea869482019-04-09 15:16:11 -0400318 // Launch a go routine to listen for updates
kdarapub26b4502019-10-05 03:02:33 +0530319 go c.listenForKeyChange(channel, ch, cancel)
William Kurkianea869482019-04-09 15:16:11 -0400320
321 return ch
322
323}
324
325func (c *EtcdClient) addChannelMap(key string, channelMap map[chan *Event]v3Client.Watcher) []map[chan *Event]v3Client.Watcher {
326 var channels interface{}
327 var exists bool
328
329 if channels, exists = c.watchedChannels.Load(key); exists {
330 channels = append(channels.([]map[chan *Event]v3Client.Watcher), channelMap)
331 } else {
332 channels = []map[chan *Event]v3Client.Watcher{channelMap}
333 }
334 c.watchedChannels.Store(key, channels)
335
336 return channels.([]map[chan *Event]v3Client.Watcher)
337}
338
339func (c *EtcdClient) removeChannelMap(key string, pos int) []map[chan *Event]v3Client.Watcher {
340 var channels interface{}
341 var exists bool
342
343 if channels, exists = c.watchedChannels.Load(key); exists {
344 channels = append(channels.([]map[chan *Event]v3Client.Watcher)[:pos], channels.([]map[chan *Event]v3Client.Watcher)[pos+1:]...)
345 c.watchedChannels.Store(key, channels)
346 }
347
348 return channels.([]map[chan *Event]v3Client.Watcher)
349}
350
351func (c *EtcdClient) getChannelMaps(key string) ([]map[chan *Event]v3Client.Watcher, bool) {
352 var channels interface{}
353 var exists bool
354
355 channels, exists = c.watchedChannels.Load(key)
356
357 if channels == nil {
358 return nil, exists
359 }
360
361 return channels.([]map[chan *Event]v3Client.Watcher), exists
362}
363
364// CloseWatch closes a specific watch. Both the key and the channel are required when closing a watch as there
365// may be multiple listeners on the same key. The previously created channel serves as a key
366func (c *EtcdClient) CloseWatch(key string, ch chan *Event) {
367 // Get the array of channels mapping
368 var watchedChannels []map[chan *Event]v3Client.Watcher
369 var ok bool
370 c.writeLock.Lock()
371 defer c.writeLock.Unlock()
372
373 if watchedChannels, ok = c.getChannelMaps(key); !ok {
Esin Karamanccb714b2019-11-29 15:02:06 +0000374 logger.Warnw("key-has-no-watched-channels", log.Fields{"key": key})
William Kurkianea869482019-04-09 15:16:11 -0400375 return
376 }
377 // Look for the channels
378 var pos = -1
379 for i, chMap := range watchedChannels {
380 if t, ok := chMap[ch]; ok {
Esin Karamanccb714b2019-11-29 15:02:06 +0000381 logger.Debug("channel-found")
William Kurkianea869482019-04-09 15:16:11 -0400382 // Close the etcd watcher before the client channel. This should close the etcd channel as well
383 if err := t.Close(); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000384 logger.Errorw("watcher-cannot-be-closed", log.Fields{"key": key, "error": err})
William Kurkianea869482019-04-09 15:16:11 -0400385 }
William Kurkianea869482019-04-09 15:16:11 -0400386 pos = i
387 break
388 }
389 }
390
391 channelMaps, _ := c.getChannelMaps(key)
392 // Remove that entry if present
393 if pos >= 0 {
394 channelMaps = c.removeChannelMap(key, pos)
395 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000396 logger.Infow("watcher-channel-exiting", log.Fields{"key": key, "channel": channelMaps})
William Kurkianea869482019-04-09 15:16:11 -0400397}
398
kdarapub26b4502019-10-05 03:02:33 +0530399func (c *EtcdClient) listenForKeyChange(channel v3Client.WatchChan, ch chan<- *Event, cancel context.CancelFunc) {
Esin Karamanccb714b2019-11-29 15:02:06 +0000400 logger.Debug("start-listening-on-channel ...")
kdarapub26b4502019-10-05 03:02:33 +0530401 defer cancel()
402 defer close(ch)
William Kurkianea869482019-04-09 15:16:11 -0400403 for resp := range channel {
404 for _, ev := range resp.Events {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400405 ch <- NewEvent(getEventType(ev), ev.Kv.Key, ev.Kv.Value, ev.Kv.Version)
William Kurkianea869482019-04-09 15:16:11 -0400406 }
407 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000408 logger.Debug("stop-listening-on-channel ...")
William Kurkianea869482019-04-09 15:16:11 -0400409}
410
411func getEventType(event *v3Client.Event) int {
412 switch event.Type {
413 case v3Client.EventTypePut:
414 return PUT
415 case v3Client.EventTypeDelete:
416 return DELETE
417 }
418 return UNKNOWN
419}
420
421// Close closes the KV store client
422func (c *EtcdClient) Close() {
423 c.writeLock.Lock()
424 defer c.writeLock.Unlock()
425 if err := c.ectdAPI.Close(); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000426 logger.Errorw("error-closing-client", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400427 }
428}
429
430func (c *EtcdClient) addLockName(lockName string, lock *v3Concurrency.Mutex, session *v3Concurrency.Session) {
431 c.lockToMutexLock.Lock()
432 defer c.lockToMutexLock.Unlock()
433 c.lockToMutexMap[lockName] = lock
434 c.lockToSessionMap[lockName] = session
435}
436
437func (c *EtcdClient) deleteLockName(lockName string) {
438 c.lockToMutexLock.Lock()
439 defer c.lockToMutexLock.Unlock()
440 delete(c.lockToMutexMap, lockName)
441 delete(c.lockToSessionMap, lockName)
442}
443
444func (c *EtcdClient) getLock(lockName string) (*v3Concurrency.Mutex, *v3Concurrency.Session) {
445 c.lockToMutexLock.Lock()
446 defer c.lockToMutexLock.Unlock()
447 var lock *v3Concurrency.Mutex
448 var session *v3Concurrency.Session
449 if l, exist := c.lockToMutexMap[lockName]; exist {
450 lock = l
451 }
452 if s, exist := c.lockToSessionMap[lockName]; exist {
453 session = s
454 }
455 return lock, session
456}
457
npujarec5762e2020-01-01 14:08:48 +0530458func (c *EtcdClient) AcquireLock(ctx context.Context, lockName string, timeout int) error {
William Kurkianea869482019-04-09 15:16:11 -0400459 session, _ := v3Concurrency.NewSession(c.ectdAPI, v3Concurrency.WithContext(ctx))
460 mu := v3Concurrency.NewMutex(session, "/devicelock_"+lockName)
461 if err := mu.Lock(context.Background()); err != nil {
npujarec5762e2020-01-01 14:08:48 +0530462 //cancel()
William Kurkianea869482019-04-09 15:16:11 -0400463 return err
464 }
465 c.addLockName(lockName, mu, session)
William Kurkianea869482019-04-09 15:16:11 -0400466 return nil
467}
468
469func (c *EtcdClient) ReleaseLock(lockName string) error {
470 lock, session := c.getLock(lockName)
471 var err error
472 if lock != nil {
473 if e := lock.Unlock(context.Background()); e != nil {
474 err = e
475 }
476 }
477 if session != nil {
478 if e := session.Close(); e != nil {
479 err = e
480 }
481 }
482 c.deleteLockName(lockName)
483
484 return err
485}