summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Danjou <julien@danjou.info>2017-06-09 09:53:08 +0200
committerJulien Danjou <julien@danjou.info>2017-06-16 11:40:42 +0000
commitee66b6e4e416569cb8d51a9b19263c22c6e1f87d (patch)
tree7b08c6b0d05282797180527e9677b8fe45c5be01
parent5625106703e1c018a04eb1bd01b1732e637dd950 (diff)
{my,pg}sql: close connections when out of retry1.57.2
The connection is supposedly closed when blocking is False and the lock is not acquired. Or when an exception happens in the locking code. But if blocking is e.g. 5 seconds, then RetryError is raised but without closing any connection at the end. This fixes that. Change-Id: I7b40e466fe5fc01ee3f8c012f765aafca9ce716a
Notes
Notes (review): Code-Review+2: Mehdi Abaakouk (sileht) <sileht@sileht.net> Code-Review+2: gordon chung <gord@live.ca> Workflow+1: Julien Danjou <julien@danjou.info> Verified+2: Jenkins Submitted-by: Jenkins Submitted-at: Fri, 16 Jun 2017 13:56:44 +0000 Reviewed-on: https://review.openstack.org/472598 Project: openstack/tooz Branch: refs/heads/master
-rw-r--r--tooz/drivers/mysql.py9
-rw-r--r--tooz/drivers/pgsql.py45
2 files changed, 29 insertions, 25 deletions
diff --git a/tooz/drivers/mysql.py b/tooz/drivers/mysql.py
index a86a3dc..72853d7 100644
--- a/tooz/drivers/mysql.py
+++ b/tooz/drivers/mysql.py
@@ -68,7 +68,6 @@ class MySQLLock(locking.Lock):
68 self.acquired = True 68 self.acquired = True
69 return True 69 return True
70 except pymysql.MySQLError as e: 70 except pymysql.MySQLError as e:
71 self._conn.close()
72 utils.raise_with_cause( 71 utils.raise_with_cause(
73 tooz.ToozError, 72 tooz.ToozError,
74 encodeutils.exception_to_unicode(e), 73 encodeutils.exception_to_unicode(e),
@@ -79,7 +78,13 @@ class MySQLLock(locking.Lock):
79 self._conn.close() 78 self._conn.close()
80 return False 79 return False
81 80
82 return _lock() 81 try:
82 return _lock()
83 except Exception:
84 # Close the connection if we tried too much and finally failed, or
85 # anything else bad happened.
86 self._conn.close()
87 raise
83 88
84 def release(self): 89 def release(self):
85 if not self.acquired: 90 if not self.acquired:
diff --git a/tooz/drivers/pgsql.py b/tooz/drivers/pgsql.py
index 6e68617..ee92db1 100644
--- a/tooz/drivers/pgsql.py
+++ b/tooz/drivers/pgsql.py
@@ -123,32 +123,31 @@ class PostgresLock(locking.Lock):
123 if not self._conn or self._conn.closed: 123 if not self._conn or self._conn.closed:
124 self._conn = PostgresDriver.get_connection(self._parsed_url, 124 self._conn = PostgresDriver.get_connection(self._parsed_url,
125 self._options) 125 self._options)
126 try: 126
127 with _translating_cursor(self._conn) as cur: 127 with _translating_cursor(self._conn) as cur:
128 if blocking is True: 128 if blocking is True:
129 cur.execute("SELECT pg_advisory_lock(%s, %s);", 129 cur.execute("SELECT pg_advisory_lock(%s, %s);",
130 self.key) 130 self.key)
131 cur.fetchone() 131 cur.fetchone()
132 self.acquired = True
133 return True
134 else:
135 cur.execute("SELECT pg_try_advisory_lock(%s, %s);",
136 self.key)
137 if cur.fetchone()[0] is True:
132 self.acquired = True 138 self.acquired = True
133 return True 139 return True
140 elif blocking is False:
141 self._conn.close()
142 return False
134 else: 143 else:
135 cur.execute("SELECT pg_try_advisory_lock(%s, %s);", 144 raise _retry.TryAgain
136 self.key) 145
137 if cur.fetchone()[0] is True: 146 try:
138 self.acquired = True 147 return _lock()
139 return True 148 except Exception:
140 elif blocking is False: 149 self._conn.close()
141 self._conn.close() 150 raise
142 return False
143 else:
144 raise _retry.TryAgain
145 except _retry.TryAgain:
146 pass # contine to retrieve lock on same conn
147 except Exception:
148 self._conn.close()
149 raise
150
151 return _lock()
152 151
153 def release(self): 152 def release(self):
154 if not self.acquired: 153 if not self.acquired: