Skip to content

Commit

Permalink
Remove compatibility code for instance groups
Browse files Browse the repository at this point in the history
The /345_require_online_migration_completion cell database
migration added in Ocata prevents anyone from upgrading
the cell database schema until they have performed the online
instance group data migrations via the
"nova-manage db online_data_migrations" command.

This means we can remove the compatibility code in the InstanceGroup
object which performs a lookup routine in the API database and
if not found it falls back to the cell database, which at this
point should be empty, therefore making that dead code now.

The test_get_by_name and test_get_by_hint tests are moved
from _TestInstanceGroupListObject to _TestInstanceGroupObject
since they don't have anything to do with InstanceGroupList.

A follow up change will remove the now unused main DB API
methods for instance groups.

Change-Id: Ifbd53b13fa0fef62e0329283b73d587f367e46c2
  • Loading branch information
mriedem committed Jun 27, 2018
1 parent 53f14a7 commit 1160921
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 345 deletions.
3 changes: 0 additions & 3 deletions nova/cmd/manage.py
Expand Up @@ -60,7 +60,6 @@
from nova.objects import build_request as build_request_obj
from nova.objects import host_mapping as host_mapping_obj
from nova.objects import instance as instance_obj
from nova.objects import instance_group as instance_group_obj
from nova.objects import keypair as keypair_obj
from nova.objects import quotas as quotas_obj
from nova.objects import request_spec
Expand Down Expand Up @@ -394,8 +393,6 @@ class DbCommands(object):
request_spec.migrate_instances_add_request_spec,
# Added in Newton
keypair_obj.migrate_keypairs_to_api_db,
# Added in Newton
instance_group_obj.migrate_instance_groups_to_api_db,
# Added in Ocata
# NOTE(mriedem): This online migration is going to be backported to
# Newton also since it's an upgrade issue when upgrading from Mitaka.
Expand Down
123 changes: 15 additions & 108 deletions nova/objects/instance_group.py
Expand Up @@ -21,10 +21,8 @@
from sqlalchemy.orm import joinedload

from nova.compute import utils as compute_utils
from nova import db
from nova.db.sqlalchemy import api as db_api
from nova.db.sqlalchemy import api_models
from nova.db.sqlalchemy import models as main_models
from nova import exception
from nova import objects
from nova.objects import base
Expand Down Expand Up @@ -108,6 +106,7 @@ def _instance_group_members_add_by_uuid(context, group_uuid, members):


# TODO(berrange): Remove NovaObjectDictCompat
# TODO(mriedem): Replace NovaPersistentObject with TimestampedObject in v2.0.
@base.NovaObjectRegistry.register
class InstanceGroup(base.NovaPersistentObject, base.NovaObject,
base.NovaObjectDictCompat):
Expand Down Expand Up @@ -160,6 +159,7 @@ def _from_db_object(context, instance_group, db_inst):
# the api database, we have removed soft-delete, so
# the object fields for delete must be filled in with
# default values for db models from the api database.
# TODO(mriedem): Remove this when NovaPersistentObject is removed.
ignore = {'deleted': False,
'deleted_at': None}
if field in ignore and not hasattr(db_inst, field):
Expand Down Expand Up @@ -212,12 +212,7 @@ def _get_from_db_by_instance(context, instance_uuid):
@staticmethod
@db_api.api_context_manager.writer
def _save_in_db(context, group_uuid, values):
grp = _instance_group_get_query(context,
id_field=api_models.InstanceGroup.uuid,
id=group_uuid).first()
if not grp:
raise exception.InstanceGroupNotFound(group_uuid=group_uuid)

grp = InstanceGroup._get_from_db_by_uuid(context, group_uuid)
values_copy = copy.copy(values)
policies = values_copy.pop('policies', None)
members = values_copy.pop('members', None)
Expand Down Expand Up @@ -302,38 +297,17 @@ def obj_load_attr(self, attrname):

@base.remotable_classmethod
def get_by_uuid(cls, context, uuid):
db_group = None
try:
db_group = cls._get_from_db_by_uuid(context, uuid)
except exception.InstanceGroupNotFound:
pass
if db_group is None:
db_group = db.instance_group_get(context, uuid)
db_group = cls._get_from_db_by_uuid(context, uuid)
return cls._from_db_object(context, cls(), db_group)

@base.remotable_classmethod
def get_by_name(cls, context, name):
try:
db_group = cls._get_from_db_by_name(context, name)
except exception.InstanceGroupNotFound:
igs = InstanceGroupList._get_main_by_project_id(context,
context.project_id)
for ig in igs:
if ig.name == name:
return ig
raise exception.InstanceGroupNotFound(group_uuid=name)
db_group = cls._get_from_db_by_name(context, name)
return cls._from_db_object(context, cls(), db_group)

@base.remotable_classmethod
def get_by_instance_uuid(cls, context, instance_uuid):
db_group = None
try:
db_group = cls._get_from_db_by_instance(context, instance_uuid)
except exception.InstanceGroupNotFound:
pass
if db_group is None:
db_group = db.instance_group_get_by_instance(context,
instance_uuid)
db_group = cls._get_from_db_by_instance(context, instance_uuid)
return cls._from_db_object(context, cls(), db_group)

