Remove flavor seeding from the base migration

In a time long ago and a land far far away, someone thought it was a
good idea to populate the database with default flavors. That was
probably reasonable at the time, but it no longer makes sense and
in fact causes us some pain now.

This patch removes those default flavors from the database. That means
that new deploys will not have them, but doesn't actually rewrite
history in any way.

This will require changes to our docs, which largely assume the presence
of these default flavors from time zero.

DocImpact

Depends-On: Ic275887e97221d9ce5ce6f12cdcfb5ac94e300b0
Change-Id: I80b63ce1ebca01be61ac0f43d26a2992ecf16678
This commit is contained in:
Dan Smith 2016-03-31 10:57:14 -07:00
parent 6653b756c6
commit 1a1a41bdbe
6 changed files with 81 additions and 56 deletions

View File

@ -85,33 +85,6 @@ def _create_shadow_tables(migrate_engine):
raise
def _populate_instance_types(instance_types_table):
default_inst_types = {
'm1.tiny': dict(mem=512, vcpus=1, root_gb=1, eph_gb=0, flavid=1),
'm1.small': dict(mem=2048, vcpus=1, root_gb=20, eph_gb=0, flavid=2),
'm1.medium': dict(mem=4096, vcpus=2, root_gb=40, eph_gb=0, flavid=3),
'm1.large': dict(mem=8192, vcpus=4, root_gb=80, eph_gb=0, flavid=4),
'm1.xlarge': dict(mem=16384, vcpus=8, root_gb=160, eph_gb=0, flavid=5)
}
try:
i = instance_types_table.insert()
for name, values in default_inst_types.items():
i.execute({'name': name, 'memory_mb': values["mem"],
'vcpus': values["vcpus"], 'deleted': 0,
'root_gb': values["root_gb"],
'ephemeral_gb': values["eph_gb"],
'rxtx_factor': 1,
'swap': 0,
'flavorid': values["flavid"],
'disabled': False,
'is_public': True})
except Exception:
LOG.info(repr(instance_types_table))
LOG.exception(_LE('Exception while seeding instance_types table'))
raise
# NOTE(dprince): we add these here so our schema contains dump tables
# which were added in migration 209 (in Havana). We can drop these in
# Icehouse: https://bugs.launchpad.net/nova/+bug/1266538
@ -1574,7 +1547,4 @@ def upgrade(migrate_engine):
_create_shadow_tables(migrate_engine)
# populate initial instance types
_populate_instance_types(instance_types)
_create_dump_tables(migrate_engine)

View File

@ -49,7 +49,6 @@ from nova import db
from nova.network import manager as network_manager
from nova.network.security_group import openstack_driver
from nova.objects import base as objects_base
from nova.objects import flavor as flavor_obj
from nova.tests import fixtures as nova_fixtures
from nova.tests.unit import conf_fixture
from nova.tests.unit import policy_fixture
@ -214,11 +213,7 @@ class TestCase(testtools.TestCase):
if self.USES_DB:
self.useFixture(nova_fixtures.Database())
self.useFixture(nova_fixtures.Database(database='api'))
# NOTE(danms): Flavors are encoded in our original migration
# which means we have no real option other than to migrate them
# onlineish every time we build a new database (for now).
ctxt = context.get_admin_context()
flavor_obj.migrate_flavors(ctxt, 100, hard_delete=True)
self.useFixture(nova_fixtures.DefaultFlavorsFixture())
elif not self.USES_DB_SELF:
self.useFixture(nova_fixtures.DatabasePoisonFixture())

View File

