From cfa0b34476a5cd9c4d570b6d0023ff49b781d8d7 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Tue, 10 Oct 2023 10:53:21 +0000 Subject: [PATCH] [sqlalchemy-20] Replace the context writer/reader This patch replaces the old DB contexts and removes the subtransactions. Change-Id: I9ac48eeba9f7d5d01153edb4b2f3341008ac2e01 --- .../db/firewall/v2/firewall_db_v2.py | 9 ++++---- .../6941ce70131e_add_standard_attr_id.py | 21 +++++++++---------- .../firewall/service_drivers/driver_api.py | 15 ++++++------- .../service_drivers/agents/test_agents.py | 7 ++++--- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/neutron_fwaas/db/firewall/v2/firewall_db_v2.py b/neutron_fwaas/db/firewall/v2/firewall_db_v2.py index bc432683d..196325567 100644 --- a/neutron_fwaas/db/firewall/v2/firewall_db_v2.py +++ b/neutron_fwaas/db/firewall/v2/firewall_db_v2.py @@ -846,11 +846,10 @@ class FirewallPluginDb(object): exc_ports = [] for port_id in port_id_list: try: - with context.session.begin(subtransactions=True): - fwg_port_db = FirewallGroupPortAssociation( - firewall_group_id=fwg_db['id'], - port_id=port_id) - context.session.add(fwg_port_db) + fwg_port_db = FirewallGroupPortAssociation( + firewall_group_id=fwg_db['id'], + port_id=port_id) + context.session.add(fwg_port_db) except db_exc.DBDuplicateEntry: exc_ports.append(port_id) if exc_ports: diff --git a/neutron_fwaas/db/migration/alembic_migrations/versions/2023.2/expand/6941ce70131e_add_standard_attr_id.py b/neutron_fwaas/db/migration/alembic_migrations/versions/2023.2/expand/6941ce70131e_add_standard_attr_id.py index 27213be89..a5a4fafb1 100644 --- a/neutron_fwaas/db/migration/alembic_migrations/versions/2023.2/expand/6941ce70131e_add_standard_attr_id.py +++ b/neutron_fwaas/db/migration/alembic_migrations/versions/2023.2/expand/6941ce70131e_add_standard_attr_id.py @@ -45,17 +45,16 @@ def generate_records_for_existing(table): sa.Column('standard_attr_id', sa.BigInteger(), nullable=True)) session = sa.orm.Session(bind=op.get_bind()) - with session.begin(subtransactions=True): - for row in session.query(model): - res = session.execute( - standardattrs.insert().values(resource_type=table, - description=row[1]) - ) - session.execute( - model.update().values( - standard_attr_id=res.inserted_primary_key[0]).where( - model.c.id == row[0]) - ) + for row in session.query(model): + res = session.execute( + standardattrs.insert().values(resource_type=table, + description=row[1]) + ) + session.execute( + model.update().values( + standard_attr_id=res.inserted_primary_key[0]).where( + model.c.id == row[0]) + ) session.commit() diff --git a/neutron_fwaas/services/firewall/service_drivers/driver_api.py b/neutron_fwaas/services/firewall/service_drivers/driver_api.py index 58b7eec8c..a23951b77 100644 --- a/neutron_fwaas/services/firewall/service_drivers/driver_api.py +++ b/neutron_fwaas/services/firewall/service_drivers/driver_api.py @@ -20,6 +20,7 @@ import copy from neutron_lib.callbacks import events from neutron_lib.callbacks import registry from neutron_lib import constants as nl_constants +from neutron_lib.db import api as db_api from neutron_lib.plugins import directory from oslo_log import log as logging @@ -134,16 +135,16 @@ class FirewallDriverDBMixin(FirewallDriver, metaclass=abc.ABCMeta): self.firewall_db = firewall_db_v2.FirewallPluginDb() @staticmethod + @db_api.CONTEXT_READER def _update_resource_status(context, resource_type, resource_dict): - with context.session.begin(subtransactions=True): - context.session.query(resource_type).\ - filter_by(id=resource_dict['id']).\ - update({'status': resource_dict['status']}) + context.session.query(resource_type).\ + filter_by(id=resource_dict['id']).\ + update({'status': resource_dict['status']}) # Firewall Group def create_firewall_group(self, context, firewall_group): request_body = firewall_group - with context.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(context): firewall_group = self.firewall_db.create_firewall_group( context, firewall_group) self.create_firewall_group_precommit(context, firewall_group) @@ -231,7 +232,7 @@ class FirewallDriverDBMixin(FirewallDriver, metaclass=abc.ABCMeta): # Firewall Policy def create_firewall_policy(self, context, firewall_policy): request_body = firewall_policy - with context.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(context): firewall_policy = self.firewall_db.create_firewall_policy( context, firewall_policy) self.create_firewall_policy_precommit(context, firewall_policy) @@ -310,7 +311,7 @@ class FirewallDriverDBMixin(FirewallDriver, metaclass=abc.ABCMeta): # Firewall Rule def create_firewall_rule(self, context, firewall_rule): request_body = firewall_rule - with context.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(context): firewall_rule = self.firewall_db.create_firewall_rule( context, firewall_rule) self.create_firewall_rule_precommit(context, firewall_rule) diff --git a/neutron_fwaas/tests/unit/services/firewall/service_drivers/agents/test_agents.py b/neutron_fwaas/tests/unit/services/firewall/service_drivers/agents/test_agents.py index c54be78b0..b1ee88f3e 100644 --- a/neutron_fwaas/tests/unit/services/firewall/service_drivers/agents/test_agents.py +++ b/neutron_fwaas/tests/unit/services/firewall/service_drivers/agents/test_agents.py @@ -20,6 +20,7 @@ from neutron import extensions as neutron_extensions from neutron.tests.unit.extensions import test_l3 from neutron_lib import constants as nl_constants from neutron_lib import context +from neutron_lib.db import api as db_api from neutron_lib.exceptions import firewall_v2 as f_exc from neutron_lib.plugins import directory from oslo_config import cfg @@ -163,7 +164,7 @@ class TestAgentDriver(test_fwaas_plugin_v2.FirewallPluginV2TestCase, do_delete=False ) as fwg: fwg_id = fwg['firewall_group']['id'] - with ctx.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(ctx): fwg_db = self.db._get_firewall_group(ctx, fwg_id) fwg_db['status'] = nl_constants.PENDING_DELETE @@ -183,7 +184,7 @@ class TestAgentDriver(test_fwaas_plugin_v2.FirewallPluginV2TestCase, def getdelete(context, fwg_id): fwg_db = _get_firewall_group(context, fwg_id) # NOTE(cby): Use a different session to simulate a concurrent del - with alt_ctx.session.begin(subtransactions=True): + with db_api.CONTEXT_READER.using(alt_ctx): alt_ctx.session.query(FirewallGroup).filter_by( id=fwg_id).delete() return fwg_db @@ -197,7 +198,7 @@ class TestAgentDriver(test_fwaas_plugin_v2.FirewallPluginV2TestCase, as_admin=True, ) as fwg: fwg_id = fwg['firewall_group']['id'] - with ctx.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(ctx): fwg_db = self.db._get_firewall_group(ctx, fwg_id) fwg_db['status'] = nl_constants.PENDING_DELETE ctx.session.flush()