Browse Source

Restore soft-deleted compute node with same uuid

There is a unique index on the compute_nodes.uuid column which
means we can't have more than one compute_nodes record in the
same DB with the same UUID even if one is soft deleted because
the deleted column is not part of that unique index constraint.

This is a problem with ironic nodes where the node is 1:1 with
the compute node record, and when a node is undergoing maintenance
the driver doesn't return it from get_available_nodes() so the
ComputeManager.update_available_resource periodic task (soft)
deletes the compute node record, but when the node is no longer
under maintenance in ironic and the driver reports it, the
ResourceTracker._init_compute_node code will fail to create the
ComputeNode record again because of the duplicate uuid.

This change handles the DBDuplicateEntry error in compute_node_create
by finding the soft-deleted compute node with the same uuid and
simply updating it to no longer be (soft) deleted.

Closes-Bug: #1839560

Change-Id: Iafba419fe86446ffe636721f523fb619f8f787b3
(cherry picked from commit 8b007266f4)
(cherry picked from commit 1b02166528)
tags/18.2.3
Matt Riedemann 2 months ago
parent
commit
9ce94844fa

+ 45
- 1
nova/db/sqlalchemy/api.py View File

@@ -30,6 +30,7 @@ from oslo_db.sqlalchemy import enginefacade
30 30
 from oslo_db.sqlalchemy import update_match
31 31
 from oslo_db.sqlalchemy import utils as sqlalchemyutils
32 32
 from oslo_log import log as logging
33
+from oslo_utils import excutils
33 34
 from oslo_utils import importutils
34 35
 from oslo_utils import timeutils
35 36
 from oslo_utils import uuidutils
@@ -692,11 +693,54 @@ def compute_node_create(context, values):
692 693
 
693 694
     compute_node_ref = models.ComputeNode()
694 695
     compute_node_ref.update(values)
695
-    compute_node_ref.save(context.session)
696
+    try:
697
+        compute_node_ref.save(context.session)
698
+    except db_exc.DBDuplicateEntry:
699
+        with excutils.save_and_reraise_exception(logger=LOG) as err_ctx:
700
+            # Check to see if we have a (soft) deleted ComputeNode with the
701
+            # same UUID and if so just update it and mark as no longer (soft)
702
+            # deleted. See bug 1839560 for details.
703
+            if 'uuid' in values:
704
+                # Get a fresh context for a new DB session and allow it to
705
+                # get a deleted record.
706
+                ctxt = nova.context.get_admin_context(read_deleted='yes')
707
+                compute_node_ref = _compute_node_get_and_update_deleted(
708
+                    ctxt, values)
709
+                # If we didn't get anything back we failed to find the node
710
+                # by uuid and update it so re-raise the DBDuplicateEntry.
711
+                if compute_node_ref:
712
+                    err_ctx.reraise = False
696 713
 
697 714
     return compute_node_ref
698 715
 
699 716
 
717
+@pick_context_manager_writer
718
+def _compute_node_get_and_update_deleted(context, values):
719
+    """Find a ComputeNode by uuid, update and un-delete it.
720
+
721
+    This is a special case from the ``compute_node_create`` method which
722
+    needs to be separate to get a new Session.
723
+
724
+    This method will update the ComputeNode, if found, to have deleted=0 and
725
+    deleted_at=None values.
726
+
727
+    :param context: request auth context which should be able to read deleted
728
+        records
729
+    :param values: values used to update the ComputeNode record - must include
730
+        uuid
731
+    :return: updated ComputeNode sqlalchemy model object if successfully found
732
+        and updated, None otherwise
733
+    """
734
+    cn = model_query(
735
+        context, models.ComputeNode).filter_by(uuid=values['uuid']).first()
736
+    if cn:
737
+        # Update with the provided values but un-soft-delete.
738
+        update_values = copy.deepcopy(values)
739
+        update_values['deleted'] = 0
740
+        update_values['deleted_at'] = None
741
+        return compute_node_update(context, cn.id, update_values)
742
+
743
+
700 744
 @oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
701 745
 @pick_context_manager_writer
702 746
 def compute_node_update(context, compute_id, values):

+ 17
- 23
nova/tests/functional/regressions/test_bug_1839560.py View File

@@ -14,7 +14,6 @@ from oslo_log import log as logging
14 14
 
15 15
 from nova import context
16 16
 from nova.db import api as db_api
17
-from nova import exception
18 17
 from nova import objects
19 18
 from nova import test
