Save the backup.service just before _run_backup

When the judgement of self.is_working() is false, it means the
backup service is not ready and the backup will be interrupted.
We will not allow to delete this backup util the backup service
is configured correctly and starts successfully.
This patch fix this, if the backup service was not working or we
just used a wrong backup service at that time, and now we want to
cleanup the backups, it is OK to do backup delete. There is no
need to do more!

Change-Id: I7163f026a497db8876c495d15bef9c006eadd17b
This commit is contained in:
yenai 2018-08-01 10:17:55 +08:00
parent 5e5627e9ea
commit 0109b874a9
2 changed files with 27 additions and 4 deletions

View File

@ -369,7 +369,6 @@ class BackupManager(manager.ThreadPoolManager):
self._notify_about_backup_usage(context, backup, "create.start")
backup.host = self.host
backup.service = self.driver_name
backup.availability_zone = self.az
backup.save()
@ -408,9 +407,12 @@ class BackupManager(manager.ThreadPoolManager):
try:
if not self.is_working():
err = _('Create backup aborted due to backup service is down')
err = _('Create backup aborted due to backup service is down.')
self._update_backup_error(backup, err)
raise exception.InvalidBackup(reason=err)
backup.service = self.driver_name
backup.save()
updates = self._run_backup(context, backup, volume)
except Exception as err:
with excutils.save_and_reraise_exception():
@ -736,8 +738,8 @@ class BackupManager(manager.ThreadPoolManager):
self._update_backup_error(backup, err)
raise exception.InvalidBackup(reason=err)
if not self.is_working():
err = _('Delete backup is aborted due to backup service is down')
if backup.service and not self.is_working():
err = _('Delete backup is aborted due to backup service is down.')
status = fields.BackupStatus.ERROR_DELETING
self._update_backup_error(backup, err, status)
raise exception.InvalidBackup(reason=err)

View File

@ -1440,6 +1440,27 @@ class BackupsAPITestCase(test.TestCase):
backup.destroy()
@mock.patch('cinder.backup.manager.BackupManager.is_working')
@mock.patch('cinder.db.service_get_all')
def test_delete_backup_service_is_none_and_is_not_working(
self, _mock_service_get_all, _mock_backup_is_working):
_mock_service_get_all.return_value = [
{'availability_zone': 'az1', 'host': 'testhost',
'disabled': 0, 'updated_at': timeutils.utcnow(),
'uuid': 'a3a593da-7f8d-4bb7-8b4c-f2bc1e0b4824'}]
_mock_backup_is_working.return_value = False
backup = utils.create_backup(self.context,
status=fields.BackupStatus.AVAILABLE,
availability_zone='az1', host='testhost',
service=None)
req = webob.Request.blank('/v2/%s/backups/%s' % (
fake.PROJECT_ID, backup.id))
req.method = 'DELETE'
req.headers['Content-Type'] = 'application/json'
res = req.get_response(fakes.wsgi_app(
fake_auth_context=self.user_context))
self.assertEqual(http_client.ACCEPTED, res.status_int)
@mock.patch('cinder.backup.api.API._get_available_backup_service_host')
def test_restore_backup_volume_id_specified_json(
self, _mock_get_backup_host):