Rename variable names from volume to volmap

In before, we use the name 'volume' to reference a volume mapping
object. This will cause confusion since we have introduced
the volume concept recently [1]. This commit rename 'volume'
to 'volmap' whenever it is appropriate.

[1] https://review.openstack.org/#/c/599902/

Change-Id: I73bd65138e01de2a229d28aa677e266ab13d0856
This commit is contained in:
Hongbin Lu
2018-09-27 03:12:08 +00:00
parent 0ac04cc1bf
commit 3ce95424b8
3 changed files with 105 additions and 106 deletions

View File

@@ -169,27 +169,27 @@ class Manager(periodic_task.PeriodicTasks):
container.host = None container.host = None
container.save(context) container.save(context)
def _wait_for_volumes_available(self, context, volumes, container, def _wait_for_volumes_available(self, context, volmaps, container,
timeout=60, poll_interval=1): timeout=60, poll_interval=1):
start_time = time.time() start_time = time.time()
request_volumes = copy.deepcopy(volumes) request_volumes = copy.deepcopy(volmaps)
try: try:
volumes = itertools.chain(volumes) volmaps = itertools.chain(volmaps)
volume = next(volumes) volmap = next(volmaps)
while time.time() - start_time < timeout: while time.time() - start_time < timeout:
is_available, is_error = self.driver.is_volume_available( is_available, is_error = self.driver.is_volume_available(
context, volume) context, volmap)
if is_available: if is_available:
volume = next(volumes) volmap = next(volmaps)
if is_error: if is_error:
break break
time.sleep(poll_interval) time.sleep(poll_interval)
except StopIteration: except StopIteration:
return return
for volume in request_volumes: for volmap in request_volumes:
if volume.auto_remove: if volmap.auto_remove:
try: try:
self.driver.delete_volume(context, volume) self.driver.delete_volume(context, volmap)
except Exception: except Exception:
LOG.exception("Failed to delete volume") LOG.exception("Failed to delete volume")
msg = _("Volumes did not reach available status after" msg = _("Volumes did not reach available status after"
@@ -197,19 +197,19 @@ class Manager(periodic_task.PeriodicTasks):
self._fail_container(context, container, msg, unset_host=True) self._fail_container(context, container, msg, unset_host=True)
raise exception.Conflict(msg) raise exception.Conflict(msg)
def _wait_for_volumes_deleted(self, context, volumes, container, def _wait_for_volumes_deleted(self, context, volmaps, container,
timeout=60, poll_interval=1): timeout=60, poll_interval=1):
start_time = time.time() start_time = time.time()
try: try:
volumes = itertools.chain(volumes) volmaps = itertools.chain(volmaps)
volume = next(volumes) volmap = next(volmaps)
while time.time() - start_time < timeout: while time.time() - start_time < timeout:
if not volume.auto_remove: if not volmap.auto_remove:
volume = next(volumes) volmap = next(volmaps)
is_deleted, is_error = self.driver.is_volume_deleted( is_deleted, is_error = self.driver.is_volume_deleted(
context, volume) context, volmap)
if is_deleted: if is_deleted:
volume = next(volumes) volmap = next(volmaps)
if is_error: if is_error:
break break
time.sleep(poll_interval) time.sleep(poll_interval)
@@ -375,61 +375,61 @@ class Manager(periodic_task.PeriodicTasks):
self._fail_container(context, container, six.text_type(e), self._fail_container(context, container, six.text_type(e),
unset_host=True) unset_host=True)
def _attach_volumes(self, context, container, volumes): def _attach_volumes(self, context, container, volmaps):
try: try:
for volume in volumes: for volmap in volmaps:
volume.container_uuid = container.uuid volmap.container_uuid = container.uuid
volume.host = self.host volmap.host = self.host
self._attach_volume(context, volume) self._attach_volume(context, volmap)
except Exception as e: except Exception as e:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
self._fail_container(context, container, six.text_type(e), self._fail_container(context, container, six.text_type(e),
unset_host=True) unset_host=True)
def _attach_volume(self, context, volume): def _attach_volume(self, context, volmap):
volume.create(context) volmap.create(context)
context = context.elevated() context = context.elevated()
LOG.info('Attaching volume %(volume_id)s to %(host)s', LOG.info('Attaching volume %(volume_id)s to %(host)s',
{'volume_id': volume.cinder_volume_id, {'volume_id': volmap.cinder_volume_id,
'host': CONF.host}) 'host': CONF.host})
try: try:
self.driver.attach_volume(context, volume) self.driver.attach_volume(context, volmap)
except Exception: except Exception:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
LOG.error("Failed to attach volume %(volume_id)s to " LOG.error("Failed to attach volume %(volume_id)s to "
"container %(container_id)s", "container %(container_id)s",
{'volume_id': volume.cinder_volume_id, {'volume_id': volmap.cinder_volume_id,
'container_id': volume.container_uuid}) 'container_id': volmap.container_uuid})
if volume.auto_remove: if volmap.auto_remove:
try: try:
self.driver.delete_volume(context, volume) self.driver.delete_volume(context, volmap)
except Exception: except Exception:
LOG.exception("Failed to delete volume %s.", LOG.exception("Failed to delete volume %s.",
volume.cinder_volume_id) volmap.cinder_volume_id)
volume.destroy() volmap.destroy()
def _detach_volumes(self, context, container, reraise=True): def _detach_volumes(self, context, container, reraise=True):
volumes = objects.VolumeMapping.list_by_container(context, volmaps = objects.VolumeMapping.list_by_container(context,
container.uuid) container.uuid)
for volume in volumes: for volmap in volmaps:
db_volumes = objects.VolumeMapping.list_by_cinder_volume( db_volmaps = objects.VolumeMapping.list_by_cinder_volume(
context, volume.cinder_volume_id) context, volmap.cinder_volume_id)
self._detach_volume(context, volume, reraise=reraise) self._detach_volume(context, volmap, reraise=reraise)
if volume.auto_remove and len(db_volumes) == 1: if volmap.auto_remove and len(db_volmaps) == 1:
self.driver.delete_volume(context, volume) self.driver.delete_volume(context, volmap)
self._wait_for_volumes_deleted(context, volumes, container) self._wait_for_volumes_deleted(context, volmaps, container)
def _detach_volume(self, context, volume, reraise=True): def _detach_volume(self, context, volmap, reraise=True):
context = context.elevated() context = context.elevated()
try: try:
self.driver.detach_volume(context, volume) self.driver.detach_volume(context, volmap)
except Exception: except Exception:
with excutils.save_and_reraise_exception(reraise=reraise): with excutils.save_and_reraise_exception(reraise=reraise):
LOG.error("Failed to detach volume %(volume_id)s from " LOG.error("Failed to detach volume %(volume_id)s from "
"container %(container_id)s", "container %(container_id)s",
{'volume_id': volume.cinder_volume_id, {'volume_id': volmap.cinder_volume_id,
'container_id': volume.container_uuid}) 'container_id': volmap.container_uuid})
volume.destroy() volmap.destroy()
def _use_sandbox(self): def _use_sandbox(self):
if CONF.use_sandbox and self.driver.capabilities["support_sandbox"]: if CONF.use_sandbox and self.driver.capabilities["support_sandbox"]:
@@ -677,9 +677,8 @@ class Manager(periodic_task.PeriodicTasks):
self._do_container_start(context, created_container) self._do_container_start(context, created_container)
def _get_vol_info(self, context, container): def _get_vol_info(self, context, container):
volumes = objects.VolumeMapping.list_by_container(context, return objects.VolumeMapping.list_by_container(context,
container.uuid) container.uuid)
return volumes
def _get_network_info(self, context, container): def _get_network_info(self, context, container):
neutron_api = neutron.NeutronAPI(context) neutron_api = neutron.NeutronAPI(context)

View File

@@ -75,18 +75,18 @@ class CinderWorkflow(object):
self.context = context self.context = context
self.cinder_api = cinder.CinderAPI(self.context) self.cinder_api = cinder.CinderAPI(self.context)
def attach_volume(self, volume): def attach_volume(self, volmap):
try: try:
return self._do_attach_volume(self.cinder_api, volume) return self._do_attach_volume(self.cinder_api, volmap)
except Exception: except Exception:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
LOG.exception("Failed to attach volume %(volume_id)s", LOG.exception("Failed to attach volume %(volume_id)s",
{'volume_id': volume.cinder_volume_id}) {'volume_id': volmap.cinder_volume_id})
self.cinder_api.unreserve_volume(volume.cinder_volume_id) self.cinder_api.unreserve_volume(volmap.cinder_volume_id)
def _do_attach_volume(self, cinder_api, volume): def _do_attach_volume(self, cinder_api, volmap):
volume_id = volume.cinder_volume_id volume_id = volmap.cinder_volume_id
container_uuid = volume.container_uuid container_uuid = volmap.container_uuid
cinder_api.reserve_volume(volume_id) cinder_api.reserve_volume(volume_id)
conn_info = cinder_api.initialize_connection( conn_info = cinder_api.initialize_connection(
@@ -106,14 +106,14 @@ class CinderWorkflow(object):
conn_info['data']['device_path'] = device_info['path'] conn_info['data']['device_path'] = device_info['path']
mountpoint = device_info['path'] mountpoint = device_info['path']
try: try:
volume.connection_info = jsonutils.dumps(conn_info) volmap.connection_info = jsonutils.dumps(conn_info)
except TypeError: except TypeError:
pass pass
# NOTE(hongbin): save connection_info in the database # NOTE(hongbin): save connection_info in the database
# before calling cinder_api.attach because the volume status # before calling cinder_api.attach because the volume status
# will go to 'in-use' then caller immediately try to detach # will go to 'in-use' then caller immediately try to detach
# the volume and connection_info is required for detach. # the volume and connection_info is required for detach.
volume.save() volmap.save()
try: try:
cinder_api.attach(volume_id=volume_id, cinder_api.attach(volume_id=volume_id,
@@ -134,7 +134,7 @@ class CinderWorkflow(object):
# Cinder-volume might have completed volume attach. So # Cinder-volume might have completed volume attach. So
# we should detach the volume. If the attach did not # we should detach the volume. If the attach did not
# happen, the detach request will be ignored. # happen, the detach request will be ignored.
cinder_api.detach(volume) cinder_api.detach(volmap)
return device_info['path'] return device_info['path']
@@ -149,15 +149,15 @@ class CinderWorkflow(object):
connector = get_volume_connector(protocol) connector = get_volume_connector(protocol)
connector.disconnect_volume(conn_info['data'], None) connector.disconnect_volume(conn_info['data'], None)
def detach_volume(self, context, volume): def detach_volume(self, context, volmap):
volume_id = volume.cinder_volume_id volume_id = volmap.cinder_volume_id
try: try:
self.cinder_api.begin_detaching(volume_id) self.cinder_api.begin_detaching(volume_id)
except cinder_exception.BadRequest as e: except cinder_exception.BadRequest as e:
raise exception.Invalid(_("Invalid volume: %s") % raise exception.Invalid(_("Invalid volume: %s") %
six.text_type(e)) six.text_type(e))
conn_info = jsonutils.loads(volume.connection_info) conn_info = jsonutils.loads(volmap.connection_info)
if not self._volume_connection_keep(context, volume_id): if not self._volume_connection_keep(context, volume_id):
try: try:
self._disconnect_volume(conn_info) self._disconnect_volume(conn_info)
@@ -169,7 +169,7 @@ class CinderWorkflow(object):
self.cinder_api.terminate_connection( self.cinder_api.terminate_connection(
volume_id, get_volume_connector_properties()) volume_id, get_volume_connector_properties())
self.cinder_api.detach(volume) self.cinder_api.detach(volmap)
def _volume_connection_keep(self, context, volume_id): def _volume_connection_keep(self, context, volume_id):
host = CONF.host host = CONF.host
@@ -181,8 +181,8 @@ class CinderWorkflow(object):
return False return False
return True return True
def delete_volume(self, volume): def delete_volume(self, volmap):
volume_id = volume.cinder_volume_id volume_id = volmap.cinder_volume_id
try: try:
self.cinder_api.delete_volume(volume_id) self.cinder_api.delete_volume(volume_id)
except cinder_exception as e: except cinder_exception as e:

View File

@@ -79,10 +79,10 @@ class VolumeDriver(object):
def bind_mount(self, *args, **kwargs): def bind_mount(self, *args, **kwargs):
raise NotImplementedError() raise NotImplementedError()
def is_volume_available(self, context, volume): def is_volume_available(self, context, volmap):
raise NotImplementedError() raise NotImplementedError()
def is_volume_deleted(self, context, volume): def is_volume_deleted(self, context, volmap):
raise NotImplementedError() raise NotImplementedError()
@@ -91,33 +91,33 @@ class Local(VolumeDriver):
supported_providers = ['local'] supported_providers = ['local']
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def attach(self, context, volume): def attach(self, context, volmap):
mountpoint = mount.get_mountpoint(volume.uuid) mountpoint = mount.get_mountpoint(volmap.uuid)
fileutils.ensure_tree(mountpoint) fileutils.ensure_tree(mountpoint)
filename = '/'.join([mountpoint, volume.uuid]) filename = '/'.join([mountpoint, volmap.uuid])
with open(filename, 'wb') as fd: with open(filename, 'wb') as fd:
content = utils.decode_file_data(volume.contents) content = utils.decode_file_data(volmap.contents)
fd.write(content) fd.write(content)
def _remove_local_file(self, volume): def _remove_local_file(self, volmap):
mountpoint = mount.get_mountpoint(volume.uuid) mountpoint = mount.get_mountpoint(volmap.uuid)
shutil.rmtree(mountpoint) shutil.rmtree(mountpoint)
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def detach(self, context, volume): def detach(self, context, volmap):
self._remove_local_file(volume) self._remove_local_file(volmap)
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def delete(self, context, volume): def delete(self, context, volmap):
self._remove_local_file(volume) self._remove_local_file(volmap)
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def bind_mount(self, context, volume): def bind_mount(self, context, volmap):
mountpoint = mount.get_mountpoint(volume.uuid) mountpoint = mount.get_mountpoint(volmap.uuid)
filename = '/'.join([mountpoint, volume.uuid]) filename = '/'.join([mountpoint, volmap.uuid])
return filename, volume.container_path return filename, volmap.container_path
def is_volume_available(self, context, volume): def is_volume_available(self, context, volmap):
return True, False return True, False
@@ -128,64 +128,64 @@ class Cinder(VolumeDriver):
] ]
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def attach(self, context, volume): def attach(self, context, volmap):
cinder = cinder_workflow.CinderWorkflow(context) cinder = cinder_workflow.CinderWorkflow(context)
devpath = cinder.attach_volume(volume) devpath = cinder.attach_volume(volmap)
try: try:
self._mount_device(volume, devpath) self._mount_device(volmap, devpath)
except Exception: except Exception:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
LOG.exception("Failed to mount device") LOG.exception("Failed to mount device")
try: try:
cinder.detach_volume(volume) cinder.detach_volume(volmap)
except Exception: except Exception:
LOG.exception("Failed to detach volume") LOG.exception("Failed to detach volume")
def _mount_device(self, volume, devpath): def _mount_device(self, volmap, devpath):
mountpoint = mount.get_mountpoint(volume.uuid) mountpoint = mount.get_mountpoint(volmap.uuid)
fileutils.ensure_tree(mountpoint) fileutils.ensure_tree(mountpoint)
mount.do_mount(devpath, mountpoint, CONF.volume.fstype) mount.do_mount(devpath, mountpoint, CONF.volume.fstype)
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def detach(self, context, volume): def detach(self, context, volmap):
self._unmount_device(volume) self._unmount_device(volmap)
cinder = cinder_workflow.CinderWorkflow(context) cinder = cinder_workflow.CinderWorkflow(context)
cinder.detach_volume(context, volume) cinder.detach_volume(context, volmap)
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def delete(self, context, volume): def delete(self, context, volmap):
cinder = cinder_workflow.CinderWorkflow(context) cinder = cinder_workflow.CinderWorkflow(context)
cinder.delete_volume(volume) cinder.delete_volume(volmap)
def _unmount_device(self, volume): def _unmount_device(self, volmap):
if hasattr(volume, 'connection_info'): if hasattr(volmap, 'connection_info'):
mountpoint = mount.get_mountpoint(volume.uuid) mountpoint = mount.get_mountpoint(volmap.uuid)
mount.do_unmount(mountpoint) mount.do_unmount(mountpoint)
shutil.rmtree(mountpoint) shutil.rmtree(mountpoint)
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def bind_mount(self, context, volume): def bind_mount(self, context, volmap):
mountpoint = mount.get_mountpoint(volume.uuid) mountpoint = mount.get_mountpoint(volmap.uuid)
return mountpoint, volume.container_path return mountpoint, volmap.container_path
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def get_volume_status(self, context, volume): def get_volume_status(self, context, volmap):
ca = cinder_api.CinderAPI(context) ca = cinder_api.CinderAPI(context)
return ca.get(volume.cinder_volume_id).status return ca.get(volmap.cinder_volume_id).status
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def check_multiattach(self, context, volume): def check_multiattach(self, context, volmap):
ca = cinder_api.CinderAPI(context) ca = cinder_api.CinderAPI(context)
return ca.get(volume.cinder_volume_id).multiattach return ca.get(volmap.cinder_volume_id).multiattach
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def is_volume_available(self, context, volume): def is_volume_available(self, context, volmap):
status = self.get_volume_status(context, volume) status = self.get_volume_status(context, volmap)
if status == 'available': if status == 'available':
is_available = True is_available = True
is_error = False is_error = False
elif status == 'in-use': elif status == 'in-use':
multiattach = self.check_multiattach(context, volume) multiattach = self.check_multiattach(context, volmap)
is_available = multiattach is_available = multiattach
is_error = False is_error = False
elif status == 'error': elif status == 'error':
@@ -198,10 +198,10 @@ class Cinder(VolumeDriver):
return is_available, is_error return is_available, is_error
@validate_volume_provider(supported_providers) @validate_volume_provider(supported_providers)
def is_volume_deleted(self, context, volume): def is_volume_deleted(self, context, volmap):
try: try:
volume = cinder_api.CinderAPI(context).search_volume( volume = cinder_api.CinderAPI(context).search_volume(
volume.cinder_volume_id) volmap.cinder_volume_id)
is_deleted = False is_deleted = False
# Cinder volume error states: 'error', 'error_deleting', # Cinder volume error states: 'error', 'error_deleting',
# 'error_backing-up', 'error_restoring', 'error_extending', # 'error_backing-up', 'error_restoring', 'error_extending',