db: retry on deadlocks while adding an instance

We are hitting deadlocks in the gate when we are inserting the new
instance_extra row into the DB.

We should follow up this fix and look at way to avoid the deadlock
happening rather than retrying it. It currently doesn't happen too
often, so this should be enough to stop the problem while we work on a
better fix.

Closes-Bug: #1480305

Change-Id: Iba218bf28c7d1e6040c551fe836d6fa5e5e45f4d
This commit is contained in:
John Garbutt 2016-03-15 10:21:47 +00:00 committed by Matt Riedemann
parent b61cb28e98
commit df15e467b6
2 changed files with 7 additions and 0 deletions

View File

@ -1734,6 +1734,7 @@ def _check_instance_exists_in_project(context, instance_uuid):
@require_context
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
@pick_context_manager_writer
def instance_create(context, values):
"""Create a new Instance record in the database.

View File

@ -2248,6 +2248,12 @@ class InstanceTestCase(test.TestCase, ModelsObjectComparatorMixin):
instance = self.create_instance_with_args()
self.assertTrue(uuidutils.is_uuid_like(instance['uuid']))
@mock.patch.object(db.sqlalchemy.api, 'security_group_ensure_default')
def test_instance_create_with_deadlock_retry(self, mock_sg):
mock_sg.side_effect = [db_exc.DBDeadlock(), None]
instance = self.create_instance_with_args()
self.assertTrue(uuidutils.is_uuid_like(instance['uuid']))
def test_instance_create_with_object_values(self):
values = {
'access_ip_v4': netaddr.IPAddress('1.2.3.4'),