diff options
| author | Vishvananda Ishaya <vishvananda@yahoo.com> | 2010-09-29 02:38:15 +0000 |
|---|---|---|
| committer | Tarmac <> | 2010-09-29 02:38:15 +0000 |
| commit | c27ec0f51d495f9547dfcd135696e839f564ded1 (patch) | |
| tree | ab3e30d9c264a6a0c53fde916f05b6f43e2ac657 | |
| parent | 1540480db44f55dcee8fe1998ed58a306b03f1df (diff) | |
| parent | 970654239267fc702f767bbaff3e22207576d0cd (diff) | |
| download | nova-c27ec0f51d495f9547dfcd135696e839f564ded1.tar.gz nova-c27ec0f51d495f9547dfcd135696e839f564ded1.tar.xz nova-c27ec0f51d495f9547dfcd135696e839f564ded1.zip | |
Makes sure that multiple copies of nova-network don't create multiple copies of the same NetworkIndex.
| -rw-r--r-- | nova/db/api.py | 9 | ||||
| -rw-r--r-- | nova/db/sqlalchemy/api.py | 8 | ||||
| -rw-r--r-- | nova/db/sqlalchemy/models.py | 2 | ||||
| -rw-r--r-- | nova/network/manager.py | 2 |
4 files changed, 14 insertions, 7 deletions
diff --git a/nova/db/api.py b/nova/db/api.py index 3d633dd0f..b68a0fe8f 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -401,9 +401,12 @@ def network_index_count(context): return IMPL.network_index_count(context) -def network_index_create(context, values): - """Create a network index from the values dict""" - return IMPL.network_index_create(context, values) +def network_index_create_safe(context, values): + """Create a network index from the values dict + + The index is not returned. If the create violates the unique + constraints because the index already exists, no exception is raised.""" + return IMPL.network_index_create_safe(context, values) def network_set_cidr(context, network_id, cidr): diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 05d38d73d..9c3caf9af 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -28,6 +28,7 @@ from nova import utils from nova.db.sqlalchemy import models from nova.db.sqlalchemy.session import get_session from sqlalchemy import or_ +from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import joinedload_all from sqlalchemy.sql import exists, func @@ -659,11 +660,14 @@ def network_index_count(_context): return models.NetworkIndex.count() -def network_index_create(_context, values): +def network_index_create_safe(_context, values): network_index_ref = models.NetworkIndex() for (key, value) in values.iteritems(): network_index_ref[key] = value - network_index_ref.save() + try: + network_index_ref.save() + except IntegrityError: + pass def network_set_host(_context, network_id, host_id): diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 3d2460c39..0067cedf4 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -398,7 +398,7 @@ class NetworkIndex(BASE, NovaBase): """ __tablename__ = 'network_indexes' id = Column(Integer, primary_key=True) - index = Column(Integer) + index = Column(Integer, unique=True) network_id = Column(Integer, ForeignKey('networks.id'), nullable=True) network = relationship(Network, backref=backref('network_index', uselist=False)) diff --git a/nova/network/manager.py b/nova/network/manager.py index 28c20e994..7afc30d6c 100644 --- a/nova/network/manager.py +++ b/nova/network/manager.py @@ -343,7 +343,7 @@ class VlanManager(NetworkManager): This could use a manage command instead of keying off of a flag""" if not self.db.network_index_count(context): for index in range(FLAGS.num_networks): - self.db.network_index_create(context, {'index': index}) + self.db.network_index_create_safe(context, {'index': index}) def _on_set_network_host(self, context, network_id): """Called when this host becomes the host for a project""" |
