Browse Source

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

changes/49/675349/2
Zuul 2 weeks ago
parent
commit
2f2e9f7f93

+ 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

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