Optimize _prep_server_detail to avoid redundant find_resource

When showing a server or doing a rebuild, we already have
the latest version of the server so _prep_server_detail
getting the server again is an unnecessary performance hit.

ShowServer is pretty obvious here. For RebuildServer, the
compute API actually refreshes the server before returning
it in the response, so the client already gets the latest
when the rebuild call returns.

The only other usage of _prep_server_detail that does require
a refresh is CreateServer since the POST /servers response is
a minimal version of the server object.

This adds a new refresh kwarg, backward compatible by default,
to _prep_server_detail but changes ShowServer and RebuildServer
to no longer refresh.

Change-Id: Ib1c9c424ed1cafc2dfd8be90af8de8a774bdfbf0
This commit is contained in:
Matt Riedemann 2018-06-08 11:46:29 -04:00
parent 56b3467549
commit 752a2db332
2 changed files with 15 additions and 10 deletions

View File

@ -120,18 +120,21 @@ def _prefix_checked_value(prefix):
return func
def _prep_server_detail(compute_client, image_client, server):
def _prep_server_detail(compute_client, image_client, server, refresh=True):
"""Prepare the detailed server dict for printing
:param compute_client: a compute client instance
:param image_client: an image client instance
:param server: a Server resource
:param refresh: Flag indicating if ``server`` is already the latest version
or if it needs to be refreshed, for example when showing
the latest details of a server after creating it.
:rtype: a dict of server details
"""
info = server.to_dict()
server = utils.find_resource(compute_client.servers, info['id'])
info.update(server.to_dict())
if refresh:
server = utils.find_resource(compute_client.servers, info['id'])
info.update(server.to_dict())
# Convert the image blob to a name
image_info = info.get('image', {})
@ -1540,7 +1543,8 @@ class RebuildServer(command.ShowOne):
self.app.stdout.write(_('Error rebuilding server\n'))
raise SystemExit
details = _prep_server_detail(compute_client, image_client, server)
details = _prep_server_detail(compute_client, image_client, server,
refresh=False)
return zip(*sorted(six.iteritems(details)))
@ -2021,7 +2025,8 @@ class ShowServer(command.ShowOne):
return ({}, {})
else:
data = _prep_server_detail(compute_client,
self.app.client_manager.image, server)
self.app.client_manager.image, server,
refresh=False)
return zip(*sorted(six.iteritems(data)))

View File

@ -2331,17 +2331,17 @@ class TestServerRebuild(TestServer):
self.images_mock.get.return_value = self.image
# Fake the rebuilt new server.
new_server = compute_fakes.FakeServer.create_one_server()
# Fake the server to be rebuilt. The IDs of them should be the same.
attrs = {
'id': new_server.id,
'image': {
'id': self.image.id
},
'networks': {},
'adminPass': 'passw0rd',
}
new_server = compute_fakes.FakeServer.create_one_server(attrs=attrs)
# Fake the server to be rebuilt. The IDs of them should be the same.
attrs['id'] = new_server.id
methods = {
'rebuild': new_server,
}