Browse Source

Cache database and message queue connection objects

Recently in the gate we have seen a trace on some work-in-progress
patches:

  OperationalError: (pymysql.err.OperationalError)
                    (1040, u'Too many connections')

and at least one operator has reported that the number of database
connections increased significantly going from Mitaka to Newton.

It was suspected that the increase was caused by creating new oslo.db
transaction context managers on-the-fly when switching database
connections for cells. Comparing the dstat --tcp output of runs of the
gate-tempest-dsvm-neutron-full-ubuntu-xenial job with and without
caching of the database connections showed a difference of 445 active
TCP connections and 1495 active TCP connections, respectively [1].

This adds caching of the oslo.db transaction context managers and the
oslo.messaging transports to avoid creating a large number of objects
that are not being garbage-collected as expected.

Closes-Bug: #1691545

[1] https://docs.google.com/spreadsheets/d/1DIfFfX3kaA_SRoCM-aO7BN4IBEShChXLztOBFeKryt4/edit?usp=sharing

 Conflicts:
	nova/test.py
	nova/tests/unit/test_context.py

NOTE(melwitt): Conflicts caused by the fact that no other global cache
resets exist in nova.test.py in Newton and the get_context function
doesn't exist in Newton.

Change-Id: I17e0eb836dd87aac5859f506e7d771d42753d31a
(cherry picked from commit f4159d1755)
tags/14.0.7
melanie witt 2 years ago
parent
commit
d6a628da62

+ 22
- 2
nova/context.py View File

@@ -34,6 +34,10 @@ from nova import policy
34 34
 from nova import utils
35 35
 
36 36
 LOG = logging.getLogger(__name__)
37
+# TODO(melwitt): This cache should be cleared whenever WSGIService receives a
38
+# SIGHUP and periodically based on an expiration time. Currently, none of the
39
+# cell caches are purged, so neither is this one, for now.
40
+CELL_CACHE = {}
37 41
 
38 42
 
39 43
 class _ContextAuthPlugin(plugin.BaseAuthPlugin):
@@ -349,11 +353,27 @@ def target_cell(context, cell_mapping):
349 353
     :param context: The RequestContext to add connection information
350 354
     :param cell_mapping: A objects.CellMapping object
351 355
     """
356
+    global CELL_CACHE
357
+
352 358
     original_db_connection = context.db_connection
353 359
     # avoid circular import
354 360
     from nova import db
355
-    db_connection_string = cell_mapping.database_connection
356
-    context.db_connection = db.create_context_manager(db_connection_string)
361
+
362
+    # Synchronize access to the cache by multiple API workers.
363
+    @utils.synchronized(cell_mapping.uuid)
364
+    def get_or_set_cached_cell_and_set_connections():
365
+        try:
366
+            cell_db_conn = CELL_CACHE[cell_mapping.uuid]
367
+        except KeyError:
368
+            db_connection_string = cell_mapping.database_connection
369
+            context.db_connection = db.create_context_manager(
370
+                db_connection_string)
371
+            CELL_CACHE[cell_mapping.uuid] = context.db_connection
372
+        else:
373
+            context.db_connection = cell_db_conn
374
+
375
+    get_or_set_cached_cell_and_set_connections()
376
+
357 377
     try:
358 378
         yield context
359 379
     finally:

+ 3
- 0
nova/test.py View File

@@ -211,6 +211,9 @@ class TestCase(testtools.TestCase):
211 211
         self.useFixture(conf_fixture.ConfFixture(CONF))
212 212
         self.useFixture(nova_fixtures.RPCFixture('nova.test'))
213 213
 
214
+        # NOTE(melwitt): Reset the cached db connection objects
215
+        context.CELL_CACHE = {}
216
+
214 217
         if self.USES_DB:
215 218
             self.useFixture(nova_fixtures.Database())
216 219
             self.useFixture(nova_fixtures.Database(database='api'))

+ 21
- 1
nova/tests/unit/test_context.py View File

@@ -20,6 +20,7 @@ from nova import context
20 20
 from nova import exception
21 21
 from nova import objects
22 22
 from nova import test
23
+from nova.tests import uuidsentinel as uuids
23 24
 
24 25
 
25 26
 class ContextTestCase(test.NoDBTestCase):
@@ -286,7 +287,26 @@ class ContextTestCase(test.NoDBTestCase):
286 287
                                       roles=['admin', 'weasel'])
287 288
         # Verify the existing db_connection, if any, is restored
288 289
         ctxt.db_connection = mock.sentinel.db_conn
289
-        mapping = objects.CellMapping(database_connection='fake://')
290
+        mapping = objects.CellMapping(database_connection='fake://',
291
+                                      transport_url='fake://',
292
+                                      uuid=uuids.cell)
290 293
         with context.target_cell(ctxt, mapping):
291 294
             self.assertEqual(ctxt.db_connection, mock.sentinel.cm)
292 295
         self.assertEqual(mock.sentinel.db_conn, ctxt.db_connection)
296
+
297
+    @mock.patch('nova.db.create_context_manager')
298
+    def test_target_cell_caching(self, mock_create_cm):
299
+        mock_create_cm.return_value = mock.sentinel.db_conn_obj
300
+        ctxt = context.RequestContext('111', '222')
301
+        mapping = objects.CellMapping(database_connection='fake://db',
302
+                                      transport_url='fake://mq',
303
+                                      uuid=uuids.cell)
304
+        # First call should create new connection objects.
305
+        with context.target_cell(ctxt, mapping):
306
+            self.assertEqual(mock.sentinel.db_conn_obj, ctxt.db_connection)
307
+        mock_create_cm.assert_called_once_with('fake://db')
308
+        # Second call should use cached objects.
309
+        mock_create_cm.reset_mock()
310
+        with context.target_cell(ctxt, mapping):
311
+            self.assertEqual(mock.sentinel.db_conn_obj, ctxt.db_connection)
312
+        mock_create_cm.assert_not_called()

+ 10
- 0
releasenotes/notes/bug-1691545-1acd6512effbdffb.yaml View File

@@ -0,0 +1,10 @@
1
+---
2
+fixes:
3
+  - |
4
+    Fixes `bug 1691545`_ in which there was a significant increase in database
5
+    connections because of the way connections to cell databases were being
6
+    established. With this fix, objects related to database connections are
7
+    cached in the API service and reused to prevent new connections being
8
+    established for every communication with cell databases.
9
+
10
+    .. _bug 1691545: https://bugs.launchpad.net/nova/+bug/1691545

Loading…
Cancel
Save