Skip to content

Commit cd2c2f3

Browse files
committed
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
1 parent 84a84f7 commit cd2c2f3

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
@@ -1717,27 +1717,32 @@ def _validate_instance_group_policy(self, context, instance,
17171717
# hosts. This is a validation step to make sure that starting the
17181718
# instance here doesn't violate the policy.
17191719
if scheduler_hints is not None:
1720-
# only go through here if scheduler_hints is provided, even if it
1721-
# is empty.
1720+
# only go through here if scheduler_hints is provided,
1721+
# even if it is empty.
17221722
group_hint = scheduler_hints.get('group')
17231723
if not group_hint:
17241724
return
17251725
else:
1726-
# The RequestSpec stores scheduler_hints as key=list pairs so
1727-
# we need to check the type on the value and pull the single
1728-
# entry out. The API request schema validates that
1726+
# The RequestSpec stores scheduler_hints as key=list pairs
1727+
# so we need to check the type on the value and pull the
1728+
# single entry out. The API request schema validates that
17291729
# the 'group' hint is a single value.
17301730
if isinstance(group_hint, list):
17311731
group_hint = group_hint[0]
1732-
1733-
group = objects.InstanceGroup.get_by_hint(context, group_hint)
1732+
try:
1733+
group = objects.InstanceGroup.get_by_hint(
1734+
context, group_hint
1735+
)
1736+
except exception.InstanceGroupNotFound:
1737+
return
17341738
else:
17351739
# TODO(ganso): a call to DB can be saved by adding request_spec
17361740
# to rpcapi payload of live_migration, pre_live_migration and
17371741
# check_can_live_migrate_destination
17381742
try:
17391743
group = objects.InstanceGroup.get_by_instance_uuid(
1740-
context, instance.uuid)
1744+
context, instance.uuid
1745+
)
17411746
except exception.InstanceGroupNotFound:
17421747
return
17431748

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
@@ -7601,11 +7601,14 @@ def test_validate_instance_group_policy_deleted_group(self, mock_get):
76017601
mock_get.side_effect = exception.InstanceGroupNotFound(
76027602
group_uuid=uuids.group_hint
76037603
)
7604-
# FIXME(sean-k-mooney): this should not leak the exception
7605-
self.assertRaises(
7606-
exception.InstanceGroupNotFound,
7607-
self.compute._validate_instance_group_policy, self.context,
7608-
instance, hints)
7604+
# This implicitly asserts that no exception is raised since
7605+
# uncaught exceptions would be treated as a test failure.
7606+
self.compute._validate_instance_group_policy(
7607+
self.context, instance, hints
7608+
)
7609+
# and this just assert that we did in fact invoke the method
7610+
# that raises to ensure that if we refactor in the future this
7611+
# this test will fail if the function we mock is no longer called.
76097612
mock_get.assert_called_once_with(self.context, uuids.group_hint)
76107613

76117614
@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
@@ -621,6 +621,30 @@ def test_get_by_instance_uuid(self, mock_get_ig, get_by_uuid):
621621
self.assertIsInstance(req_obj.instance_group, objects.InstanceGroup)
622622
self.assertEqual('fresh', req_obj.instance_group.name)
623623

624+
@mock.patch.object(
625+
request_spec.RequestSpec, '_get_by_instance_uuid_from_db'
626+
)
627+
@mock.patch('nova.objects.InstanceGroup.get_by_uuid')
628+
def test_get_by_instance_uuid_deleted_group(
629+
self, mock_get_ig, get_by_uuid
630+
):
631+
fake_spec_obj = fake_request_spec.fake_spec_obj()
632+
fake_spec_obj.scheduler_hints['group'] = ['fresh']
633+
fake_spec = fake_request_spec.fake_db_spec(fake_spec_obj)
634+
get_by_uuid.return_value = fake_spec
635+
mock_get_ig.side_effect = exception.InstanceGroupNotFound(
636+
group_uuid=uuids.instgroup
637+
)
638+
639+
req_obj = request_spec.RequestSpec.get_by_instance_uuid(
640+
self.context, fake_spec['instance_uuid']
641+
)
642+
# assert that both the instance_group object and scheduler hint
643+
# are cleared if the instance_group was deleted since the request
644+
# spec was last saved to the db.
645+
self.assertIsNone(req_obj.instance_group, objects.InstanceGroup)
646+
self.assertEqual({'hint': ['over-there']}, req_obj.scheduler_hints)
647+
624648
@mock.patch('nova.objects.request_spec.RequestSpec.save')
625649
@mock.patch.object(
626650
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)