Skip to content

Commit

Permalink
Add 'detaching' to volume status
Browse files Browse the repository at this point in the history
Fixes bug #1004382

When attach a volume , the volume status is "available -> attaching ->
in-use", But when detaching a volume , the volume status is "in-use ->
available". So We need 'detaching' volume status, it make the change of
state of the volume more clearly.

Change-Id: I544220c206899307294d50e26679f5351fdc0cd7
  • Loading branch information
zhurongze committed Sep 12, 2012
1 parent 511807e commit ca94280
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions nova/compute/api.py
Expand Up @@ -1842,6 +1842,7 @@ def _detach_volume(self, context, instance, volume_id):

volume = self.volume_api.get(context, volume_id)
self.volume_api.check_detach(context, volume)
self.volume_api.begin_detaching(context, volume)

self.compute_rpcapi.detach_volume(context, instance=instance,
volume_id=volume_id)
Expand Down
14 changes: 11 additions & 3 deletions nova/compute/manager.py
Expand Up @@ -1970,9 +1970,17 @@ def _detach_volume(self, context, instance, bdm):
# but added for completeness in case we ever do.
if connection_info and 'serial' not in connection_info:
connection_info['serial'] = volume_id
self.driver.detach_volume(connection_info,
instance['name'],
mp)
try:
self.driver.detach_volume(connection_info,
instance['name'],
mp)
except Exception: # pylint: disable=W0702
with excutils.save_and_reraise_exception():
msg = _("Faild to detach volume %(volume_id)s from %(mp)s")
LOG.exception(msg % locals(), context=context,
instance=instance)
volume = self.volume_api.get(context, volume_id)
self.volume_api.roll_detaching(context, volume)

@exception.wrap_exception(notifier=notifier, publisher_id=publisher_id())
@reverts_task_state
Expand Down
10 changes: 10 additions & 0 deletions nova/tests/fake_volume.py
Expand Up @@ -258,3 +258,13 @@ def unreserve_volume(self, context, volume):
LOG.info('unreserving volume %s', volume['id'])
volume = self.get(context, volume['id'])
volume['status'] = 'available'

def begin_detaching(self, context, volume):
LOG.info('beging detaching volume %s', volume['id'])
volume = self.get(context, volume['id'])
volume['status'] = 'detaching'

def roll_detaching(self, context, volume):
LOG.info('roll detaching volume %s', volume['id'])
volume = self.get(context, volume['id'])
volume['status'] = 'in-use'
2 changes: 2 additions & 0 deletions nova/tests/policy.json
Expand Up @@ -137,6 +137,8 @@
"volume:detach": [],
"volume:reserve_volume": [],
"volume:unreserve_volume": [],
"volume:begin_detaching": [],
"volume:roll_detaching": [],
"volume:check_attach": [],
"volume:check_detach": [],
"volume:initialize_connection": [],
Expand Down
11 changes: 11 additions & 0 deletions nova/tests/test_volume.py
Expand Up @@ -490,6 +490,17 @@ def fake_rollback(context, reservations):
'name',
'description')

def test_begin_roll_detaching_volume(self):
"""Test begin_detaching and roll_detaching functions."""
volume = self._create_volume()
volume_api = nova.volume.api.API()
volume_api.begin_detaching(self.context, volume)
volume = db.volume_get(self.context, volume['id'])
self.assertEqual(volume['status'], "detaching")
volume_api.roll_detaching(self.context, volume)
volume = db.volume_get(self.context, volume['id'])
self.assertEqual(volume['status'], "in-use")


class DriverTestCase(test.TestCase):
"""Base Test class for Drivers."""
Expand Down
11 changes: 11 additions & 0 deletions nova/volume/api.py
Expand Up @@ -303,6 +303,17 @@ def unreserve_volume(self, context, volume):
volume['id'],
{"status": "available"})

@wrap_check_policy
def begin_detaching(self, context, volume):
self.db.volume_update(context, volume['id'], {"status": "detaching"})

@wrap_check_policy
def roll_detaching(self, context, volume):
if volume['status'] == "detaching":
self.db.volume_update(context,
volume['id'],
{"status": "in-use"})

@wrap_check_policy
def attach(self, context, volume, instance_uuid, mountpoint):
host = volume['host']
Expand Down
6 changes: 6 additions & 0 deletions nova/volume/cinder.py
Expand Up @@ -165,6 +165,12 @@ def reserve_volume(self, context, volume):
def unreserve_volume(self, context, volume):
cinderclient(context).volumes.unreserve(volume['id'])

def begin_detaching(self, context, volume):
cinderclient(context).volumes.begin_detaching(volume['id'])

def roll_detaching(self, context, volume):
cinderclient(context).volumes.roll_detaching(volume['id'])

def attach(self, context, volume, instance_uuid, mountpoint):
cinderclient(context).volumes.attach(volume['id'],
instance_uuid,
Expand Down

0 comments on commit ca94280

Please sign in to comment.