summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-08-12 21:21:48 +0000
committerGerrit Code Review <review@openstack.org>2018-08-12 21:21:49 +0000
commite0d39d74257c322611ab789aee625d2e09ee08c2 (patch)
treed0fee747b78994f2c3fe7dc7067f6799c58e7368
parentbc79f0461355cc60a5d901a21ea35504eeddaa11 (diff)
parentf4a684cdcb28228cee94aed43c9db4caee681460 (diff)
Merge "use autonested_transaction from neutron-lib"
-rw-r--r--neutron/core_extensions/qos.py2
-rw-r--r--neutron/db/api.py12
-rw-r--r--neutron/db/l3_db.py3
-rw-r--r--neutron/objects/base.py5
-rw-r--r--neutron/scheduler/l3_agent_scheduler.py3
-rw-r--r--neutron/services/qos/qos_plugin.py11
-rw-r--r--neutron/services/trunk/plugin.py2
-rw-r--r--neutron/services/trunk/rpc/server.py2
-rw-r--r--neutron/tests/unit/db/test_db_base_plugin_v2.py2
-rw-r--r--neutron/tests/unit/objects/test_base.py5
10 files changed, 19 insertions, 28 deletions
diff --git a/neutron/core_extensions/qos.py b/neutron/core_extensions/qos.py
index 72eecff..4eba04d 100644
--- a/neutron/core_extensions/qos.py
+++ b/neutron/core_extensions/qos.py
@@ -13,13 +13,13 @@
13# License for the specific language governing permissions and limitations 13# License for the specific language governing permissions and limitations
14# under the License. 14# under the License.
15 15
16from neutron_lib.db import api as db_api
16from neutron_lib.plugins import constants as plugin_constants 17from neutron_lib.plugins import constants as plugin_constants
17from neutron_lib.plugins import directory 18from neutron_lib.plugins import directory
18from neutron_lib.services.qos import constants as qos_consts 19from neutron_lib.services.qos import constants as qos_consts
19 20
20from neutron.common import exceptions as n_exc 21from neutron.common import exceptions as n_exc
21from neutron.core_extensions import base 22from neutron.core_extensions import base
22from neutron.db import api as db_api
23from neutron.objects.qos import policy as policy_object 23from neutron.objects.qos import policy as policy_object
24 24
25 25
diff --git a/neutron/db/api.py b/neutron/db/api.py
index 9a9016d..08d6c61 100644
--- a/neutron/db/api.py
+++ b/neutron/db/api.py
@@ -13,7 +13,6 @@
13# License for the specific language governing permissions and limitations 13# License for the specific language governing permissions and limitations
14# under the License. 14# under the License.
15 15
16import contextlib
17import copy 16import copy
18import weakref 17import weakref
19 18
@@ -136,17 +135,6 @@ def _is_nested_instance(e, etypes):
136 return False 135 return False
137 136
138 137
139@contextlib.contextmanager
140def autonested_transaction(sess):
141 """This is a convenience method to not bother with 'nested' parameter."""
142 if sess.is_active:
143 session_context = sess.begin(nested=True)
144 else:
145 session_context = sess.begin(subtransactions=True)
146 with session_context as tx:
147 yield tx
148
149
150@event.listens_for(orm.session.Session, "after_flush") 138@event.listens_for(orm.session.Session, "after_flush")
151def add_to_rel_load_list(session, flush_context=None): 139def add_to_rel_load_list(session, flush_context=None):
152 # keep track of new items to load relationships on during commit 140 # keep track of new items to load relationships on during commit
diff --git a/neutron/db/l3_db.py b/neutron/db/l3_db.py
index b741aae..1291904 100644
--- a/neutron/db/l3_db.py
+++ b/neutron/db/l3_db.py
@@ -26,6 +26,7 @@ from neutron_lib.callbacks import registry
26from neutron_lib.callbacks import resources 26from neutron_lib.callbacks import resources
27from neutron_lib import constants 27from neutron_lib import constants
28from neutron_lib import context as n_ctx 28from neutron_lib import context as n_ctx
29from neutron_lib.db import api as lib_db_api
29from neutron_lib.db import utils as lib_db_utils 30from neutron_lib.db import utils as lib_db_utils
30from neutron_lib import exceptions as n_exc 31from neutron_lib import exceptions as n_exc
31from neutron_lib.exceptions import l3 as l3_exc 32from neutron_lib.exceptions import l3 as l3_exc
@@ -716,7 +717,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
716 raise n_exc.BadRequest(resource='router', msg=msg) 717 raise n_exc.BadRequest(resource='router', msg=msg)
717 718
718 def _validate_router_port_info(self, context, router, port_id): 719 def _validate_router_port_info(self, context, router, port_id):
719 with db_api.autonested_transaction(context.session): 720 with lib_db_api.autonested_transaction(context.session):
720 # check again within transaction to mitigate race 721 # check again within transaction to mitigate race
721 port = self._check_router_port(context, port_id, router.id) 722 port = self._check_router_port(context, port_id, router.id)
722 723
diff --git a/neutron/objects/base.py b/neutron/objects/base.py
index cd39b11..0c5745c 100644
--- a/neutron/objects/base.py
+++ b/neutron/objects/base.py
@@ -16,6 +16,7 @@ import copy
16import functools 16import functools
17import itertools 17import itertools
18 18
19from neutron_lib.db import api as lib_db_api
19from neutron_lib import exceptions as n_exc 20from neutron_lib import exceptions as n_exc
20from neutron_lib.objects import exceptions as o_exc 21from neutron_lib.objects import exceptions as o_exc
21from oslo_db import exception as obj_exc 22from oslo_db import exception as obj_exc
@@ -525,14 +526,14 @@ class NeutronDbObject(NeutronObject):
525 """Return read-write session activation decorator.""" 526 """Return read-write session activation decorator."""
526 if cls.new_facade or cls._use_db_facade(context): 527 if cls.new_facade or cls._use_db_facade(context):
527 return db_api.context_manager.writer.using(context) 528 return db_api.context_manager.writer.using(context)
528 return db_api.autonested_transaction(context.session) 529 return lib_db_api.autonested_transaction(context.session)
529 530
530 @classmethod 531 @classmethod
531 def db_context_reader(cls, context): 532 def db_context_reader(cls, context):
532 """Return read-only session activation decorator.""" 533 """Return read-only session activation decorator."""
533 if cls.new_facade or cls._use_db_facade(context): 534 if cls.new_facade or cls._use_db_facade(context):
534 return db_api.context_manager.reader.using(context) 535 return db_api.context_manager.reader.using(context)
535 return db_api.autonested_transaction(context.session) 536 return lib_db_api.autonested_transaction(context.session)
536 537
537 @classmethod 538 @classmethod
538 def get_object(cls, context, **kwargs): 539 def get_object(cls, context, **kwargs):
diff --git a/neutron/scheduler/l3_agent_scheduler.py b/neutron/scheduler/l3_agent_scheduler.py
index de499d7..924a72e 100644
--- a/neutron/scheduler/l3_agent_scheduler.py
+++ b/neutron/scheduler/l3_agent_scheduler.py
@@ -30,7 +30,6 @@ import six
30 30
31from neutron.common import utils 31from neutron.common import utils
32from neutron.conf.db import l3_hamode_db 32from neutron.conf.db import l3_hamode_db
33from neutron.db import api as db_api
34from neutron.db.models import l3agent as rb_model 33from neutron.db.models import l3agent as rb_model
35from neutron.objects import l3agent as rb_obj 34from neutron.objects import l3agent as rb_obj
36 35
@@ -287,7 +286,7 @@ class L3Scheduler(object):
287 port_binding = utils.create_object_with_dependency( 286 port_binding = utils.create_object_with_dependency(
288 creator, dep_getter, dep_creator, 287 creator, dep_getter, dep_creator,
289 dep_id_attr, dep_deleter)[0] 288 dep_id_attr, dep_deleter)[0]
290 with db_api.autonested_transaction(context.session): 289 with lib_db_api.autonested_transaction(context.session):
291 port_binding.l3_agent_id = agent['id'] 290 port_binding.l3_agent_id = agent['id']
292 except db_exc.DBDuplicateEntry: 291 except db_exc.DBDuplicateEntry:
293 LOG.debug("Router %(router)s already scheduled for agent " 292 LOG.debug("Router %(router)s already scheduled for agent "
diff --git a/neutron/services/qos/qos_plugin.py b/neutron/services/qos/qos_plugin.py
index c870e5a..59ddd60 100644
--- a/neutron/services/qos/qos_plugin.py
+++ b/neutron/services/qos/qos_plugin.py
@@ -17,6 +17,7 @@ from neutron_lib.api.definitions import qos as qos_apidef
17from neutron_lib.callbacks import events as callbacks_events 17from neutron_lib.callbacks import events as callbacks_events
18from neutron_lib.callbacks import registry as callbacks_registry 18from neutron_lib.callbacks import registry as callbacks_registry
19from neutron_lib.callbacks import resources as callbacks_resources 19from neutron_lib.callbacks import resources as callbacks_resources
20from neutron_lib.db import api as lib_db_api
20from neutron_lib import exceptions as lib_exc 21from neutron_lib import exceptions as lib_exc
21from neutron_lib.services.qos import constants as qos_consts 22from neutron_lib.services.qos import constants as qos_consts
22 23
@@ -298,7 +299,7 @@ class QoSPlugin(qos.QoSPluginBase):
298 rule_type = rule_cls.rule_type 299 rule_type = rule_cls.rule_type
299 rule_data = rule_data[rule_type + '_rule'] 300 rule_data = rule_data[rule_type + '_rule']
300 301
301 with db_api.autonested_transaction(context.session): 302 with lib_db_api.autonested_transaction(context.session):
302 # Ensure that we have access to the policy. 303 # Ensure that we have access to the policy.
303 policy = policy_object.QosPolicy.get_policy_obj(context, policy_id) 304 policy = policy_object.QosPolicy.get_policy_obj(context, policy_id)
304 checker.check_bandwidth_rule_conflict(policy, rule_data) 305 checker.check_bandwidth_rule_conflict(policy, rule_data)
@@ -335,7 +336,7 @@ class QoSPlugin(qos.QoSPluginBase):
335 rule_type = rule_cls.rule_type 336 rule_type = rule_cls.rule_type
336 rule_data = rule_data[rule_type + '_rule'] 337 rule_data = rule_data[rule_type + '_rule']
337 338
338 with db_api.autonested_transaction(context.session): 339 with lib_db_api.autonested_transaction(context.session):
339 # Ensure we have access to the policy. 340 # Ensure we have access to the policy.
340 policy = policy_object.QosPolicy.get_policy_obj(context, policy_id) 341 policy = policy_object.QosPolicy.get_policy_obj(context, policy_id)
341 # Ensure the rule belongs to the policy. 342 # Ensure the rule belongs to the policy.
@@ -368,7 +369,7 @@ class QoSPlugin(qos.QoSPluginBase):
368 369
369 :returns: None 370 :returns: None
370 """ 371 """
371 with db_api.autonested_transaction(context.session): 372 with lib_db_api.autonested_transaction(context.session):
372 # Ensure we have access to the policy. 373 # Ensure we have access to the policy.
373 policy = policy_object.QosPolicy.get_policy_obj(context, policy_id) 374 policy = policy_object.QosPolicy.get_policy_obj(context, policy_id)
374 rule = policy.get_rule_by_id(rule_id) 375 rule = policy.get_rule_by_id(rule_id)
@@ -397,7 +398,7 @@ class QoSPlugin(qos.QoSPluginBase):
397 :returns: a QoS policy rule object 398 :returns: a QoS policy rule object
398 :raises: n_exc.QosRuleNotFound 399 :raises: n_exc.QosRuleNotFound
399 """ 400 """
400 with db_api.autonested_transaction(context.session): 401 with lib_db_api.autonested_transaction(context.session):
401 # Ensure we have access to the policy. 402 # Ensure we have access to the policy.
402 policy_object.QosPolicy.get_policy_obj(context, policy_id) 403 policy_object.QosPolicy.get_policy_obj(context, policy_id)
403 rule = rule_cls.get_object(context, id=rule_id) 404 rule = rule_cls.get_object(context, id=rule_id)
@@ -422,7 +423,7 @@ class QoSPlugin(qos.QoSPluginBase):
422 423
423 :returns: QoS policy rule objects meeting the search criteria 424 :returns: QoS policy rule objects meeting the search criteria
424 """ 425 """
425 with db_api.autonested_transaction(context.session): 426 with lib_db_api.autonested_transaction(context.session):
426 # Ensure we have access to the policy. 427 # Ensure we have access to the policy.
427 policy_object.QosPolicy.get_policy_obj(context, policy_id) 428 policy_object.QosPolicy.get_policy_obj(context, policy_id)
428 filters = filters or dict() 429 filters = filters or dict()
diff --git a/neutron/services/trunk/plugin.py b/neutron/services/trunk/plugin.py
index 6270ea0..88afb45 100644
--- a/neutron/services/trunk/plugin.py
+++ b/neutron/services/trunk/plugin.py
@@ -20,13 +20,13 @@ from neutron_lib.callbacks import events
20from neutron_lib.callbacks import registry 20from neutron_lib.callbacks import registry
21from neutron_lib.callbacks import resources 21from neutron_lib.callbacks import resources
22from neutron_lib import context 22from neutron_lib import context
23from neutron_lib.db import api as db_api
23from neutron_lib.plugins import directory 24from neutron_lib.plugins import directory
24from neutron_lib.services import base as service_base 25from neutron_lib.services import base as service_base
25from oslo_log import log as logging 26from oslo_log import log as logging
26from oslo_utils import uuidutils 27from oslo_utils import uuidutils
27 28
28from neutron.db import _resource_extend as resource_extend 29from neutron.db import _resource_extend as resource_extend
29from neutron.db import api as db_api
30from neutron.db import common_db_mixin 30from neutron.db import common_db_mixin
31from neutron.db import db_base_plugin_common 31from neutron.db import db_base_plugin_common
32from neutron.objects import base as objects_base 32from neutron.objects import base as objects_base
diff --git a/neutron/services/trunk/rpc/server.py b/neutron/services/trunk/rpc/server.py
index 7004632..677d8b8 100644
--- a/neutron/services/trunk/rpc/server.py
+++ b/neutron/services/trunk/rpc/server.py
@@ -15,6 +15,7 @@
15import collections 15import collections
16 16
17from neutron_lib.api.definitions import portbindings 17from neutron_lib.api.definitions import portbindings
18from neutron_lib.db import api as db_api
18from neutron_lib.plugins import directory 19from neutron_lib.plugins import directory
19from oslo_log import helpers as log_helpers 20from oslo_log import helpers as log_helpers
20from oslo_log import log as logging 21from oslo_log import log as logging
@@ -25,7 +26,6 @@ from neutron.api.rpc.callbacks.producer import registry
25from neutron.api.rpc.callbacks import resources 26from neutron.api.rpc.callbacks import resources
26from neutron.api.rpc.handlers import resources_rpc 27from neutron.api.rpc.handlers import resources_rpc
27from neutron.common import rpc as n_rpc 28from neutron.common import rpc as n_rpc
28from neutron.db import api as db_api
29from neutron.objects import trunk as trunk_objects 29from neutron.objects import trunk as trunk_objects
30from neutron.services.trunk import constants as trunk_consts 30from neutron.services.trunk import constants as trunk_consts
31from neutron.services.trunk import exceptions as trunk_exc 31from neutron.services.trunk import exceptions as trunk_exc
diff --git a/neutron/tests/unit/db/test_db_base_plugin_v2.py b/neutron/tests/unit/db/test_db_base_plugin_v2.py
index ed9145d..a1125e4 100644
--- a/neutron/tests/unit/db/test_db_base_plugin_v2.py
+++ b/neutron/tests/unit/db/test_db_base_plugin_v2.py
@@ -6274,7 +6274,7 @@ class DbModelMixin(object):
6274 network = models_v2.Network(name="net_net", status="OK", 6274 network = models_v2.Network(name="net_net", status="OK",
6275 admin_state_up=True) 6275 admin_state_up=True)
6276 ctx.session.add(network) 6276 ctx.session.add(network)
6277 with db_api.autonested_transaction(ctx.session): 6277 with lib_db_api.autonested_transaction(ctx.session):
6278 sg = sg_models.SecurityGroup(name='sg', description='sg') 6278 sg = sg_models.SecurityGroup(name='sg', description='sg')
6279 ctx.session.add(sg) 6279 ctx.session.add(sg)
6280 # ensure db rels aren't loaded until commit for network object 6280 # ensure db rels aren't loaded until commit for network object
diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py
index a06551a..a5710e0 100644
--- a/neutron/tests/unit/objects/test_base.py
+++ b/neutron/tests/unit/objects/test_base.py
@@ -20,6 +20,7 @@ import mock
20import netaddr 20import netaddr
21from neutron_lib import constants 21from neutron_lib import constants
22from neutron_lib import context 22from neutron_lib import context
23from neutron_lib.db import api as lib_db_api
23from neutron_lib import exceptions as n_exc 24from neutron_lib import exceptions as n_exc
24from neutron_lib.objects import exceptions as o_exc 25from neutron_lib.objects import exceptions as o_exc
25from neutron_lib.objects import utils as obj_utils 26from neutron_lib.objects import utils as obj_utils
@@ -1706,7 +1707,7 @@ class BaseDbObjectTestCase(_BaseObjectTestCase,
1706 1707
1707 def test_get_objects_single_transaction(self): 1708 def test_get_objects_single_transaction(self):
1708 with mock.patch(self._get_ro_txn_exit_func_name()) as mock_exit: 1709 with mock.patch(self._get_ro_txn_exit_func_name()) as mock_exit:
1709 with db_api.autonested_transaction(self.context.session): 1710 with lib_db_api.autonested_transaction(self.context.session):
1710 self._test_class.get_objects(self.context) 1711 self._test_class.get_objects(self.context)
1711 self.assertEqual(1, mock_exit.call_count) 1712 self.assertEqual(1, mock_exit.call_count)
1712 1713
@@ -1721,7 +1722,7 @@ class BaseDbObjectTestCase(_BaseObjectTestCase,
1721 obj.create() 1722 obj.create()
1722 1723
1723 with mock.patch(self._get_ro_txn_exit_func_name()) as mock_exit: 1724 with mock.patch(self._get_ro_txn_exit_func_name()) as mock_exit:
1724 with db_api.autonested_transaction(self.context.session): 1725 with lib_db_api.autonested_transaction(self.context.session):
1725 obj = self._test_class.get_object(self.context, 1726 obj = self._test_class.get_object(self.context,
1726 **obj._get_composite_keys()) 1727 **obj._get_composite_keys())
1727 self.assertEqual(1, mock_exit.call_count) 1728 self.assertEqual(1, mock_exit.call_count)