Merge "Ensure lock(s) acquire/release returns boolean values"

This commit is contained in:
Jenkins 2015-06-23 10:25:23 +00:00 committed by Gerrit Code Review
commit 3361ef1473
6 changed files with 49 additions and 22 deletions

View File

@ -101,6 +101,9 @@ class FileLock(locking.Lock):
self._lock.release() self._lock.release()
self.acquired = False self.acquired = False
self._cond.notify_all() self._cond.notify_all()
return True
else:
return False
def __del__(self): def __del__(self):
if self.acquired: if self.acquired:

View File

@ -73,11 +73,14 @@ class MySQLLock(locking.Lock):
return _lock() return _lock()
def release(self): def release(self):
if not self.acquired:
return False
try: try:
with self._conn as cur: with self._conn as cur:
cur.execute("SELECT RELEASE_LOCK(%s);", self.name) cur.execute("SELECT RELEASE_LOCK(%s);", self.name)
cur.fetchone() cur.fetchone()
self.acquired = False self.acquired = False
return True
except pymysql.MySQLError as e: except pymysql.MySQLError as e:
coordination.raise_with_cause(coordination.ToozError, coordination.raise_with_cause(coordination.ToozError,
utils.exception_message(e), utils.exception_message(e),

View File

@ -103,6 +103,7 @@ class PostgresLock(locking.Lock):
self.key = h.digest()[0:2] self.key = h.digest()[0:2]
def acquire(self, blocking=True): def acquire(self, blocking=True):
@_retry.retry(stop_max_delay=blocking) @_retry.retry(stop_max_delay=blocking)
def _lock(): def _lock():
# NOTE(sileht) One the same session the lock is not exclusive # NOTE(sileht) One the same session the lock is not exclusive
@ -134,11 +135,14 @@ class PostgresLock(locking.Lock):
return _lock() return _lock()
def release(self): def release(self):
if not self.acquired:
return False
with _translating_cursor(self._conn) as cur: with _translating_cursor(self._conn) as cur:
cur.execute("SELECT pg_advisory_unlock(%s, %s);", cur.execute("SELECT pg_advisory_unlock(%s, %s);",
self.key) self.key)
cur.fetchone() cur.fetchone()
self.acquired = False self.acquired = False
return True
def __del__(self): def __del__(self):
if self.acquired: if self.acquired:

View File

@ -45,12 +45,10 @@ class ZooKeeperLock(locking.Lock):
if blocking: if blocking:
raise _retry.Retry raise _retry.Retry
return False return False
if self._lock.acquire(blocking=bool(blocking), if self._lock.acquire(blocking=bool(blocking),
timeout=0): timeout=0):
self.acquired = True self.acquired = True
return True return True
if blocking: if blocking:
raise _retry.Retry raise _retry.Retry
return False return False
@ -58,8 +56,12 @@ class ZooKeeperLock(locking.Lock):
return _lock() return _lock()
def release(self): def release(self):
self._lock.release() if self.acquired:
self.acquired = False self._lock.release()
self.acquired = False
return True
else:
return False
class BaseZooKeeperDriver(coordination.CoordinationDriver): class BaseZooKeeperDriver(coordination.CoordinationDriver):

View File

@ -99,6 +99,15 @@ class SharedWeakLockHelper(Lock):
def release(self): def release(self):
with self.LOCKS_LOCK: with self.LOCKS_LOCK:
l = self.ACQUIRED_LOCKS.pop(self._lock_key) try:
self.RELEASED_LOCKS[self._lock_key] = l l = self.ACQUIRED_LOCKS.pop(self._lock_key)
l.release() except KeyError:
return False
else:
if l.release():
self.RELEASED_LOCKS[self._lock_key] = l
return True
else:
# Put it back...
self.ACQUIRED_LOCKS[self._lock_key] = l
return False

View File

@ -588,8 +588,8 @@ class TestAPI(testscenarios.TestWithScenarios,
def test_get_lock(self): def test_get_lock(self):
lock = self._coord.get_lock(self._get_random_uuid()) lock = self._coord.get_lock(self._get_random_uuid())
self.assertEqual(True, lock.acquire()) self.assertTrue(lock.acquire())
lock.release() self.assertTrue(lock.release())
with lock: with lock:
pass pass
@ -600,7 +600,7 @@ class TestAPI(testscenarios.TestWithScenarios,
def thread(): def thread():
self.assertTrue(lock.acquire()) self.assertTrue(lock.acquire())
lock.release() self.assertTrue(lock.release())
graceful_ending.set() graceful_ending.set()
t = threading.Thread(target=thread) t = threading.Thread(target=thread)
@ -668,23 +668,29 @@ class TestAPI(testscenarios.TestWithScenarios,
lock1 = self._coord.get_lock(name) lock1 = self._coord.get_lock(name)
lock2 = self._coord.get_lock(name) lock2 = self._coord.get_lock(name)
with lock1: with lock1:
self.assertEqual(False, lock2.acquire(blocking=False)) self.assertFalse(lock2.acquire(blocking=False))
def test_get_lock_locked_twice(self): def test_get_lock_locked_twice(self):
name = self._get_random_uuid() name = self._get_random_uuid()
lock = self._coord.get_lock(name) lock = self._coord.get_lock(name)
with lock: with lock:
self.assertEqual(False, lock.acquire(blocking=False)) self.assertFalse(lock.acquire(blocking=False))
def test_get_multiple_locks_with_same_coord(self): def test_get_multiple_locks_with_same_coord(self):
name = self._get_random_uuid() name = self._get_random_uuid()
lock1 = self._coord.get_lock(name) lock1 = self._coord.get_lock(name)
lock2 = self._coord.get_lock(name) lock2 = self._coord.get_lock(name)
self.assertEqual(True, lock1.acquire()) self.assertTrue(lock1.acquire())
self.assertEqual(False, lock2.acquire(blocking=False)) self.assertFalse(lock2.acquire(blocking=False))
self.assertEqual(False, self.assertFalse(self._coord.get_lock(name).acquire(blocking=False))
self._coord.get_lock(name).acquire(blocking=False)) self.assertTrue(lock1.release())
lock1.release()
def test_ensure_acquire_release_return(self):
name = self._get_random_uuid()
lock1 = self._coord.get_lock(name)
self.assertTrue(lock1.acquire())
self.assertTrue(lock1.release())
self.assertFalse(lock1.release())
def test_get_lock_multiple_coords(self): def test_get_lock_multiple_coords(self):
member_id2 = self._get_random_uuid() member_id2 = self._get_random_uuid()
@ -694,13 +700,13 @@ class TestAPI(testscenarios.TestWithScenarios,
lock_name = self._get_random_uuid() lock_name = self._get_random_uuid()
lock = self._coord.get_lock(lock_name) lock = self._coord.get_lock(lock_name)
self.assertEqual(True, lock.acquire()) self.assertTrue(lock.acquire())
lock2 = client2.get_lock(lock_name) lock2 = client2.get_lock(lock_name)
self.assertEqual(False, lock2.acquire(blocking=False)) self.assertFalse(lock2.acquire(blocking=False))
lock.release() self.assertTrue(lock.release())
self.assertEqual(True, lock2.acquire(blocking=True)) self.assertTrue(lock2.acquire(blocking=True))
lock2.release() self.assertTrue(lock2.release())
@staticmethod @staticmethod
def _get_random_uuid(): def _get_random_uuid():