From 9578eb5f79f7e45739ef6154f664cbe26de38877 Mon Sep 17 00:00:00 2001 From: Julia Kreger Date: Thu, 17 Aug 2023 10:13:43 -0700 Subject: [PATCH] Don't hold on to the API request dbapi ... And only execute the loader once. We now use a cached dbapi instance, and then set the object on the request to None, disconnecting the request from the request from the database, which doesn't need to be held open any longer than absolutely necessary. Interestingly... locally, my total unit test time dropped with this change from an aggregate around 200 seconds to 130-140 seconds. It seems there was quite a bit of overhead in that call to begin with. Change-Id: Ibffbc0b14d3b908f772b2cac8c6fedae432b267a --- ironic/api/hooks.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ironic/api/hooks.py b/ironic/api/hooks.py index 68e048e6f6..547958fe8c 100644 --- a/ironic/api/hooks.py +++ b/ironic/api/hooks.py @@ -34,6 +34,10 @@ GLOBAL_REQ_ID = 'openstack.global_request_id' ID_FORMAT = (r'^req-[a-f0-9]{8}-[a-f0-9]{4}-' r'[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$') +# Call once, don't call on each request because it is +# a ton of extra overhead. +DBAPI = dbapi.get_instance() + def policy_deprecation_check(): global CHECKED_DEPRECATED_POLICY_ARGS @@ -77,7 +81,12 @@ class DBHook(hooks.PecanHook): """Attach the dbapi object to the request so controllers can get to it.""" def before(self, state): - state.request.dbapi = dbapi.get_instance() + state.request.dbapi = DBAPI + + def after(self, state): + # Explicitly set to None since we don't need the DB connection + # after we're done processing the request. + state.request.dbapi = None class ContextHook(hooks.PecanHook):