]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add index for reservations on (deleted, expire)
authorVishvananda Ishaya <vishvananda@gmail.com>
Fri, 25 Jul 2014 17:15:27 +0000 (10:15 -0700)
committerVishvananda Ishaya <vishvananda@gmail.com>
Fri, 25 Jul 2014 17:15:27 +0000 (10:15 -0700)
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

cinder/db/sqlalchemy/migrate_repo/versions/023_add_expire_reservations_index.py [new file with mode: 0644]
cinder/tests/test_migrations.py

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 (file)
index 0000000..52f0461
--- /dev/null
@@ -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)
index 156fdc36260d8cddda73ecdc14f67ee5175a03cd..8537b623103759e8f1f4dc32f887e04761121672 100644 (file)
@@ -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)