Handle PortNotFoundClient exception when getting ports

There could be a race between deleting an instance and retrieving its
port groups from Neutron. In this case PortNotFoundClient is raised and
it can be safely ignored.

Change-Id: I31c9ea8628c6f3985f8e9118d9687bbfb8789b68
Closes-Bug: #1767058
This commit is contained in:
Radoslav Gerganov 2018-04-30 13:18:26 +03:00
parent c3c34a301a
commit 1a0179f7ab
2 changed files with 20 additions and 1 deletions

View File

@ -332,7 +332,13 @@ class SecurityGroupAPI(security_group_base.SecurityGroupBase):
ports = []
for ids in _chunk_by_ids(servers, MAX_SEARCH_IDS):
search_opts = {'device_id': ids}
ports.extend(neutron.list_ports(**search_opts).get('ports'))
try:
ports.extend(neutron.list_ports(**search_opts).get('ports'))
except n_exc.PortNotFoundClient:
# There could be a race between deleting an instance and
# retrieving its port groups from Neutron. In this case
# PortNotFoundClient is raised and it can be safely ignored
LOG.debug("Port not found for device with id %s", ids)
return ports

View File

@ -305,6 +305,19 @@ class TestNeutronDriver(test.NoDBTestCase):
self.assertEqual(sorted([sg1_id, sg2_id]),
sorted(self.mocked_client.list_security_groups.call_args[1]['id']))
def test_instances_security_group_bindings_port_not_found(self):
server_id = 'c5a20e8d-c4b0-47cf-9dca-ebe4f758acb1'
servers = [{'id': server_id}]
self.mocked_client.list_ports.side_effect = n_exc.PortNotFoundClient()
sg_api = neutron_driver.SecurityGroupAPI()
result = sg_api.get_instances_security_groups_bindings(
self.context, servers)
self.mocked_client.list_ports.assert_called_once_with(
device_id=[server_id])
self.assertEqual({}, result)
def _test_instances_security_group_bindings_scale(self, num_servers):
max_query = 150
sg1_id = '2f7ce969-1a73-4ef9-bbd6-c9a91780ecd4'