]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Filter hosts with pool in snapshot_get_by_host
authorIvan Kolodyazhny <e0ne@e0ne.info>
Wed, 9 Sep 2015 13:59:55 +0000 (16:59 +0300)
committerIvan Kolodyazhny <e0ne@e0ne.info>
Wed, 9 Sep 2015 14:37:56 +0000 (17:37 +0300)
Snapshots use host field from the volume record. We need to ignore pool
if it is not specified in a host param for snapshot_get_by_host method.

Also snapshot_get_by_host will return empty list if host is None or an
empty string like a volume filer does.

Change-Id: I0d9eef8675d8dada3a0b6886dbb3f5807eecee73
Closes-Bug: #1493856

cinder/db/sqlalchemy/api.py
cinder/tests/unit/test_db_api.py

index c6e70ff5bfc41d89bd7c10ad38a26675e0da9950..622b7eb0d320f22109dd98131a0338f394c01c7d 100644 (file)
@@ -2159,9 +2159,23 @@ def snapshot_get_by_host(context, host, filters=None):
     if filters:
         query = query.filter_by(**filters)
 
-    return query.join(models.Snapshot.volume).filter(
-        models.Volume.host == host).options(
-            joinedload('snapshot_metadata')).all()
+    # As a side effect of the introduction of pool-aware scheduler,
+    # newly created volumes will have pool information appended to
+    # 'host' field of a volume record. So a volume record in DB can
+    # now be either form below:
+    #     Host
+    #     Host#Pool
+    if host and isinstance(host, six.string_types):
+        session = get_session()
+        with session.begin():
+            host_attr = getattr(models.Volume, 'host')
+            conditions = [host_attr == host,
+                          host_attr.op('LIKE')(host + '#%')]
+            query = query.join(models.Snapshot.volume).filter(
+                or_(*conditions)).options(joinedload('snapshot_metadata'))
+            return query.all()
+    elif not host:
+        return []
 
 
 @require_context
index 3a6b2c1e9c4776e7521423c5ab33277fc3a316d8..d732f61ee5c180e0d5fd7a302f239c365b1accfa 100644 (file)
@@ -1149,6 +1149,30 @@ class DBAPISnapshotTestCase(BaseTest):
                                             'host2', {'fake_key': 'fake'}),
                                         ignored_keys='volume')
 
+    def test_snapshot_get_by_host_with_pools(self):
+        db.volume_create(self.ctxt, {'id': 1, 'host': 'host1#pool1'})
+        db.volume_create(self.ctxt, {'id': 2, 'host': 'host1#pool2'})
+
+        snapshot1 = db.snapshot_create(self.ctxt, {'id': 1, 'volume_id': 1})
+        snapshot2 = db.snapshot_create(self.ctxt, {'id': 2, 'volume_id': 2})
+
+        self._assertEqualListsOfObjects([snapshot1, snapshot2],
+                                        db.snapshot_get_by_host(
+                                            self.ctxt,
+                                            'host1'),
+                                        ignored_keys='volume')
+        self._assertEqualListsOfObjects([snapshot1],
+                                        db.snapshot_get_by_host(
+                                            self.ctxt,
+                                            'host1#pool1'),
+                                        ignored_keys='volume')
+
+        self._assertEqualListsOfObjects([],
+                                        db.snapshot_get_by_host(
+                                            self.ctxt,
+                                            'host1#pool0'),
+                                        ignored_keys='volume')
+
     def test_snapshot_get_all_by_project(self):
         db.volume_create(self.ctxt, {'id': 1})
         db.volume_create(self.ctxt, {'id': 2})