20 19
 from nova.tests import fixtures as nova_fixtures
@@ -90,30 +89,25 @@ class PeriodicNodeRecreateTestCase(test.TestCase,
90 89
         # Now stub the driver again to report node2 as being back and run
91 90
         # the periodic task.
92 91
         compute.manager.driver._nodes = ['node1', 'node2']
92
+        LOG.info('Running update_available_resource which should bring back '
93
+                 'node2.')
93 94
         compute.manager.update_available_resource(ctxt)
94
-        # FIXME(mriedem): This is bug 1839560 where the ResourceTracker fails
95
-        # to create a ComputeNode for node2 because of conflicting UUIDs.
95
+        # The DBDuplicateEntry error should have been handled and resulted in
96
+        # updating the (soft) deleted record to no longer be deleted.
96 97
         log = self.stdlog.logger.output
97
-        self.assertIn('Error updating resources for node node2', log)
98
-        self.assertIn('DBDuplicateEntry', log)
99
-        # Should still only have one reported hypervisor (node1).
98
+        self.assertNotIn('DBDuplicateEntry', log)
99
+        # Should have two reported hypervisors again.
100 100
         hypervisors = self.api.api_get('/os-hypervisors').body['hypervisors']
101
-        self.assertEqual(1, len(hypervisors), hypervisors)
102
-        # Test the workaround for bug 1839560 by archiving the deleted node2
103
-        # compute_nodes table record which will allow the periodic to create a
104
-        # new entry for node2. We can remove this when the bug is fixed.
101
+        self.assertEqual(2, len(hypervisors), hypervisors)
102
+        # Now that the node2 record was un-soft-deleted, archiving should not
103
+        # remove any compute_nodes.
105 104
         LOG.info('Archiving the database.')
106 105
         archived = db_api.archive_deleted_rows(1000)[0]
107
-        self.assertIn('compute_nodes', archived)
108
-        self.assertEqual(1, archived['compute_nodes'])
109
-        with utils.temporary_mutation(ctxt, read_deleted='yes'):
110
-            self.assertRaises(exception.ComputeHostNotFound,
111
-                              objects.ComputeNode.get_by_host_and_nodename,
112
-                              ctxt, 'node1', 'node2')
113
-        # Now run the periodic again and we should have a new ComputeNode for
114
-        # node2.
115
-        LOG.info('Running update_available_resource which should create a new '
116
-                 'ComputeNode record for node2.')
117
-        compute.manager.update_available_resource(ctxt)
118
-        hypervisors = self.api.api_get('/os-hypervisors').body['hypervisors']
119
-        self.assertEqual(2, len(hypervisors), hypervisors)
106
+        self.assertNotIn('compute_nodes', archived)
107
+        cn2 = objects.ComputeNode.get_by_host_and_nodename(
108
+            ctxt, 'node1', 'node2')
109
+        self.assertFalse(cn2.deleted)
110
+        self.assertIsNone(cn2.deleted_at)
111
+        # The node2 id and uuid should not have changed in the DB.
112
+        self.assertEqual(cn.id, cn2.id)
113
+        self.assertEqual(cn.uuid, cn2.uuid)

+ 12
- 0
nova/tests/unit/db/test_db_api.py View File

@@ -6912,6 +6912,18 @@ class ComputeNodeTestCase(test.TestCase, ModelsObjectComparatorMixin):
6912 6912
         new_stats = jsonutils.loads(self.item['stats'])
6913 6913
         self.assertEqual(self.stats, new_stats)
6914 6914
 
6915
+    def test_compute_node_create_duplicate_host_hypervisor_hostname(self):
6916
+        """Tests to make sure that DBDuplicateEntry is raised when trying to
6917
+        create a duplicate ComputeNode with the same host and
6918
+        hypervisor_hostname values but different uuid values. This makes
6919
+        sure that when _compute_node_get_and_update_deleted returns None
6920
+        the DBDuplicateEntry is re-raised.
6921
+        """
6922
+        other_node = dict(self.compute_node_dict)
6923
+        other_node['uuid'] = uuidutils.generate_uuid()
6924
+        self.assertRaises(db_exc.DBDuplicateEntry,
6925
+                          db.compute_node_create, self.ctxt, other_node)
6926
+
6915 6927
     def test_compute_node_get_all(self):
6916 6928
         nodes = db.compute_node_get_all(self.ctxt)
6917 6929
         self.assertEqual(1, len(nodes))

Loading…
Cancel
Save