Fix attachments after attached migration

When migrating an attached volume, after the migration completion,
the volume gots stuck and can not be deleted or detached. This
is happening due an error in the volume completion implementation.
Now, before detaching the source volume, we first save the
attachments and then re-connect then in the destination volume.

Closes-bug: #1659914
Change-Id: Ie9a7815df99869b62209d99bfdcf51b250a8dba3
This commit is contained in:
Erlon R. Cruz 2017-01-31 11:53:58 -02:00
parent 7aecdf919c
commit a5ae53867b
2 changed files with 21 additions and 8 deletions

View File

@ -532,19 +532,19 @@ class VolumeMigrationTestCase(base.BaseVolumeTestCase):
retyping=False,
previous_status='available'):
initial_status = 'retyping' if retyping else status
initial_status = retyping and 'retyping' or status
old_volume = tests_utils.create_volume(self.context, size=0,
host=CONF.host,
status=initial_status,
migration_status='migrating',
previous_status=previous_status)
attachment_id = None
attachment = None
if status == 'in-use':
vol = tests_utils.attach_volume(self.context, old_volume.id,
instance_uuid, attached_host,
'/dev/vda')
self.assertEqual('in-use', vol['status'])
attachment_id = vol['volume_attachment'][0]['id']
attachment = vol['volume_attachment'][0]
target_status = 'target:%s' % old_volume.id
new_host = CONF.host + 'new'
new_volume = tests_utils.create_volume(self.context, size=0,
@ -571,9 +571,17 @@ class VolumeMigrationTestCase(base.BaseVolumeTestCase):
if status == 'in-use':
mock_detach_volume.assert_called_with(self.context,
old_volume.id,
attachment_id)
attachment['id'])
attachments = db.volume_attachment_get_all_by_instance_uuid(
self.context, instance_uuid)
mock_attach_volume.assert_called_once_with(
self.context,
old_volume,
attachment['instance_uuid'],
attachment['attached_host'],
attachment['mountpoint'],
'rw'
)
self.assertIsNotNone(attachments)
self.assertEqual(attached_host,
attachments[0]['attached_host'])

View File

@ -1919,8 +1919,11 @@ class VolumeManager(manager.CleanableManager,
# Detach the source volume (if it fails, don't fail the migration)
# As after detach and refresh, volume_attchments will be None.
# We keep volume_attachment for later attach.
volume_attachments = []
if orig_volume_status == 'in-use':
for attachment in volume.volume_attachment:
# Save the attachments the volume currently have
volume_attachments.append(attachment)
try:
self.detach_volume(ctxt, volume.id, attachment.id)
except Exception as ex:
@ -1948,12 +1951,14 @@ class VolumeManager(manager.CleanableManager,
'previous_status': volume.status,
'migration_status': 'success'}
# Restore the attachmens
if orig_volume_status == 'in-use':
for attachment in volume.volume_attachment:
for attachment in volume_attachments:
LOG.debug('Re-attaching: %s', attachment)
rpcapi.attach_volume(ctxt, volume,
attachment['instance_uuid'],
attachment['attached_host'],
attachment['mountpoint'],
attachment.instance_uuid,
attachment.attached_host,
attachment.mountpoint,
'rw')
volume.update(updates)
volume.save()