-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsynchronization.py
65 lines (54 loc) · 2.02 KB
/
synchronization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import inspect
import json
from oslo_log import log as logging
import hpedockerplugin.exception as exception
LOG = logging.getLogger(__name__)
def __synchronized(lock_type, lock_name, f, *a, **k):
call_args = inspect.getcallargs(f, *a, **k)
call_args['f_name'] = f.__name__
lck_name = lock_name.format(**call_args)
lock_acquired = False
self = call_args['self']
lock = self._etcd.get_lock(lock_type, lck_name)
try:
lock.try_lock_name()
lock_acquired = True
LOG.info('Lock acquired: [caller=%s, lock-name=%s]'
% (f.__name__, lck_name))
return f(*a, **k)
except exception.HPEPluginLockFailed:
LOG.exception('Lock acquire failed: [caller=%(caller)s, '
'lock-name=%(name)s]',
{'caller': f.__name__,
'name': lck_name})
response = json.dumps({u"Err": ''})
return response
finally:
if lock_acquired:
try:
lock.try_unlock_name()
LOG.info('Lock released: [caller=%s, lock-name=%s]' %
(f.__name__, lck_name))
except exception.HPEPluginUnlockFailed:
LOG.exception('Lock release failed: [caller=%(caller)s'
', lock-name=%(name)s]',
{'caller': f.__name__,
'name': lck_name})
def synchronized_volume(lock_name):
def _synchronized(f):
def _wrapped(*a, **k):
return __synchronized('VOL', lock_name, f, *a, **k)
return _wrapped
return _synchronized
def synchronized_rcg(lock_name):
def _synchronized(f):
def _wrapped(*a, **k):
return __synchronized('RCG', lock_name, f, *a, **k)
return _wrapped
return _synchronized
def synchronized_fp_share(lock_name):
def _synchronized(f):
def _wrapped(*a, **k):
return __synchronized('FP_SHARE', lock_name, f, *a, **k)
return _wrapped
return _synchronized