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
This commit is contained in:
Ann Kamyshnikova 2016-03-31 16:43:41 +03:00
parent 1019d2b1e5
commit aa630f2ac8
3 changed files with 33 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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()