Skip to content

Commit 7934b9e

Browse files
SeanMooneydosaboy
authored andcommitted
ignore deleted server groups in validation
This change simply catches the exception raised when we lookup a servergroup via a hint and the validation upcall is enabled. Change-Id: I858b4da35382a9f4dcf88f4b6db340e1f34eb82d Closes-Bug: #1890244 (cherry picked from commit cd2c2f3)
1 parent f57900a commit 7934b9e

File tree

6 files changed

+155
-13
lines changed

6 files changed

+155
-13
lines changed

nova/compute/manager.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,27 +1732,32 @@ def _validate_instance_group_policy(self, context, instance,
17321732
# hosts. This is a validation step to make sure that starting the
17331733
# instance here doesn't violate the policy.
17341734
if scheduler_hints is not None:
1735-
# only go through here if scheduler_hints is provided, even if it
1736-
# is empty.
1735+
# only go through here if scheduler_hints is provided,
1736+
# even if it is empty.
17371737
group_hint = scheduler_hints.get('group')
17381738
if not group_hint:
17391739
return
17401740
else:
1741-
# The RequestSpec stores scheduler_hints as key=list pairs so
1742-
# we need to check the type on the value and pull the single
1743-
# entry out. The API request schema validates that
1741+
# The RequestSpec stores scheduler_hints as key=list pairs
1742+
# so we need to check the type on the value and pull the
1743+
# single entry out. The API request schema validates that
17441744
# the 'group' hint is a single value.
17451745
if isinstance(group_hint, list):
17461746
group_hint = group_hint[0]
1747-
1748-
group = objects.InstanceGroup.get_by_hint(context, group_hint)
1747+
try:
1748+
group = objects.InstanceGroup.get_by_hint(
1749+
context, group_hint
1750+
)
1751+
except exception.InstanceGroupNotFound:
1752+
return
17491753
else:
17501754
# TODO(ganso): a call to DB can be saved by adding request_spec
17511755
# to rpcapi payload of live_migration, pre_live_migration and
17521756
# check_can_live_migrate_destination
17531757
try:
17541758
group = objects.InstanceGroup.get_by_instance_uuid(
1755-
context, instance.uuid)
1759+
context, instance.uuid
1760+
)
17561761
except exception.InstanceGroupNotFound:
17571762
return
17581763

nova/objects/request_spec.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ def _from_db_object(context, spec, db_spec):
645645
except exception.InstanceGroupNotFound:
646646
# NOTE(danms): Instance group may have been deleted
647647
spec.instance_group = None
648+
spec.scheduler_hints.pop('group', None)
648649

649650
if data_migrated:
650651
spec.save()
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright 2017 Ericsson
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from nova import context
16+
from nova import objects
17+
from nova import test
18+
from nova.tests import fixtures as nova_fixtures
19+
from nova.tests.functional import fixtures as func_fixtures
20+
from nova.tests.functional import integrated_helpers
21+
22+
23+
class IgnoreDeletedServerGroupsTest(
24+
test.TestCase, integrated_helpers.InstanceHelperMixin,
25+
):
26+
"""Regression test for bug 1890244
27+
28+
If instance are created as member of server groups it
29+
should be possibel to evacuate them if the server groups are
30+
deleted prior to the host failure.
31+
"""
32+
33+
def setUp(self):
34+
super().setUp()
35+
# Stub out external dependencies.
36+
self.useFixture(nova_fixtures.NeutronFixture(self))
37+
self.useFixture(nova_fixtures.GlanceFixture(self))
38+
self.useFixture(func_fixtures.PlacementFixture())
39+
# Start nova controller services.
40+
api_fixture = self.useFixture(nova_fixtures.OSAPIFixture(
41+
api_version='v2.1'))
42+
self.api = api_fixture.admin_api
43+
self.start_service('conductor')
44+
# Use a custom weigher to make sure that we have a predictable
45+
# scheduling sort order.
46+
self.useFixture(nova_fixtures.HostNameWeigherFixture())
47+
self.start_service('scheduler')
48+
# Start two computes, one where the server will be created and another
49+
# where we'll evacuate it to.
50+
self.src = self._start_compute('host1')
51+
self.dest = self._start_compute('host2')
52+
self.notifier = self.useFixture(
53+
nova_fixtures.NotificationFixture(self)
54+
)
55+
56+
def test_evacuate_after_group_delete(self):
57+
# Create an anti-affinity group for the server.
58+
body = {
59+
'server_group': {
60+
'name': 'test-group',
61+
'policies': ['anti-affinity']
62+
}
63+
}
64+
group_id = self.api.api_post(
65+
'/os-server-groups', body).body['server_group']['id']
66+
67+
# Create a server in the group which should land on host1 due to our
68+
# custom weigher.
69+
body = {'server': self._build_server()}
70+
body['os:scheduler_hints'] = {'group': group_id}
71+
server = self.api.post_server(body)
72+
server = self._wait_for_state_change(server, 'ACTIVE')
73+
self.assertEqual('host1', server['OS-EXT-SRV-ATTR:host'])
74+
75+
# Down the source compute to enable the evacuation
76+
self.api.microversion = '2.11' # Cap for the force-down call.
77+
self.api.force_down_service('host1', 'nova-compute', True)
78+
self.api.microversion = 'latest'
79+
self.src.stop()
80+
81+
# assert the server currently has a server group
82+
reqspec = objects.RequestSpec.get_by_instance_uuid(
83+
context.get_admin_context(), server['id'])
84+
self.assertIsNotNone(reqspec.instance_group)
85+
self.assertIn('group', reqspec.scheduler_hints)
86+
# then delete it so that we need to clean it up on evac
87+
self.api.api_delete(f'/os-server-groups/{group_id}')
88+
89+
# Initiate evacuation
90+
server = self._evacuate_server(
91+
server, expected_host='host2', expected_migration_status='done'
92+
)
93+
reqspec = objects.RequestSpec.get_by_instance_uuid(
94+
context.get_admin_context(), server['id'])
95+
self.assertIsNone(reqspec.instance_group)
96+
self.assertNotIn('group', reqspec.scheduler_hints)

