summaryrefslogtreecommitdiff
path: root/nova/objects/migration.py
blob: 6f5f217b8023fea6d3544c12b97aa9351cbc9dd9 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#    Copyright 2013 IBM Corp.
#
#    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_db import exception as db_exc
from oslo_utils import uuidutils
from oslo_utils import versionutils

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


def determine_migration_type(migration):
    if isinstance(migration, dict):
        old_instance_type_id = migration['old_instance_type_id']
        new_instance_type_id = migration['new_instance_type_id']
    else:
        old_instance_type_id = migration.old_instance_type_id
        new_instance_type_id = migration.new_instance_type_id

    if old_instance_type_id != new_instance_type_id:
        return 'resize'

    return 'migration'


@base.NovaObjectRegistry.register
class Migration(base.NovaPersistentObject, base.NovaObject):
    # Version 1.0: Initial version
    # Version 1.1: String attributes updated to support unicode
    # Version 1.2: Added migration_type and hidden
    # Version 1.3: Added get_by_id_and_instance()
    # Version 1.4: Added migration progress detail
    # Version 1.5: Added uuid
    # Version 1.6: Added cross_cell_move and get_by_uuid().
    # Version 1.7: Added user_id and project_id
    VERSION = '1.7'

    fields = {
        'id': fields.IntegerField(),
        'uuid': fields.UUIDField(),
        'source_compute': fields.StringField(nullable=True),  # source hostname
        'dest_compute': fields.StringField(nullable=True),    # dest hostname
        'source_node': fields.StringField(nullable=True),     # source nodename
        'dest_node': fields.StringField(nullable=True),       # dest nodename
        'dest_host': fields.StringField(nullable=True),       # dest host IP
        # TODO(stephenfin): Rename these to old_flavor_id, new_flavor_id in
        # v2.0
        'old_instance_type_id': fields.IntegerField(nullable=True),
        'new_instance_type_id': fields.IntegerField(nullable=True),
        'instance_uuid': fields.StringField(nullable=True),
        'status': fields.StringField(nullable=True),
        'migration_type': fields.MigrationTypeField(nullable=False),
        'hidden': fields.BooleanField(nullable=False, default=False),
        'memory_total': fields.IntegerField(nullable=True),
        'memory_processed': fields.IntegerField(nullable=True),
        'memory_remaining': fields.IntegerField(nullable=True),
        'disk_total': fields.IntegerField(nullable=True),
        'disk_processed': fields.IntegerField(nullable=True),
        'disk_remaining': fields.IntegerField(nullable=True),
        'cross_cell_move': fields.BooleanField(default=False),
        # request context user id
        'user_id': fields.StringField(nullable=True),
        # request context project id
        'project_id': fields.StringField(nullable=True),
        }

    @staticmethod
    def _from_db_object(context, migration, db_migration):
        for key in migration.fields:
            value = db_migration[key]
            if key == 'migration_type' and value is None:
                value = determine_migration_type(db_migration)
            elif key == 'uuid' and value is None:
                continue
            setattr(migration, key, value)

        migration._context = context
        migration.obj_reset_changes()
        migration._ensure_uuid()
        return migration

    def obj_make_compatible(self, primitive, target_version):
        super(Migration, self).obj_make_compatible(primitive, target_version)
        target_version = versionutils.convert_version_to_tuple(target_version)
        if target_version < (1, 2):
            if 'migration_type' in primitive:
                del primitive['migration_type']
                del primitive['hidden']
        if target_version < (1, 4):
            if 'memory_total' in primitive:
                del primitive['memory_total']
                del primitive['memory_processed']
                del primitive['memory_remaining']
                del primitive['disk_total']
                del primitive['disk_processed']
                del primitive['disk_remaining']
        if target_version < (1, 5):
            if 'uuid' in primitive:
                del primitive['uuid']
        if target_version < (1, 6) and 'cross_cell_move' in primitive:
            del primitive['cross_cell_move']
        if target_version < (1, 7):
            if 'user_id' in primitive:
                del primitive['user_id']
            if 'project_id' in primitive:
                del primitive['project_id']

    def obj_load_attr(self, attrname):
        if attrname == 'migration_type':
            # NOTE(danms): The only reason we'd need to load this is if
            # some older node sent us one. So, guess the type.
            self.migration_type = determine_migration_type(self)
        elif attrname in ['hidden', 'cross_cell_move']:
            self.obj_set_defaults(attrname)
        else:
            super(Migration, self).obj_load_attr(attrname)

    def _ensure_uuid(self):
        if 'uuid' in self:
            return

        self.uuid = uuidutils.generate_uuid()
        try:
            self.save()
        except db_exc.DBDuplicateEntry:
            # NOTE(danms) We raced to generate a uuid for this,
            # so fetch the winner and use that uuid
            fresh = self.__class__.get_by_id(self.context, self.id)
            self.uuid = fresh.uuid

    @base.remotable_classmethod
    def get_by_uuid(cls, context, migration_uuid):
        db_migration = db.migration_get_by_uuid(context, migration_uuid)
        return cls._from_db_object(context, cls(), db_migration)

    @base.remotable_classmethod
    def get_by_id(cls, context, migration_id):
        db_migration = db.migration_get(context, migration_id)
        return cls._from_db_object(context, cls(), db_migration)

    @base.remotable_classmethod
    def get_by_id_and_instance(cls, context, migration_id, instance_uuid):
        db_migration = db.migration_get_by_id_and_instance(
            context, migration_id, instance_uuid)
        return cls._from_db_object(context, cls(), db_migration)

    @base.remotable_classmethod
    def get_by_instance_and_status(cls, context, instance_uuid, status):
        db_migration = db.migration_get_by_instance_and_status(
            context, instance_uuid, status)
        return cls._from_db_object(context, cls(), db_migration)

    @base.remotable
    def create(self):
        if self.obj_attr_is_set('id'):
            raise exception.ObjectActionError(action='create',
                                              reason='already created')
        if 'uuid' not in self:
            self.uuid = uuidutils.generate_uuid()
        # Record who is initiating the migration which is
        # not necessarily the owner of the instance.
        if 'user_id' not in self:
            self.user_id = self._context.user_id
        if 'project_id' not in self:
            self.project_id = self._context.project_id
        updates = self.obj_get_changes()
        if 'migration_type' not in updates:
            raise exception.ObjectActionError(
                action="create",
                reason=_("cannot create a Migration object without a "
                         "migration_type set"))
        db_migration = db.migration_create(self._context, updates)
        self._from_db_object(self._context, self, db_migration)

    @base.remotable
    def save(self):
        updates = self.obj_get_changes()
        updates.pop('id', None)
        db_migration = db.migration_update(self._context, self.id, updates)
        self._from_db_object(self._context, self, db_migration)
        self.obj_reset_changes()

    @property
    def instance(self):
        if not hasattr(self, '_cached_instance'):
            self._cached_instance = objects.Instance.get_by_uuid(
                self._context, self.instance_uuid,
                expected_attrs=['migration_context', 'flavor'])
        return self._cached_instance

    @instance.setter
    def instance(self, instance):
        self._cached_instance = instance

    @property
    def is_live_migration(self):
        return self.migration_type == fields.MigrationType.LIVE_MIGRATION

    @property
    def is_resize(self):
        return self.migration_type == fields.MigrationType.RESIZE

    @property
    def is_same_host_resize(self):
        return self.is_resize and self.source_node == self.dest_node