@ -28,9 +28,11 @@ from oslo_db.sqlalchemy import enginefacade
from oslo_messaging import conffixture as messaging_conffixture
import six
from nova import context
from nova.db import migration
from nova.db.sqlalchemy import api as session
from nova import exception
from nova import objects
from nova.objects import base as obj_base
from nova import rpc
from nova import service
@ -297,6 +299,33 @@ class DatabaseAtVersion(fixtures.Fixture):
self.addCleanup(self.cleanup)
class DefaultFlavorsFixture(fixtures.Fixture):
def setUp(self):
super(DefaultFlavorsFixture, self).setUp()
ctxt = context.get_admin_context()
defaults = {'rxtx_factor': 1.0, 'disabled': False, 'is_public': True,
'ephemeral_gb': 0, 'swap': 0}
default_flavors = [
objects.Flavor(context=ctxt, memory_mb=512, vcpus=1,
root_gb=1, flavorid='1', name='m1.tiny',
**defaults),
objects.Flavor(context=ctxt, memory_mb=2048, vcpus=1,
root_gb=20, flavorid='2', name='m1.small',
**defaults),
objects.Flavor(context=ctxt, memory_mb=4096, vcpus=2,
root_gb=40, flavorid='3', name='m1.medium',
**defaults),
objects.Flavor(context=ctxt, memory_mb=8192, vcpus=4,
root_gb=80, flavorid='4', name='m1.large',
**defaults),
objects.Flavor(context=ctxt, memory_mb=16384, vcpus=8,
root_gb=160, flavorid='5', name='m1.xlarge',
**defaults),
]
for flavor in default_flavors:
flavor.create()
class RPCFixture(fixtures.Fixture):
def __init__(self, *exmods):
super(RPCFixture, self).__init__()

View File

@ -114,10 +114,13 @@ class FlavorObjectTestCase(test.NoDBTestCase):
self._test_query(flavor)
def test_query_main(self):
flavor = objects.Flavor.get_by_flavor_id(self.context, '3')
self._create_main_flavor()
flavor = objects.Flavor.get_by_flavor_id(self.context, 'mainflavor')
self._test_query(flavor)
def _test_save(self, flavor):
def test_save(self):
flavor = objects.Flavor(context=self.context, **fake_api_flavor)
flavor.create()
flavor.extra_specs['marty'] = 'mcfly'
flavor.extra_specs['foo'] = 'bart'
projects = list(flavor.projects)
@ -139,16 +142,6 @@ class FlavorObjectTestCase(test.NoDBTestCase):
self.assertEqual({'marty': 'mcfly'}, flavor2.extra_specs)
self.assertEqual(set(projects), set(flavor2.projects))
def test_save_api(self):
self._delete_main_flavors()
flavor = objects.Flavor(context=self.context, **fake_api_flavor)
flavor.create()
self._test_save(flavor)
def test_save_main(self):
flavor = objects.Flavor.get_by_flavor_id(self.context, '3')
self._test_save(flavor)
@staticmethod
@db_api.api_context_manager.reader
def _collect_flavor_residue_api(context, flavor):
@ -176,8 +169,16 @@ class FlavorObjectTestCase(test.NoDBTestCase):
self.assertEqual(
0, self._collect_flavor_residue_api(self.context, flavor))
def _create_main_flavor(self, **updates):
values = dict(fake_api_flavor, flavorid='mainflavor')
del values['projects']
del values['extra_specs']
values.update(updates)
return db_api.flavor_create(self.context, values)
def test_destroy_main(self):
flavor = objects.Flavor.get_by_flavor_id(self.context, '3')
self._create_main_flavor()
flavor = objects.Flavor.get_by_flavor_id(self.context, 'mainflavor')
self._test_destroy(flavor)
def test_destroy_missing_flavor_by_name(self):
@ -215,7 +216,9 @@ class FlavorObjectTestCase(test.NoDBTestCase):
self._test_get_all(1)
def test_get_all_with_marker_in_api(self):
db_flavors = db_api.flavor_get_all(self.context)
db_flavors = [self._create_main_flavor(),
self._create_main_flavor(flavorid='mainflavor2',
name='m1.foo2')]
db_flavor_ids = [x['flavorid'] for x in db_flavors]
flavor = ForcedFlavor(context=self.context, **fake_api_flavor)
flavor.create()
@ -227,16 +230,19 @@ class FlavorObjectTestCase(test.NoDBTestCase):
self.assertEqual(['m1.zoo'] + db_flavor_ids[:2], result_flavorids)
def test_get_all_with_marker_in_main(self):
db_flavors = db_api.flavor_get_all(self.context)
db_flavors = [self._create_main_flavor(flavorid='mainflavor1',
name='main1'),
self._create_main_flavor(flavorid='mainflavor2',
name='main2')]
db_flavor_ids = [x['flavorid'] for x in db_flavors]
flavor = ForcedFlavor(context=self.context, **fake_api_flavor)
flavor.create()
fake_flavor2 = dict(fake_api_flavor, name='m1.zoo', flavorid='m1.zoo')
flavor = ForcedFlavor(context=self.context, **fake_flavor2)
flavor.create()
result = self._test_get_all(2, marker='3', limit=3)
result = self._test_get_all(1, marker='mainflavor1', limit=3)
result_flavorids = [x.flavorid for x in result]
self.assertEqual(db_flavor_ids[3:], result_flavorids)
self.assertEqual(db_flavor_ids[1:], result_flavorids)
def test_get_all_with_marker_in_neither(self):
flavor = ForcedFlavor(context=self.context, **fake_api_flavor)
@ -248,6 +254,7 @@ class FlavorObjectTestCase(test.NoDBTestCase):
self._test_get_all, 2, marker='noflavoratall')
def test_create_checks_main_flavors(self):
self._create_main_flavor()
flavor = objects.Flavor(context=self.context, **fake_api_flavor)
self.assertRaises(exception.ObjectActionError, flavor.create)
self._delete_main_flavors()

