From aa630f2ac88bfb4a9828b432bddfed1f04e077d3 Mon Sep 17 00:00:00 2001 From: Ann Kamyshnikova Date: Thu, 31 Mar 2016 16:43:41 +0300 Subject: [PATCH] New engine facade from oslo_db: Step 1 Start using new engine facade. Existing property session for Context is needed for backward compatibility. Temporary created class ContextBaseWithSession unless reader and writer will be used in proper places. Usage of lazy init for engine facade will be removed in next patch set. Partial-Bug: #1520719 Change-Id: I4f0693789f1c928ef47c5fdd982c147d1a9a89e1 --- neutron/context.py | 12 +++++++++++- neutron/db/api.py | 7 +++++-- .../services/metering/test_metering_plugin.py | 19 +++++++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/neutron/context.py b/neutron/context.py index 811d740acf4..840a8f68565 100644 --- a/neutron/context.py +++ b/neutron/context.py @@ -19,6 +19,7 @@ import copy import datetime from oslo_context import context as oslo_context +from oslo_db.sqlalchemy import enginefacade from neutron.db import api as db_api from neutron import policy @@ -109,13 +110,22 @@ class ContextBase(oslo_context.RequestContext): return context -class Context(ContextBase): +@enginefacade.transaction_context_provider +class ContextBaseWithSession(ContextBase): + pass + + +class Context(ContextBaseWithSession): def __init__(self, *args, **kwargs): super(Context, self).__init__(*args, **kwargs) self._session = None @property def session(self): + # TODO(akamyshnikova): checking for session attribute won't be needed + # when reader and writer will be used + if hasattr(super(Context, self), 'session'): + return super(Context, self).session if self._session is None: self._session = db_api.get_session() return self._session diff --git a/neutron/db/api.py b/neutron/db/api.py index 90ed6a1e8f8..b721c35b2f8 100644 --- a/neutron/db/api.py +++ b/neutron/db/api.py @@ -20,12 +20,14 @@ from neutron_lib import exceptions as n_exc from oslo_config import cfg from oslo_db import api as oslo_db_api from oslo_db import exception as db_exc -from oslo_db.sqlalchemy import session +from oslo_db.sqlalchemy import enginefacade from oslo_utils import excutils from oslo_utils import uuidutils from neutron.db import common_db_mixin +context_manager = enginefacade.transaction_context() + _FACADE = None @@ -53,7 +55,8 @@ def _create_facade_lazily(): global _FACADE if _FACADE is None: - _FACADE = session.EngineFacade.from_config(cfg.CONF, sqlite_fk=True) + context_manager.configure(sqlite_fk=True, **cfg.CONF.database) + _FACADE = context_manager._factory.get_legacy_facade() return _FACADE diff --git a/neutron/tests/unit/services/metering/test_metering_plugin.py b/neutron/tests/unit/services/metering/test_metering_plugin.py index b2443bf3857..00e95771fa7 100644 --- a/neutron/tests/unit/services/metering/test_metering_plugin.py +++ b/neutron/tests/unit/services/metering/test_metering_plugin.py @@ -18,6 +18,7 @@ from oslo_utils import uuidutils from neutron.api.v2 import attributes as attr from neutron import context from neutron.db import agents_db +from neutron.db import api as db_api from neutron.db import l3_agentschedulers_db from neutron.db.metering import metering_rpc from neutron.extensions import l3 as ext_l3 @@ -57,6 +58,20 @@ class MeteringTestExtensionManager(object): return [] +# TODO(akamyshnikova):we need this temporary FakeContext class while Context +# checking for existence of session attribute. +class FakeContext(context.ContextBaseWithSession): + def __init__(self, *args, **kwargs): + super(FakeContext, self).__init__(*args, **kwargs) + self._session = None + + @property + def session(self): + if self._session is None: + self._session = db_api.get_session() + return self._session + + class TestMeteringPlugin(test_db_base_plugin_v2.NeutronDbPluginV2TestCase, test_l3.L3NatTestCaseMixin, test_metering_db.MeteringPluginDbTestCaseMixin): @@ -81,7 +96,7 @@ class TestMeteringPlugin(test_db_base_plugin_v2.NeutronDbPluginV2TestCase, self.mock_uuid = self.uuid_patch.start() self.tenant_id = 'a7e61382-47b8-4d40-bae3-f95981b5637b' - self.ctx = context.Context('', self.tenant_id, is_admin=True) + self.ctx = FakeContext('', self.tenant_id, is_admin=True) self.context_patch = mock.patch('neutron.context.Context', return_value=self.ctx) self.mock_context = self.context_patch.start() @@ -309,7 +324,7 @@ class TestMeteringPluginL3AgentScheduler( self.mock_uuid = self.uuid_patch.start() self.tenant_id = 'a7e61382-47b8-4d40-bae3-f95981b5637b' - self.ctx = context.Context('', self.tenant_id, is_admin=True) + self.ctx = FakeContext('', self.tenant_id, is_admin=True) self.context_patch = mock.patch('neutron.context.Context', return_value=self.ctx) self.mock_context = self.context_patch.start()