@base.NovaObjectRegistry.register
class MigrationList(base.ObjectListBase, base.NovaObject):
    # Version 1.0: Initial version
    #              Migration <= 1.1
    # Version 1.1: Added use_slave to get_unconfirmed_by_dest_compute
    # Version 1.2: Migration version 1.2
    # Version 1.3: Added a new function to get in progress migrations
    #              for an instance.
    # Version 1.4: Added sort_keys, sort_dirs, limit, marker kwargs to
    #              get_by_filters for migrations pagination support.
    # Version 1.5: Added a new function to get in progress migrations
    #              and error migrations for a given host + node.
    VERSION = '1.5'

    fields = {
        'objects': fields.ListOfObjectsField('Migration'),
        }

    @staticmethod
    @db.select_db_reader_mode
    def _db_migration_get_unconfirmed_by_dest_compute(
            context, confirm_window, dest_compute, use_slave=False):
        return db.migration_get_unconfirmed_by_dest_compute(
            context, confirm_window, dest_compute)

    @base.remotable_classmethod
    def get_unconfirmed_by_dest_compute(cls, context, confirm_window,
                                        dest_compute, use_slave=False):
        db_migrations = cls._db_migration_get_unconfirmed_by_dest_compute(
            context, confirm_window, dest_compute, use_slave=use_slave)
        return base.obj_make_list(context, cls(context), objects.Migration,
                                  db_migrations)

    @base.remotable_classmethod
    def get_in_progress_by_host_and_node(cls, context, host, node):
        db_migrations = db.migration_get_in_progress_by_host_and_node(
            context, host, node)
        return base.obj_make_list(context, cls(context), objects.Migration,
                                  db_migrations)

    @base.remotable_classmethod
    def get_by_filters(cls, context, filters, sort_keys=None, sort_dirs=None,
                       limit=None, marker=None):
        db_migrations = db.migration_get_all_by_filters(
            context, filters, sort_keys=sort_keys, sort_dirs=sort_dirs,
            limit=limit, marker=marker)
        return base.obj_make_list(context, cls(context), objects.Migration,
                                  db_migrations)

    @base.remotable_classmethod
    def get_in_progress_by_instance(cls, context, instance_uuid,
                                    migration_type=None):
        db_migrations = db.migration_get_in_progress_by_instance(
            context, instance_uuid, migration_type)
        return base.obj_make_list(context, cls(context), objects.Migration,
                                  db_migrations)

    @base.remotable_classmethod
    def get_in_progress_and_error(cls, context, host, node):
        db_migrations = \
            db.migration_get_in_progress_and_error_by_host_and_node(
                context, host, node)
        return base.obj_make_list(context, cls(context), objects.Migration,
                                  db_migrations)