View File

@ -192,7 +192,7 @@ class TestDatabaseFixture(testtools.TestCase):
conn = engine.connect()
result = conn.execute("select * from instance_types")
rows = result.fetchall()
self.assertEqual(5, len(rows), "Rows %s" % rows)
self.assertEqual(0, len(rows), "Rows %s" % rows)
# insert a 6th instance type, column 5 below is an int id
# which has a constraint on it, so if new standard instance
@ -202,7 +202,7 @@ class TestDatabaseFixture(testtools.TestCase):
", 1.0, 40, 0, 0, 1, 0)")
result = conn.execute("select * from instance_types")
rows = result.fetchall()
self.assertEqual(6, len(rows), "Rows %s" % rows)
self.assertEqual(1, len(rows), "Rows %s" % rows)
# reset by invoking the fixture again
#
@ -213,7 +213,7 @@ class TestDatabaseFixture(testtools.TestCase):
conn = engine.connect()
result = conn.execute("select * from instance_types")
rows = result.fetchall()
self.assertEqual(5, len(rows), "Rows %s" % rows)
self.assertEqual(0, len(rows), "Rows %s" % rows)
def test_api_fixture_reset(self):
# This sets up reasonable db connection strings
@ -311,6 +311,25 @@ class TestDatabaseAtVersionFixture(testtools.TestCase):
self.useFixture(fixtures.DatabaseAtVersion(318))
class TestDefaultFlavorsFixture(testtools.TestCase):
def test_flavors(self):
self.useFixture(conf_fixture.ConfFixture())
self.useFixture(fixtures.Database())
self.useFixture(fixtures.Database(database='api'))
engine = session.get_api_engine()
conn = engine.connect()
result = conn.execute("select * from flavors")
rows = result.fetchall()
self.assertEqual(0, len(rows), "Rows %s" % rows)
self.useFixture(fixtures.DefaultFlavorsFixture())
result = conn.execute("select * from flavors")
rows = result.fetchall()
self.assertEqual(5, len(rows), "Rows %s" % rows)
class TestIndirectionAPIFixture(testtools.TestCase):
def test_indirection_api(self):
# Should initially be None

View File

@ -0,0 +1,5 @@
---
other:
- The default flavors that nova has previously had are no longer
created as part of the first database migration. New deployments
will need to create appropriate flavors before first use.