@classmethod
Expand Down Expand Up @@ -367,11 +341,7 @@ def save(self):
payload = dict(updates)
payload['server_group_id'] = self.uuid

try:
db_group = self._save_in_db(self._context, self.uuid, updates)
except exception.InstanceGroupNotFound:
db.instance_group_update(self._context, self.uuid, updates)
db_group = db.instance_group_get(self._context, self.uuid)
db_group = self._save_in_db(self._context, self.uuid, updates)
self._from_db_object(self._context, self, db_group)
compute_utils.notify_about_server_group_update(self._context,
"update", payload)
Expand All @@ -385,10 +355,8 @@ def refresh(self):
self[field] = current[field]
self.obj_reset_changes()

def _create(self, skipcheck=False):
# NOTE(danms): This is just for the migration routine, and
# can be removed once we're no longer supporting the migration
# of instance groups from the main to api database.
@base.remotable
def create(self):
if self.obj_attr_is_set('id'):
raise exception.ObjectActionError(action='create',
reason='already created')
Expand All @@ -402,14 +370,6 @@ def _create(self, skipcheck=False):
self.uuid = uuidutils.generate_uuid()
updates['uuid'] = self.uuid

if not skipcheck:
try:
db.instance_group_get(self._context, self.uuid)
raise exception.ObjectActionError(
action='create',
reason='already created in main')
except exception.InstanceGroupNotFound:
pass
db_group = self._create_in_db(self._context, updates,
policies=policies,
members=members)
Expand All @@ -422,17 +382,10 @@ def _create(self, skipcheck=False):
group=self,
action=fields.NotificationAction.CREATE)

@base.remotable
def create(self):
self._create()

@base.remotable
def destroy(self):
payload = {'server_group_id': self.uuid}
try:
self._destroy_in_db(self._context, self.uuid)
except exception.InstanceGroupNotFound:
db.instance_group_delete(self._context, self.uuid)
self._destroy_in_db(self._context, self.uuid)
self.obj_reset_changes()
compute_utils.notify_about_server_group_update(self._context,
"delete", payload)
Expand All @@ -445,13 +398,9 @@ def destroy(self):
def add_members(cls, context, group_uuid, instance_uuids):
payload = {'server_group_id': group_uuid,
'instance_uuids': instance_uuids}
try:
members = cls._add_members_in_db(context, group_uuid,
instance_uuids)
members = [member['instance_uuid'] for member in members]
except exception.InstanceGroupNotFound:
members = db.instance_group_members_add(context, group_uuid,
instance_uuids)
members = cls._add_members_in_db(context, group_uuid,
instance_uuids)
members = [member['instance_uuid'] for member in members]
compute_utils.notify_about_server_group_update(context,
"addmember", payload)
compute_utils.notify_about_server_group_add_member(context, group_uuid)
Expand Down Expand Up @@ -511,13 +460,6 @@ def _get_from_db(context, project_id=None):
query = query.filter_by(project_id=project_id)
return query.all()

@classmethod
def _get_main_by_project_id(cls, context, project_id):
main_db_groups = db.instance_group_get_all_by_project_id(context,
project_id)
return base.obj_make_list(context, cls(context), objects.InstanceGroup,
main_db_groups)

@staticmethod
@db_api.api_context_manager.reader
def _get_counts_from_db(context, project_id, user_id=None):
Expand All @@ -533,17 +475,14 @@ def _get_counts_from_db(context, project_id, user_id=None):
@base.remotable_classmethod
def get_by_project_id(cls, context, project_id):
api_db_groups = cls._get_from_db(context, project_id=project_id)
main_db_groups = db.instance_group_get_all_by_project_id(context,
project_id)
return base.obj_make_list(context, cls(context), objects.InstanceGroup,
api_db_groups + main_db_groups)
api_db_groups)

@base.remotable_classmethod
def get_all(cls, context):
api_db_groups = cls._get_from_db(context)
main_db_groups = db.instance_group_get_all(context)
return base.obj_make_list(context, cls(context), objects.InstanceGroup,
api_db_groups + main_db_groups)
api_db_groups)

@base.remotable_classmethod
def get_counts(cls, context, project_id, user_id=None):
Expand All @@ -559,35 +498,3 @@ def get_counts(cls, context, project_id, user_id=None):
'user': {'server_groups': <count across user>}}
"""
return cls._get_counts_from_db(context, project_id, user_id=user_id)


@db_api.pick_context_manager_reader
def _get_main_instance_groups(context, limit):
return context.session.query(main_models.InstanceGroup).\
options(joinedload('_policies')).\
options(joinedload('_members')).\
filter_by(deleted=0).\
limit(limit).\
all()


def migrate_instance_groups_to_api_db(context, count):
main_groups = _get_main_instance_groups(context, count)
done = 0
for db_group in main_groups:
group = objects.InstanceGroup(context=context,
user_id=db_group.user_id,
project_id=db_group.project_id,
uuid=db_group.uuid,
name=db_group.name,
policies=db_group.policies,
members=db_group.members)
try:
group._create(skipcheck=True)
except exception.InstanceGroupIdExists:
# NOTE(melwitt): This might happen if there's a failure right after
# the InstanceGroup was created and the migration is re-run.
pass
db_api.instance_group_delete(context, db_group.uuid)
done += 1
return len(main_groups), done

0 comments on commit 1160921

Please sign in to comment.