Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add persistence to the RequestSpec object
In order to use the request spec for an instance boot during later scheduling operations, like resize/migrate, it needs to be persisted. This adds a db migration to store the request spec and methods to the object to create/save/get the request spec. The RequestSpec object is also important for cells v2 so that boot requests can have their information persisted before the creation of an instance record in the database. Change-Id: I94cd50ad29e10eb320d1d78f3081db6519815ff9 Implements: bp persist-request-spec
- Loading branch information
Showing
9 changed files
with
399 additions
and
2 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
nova/db/sqlalchemy/api_migrations/migrate_repo/versions/004_add_request_spec.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from migrate import UniqueConstraint | ||
from sqlalchemy import Column | ||
from sqlalchemy import DateTime | ||
from sqlalchemy import Index | ||
from sqlalchemy import Integer | ||
from sqlalchemy import MetaData | ||
from sqlalchemy import String | ||
from sqlalchemy import Table | ||
from sqlalchemy import Text | ||
|
||
|
||
def upgrade(migrate_engine): | ||
meta = MetaData() | ||
meta.bind = migrate_engine | ||
|
||
request_specs = Table('request_specs', meta, | ||
Column('created_at', DateTime), | ||
Column('updated_at', DateTime), | ||
Column('id', Integer, primary_key=True, nullable=False), | ||
Column('instance_uuid', String(36), nullable=False), | ||
Column('spec', Text, nullable=False), | ||
UniqueConstraint('instance_uuid', | ||
name='uniq_request_specs0instance_uuid'), | ||
mysql_engine='InnoDB', | ||
mysql_charset='utf8' | ||
) | ||
|
||
# NOTE(mriedem): DB2 creates an index when a unique constraint is created | ||
# so trying to add a second index on the host column will fail with error | ||
# SQL0605W, so omit the index in the case of DB2. | ||
if migrate_engine.name != 'ibm_db_sa': | ||
Index('request_spec_instance_uuid_idx', request_specs.c.instance_uuid) | ||
|
||
request_specs.create(checkfirst=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from nova import context | ||
from nova import exception | ||
from nova.objects import base as obj_base | ||
from nova.objects import request_spec | ||
from nova import test | ||
from nova.tests import fixtures | ||
from nova.tests.unit import fake_request_spec | ||
|
||
|
||
class RequestSpecTestCase(test.NoDBTestCase): | ||
def setUp(self): | ||
super(RequestSpecTestCase, self).setUp() | ||
self.useFixture(fixtures.Database(database='api')) | ||
self.context = context.RequestContext('fake-user', 'fake-project') | ||
self.spec_obj = request_spec.RequestSpec() | ||
self.instance_uuid = None | ||
|
||
def _create_spec(self): | ||
args = fake_request_spec.fake_db_spec() | ||
args.pop('id', None) | ||
self.instance_uuid = args['instance_uuid'] | ||
spec = request_spec.RequestSpec._from_db_object(self.context, | ||
self.spec_obj, | ||
self.spec_obj._create_in_db(self.context, args)) | ||
return spec | ||
|
||
def test_get_by_instance_uuid_not_found(self): | ||
self.assertRaises(exception.RequestSpecNotFound, | ||
self.spec_obj._get_by_instance_uuid_from_db, self.context, | ||
self.instance_uuid) | ||
|
||
def test_get_by_uuid(self): | ||
spec = self._create_spec() | ||
db_spec = self.spec_obj.get_by_instance_uuid(self.context, | ||
self.instance_uuid) | ||
self.assertTrue(obj_base.obj_equal_prims(spec, db_spec)) | ||
|
||
def test_save_in_db(self): | ||
spec = self._create_spec() | ||
|
||
old_az = spec.availability_zone | ||
spec.availability_zone = '%s-new' % old_az | ||
spec.save() | ||
db_spec = self.spec_obj.get_by_instance_uuid(self.context, | ||
spec.instance_uuid) | ||
self.assertTrue(obj_base.obj_equal_prims(spec, db_spec)) | ||
self.assertNotEqual(old_az, db_spec.availability_zone) | ||
|
||
def test_double_create(self): | ||
spec = self._create_spec() | ||
self.assertRaises(exception.ObjectActionError, spec.create) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from oslo_serialization import jsonutils | ||
from oslo_utils import uuidutils | ||
|
||
from nova import context | ||
from nova import objects | ||
from nova.tests.unit import fake_flavor | ||
|
||
|
||
INSTANCE_NUMA_TOPOLOGY = objects.InstanceNUMATopology( | ||
cells=[objects.InstanceNUMACell(id=0, cpuset=set([1, 2]), memory=512), | ||
objects.InstanceNUMACell(id=1, cpuset=set([3, 4]), memory=512)]) | ||
INSTANCE_NUMA_TOPOLOGY.obj_reset_changes(recursive=True) | ||
|
||
IMAGE_META = objects.ImageMeta.from_dict( | ||
{'status': 'active', | ||
'container_format': 'bare', | ||
'min_ram': 0, | ||
'updated_at': '2014-12-12T11:16:36.000000', | ||
'min_disk': '0', | ||
'owner': '2d8b9502858c406ebee60f0849486222', | ||
'protected': 'yes', | ||
'properties': { | ||
'os_type': 'Linux', | ||
'hw_video_model': 'vga', | ||
'hw_video_ram': '512', | ||
'hw_qemu_guest_agent': 'yes', | ||
'hw_scsi_model': 'virtio-scsi', | ||
}, | ||
'size': 213581824, | ||
'name': 'f16-x86_64-openstack-sda', | ||
'checksum': '755122332caeb9f661d5c978adb8b45f', | ||
'created_at': '2014-12-10T16:23:14.000000', | ||
'disk_format': 'qcow2', | ||
'id': 'c8b1790e-a07d-4971-b137-44f2432936cd', | ||
} | ||
) | ||
IMAGE_META.obj_reset_changes(recursive=True) | ||
|
||
PCI_REQUESTS = objects.InstancePCIRequests( | ||
requests=[objects.InstancePCIRequest(count=1), | ||
objects.InstancePCIRequest(count=2)]) | ||
PCI_REQUESTS.obj_reset_changes(recursive=True) | ||
|
||
|
||
def fake_db_spec(): | ||
req_obj = fake_spec_obj() | ||
db_request_spec = { | ||
'id': 1, | ||
'instance_uuid': req_obj.instance_uuid, | ||
'spec': jsonutils.dumps(req_obj.obj_to_primitive()), | ||
} | ||
|
||
return db_request_spec | ||
|
||
|
||
def fake_spec_obj(remove_id=False): | ||
ctxt = context.RequestContext('fake', 'fake') | ||
req_obj = objects.RequestSpec(ctxt) | ||
if not remove_id: | ||
req_obj.id = 42 | ||
req_obj.instance_uuid = uuidutils.generate_uuid() | ||
req_obj.image = IMAGE_META | ||
req_obj.numa_topology = INSTANCE_NUMA_TOPOLOGY | ||
req_obj.pci_requests = PCI_REQUESTS | ||
req_obj.flavor = fake_flavor.fake_flavor_obj(ctxt) | ||
req_obj.retry = objects.SchedulerRetries() | ||
req_obj.limits = objects.SchedulerLimits() | ||
req_obj.instance_group = objects.InstanceGroup() | ||
req_obj.project_id = 'fake' | ||
req_obj.num_instances = 1 | ||
req_obj.availability_zone = None | ||
req_obj.ignore_hosts = ['host2', 'host4'] | ||
req_obj.force_hosts = ['host1', 'host3'] | ||
req_obj.force_nodes = ['node1', 'node2'] | ||
req_obj.scheduler_hints = {'hint': ['over-there']} | ||
# This should never be a changed field | ||
req_obj.obj_reset_changes(['id']) | ||
return req_obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.