Disable transaction guard on plugin level

Neutron transaction guard throws an exception when certain object
modification apis are called with active session. This happens for
a bunch of GBP flows. Until a better solution is engineered, we
disable the transaction guard.

This solves a bug where network is left behind after ptg is deleted.

Note: this has to be a temporary solution, since the option to disable
transaction guard is temporary in neutron and is expected to go away.

Change-Id: I828ed5d96587831c54d0f6fe3d3ea582f64e184f
This commit is contained in:
Anna Khmelnitsky 2017-09-15 08:38:34 -07:00
parent e6032e5881
commit 741fdd2beb
4 changed files with 90 additions and 33 deletions

View File

@ -1,3 +1,17 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import functools
GBP_PLURALS = {}
@ -8,3 +22,14 @@ def register_plurals(plural_mappings):
def get_plural(single):
return GBP_PLURALS.get(single)
def disable_transaction_guard(f):
# We do not want to enforce transaction guard
# TODO(annak): this is a temporary measure since GUARD_TRANSACTION
# is expected to stop being enforced in near future
@functools.wraps(f)
def inner(self, context, *args, **kwargs):
setattr(context, 'GUARD_TRANSACTION', False)
return f(self, context, *args, **kwargs)
return inner

View File

@ -18,11 +18,10 @@
# modules save a reference to the functions being patched
from gbpservice._i18n import _LE
from gbpservice._i18n import _LI
from gbpservice.neutron import extensions as gbp_extensions
from gbpservice.neutron.extensions import patch # noqa
from gbpservice.neutron.plugins.ml2plus import patch_neutron # noqa
import functools
from neutron.api.v2 import attributes
from neutron.callbacks import events
from neutron.callbacks import registry
@ -105,15 +104,6 @@ opts = [
cfg.CONF.register_opts(opts, "ml2plus")
def disable_transaction_guard(f):
# We do not want to enforce transaction guard
@functools.wraps(f)
def inner(self, context, *args, **kwargs):
setattr(context, 'GUARD_TRANSACTION', False)
return f(self, context, *args, **kwargs)
return inner
class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
implicitsubnetpool_db.ImplicitSubnetpoolMixin):
@ -248,23 +238,23 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
address_scope)
return self._fields(res, fields)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def create_network(self, context, network):
self._ensure_tenant(context, network[attributes.NETWORK])
return super(Ml2PlusPlugin, self).create_network(context, network)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def update_network(self, context, id, network):
return super(Ml2PlusPlugin, self).update_network(context, id, network)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def delete_network(self, context, id):
return super(Ml2PlusPlugin, self).delete_network(context, id)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def create_network_bulk(self, context, networks):
self._ensure_tenant_bulk(context, networks[attributes.NETWORKS],
@ -272,23 +262,23 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
return super(Ml2PlusPlugin, self).create_network_bulk(context,
networks)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def create_subnet(self, context, subnet):
self._ensure_tenant(context, subnet[attributes.SUBNET])
return super(Ml2PlusPlugin, self).create_subnet(context, subnet)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def update_subnet(self, context, id, subnet):
return super(Ml2PlusPlugin, self).update_subnet(context, id, subnet)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def delete_subnet(self, context, id):
return super(Ml2PlusPlugin, self).delete_subnet(context, id)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def create_subnet_bulk(self, context, subnets):
self._ensure_tenant_bulk(context, subnets[attributes.SUBNETS],
@ -296,13 +286,13 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
return super(Ml2PlusPlugin, self).create_subnet_bulk(context,
subnets)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def create_port(self, context, port):
self._ensure_tenant(context, port[attributes.PORT])
return super(Ml2PlusPlugin, self).create_port(context, port)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def create_port_bulk(self, context, ports):
self._ensure_tenant_bulk(context, ports[attributes.PORTS],
@ -310,18 +300,18 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
return super(Ml2PlusPlugin, self).create_port_bulk(context,
ports)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def update_port(self, context, id, port):
return super(Ml2PlusPlugin, self).update_port(context, id, port)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def delete_port(self, context, id, l3_port_check=True):
return super(Ml2PlusPlugin, self).delete_port(
context, id, l3_port_check=l3_port_check)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive(context_var_name='plugin_context')
def get_bound_port_context(self, plugin_context, port_id, host=None,
cached_networks=None):
@ -329,26 +319,26 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
plugin_context, port_id, host=host,
cached_networks=cached_networks)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def update_port_status(self, context, port_id, status, host=None,
network=None):
return super(Ml2PlusPlugin, self).update_port_status(
context, port_id, status, host=host, network=network)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def port_bound_to_host(self, context, port_id, host):
return super(Ml2PlusPlugin, self).port_bound_to_host(
context, port_id, host)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def get_ports_from_devices(self, context, devices):
return super(Ml2PlusPlugin, self).get_ports_from_devices(
context, devices)
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def create_subnetpool(self, context, subnetpool):
self._ensure_tenant(context, subnetpool[attributes.SUBNETPOOL])
@ -374,7 +364,7 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
# REVISIT(rkukura): Is create_subnetpool_bulk() needed?
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
@db_api.retry_if_session_inactive()
def update_subnetpool(self, context, id, subnetpool):
session = context.session
@ -395,7 +385,7 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
self.mechanism_manager.update_subnetpool_postcommit(mech_context)
return updated_subnetpool
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
def delete_subnetpool(self, context, id):
session = context.session
with session.begin(subtransactions=True):
@ -412,7 +402,7 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
result['is_implicit'] = (
self.update_implicit_subnetpool(context, result))
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
def create_address_scope(self, context, address_scope):
self._ensure_tenant(context, address_scope[as_ext.ADDRESS_SCOPE])
session = context.session
@ -439,7 +429,7 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
# REVISIT(rkukura): Is create_address_scope_bulk() needed?
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
def update_address_scope(self, context, id, address_scope):
session = context.session
with session.begin(subtransactions=True):
@ -458,7 +448,7 @@ class Ml2PlusPlugin(ml2_plugin.Ml2Plugin,
self.mechanism_manager.update_address_scope_postcommit(mech_context)
return updated_address_scope
@disable_transaction_guard
@gbp_extensions.disable_transaction_guard
def delete_address_scope(self, context, id):
session = context.session
with session.begin(subtransactions=True):

View File

@ -462,6 +462,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_policy_target(self, context, policy_target):
self._ensure_tenant(context, policy_target['policy_target'])
self._add_fixed_ips_to_port_attributes(policy_target)
@ -492,6 +493,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_policy_target(self, context, policy_target_id, policy_target):
self._add_fixed_ips_to_port_attributes(policy_target)
session = context.session
@ -518,6 +520,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_policy_target(self, context, policy_target_id):
session = context.session
with session.begin(subtransactions=True):
@ -539,12 +542,14 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def get_policy_target(self, context, policy_target_id, fields=None):
return self._get_resource(context, 'policy_target', policy_target_id,
'PolicyTargetContext', fields=fields)
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def get_policy_targets(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
@ -555,6 +560,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_policy_target_group(self, context, policy_target_group):
self._ensure_tenant(context,
policy_target_group['policy_target_group'])
@ -586,6 +592,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_policy_target_group(self, context, policy_target_group_id,
policy_target_group):
session = context.session
@ -626,6 +633,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_policy_target_group(self, context, policy_target_group_id):
session = context.session
with session.begin(subtransactions=True):
@ -700,6 +708,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_application_policy_group(self, context,
application_policy_group):
self._ensure_tenant(
@ -731,6 +740,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_application_policy_group(self, context,
application_policy_group_id,
application_policy_group):
@ -764,6 +774,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_application_policy_group(self, context,
application_policy_group_id):
session = context.session
@ -806,6 +817,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_l2_policy(self, context, l2_policy):
self._ensure_tenant(context, l2_policy['l2_policy'])
session = context.session
@ -833,6 +845,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_l2_policy(self, context, l2_policy_id, l2_policy):
session = context.session
with session.begin(subtransactions=True):
@ -857,6 +870,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_l2_policy(self, context, l2_policy_id):
session = context.session
with session.begin(subtransactions=True):
@ -894,6 +908,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_network_service_policy(self, context, network_service_policy):
self._ensure_tenant(
context, network_service_policy['network_service_policy'])
@ -927,6 +942,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_network_service_policy(self, context, network_service_policy_id,
network_service_policy):
session = context.session
@ -957,6 +973,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_network_service_policy(
self, context, network_service_policy_id):
session = context.session
@ -998,6 +1015,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_l3_policy(self, context, l3_policy):
self._ensure_tenant(context, l3_policy['l3_policy'])
session = context.session
@ -1027,6 +1045,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_l3_policy(self, context, l3_policy_id, l3_policy):
session = context.session
with session.begin(subtransactions=True):
@ -1052,6 +1071,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_l3_policy(self, context, l3_policy_id, check_unused=False):
session = context.session
with session.begin(subtransactions=True):
@ -1094,6 +1114,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_policy_classifier(self, context, policy_classifier):
self._ensure_tenant(context,
policy_classifier['policy_classifier'])
@ -1125,6 +1146,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_policy_classifier(self, context, id, policy_classifier):
session = context.session
with session.begin(subtransactions=True):
@ -1150,6 +1172,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_policy_classifier(self, context, id):
session = context.session
with session.begin(subtransactions=True):
@ -1188,6 +1211,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_policy_action(self, context, policy_action):
self._ensure_tenant(context, policy_action['policy_action'])
session = context.session
@ -1218,6 +1242,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_policy_action(self, context, id, policy_action):
session = context.session
with session.begin(subtransactions=True):
@ -1244,6 +1269,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_policy_action(self, context, id):
session = context.session
with session.begin(subtransactions=True):
@ -1280,6 +1306,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_policy_rule(self, context, policy_rule):
self._ensure_tenant(context, policy_rule['policy_rule'])
session = context.session
@ -1309,6 +1336,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_policy_rule(self, context, id, policy_rule):
session = context.session
with session.begin(subtransactions=True):
@ -1333,6 +1361,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_policy_rule(self, context, id):
session = context.session
with session.begin(subtransactions=True):
@ -1370,6 +1399,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_policy_rule_set(self, context, policy_rule_set):
self._ensure_tenant(context, policy_rule_set['policy_rule_set'])
session = context.session
@ -1400,6 +1430,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_policy_rule_set(self, context, id, policy_rule_set):
session = context.session
with session.begin(subtransactions=True):
@ -1425,6 +1456,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_policy_rule_set(self, context, id):
session = context.session
with session.begin(subtransactions=True):
@ -1461,6 +1493,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_external_segment(self, context, external_segment):
self._ensure_tenant(context, external_segment['external_segment'])
session = context.session
@ -1494,6 +1527,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_external_segment(self, context, external_segment_id,
external_segment):
session = context.session
@ -1525,6 +1559,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_external_segment(self, context, external_segment_id):
session = context.session
with session.begin(subtransactions=True):
@ -1566,6 +1601,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_external_policy(self, context, external_policy):
self._ensure_tenant(context, external_policy['external_policy'])
session = context.session
@ -1596,6 +1632,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_external_policy(self, context, external_policy_id,
external_policy):
session = context.session
@ -1624,6 +1661,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_external_policy(self, context, external_policy_id,
check_unused=False):
session = context.session
@ -1662,6 +1700,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def create_nat_pool(self, context, nat_pool):
self._ensure_tenant(context, nat_pool['nat_pool'])
session = context.session
@ -1689,6 +1728,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def update_nat_pool(self, context, nat_pool_id, nat_pool):
session = context.session
with session.begin(subtransactions=True):
@ -1711,6 +1751,7 @@ class GroupPolicyPlugin(group_policy_mapping_db.GroupPolicyMappingDbPlugin):
@log.log_method_call
@db_api.retry_if_session_inactive()
@gbp_extensions.disable_transaction_guard
def delete_nat_pool(self, context, nat_pool_id, check_unused=False):
session = context.session
with session.begin(subtransactions=True):

View File

@ -266,6 +266,7 @@ class NFPNodeDriverTestCase(
class DummyMap(object):
network_function_id = '12'
status = 'UP'
class TestServiceChainInstance(NFPNodeDriverTestCase):