Add lock_with_prefix convenience utility

There's a convenience wrapper around the lockutils.synchronized
decorator that lets oslo.concurrency consumers set up a prefix to use
for all lock files (presumably to obviate collisions with other
consumers when using jejune lock names, like 'lock'). This commit adds
an equivalent wrapper around lockutils.lock, the context manager
counterpart to the lockutils.synchronized decorator.

Note that the unit test added for lock_with_prefix is pretty bare; but
it follows the precedent set by the existing tests. Future commits
should make all these tests more thorough/robust.

Change-Id: I4e723ee3be1e57c543684390b607c84388c6e930
This commit is contained in:
Eric Fried 2019-09-17 16:18:12 -05:00
parent fe86f5e4ae
commit fec03875e9
2 changed files with 32 additions and 0 deletions

View File

@ -281,6 +281,29 @@ def lock(name, lock_file_prefix=None, external=False, lock_path=None,
LOG.debug('Releasing lock "%(lock)s"', {'lock': name})
def lock_with_prefix(lock_file_prefix):
"""Partial object generator for the lock context manager.
Redefine lock in each project like so::
(in nova/utils.py)
from oslo_concurrency import lockutils
lock = lockutils.lock_with_prefix('nova-')
(in nova/foo.py)
from nova import utils
with utils.lock('mylock'):
...
The lock_file_prefix argument is used to provide lock files on disk with a
meaningful prefix.
"""
return functools.partial(lock, lock_file_prefix=lock_file_prefix)
def synchronized(name, lock_file_prefix=None, external=False, lock_path=None,
semaphores=None, delay=0.01, fair=False):
"""Synchronization decorator.

View File

@ -227,6 +227,15 @@ class LockTestCase(test_base.BaseTestCase):
self._do_test_lock_externally()
def test_lock_with_prefix(self):
# TODO(efried): Embetter this test
self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
foo = lockutils.lock_with_prefix('mypfix-')
with foo('mylock', external=True):
# We can't check much
pass
def test_synchronized_with_prefix(self):
lock_name = 'mylock'
lock_pfix = 'mypfix-'