summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZhongyue Luo <zhongyue.nah@intel.com>2013-05-07 17:25:34 +0900
committerZhongyue Luo <zhongyue.nah@intel.com>2013-05-09 13:20:29 +0900
commitb873454819df6ef35b22b92445bfb8675b7ac7c1 (patch)
tree9ef97b9e93171b283e4544f672c00c2562e57186
parente523c8c591806454e0e326bbdac57508787f8d24 (diff)
downloadoslo-b873454819df6ef35b22b92445bfb8675b7ac7c1.tar.gz
oslo-b873454819df6ef35b22b92445bfb8675b7ac7c1.tar.xz
oslo-b873454819df6ef35b22b92445bfb8675b7ac7c1.zip
Added convenience APIs for lockutils
The lock_file_prefix for each project doesn't need to be configurable or frequently changed. This patch provides a convenience API which returns a partial object of the synchronized decorator to avoid passing the prefix each time locks are used. The set_defaults method is also provided to change the default value of lock_path when needed. Fixes bug #1065524 Change-Id: I7b67f0a482da4be6d53a70db5bbd22dc91bdc10c
-rw-r--r--openstack/common/lockutils.py29
-rw-r--r--tests/unit/test_lockutils.py16
2 files changed, 45 insertions, 0 deletions
diff --git a/openstack/common/lockutils.py b/openstack/common/lockutils.py
index 892d821..79d1905 100644
--- a/openstack/common/lockutils.py
+++ b/openstack/common/lockutils.py
@@ -49,6 +49,10 @@ CONF = cfg.CONF
CONF.register_opts(util_opts)
+def set_defaults(lock_path):
+ cfg.set_defaults(util_opts, lock_path=lock_path)
+
+
class _InterProcessLock(object):
"""Lock implementation which allows multiple locks, working around
issues like bugs.debian.org/cgi-bin/bugreport.cgi?bug=632857 and does
@@ -247,3 +251,28 @@ def synchronized(name, lock_file_prefix, external=False, lock_path=None):
return retval
return inner
return wrap
+
+
+def synchronized_with_prefix(lock_file_prefix):
+ """Partial object generator for the synchronization decorator.
+
+ Redefine @synchronized in each project like so::
+
+ (in nova/utils.py)
+ from nova.openstack.common import lockutils
+
+ synchronized = lockutils.synchronized_with_prefix('nova-')
+
+
+ (in nova/foo.py)
+ from nova import utils
+
+ @utils.synchronized('mylock')
+ def bar(self, *args):
+ ...
+
+ The lock_file_prefix argument is used to provide lock files on disk with a
+ meaningful prefix. The prefix should end with a hyphen ('-') if specified.
+ """
+
+ return functools.partial(synchronized, lock_file_prefix=lock_file_prefix)
diff --git a/tests/unit/test_lockutils.py b/tests/unit/test_lockutils.py
index 1230569..c37b030 100644
--- a/tests/unit/test_lockutils.py
+++ b/tests/unit/test_lockutils.py
@@ -195,3 +195,19 @@ class LockTestCase(utils.BaseTestCase):
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
+
+ def test_synchronized_with_prefix(self):
+ lock_name = 'mylock'
+ lock_pfix = 'mypfix-'
+
+ foo = lockutils.synchronized_with_prefix(lock_pfix)
+
+ @foo(lock_name, external=True)
+ def bar(dirpath, pfix, name):
+ filepath = os.path.join(dirpath, '%s%s' % (pfix, name))
+ return os.path.isfile(filepath)
+
+ lock_dir = tempfile.mkdtemp()
+ self.config(lock_path=lock_dir)
+
+ self.assertTrue(bar(lock_dir, lock_pfix, lock_name))