Speed up standard flavor list command

currently this command tries to fetch extra_specs for any flavor
that does not have them (which is quite usual), regardless if the
command was even asked to display them (--long) at all.
This significantly slows down this command as it makes a lot of
unnecessary REST calls, one per each flavor to fetch extra_specs for.

With this patch, client only attempts to fetch flavor extra_specs if
the user actually called the client with --long.

Change-Id: Ia36414d891a41b641d7a9a04f0a1e7d43cfee351
Story: 2010343
Task: 46484
This commit is contained in:
Pavlo Shchelokovskyy 2022-08-05 14:03:21 +03:00 committed by Stephen Finucane
parent 00d8d945a1
commit ec8dba29f9
3 changed files with 66 additions and 1 deletions

View File

@ -333,7 +333,7 @@ class ListFlavor(command.Lister):
# Even if server supports 2.61 some policy might stop it sending us
# extra_specs. So try to fetch them if they are absent
for f in data:
if not f.extra_specs:
if parsed_args.long and not f.extra_specs:
compute_client.fetch_flavor_extra_specs(f)
columns = (

View File

@ -523,6 +523,7 @@ class TestFlavorList(TestFlavor):
self.sdk_client.flavors.assert_called_with(
**kwargs
)
self.sdk_client.fetch_flavor_extra_specs.assert_not_called()
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, tuple(data))
@ -550,6 +551,7 @@ class TestFlavorList(TestFlavor):
self.sdk_client.flavors.assert_called_with(
**kwargs
)
self.sdk_client.fetch_flavor_extra_specs.assert_not_called()
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, tuple(data))
@ -577,6 +579,7 @@ class TestFlavorList(TestFlavor):
self.sdk_client.flavors.assert_called_with(
**kwargs
)
self.sdk_client.fetch_flavor_extra_specs.assert_not_called()
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, tuple(data))
@ -604,6 +607,7 @@ class TestFlavorList(TestFlavor):
self.sdk_client.flavors.assert_called_with(
**kwargs
)
self.sdk_client.fetch_flavor_extra_specs.assert_not_called()
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, tuple(data))
@ -631,6 +635,58 @@ class TestFlavorList(TestFlavor):
self.sdk_client.flavors.assert_called_with(
**kwargs
)
self.sdk_client.fetch_flavor_extra_specs.assert_not_called()
self.assertEqual(self.columns_long, columns)
self.assertCountEqual(self.data_long, tuple(data))
def test_flavor_list_long_no_extra_specs(self):
# use flavor with no extra specs for this test
flavor = compute_fakes.FakeFlavor.create_one_flavor(
attrs={"extra_specs": {}})
self.data = ((
flavor.id,
flavor.name,
flavor.ram,
flavor.disk,
flavor.ephemeral,
flavor.vcpus,
flavor.is_public,
),)
self.data_long = (self.data[0] + (
flavor.swap,
flavor.rxtx_factor,
format_columns.DictColumn(flavor.extra_specs)
),)
self.api_mock.side_effect = [[flavor], [], ]
self.sdk_client.flavors = self.api_mock
self.sdk_client.fetch_flavor_extra_specs = mock.Mock(return_value=None)
arglist = [
'--long',
]
verifylist = [
('long', True),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
# In base command class Lister in cliff, abstract method take_action()
# returns a tuple containing the column names and an iterable
# containing the data to be listed.
columns, data = self.cmd.take_action(parsed_args)
# Set expected values
kwargs = {
'is_public': True,
}
self.sdk_client.flavors.assert_called_with(
**kwargs
)
self.sdk_client.fetch_flavor_extra_specs.assert_called_once_with(
flavor)
self.assertEqual(self.columns_long, columns)
self.assertCountEqual(self.data_long, tuple(data))
@ -662,6 +718,7 @@ class TestFlavorList(TestFlavor):
self.sdk_client.flavors.assert_called_with(
**kwargs
)
self.sdk_client.fetch_flavor_extra_specs.assert_not_called()
self.assertEqual(self.columns, columns)
self.assertEqual(tuple(self.data), tuple(data))

View File

@ -0,0 +1,8 @@
---
fixes:
- |
The ``flavor list`` command will no longer attempt to fetch extra specs
unless they are actually required (by using the ``--long``) option. This
should significantly improve performance on clouds with a large number of
flavors.
[Story `2010343 <https://storyboard.openstack.org/#!/story/2010343>`_]