From f8c879ddbf7628e9a873d6a213e4905097455a46 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Mon, 20 Sep 2021 15:55:04 +0000 Subject: [PATCH] Add new indexes to RBAC DB models Added two new indexes to all RBAC DB models: "target_tenant" and "action". The DB models affected are "networkrbacs", "qospolicyrbacs", "securitygrouprbacs", "addressscoperbacs", "subnetpoolrbacs" and "addressgrouprbacs". The goal of this patch is to speed up the model query if RBAC apply to this object. If the object query scope is a project, [1] will be added to the DB query. If "action" and "target_tenant" are indexed, the exact match filtering will be faster. [1]https://github.com/openstack/neutron-lib/blob/890d62a3df3f35bb18bf1a11e79a9e97e7dd2d2c/neutron_lib/db/model_query.py#L123-L131 Change-Id: I0a70a1a500fad52ca55006d6e2ebc1044aef0fc8 Closes-Bug: #1918145 --- .../alembic_migrations/versions/EXPAND_HEAD | 2 +- .../ba859d649675_add_indexes_to_rbacs.py | 41 +++++++++++++++++++ neutron/db/rbac_db_models.py | 4 +- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 neutron/db/migration/alembic_migrations/versions/yoga/expand/ba859d649675_add_indexes_to_rbacs.py diff --git a/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD b/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD index 1fabf4a11aa..e2ec2347a47 100644 --- a/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD +++ b/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD @@ -1 +1 @@ -c181bb1d89e4 +ba859d649675 diff --git a/neutron/db/migration/alembic_migrations/versions/yoga/expand/ba859d649675_add_indexes_to_rbacs.py b/neutron/db/migration/alembic_migrations/versions/yoga/expand/ba859d649675_add_indexes_to_rbacs.py new file mode 100644 index 00000000000..affd7e03fa0 --- /dev/null +++ b/neutron/db/migration/alembic_migrations/versions/yoga/expand/ba859d649675_add_indexes_to_rbacs.py @@ -0,0 +1,41 @@ +# Copyright 2021 OpenStack Foundation +# +# 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. +# + +from alembic import op + + +"""Add indexes to RBACs + +Revision ID: ba859d649675 +Revises: c181bb1d89e4 +Create Date: 2021-09-20 15:22:04.668376 + +""" + +# revision identifiers, used by Alembic. +revision = 'ba859d649675' +down_revision = 'c181bb1d89e4' + +OBJECTS = ('network', 'qospolicy', 'securitygroup', 'addressscope', + 'subnetpool', 'addressgroup') +COLUMNS = ('target_tenant', 'action') + + +def upgrade(): + for object in OBJECTS: + table = object + 'rbacs' + ix = 'ix_' + table + '_' + for column in COLUMNS: + op.create_index(op.f(ix + column), table, [column], unique=False) diff --git a/neutron/db/rbac_db_models.py b/neutron/db/rbac_db_models.py index 149a5f9e345..59d167efb0f 100644 --- a/neutron/db/rbac_db_models.py +++ b/neutron/db/rbac_db_models.py @@ -47,9 +47,9 @@ class RBACColumns(model_base.HasId, model_base.HasProject): # also be a wildcard '*' to indicate all tenants or it may be a role if # neutron gets better integration with keystone target_tenant = sa.Column(sa.String(db_const.PROJECT_ID_FIELD_SIZE), - nullable=False) + nullable=False, index=True) - action = sa.Column(sa.String(255), nullable=False) + action = sa.Column(sa.String(255), nullable=False, index=True) @property @abc.abstractmethod