Tweak create_server to use list_servers cache

The wait logic wasn't using get_server, which means it was always going
to be making direct calls, which obviates the benefit of the
list_servers cache.

Change-Id: I3883e75469a821eaacde040e66f02cd16e2752e5
This commit is contained in:
Monty Taylor 2015-10-12 09:42:58 -04:00
parent d60e11e51b
commit 08f2982455
3 changed files with 37 additions and 21 deletions

View File

@ -2827,7 +2827,11 @@ class OpenStackCloud(object):
try: try:
server = self.manager.submitTask(_tasks.ServerCreate(**bootkwargs)) server = self.manager.submitTask(_tasks.ServerCreate(**bootkwargs))
# This is a direct get task call to skip the list_servers
# cache which has absolutely no chance of containing the
# new server
server = self.get_server_by_id(server.id) server = self.get_server_by_id(server.id)
server_id = server['id']
except Exception as e: except Exception as e:
raise OpenStackCloudException( raise OpenStackCloudException(
"Error in creating instance: {0}".format(e)) "Error in creating instance: {0}".format(e))
@ -2835,14 +2839,19 @@ class OpenStackCloud(object):
raise OpenStackCloudException( raise OpenStackCloudException(
"Error in creating the server.") "Error in creating the server.")
if wait: if wait:
server_id = server['id'] # There is no point in iterating faster than the list_servers cache
for count in _utils._iterate_timeout( for count in _utils._iterate_timeout(
timeout, timeout,
"Timeout waiting for the server to come up."): "Timeout waiting for the server to come up.",
wait=self._SERVER_LIST_AGE):
try: try:
server = self.get_server_by_id(server_id) # Use the get_server call so that the list_servers
# cache can be leveraged
server = self.get_server(server_id)
except Exception: except Exception:
continue continue
if not server:
continue
server = self.get_active_server( server = self.get_active_server(
server=server, reuse=reuse_ips, server=server, reuse=reuse_ips,

View File

@ -19,11 +19,11 @@ import netifaces
from shade import exc from shade import exc
def _iterate_timeout(timeout, message): def _iterate_timeout(timeout, message, wait=2):
"""Iterate and raise an exception on timeout. """Iterate and raise an exception on timeout.
This is a generator that will continually yield and sleep for 2 This is a generator that will continually yield and sleep for
seconds, and if the timeout is reached, will raise an exception wait seconds, and if the timeout is reached, will raise an exception
with <message>. with <message>.
""" """
@ -33,7 +33,7 @@ def _iterate_timeout(timeout, message):
while (timeout is None) or (time.time() < start + timeout): while (timeout is None) or (time.time() < start + timeout):
count += 1 count += 1
yield count yield count
time.sleep(2) time.sleep(wait)
raise exc.OpenStackCloudTimeout(message) raise exc.OpenStackCloudTimeout(message)

View File

@ -67,10 +67,12 @@ class TestCreateServer(base.TestCase):
Test that a server error before we return or begin waiting for the Test that a server error before we return or begin waiting for the
server instance spawn raises an exception in create_server. server instance spawn raises an exception in create_server.
""" """
build_server = fakes.FakeServer('1234', '', 'BUILD')
error_server = fakes.FakeServer('1234', '', 'ERROR')
with patch("shade.OpenStackCloud"): with patch("shade.OpenStackCloud"):
config = { config = {
"servers.create.return_value": Mock(status="BUILD"), "servers.create.return_value": build_server,
"servers.get.return_value": Mock(status="ERROR") "servers.get.return_value": error_server,
} }
OpenStackCloud.nova_client = Mock(**config) OpenStackCloud.nova_client = Mock(**config)
self.assertRaises( self.assertRaises(
@ -82,13 +84,13 @@ class TestCreateServer(base.TestCase):
raises an exception in create_server. raises an exception in create_server.
""" """
with patch("shade.OpenStackCloud"): with patch("shade.OpenStackCloud"):
fake_server = fakes.FakeServer('1234', '', 'BUILD') build_server = fakes.FakeServer('1234', '', 'BUILD')
error_server = fakes.FakeServer('1234', '', 'ERROR') error_server = fakes.FakeServer('1234', '', 'ERROR')
config = { config = {
"servers.create.return_value": fake_server, "servers.create.return_value": build_server,
"servers.get.side_effect": [ "servers.get.return_value": build_server,
fake_server, error_server "servers.list.side_effect": [
] [build_server], [error_server]]
} }
OpenStackCloud.nova_client = Mock(**config) OpenStackCloud.nova_client = Mock(**config)
self.assertRaises( self.assertRaises(
@ -104,7 +106,8 @@ class TestCreateServer(base.TestCase):
fake_server = fakes.FakeServer('1234', '', 'BUILD') fake_server = fakes.FakeServer('1234', '', 'BUILD')
config = { config = {
"servers.create.return_value": fake_server, "servers.create.return_value": fake_server,
"servers.get.return_value": fake_server "servers.get.return_value": fake_server,
"servers.list.return_value": [fake_server],
} }
OpenStackCloud.nova_client = Mock(**config) OpenStackCloud.nova_client = Mock(**config)
self.assertRaises( self.assertRaises(
@ -132,13 +135,15 @@ class TestCreateServer(base.TestCase):
its status changes to "ACTIVE". its status changes to "ACTIVE".
""" """
with patch("shade.OpenStackCloud"): with patch("shade.OpenStackCloud"):
building_server = fakes.FakeServer( build_server = fakes.FakeServer(
'1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1')) '1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1'))
fake_server = fakes.FakeServer( fake_server = fakes.FakeServer(
'1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1')) '1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1'))
config = { config = {
"servers.create.return_value": building_server, "servers.create.return_value": build_server,
"servers.get.return_value": fake_server, "servers.get.return_value": build_server,
"servers.list.side_effect": [
[build_server], [fake_server]]
} }
OpenStackCloud.nova_client = Mock(**config) OpenStackCloud.nova_client = Mock(**config)
with patch.object(OpenStackCloud, "add_ips_to_server", with patch.object(OpenStackCloud, "add_ips_to_server",
@ -153,11 +158,13 @@ class TestCreateServer(base.TestCase):
server doesn't have addresses. server doesn't have addresses.
""" """
with patch("shade.OpenStackCloud"): with patch("shade.OpenStackCloud"):
build_server = fakes.FakeServer('1234', '', 'BUILD')
fake_server = fakes.FakeServer('1234', '', 'ACTIVE') fake_server = fakes.FakeServer('1234', '', 'ACTIVE')
config = { config = {
"servers.create.return_value": fake_server, "servers.create.return_value": build_server,
"servers.get.side_effect": [ "servers.get.return_value": build_server,
fakes.FakeServer('1234', '', 'BUILD'), fake_server] "servers.list.side_effect": [
[build_server], [fake_server]]
} }
OpenStackCloud.nova_client = Mock(**config) OpenStackCloud.nova_client = Mock(**config)
with patch.object(OpenStackCloud, "add_ips_to_server", with patch.object(OpenStackCloud, "add_ips_to_server",