From: Vishvananda Ishaya Date: Fri, 25 Jul 2014 17:15:27 +0000 (-0700) Subject: Add index for reservations on (deleted, expire) X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=4d56b91628518629b47997b93acd9cb1e48f87f5;p=openstack-build%2Fcinder-build.git Add index for reservations on (deleted, expire) the query for expire_reservations currently does a full table scan. This adds an index so frequent invocations of expire does not bog down the database. Change-Id: Ic6f6e4262746753d869ad37b8aaaa5ffc1c4efef Resolves-bug: 1348720 --- diff --git a/cinder/db/sqlalchemy/migrate_repo/versions/023_add_expire_reservations_index.py b/cinder/db/sqlalchemy/migrate_repo/versions/023_add_expire_reservations_index.py new file mode 100644 index 000000000..52f0461d4 --- /dev/null +++ b/cinder/db/sqlalchemy/migrate_repo/versions/023_add_expire_reservations_index.py @@ -0,0 +1,43 @@ +# All Rights Reserved. +# +# 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 sqlalchemy import Index, MetaData, Table +from sqlalchemy.exc import IntegrityError + + +def upgrade(migrate_engine): + meta = MetaData() + meta.bind = migrate_engine + + reservations = Table('reservations', meta, autoload=True) + + # Based on expire_reservations query + # from: cinder/db/sqlalchemy/api.py + index = Index('reservations_deleted_expire_idx', + reservations.c.deleted, reservations.c.expire) + try: + index.create(migrate_engine) + except IntegrityError: + pass + + +def downgrade(migrate_engine): + meta = MetaData() + meta.bind = migrate_engine + + reservations = Table('reservations', meta, autoload=True) + + index = Index('reservations_deleted_expire_idx', + reservations.c.deleted, reservations.c.expire) + index.drop(migrate_engine) diff --git a/cinder/tests/test_migrations.py b/cinder/tests/test_migrations.py index 156fdc362..8537b6231 100644 --- a/cinder/tests/test_migrations.py +++ b/cinder/tests/test_migrations.py @@ -1071,3 +1071,36 @@ class TestMigrations(test.TestCase): metadata, autoload=True) self.assertNotIn('disabled_reason', services.c) + + def test_migration_023(self): + """Test that adding reservations index works correctly.""" + for (key, engine) in self.engines.items(): + migration_api.version_control(engine, + TestMigrations.REPOSITORY, + migration.db_initial_version()) + migration_api.upgrade(engine, TestMigrations.REPOSITORY, 22) + metadata = sqlalchemy.schema.MetaData() + metadata.bind = engine + + migration_api.upgrade(engine, TestMigrations.REPOSITORY, 23) + reservations = sqlalchemy.Table('reservations', + metadata, + autoload=True) + index_colums = [] + for idx in reservations.indexes: + if idx.name == 'reservations_deleted_expire_idx': + index_columns = idx.columns.keys() + break + + self.assertEqual(sorted(['deleted', 'expire']), + sorted(index_columns)) + + migration_api.downgrade(engine, TestMigrations.REPOSITORY, 22) + metadata = sqlalchemy.schema.MetaData() + metadata.bind = engine + + reservations = sqlalchemy.Table('reservations', + metadata, + autoload=True) + index_names = [idx.name for idx in reservations.indexes] + self.assertNotIn('reservations_deleted_expire_idx', index_names)