From 70fbe736b76aeae5327413e14f9a78681b57fb96 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Wed, 15 Mar 2017 12:09:10 +0200 Subject: [PATCH] NSX-V3: Fix QoS delete In commit I1f863bf91f712d4b12db753b13cc6b842b6918a4 the qos driver stopped using the message queue. So the delete callback is now called in the plugin context, after the qos policy is already deleted. This means that we cannot access the backend profile id in the DB, since it was already deleted too. The fix is to remove the foreign key constraint from the mapping table, and manually delete the entry. Change-Id: I28e1c7c5ad0bb487d9c19ce76e2f7820f029678e --- vmware_nsx/db/db.py | 5 +++ .../alembic_migrations/versions/CONTRACT_HEAD | 2 +- ...fa27115_nsxv3_qos_policy_no_foreign_key.py | 35 +++++++++++++++++++ vmware_nsx/db/nsx_models.py | 2 -- vmware_nsx/services/qos/nsx_v3/utils.py | 3 ++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 vmware_nsx/db/migration/alembic_migrations/versions/pike/contract/84ceffa27115_nsxv3_qos_policy_no_foreign_key.py diff --git a/vmware_nsx/db/db.py b/vmware_nsx/db/db.py index a1d3230ec6..7f3fffcd1e 100644 --- a/vmware_nsx/db/db.py +++ b/vmware_nsx/db/db.py @@ -374,6 +374,11 @@ def get_switch_profile_by_qos_policy(session, qos_policy_id): raise nsx_exc.NsxQosPolicyMappingNotFound(policy=qos_policy_id) +def delete_qos_policy_profile_mapping(session, qos_policy_id): + return (session.query(nsx_models.QosPolicySwitchProfile). + filter_by(qos_policy_id=qos_policy_id).delete()) + + # NSXv3 Port Mirror Sessions DB methods. def add_port_mirror_session_mapping(session, tf_id, pm_session_id): with session.begin(subtransactions=True): diff --git a/vmware_nsx/db/migration/alembic_migrations/versions/CONTRACT_HEAD b/vmware_nsx/db/migration/alembic_migrations/versions/CONTRACT_HEAD index e6990a3c42..74d759e164 100644 --- a/vmware_nsx/db/migration/alembic_migrations/versions/CONTRACT_HEAD +++ b/vmware_nsx/db/migration/alembic_migrations/versions/CONTRACT_HEAD @@ -1 +1 @@ -8c0a81a07691 \ No newline at end of file +84ceffa27115 \ No newline at end of file diff --git a/vmware_nsx/db/migration/alembic_migrations/versions/pike/contract/84ceffa27115_nsxv3_qos_policy_no_foreign_key.py b/vmware_nsx/db/migration/alembic_migrations/versions/pike/contract/84ceffa27115_nsxv3_qos_policy_no_foreign_key.py new file mode 100644 index 0000000000..4940c89396 --- /dev/null +++ b/vmware_nsx/db/migration/alembic_migrations/versions/pike/contract/84ceffa27115_nsxv3_qos_policy_no_foreign_key.py @@ -0,0 +1,35 @@ +# Copyright 2017 VMware, Inc. +# +# 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. + +"""remove the foreign key constrain from nsxv3_qos_policy_mapping + +Revision ID: 84ceffa27115 +Revises: 8c0a81a07691 +Create Date: 2017-03-15 11:47:09.450116 +""" + +# revision identifiers, used by Alembic. +revision = '84ceffa27115' +down_revision = '8c0a81a07691' + +from alembic import op +from sqlalchemy.engine import reflection + + +def upgrade(): + table_name = 'neutron_nsx_qos_policy_mappings' + inspector = reflection.Inspector.from_engine(op.get_bind()) + fk_constraint = inspector.get_foreign_keys(table_name)[0] + op.drop_constraint(fk_constraint.get('name'), table_name, + type_='foreignkey') diff --git a/vmware_nsx/db/nsx_models.py b/vmware_nsx/db/nsx_models.py index 873804abaf..02003ab021 100644 --- a/vmware_nsx/db/nsx_models.py +++ b/vmware_nsx/db/nsx_models.py @@ -355,8 +355,6 @@ class QosPolicySwitchProfile(model_base.BASEV2, models.TimestampMixin): # Maps neutron qos policy identifiers to NSX-V3 switch profile identifiers __tablename__ = 'neutron_nsx_qos_policy_mappings' qos_policy_id = sa.Column(sa.String(36), - sa.ForeignKey('qos_policies.id', - ondelete='CASCADE'), primary_key=True) switch_profile_id = sa.Column(sa.String(36), nullable=False) diff --git a/vmware_nsx/services/qos/nsx_v3/utils.py b/vmware_nsx/services/qos/nsx_v3/utils.py index 9124e3d4a6..4c6ee7a63a 100644 --- a/vmware_nsx/services/qos/nsx_v3/utils.py +++ b/vmware_nsx/services/qos/nsx_v3/utils.py @@ -118,7 +118,10 @@ class QosNotificationsHandler(object): def delete_policy(self, context, policy_id): profile_id = nsx_db.get_switch_profile_by_qos_policy( context.session, policy_id) + # delete the profile id from the backend and the DB self._nsxlib_qos.delete(profile_id) + nsx_db.delete_qos_policy_profile_mapping( + context.session, policy_id) def update_policy(self, context, policy_id, policy): profile_id = nsx_db.get_switch_profile_by_qos_policy(