def _create_uuid(context, bdm_id): # NOTE(mdbooth): This method is only required until uuid is made # non-nullable in a future release. # NOTE(mdbooth): We wrap this method in a retry loop because it can # fail (safely) on multi-master galera if concurrent updates happen on # different masters. It will never fail on single-master. We can only # ever need one retry. uuid = uuidutils.generate_uuid() values = {'uuid': uuid} compare = db_models.BlockDeviceMapping(id=bdm_id, uuid=None) # NOTE(mdbooth): We explicitly use an independent transaction context # here so as not to fail if: # 1. We retry. # 2. We're in a read transaction.This is an edge case of what's # normally a read operation. Forcing everything (transitively) # which reads a BDM to be in a write transaction for a narrow # temporary edge case is undesirable. tctxt = db_api.get_context_manager(context).writer.independent with tctxt.using(context): query = context.session.query(db_models.BlockDeviceMapping).\ filter_by(id=bdm_id) try: query.update_on_match(compare, 'id', values) except update_match.NoRowsMatched: # We can only get here if we raced, and another writer already # gave this bdm a uuid result = query.one() uuid = result['uuid'] assert (uuid is not None) return uuid
def _create_legacy_bdm(context, deleted=False): # Create a BDM with no uuid values = {'instance_uuid': uuids.instance_uuid} bdm_ref = db_models.BlockDeviceMapping() bdm_ref.update(values) bdm_ref.save(context.session) if deleted: bdm_ref.soft_delete(context.session) return bdm_ref