summaryrefslogtreecommitdiff
path: root/nova/objects/migration_context.py
blob: c00e471b0e3f719b1218e977d2b2e7af852985ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#    Copyright 2015 Red Hat Inc.
#
#    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_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import versionutils

from nova.db.main import api as db
from nova import exception
from nova import objects
from nova.objects import base
from nova.objects import fields

LOG = logging.getLogger(__name__)


@base.NovaObjectRegistry.register
class MigrationContext(base.NovaPersistentObject, base.NovaObject):
    """Data representing additional resources related to a migration.

    Some resources cannot be calculated from knowing the flavor alone for the
    purpose of resources tracking, but need to be persisted at the time the
    claim was made, for subsequent resource tracking runs to be consistent.
    MigrationContext objects are created when the claim is done and are there
    to facilitate resource tracking and final provisioning of the instance on
    the destination host.
    """

    # Version 1.0: Initial version
    # Version 1.1: Add old/new pci_devices and pci_requests
    # Version 1.2: Add old/new resources
    VERSION = '1.2'

    fields = {
        'instance_uuid': fields.UUIDField(),
        'migration_id': fields.IntegerField(),
        'new_numa_topology': fields.ObjectField('InstanceNUMATopology',
                                                nullable=True),
        'old_numa_topology': fields.ObjectField('InstanceNUMATopology',
                                                nullable=True),
        'new_pci_devices': fields.ObjectField('PciDeviceList',
                                              nullable=True),
        'old_pci_devices': fields.ObjectField('PciDeviceList',
                                              nullable=True),
        'new_pci_requests': fields.ObjectField('InstancePCIRequests',
                                               nullable=True),
        'old_pci_requests': fields.ObjectField('InstancePCIRequests',
                                                nullable=True),
        'new_resources': fields.ObjectField('ResourceList',
                                            nullable=True),
        'old_resources': fields.ObjectField('ResourceList',
                                            nullable=True),
    }

    @classmethod
    def obj_make_compatible(cls, primitive, target_version):
        target_version = versionutils.convert_version_to_tuple(target_version)
        if target_version < (1, 2):
            primitive.pop('old_resources', None)
            primitive.pop('new_resources', None)
        if target_version < (1, 1):
            primitive.pop('old_pci_devices', None)
            primitive.pop('new_pci_devices', None)
            primitive.pop('old_pci_requests', None)
            primitive.pop('new_pci_requests', None)

    @classmethod
    def obj_from_db_obj(cls, db_obj):
        primitive = jsonutils.loads(db_obj)
        return cls.obj_from_primitive(primitive)

    @base.remotable_classmethod
    def get_by_instance_uuid(cls, context, instance_uuid):
        db_extra = db.instance_extra_get_by_instance_uuid(
                context, instance_uuid, columns=['migration_context'])
        if not db_extra:
            raise exception.MigrationContextNotFound(
                instance_uuid=instance_uuid)

        if db_extra['migration_context'] is None:
            return None

        return cls.obj_from_db_obj(db_extra['migration_context'])

    def get_pci_mapping_for_migration(self, revert):
        """Get the mapping between the old PCI devices and the new PCI
        devices that have been allocated during this migration.  The
        correlation is based on PCI request ID which is unique per PCI
        devices for SR-IOV ports.

        :param revert: If True, return a reverse mapping i.e
               mapping between new PCI devices and old PCI devices.
        :returns: dictionary of PCI mapping.
                  if revert==False:
                      {'<old pci address>': <New PciDevice>}
                  if revert==True:
                      {'<new pci address>': <Old PciDevice>}
        """
        step = -1 if revert else 1
        current_pci_devs, updated_pci_devs = (self.old_pci_devices,
                                              self.new_pci_devices)[::step]
        if current_pci_devs and updated_pci_devs:
            LOG.debug("Determining PCI devices mapping using migration "
                      "context: current_pci_devs: %(cur)s, "
                      "updated_pci_devs: %(upd)s",
                      {'cur': [dev for dev in current_pci_devs],
                       'upd': [dev for dev in updated_pci_devs]})
            return {curr_dev.address: upd_dev
                    for curr_dev in current_pci_devs
                        for upd_dev in updated_pci_devs
                            if curr_dev.request_id == upd_dev.request_id}
        return {}

    def is_cross_cell_move(self):
        """Helper to determine if this is a context for a cross-cell move.

        Based on the ``migration_id`` in this context, gets the Migration
        object and returns its ``cross_cell_move`` value.

        :return: True if this is a cross cell move migration, False otherwise.
        """
        migration = objects.Migration.get_by_id(
            self._context, self.migration_id)
        return migration.cross_cell_move