Merge "only rollback DB when we have a connection to the DB" into stable/stein

This commit is contained in:
Zuul 2019-09-04 01:36:49 +00:00 committed by Gerrit Code Review
commit 2f2e9f7f93
3 changed files with 26 additions and 1 deletions

View File

@ -85,6 +85,7 @@ class HealthManager(object):
futs = [] futs = []
while not self.dead.is_set(): while not self.dead.is_set():
amp_health = None amp_health = None
lock_session = None
try: try:
lock_session = db_api.get_session(autocommit=False) lock_session = db_api.get_session(autocommit=False)
amp = None amp = None
@ -123,7 +124,8 @@ class HealthManager(object):
time.sleep(CONF.health_manager.heartbeat_timeout) time.sleep(CONF.health_manager.heartbeat_timeout)
except Exception: except Exception:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
lock_session.rollback() if lock_session:
lock_session.rollback()
if amp_health is None: if amp_health is None:
break break

View File

@ -117,3 +117,21 @@ class TestHealthManager(base.TestCase):
session_mock.assert_called_once_with(autocommit=False) session_mock.assert_called_once_with(autocommit=False)
self.assertFalse(failover_mock.called) self.assertFalse(failover_mock.called)
@mock.patch('octavia.controller.worker.controller_worker.'
'ControllerWorker.failover_amphora')
@mock.patch('octavia.db.repositories.AmphoraHealthRepository.'
'get_stale_amphora', return_value=None)
@mock.patch('octavia.db.api.get_session')
def test_health_check_db_error(self, session_mock, get_stale_amp_mock,
failover_mock):
get_stale_amp_mock.return_value = None
mock_session = mock.MagicMock()
session_mock.return_value = mock_session
session_mock.side_effect = TestException('DB Error')
exit_event = threading.Event()
hm = healthmanager.HealthManager(exit_event)
self.assertRaises(TestException, hm.health_check)
self.assertEqual(0, mock_session.rollback.call_count)

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Fixed an issue with the health manager reporting an UnboundLocalError if
it gets an exception attempting to get a database connection.