nova/tests/unit/compute/test_compute_mgr.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7626,11 +7626,14 @@ def test_validate_instance_group_policy_deleted_group(self, mock_get):
76267626
mock_get.side_effect = exception.InstanceGroupNotFound(
76277627
group_uuid=uuids.group_hint
76287628
)
7629-
# FIXME(sean-k-mooney): this should not leak the exception
7630-
self.assertRaises(
7631-
exception.InstanceGroupNotFound,
7632-
self.compute._validate_instance_group_policy, self.context,
7633-
instance, hints)
7629+
# This implicitly asserts that no exception is raised since
7630+
# uncaught exceptions would be treated as a test failure.
7631+
self.compute._validate_instance_group_policy(
7632+
self.context, instance, hints
7633+
)
7634+
# and this just assert that we did in fact invoke the method
7635+
# that raises to ensure that if we refactor in the future this
7636+
# this test will fail if the function we mock is no longer called.
76347637
mock_get.assert_called_once_with(self.context, uuids.group_hint)
76357638

76367639
@mock.patch('nova.objects.InstanceGroup.get_by_uuid')

nova/tests/unit/objects/test_request_spec.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,30 @@ def test_get_by_instance_uuid(self, mock_get_ig, get_by_uuid):
615615
self.assertIsInstance(req_obj.instance_group, objects.InstanceGroup)
616616
self.assertEqual('fresh', req_obj.instance_group.name)
617617

618+
@mock.patch.object(
619+
request_spec.RequestSpec, '_get_by_instance_uuid_from_db'
620+
)
621+
@mock.patch('nova.objects.InstanceGroup.get_by_uuid')
622+
def test_get_by_instance_uuid_deleted_group(
623+
self, mock_get_ig, get_by_uuid
624+
):
625+
fake_spec_obj = fake_request_spec.fake_spec_obj()
626+
fake_spec_obj.scheduler_hints['group'] = ['fresh']
627+
fake_spec = fake_request_spec.fake_db_spec(fake_spec_obj)
628+
get_by_uuid.return_value = fake_spec
629+
mock_get_ig.side_effect = exception.InstanceGroupNotFound(
630+
group_uuid=uuids.instgroup
631+
)
632+
633+
req_obj = request_spec.RequestSpec.get_by_instance_uuid(
634+
self.context, fake_spec['instance_uuid']
635+
)
636+
# assert that both the instance_group object and scheduler hint
637+
# are cleared if the instance_group was deleted since the request
638+
# spec was last saved to the db.
639+
self.assertIsNone(req_obj.instance_group, objects.InstanceGroup)
640+
self.assertEqual({'hint': ['over-there']}, req_obj.scheduler_hints)
641+
618642
@mock.patch('nova.objects.request_spec.RequestSpec.save')
619643
@mock.patch.object(
620644
request_spec.RequestSpec, '_get_by_instance_uuid_from_db')
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
fixes:
3+
- |
4+
When the server group policy validation upcall is enabled
5+
nova will assert that the policy is not violated on move operations
6+
and initial instance creation. As noted in `bug 1890244`_, if a
7+
server was created in a server group and that group was later deleted
8+
the validation upcall would fail due to an uncaught excpetion if the
9+
server group was deleted. This prevented evacuate and other move
10+
operations form functioning. This has now been fixed and nova will
11+
ignore deleted server groups.
12+
13+
.. _bug 1890244: https://bugs.launchpad.net/nova/+bug/1890244

0 commit comments

Comments
 (0)