Browse Source

only rollback DB when we have a connection to the DB

Story: 2006062
Task: 34775

Change-Id: I728a9aa82986edef8128907a48c9df87d119fa8f
(cherry picked from commit feaa1ce021)
changes/35/678535/2
Chuck Wilson 2 months ago
parent
commit
3e1d0abece

+ 3
- 1
octavia/controller/healthmanager/health_manager.py View File

@@ -85,6 +85,7 @@ class HealthManager(object):
85 85
         futs = []
86 86
         while not self.dead.is_set():
87 87
             amp_health = None
88
+            lock_session = None
88 89
             try:
89 90
                 lock_session = db_api.get_session(autocommit=False)
90 91
                 amp = None
@@ -123,7 +124,8 @@ class HealthManager(object):
123 124
                     time.sleep(CONF.health_manager.heartbeat_timeout)
124 125
             except Exception:
125 126
                 with excutils.save_and_reraise_exception():
126
-                    lock_session.rollback()
127
+                    if lock_session:
128
+                        lock_session.rollback()
127 129
 
128 130
             if amp_health is None:
129 131
                 break

+ 18
- 0
octavia/tests/unit/controller/healthmanager/test_health_manager.py View File

@@ -114,3 +114,21 @@ class TestHealthManager(base.TestCase):
114 114
 
115 115
         session_mock.assert_called_once_with(autocommit=False)
116 116
         self.assertFalse(failover_mock.called)
117
+
118
+    @mock.patch('octavia.controller.worker.controller_worker.'
119
+                'ControllerWorker.failover_amphora')
120
+    @mock.patch('octavia.db.repositories.AmphoraHealthRepository.'
121
+                'get_stale_amphora', return_value=None)
122
+    @mock.patch('octavia.db.api.get_session')
123
+    def test_health_check_db_error(self, session_mock, get_stale_amp_mock,
124
+                                   failover_mock):
125
+        get_stale_amp_mock.return_value = None
126
+
127
+        mock_session = mock.MagicMock()
128
+        session_mock.return_value = mock_session
129
+        session_mock.side_effect = TestException('DB Error')
130
+        exit_event = threading.Event()
131
+        hm = healthmanager.HealthManager(exit_event)
132
+
133
+        self.assertRaises(TestException, hm.health_check)
134
+        self.assertEqual(0, mock_session.rollback.call_count)

+ 5
- 0
releasenotes/notes/Fix-HM-DB-Rollback-no-connection-2664c4f7823ecaec.yaml View File

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

Loading…
Cancel
Save