Bypass the database if limit=0 for server-list requests

Not all database engines handle fetching 0 rows the same and there isn't
really a point in going to the database to return nothing, so bypass the
DB query if limit=0.

The Nova API is explicitly checking if limit >= 0 so we can't make any
changes there since they would be backwards incompatible, i.e. to start
enforcing limit > 0.

Closes-Bug: #1296929

Change-Id: Ib3d0318bec972af5f1f25202101e76066f930f34
This commit is contained in:
Matt Riedemann
2014-03-24 14:43:23 -07:00
parent 0722a8fb23
commit d63a7012d2
2 changed files with 9 additions and 0 deletions

View File

@@ -1850,6 +1850,10 @@ def instance_get_all_by_filters(context, filters, sort_key, sort_dir,
include or exclude instances whose
vm_state is SOFT_DELETED.
"""
# NOTE(mriedem): If the limit is 0 there is no point in even going
# to the database since nothing is going to be returned anyway.
if limit == 0:
return []
sort_fn = {'desc': desc, 'asc': asc}

View File

@@ -1542,6 +1542,11 @@ class InstanceTestCase(test.TestCase, ModelsObjectComparatorMixin):
filtered_instances = db.instance_get_all_by_filters(self.ctxt, {})
self._assertEqualListsOfInstances(instances, filtered_instances)
def test_instance_get_all_by_filters_zero_limit(self):
self.create_instance_with_args()
instances = db.instance_get_all_by_filters(self.ctxt, {}, limit=0)
self.assertEqual([], instances)
def test_instance_metadata_get_multi(self):
uuids = [self.create_instance_with_args()['uuid'] for i in range(3)]
meta = sqlalchemy_api._instance_metadata_get_multi(self.ctxt, uuids)