Change create_network from async to sync

The network create API will create the network in docker and
return immediately, so there is no need to perform a aysnc (202)
processing which has overhead on spawning a new thread for
each request.

This patch changes this API to a sync API. As a result, the response
code of create_network switches from 202 to 200. The RPC call changes
from 'cast' to 'call'. The compute manager will process the request
in main thread instead of spawning a new thread.

Change-Id: Ie1ec42e80f816e456372fb29db6ddaaa8a47c669
This commit is contained in:
Hongbin Lu 2018-05-27 19:15:48 +00:00
parent 275d4a84ee
commit f34bba6791
6 changed files with 7 additions and 23 deletions

View File

@ -68,5 +68,4 @@ class NetworkController(base.Controller):
new_network = objects.Network(context, **network_dict)
new_network.create(context)
pecan.request.compute_api.network_create(context, new_network)
pecan.response.status = 202
return view.format_network(pecan.request.host_url, new_network)

View File

@ -1197,21 +1197,7 @@ class Manager(periodic_task.PeriodicTasks):
self._update_task_state(context, container, None)
def network_create(self, context, network):
utils.spawn_n(self._do_create_network, context, network)
def _do_create_network(self, context, network):
LOG.debug('Create network')
try:
docker_network = self.driver.create_network(context, network)
network.network_id = docker_network['Id']
network.save()
except exception.NetworkNotFound as e:
LOG.error(six.text_type(e))
return
except exception.DockerError as e:
LOG.error("Error occurred while calling Docker network API: %s",
six.text_type(e))
raise
except Exception as e:
LOG.exception("Unexpected exception: %s", six.text_type(e))
raise
docker_network = self.driver.create_network(context, network)
network.network_id = docker_network['Id']
network.save()

View File

@ -205,4 +205,4 @@ class API(rpc_service.API):
def network_create(self, context, new_network):
host = None
return self._cast(host, 'network_create', network=new_network)
return self._call(host, 'network_create', network=new_network)

View File

@ -1139,6 +1139,5 @@ class DockerDriver(driver.ContainerDriver):
context, network.neutron_net_id)
docker_network = network_api.create_network(
neutron_net_id=network.neutron_net_id,
name=docker_net_name
)
name=docker_net_name)
return docker_network

View File

@ -25,5 +25,5 @@ class TestNetworkController(api_base.FunctionalTest):
params=params,
content_type='application/json')
self.assertEqual(202, response.status_int)
self.assertEqual(200, response.status_int)
self.assertTrue(mock_network_create.called)

View File

@ -1270,6 +1270,6 @@ class TestManager(base.TestCase):
network = Network(self.context, **utils.get_test_network())
ret = ({'Id': '0eeftestnetwork'})
mock_create.return_value = ret
self.compute_manager._do_create_network(self.context, network)
self.compute_manager.network_create(self.context, network)
mock_create.assert_any_call(self.context, network)
mock_save.assert_called_once()