From: Zhongyue Luo Date: Tue, 14 May 2013 09:30:00 +0000 (+0800) Subject: Hide lock_prefix argument using synchronized_with_prefix() X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=197fbf154aafaf70363f5f1377a9495d0559436b;p=openstack-build%2Fcinder-build.git Hide lock_prefix argument using synchronized_with_prefix() The lockfile module has a new convenience API which sets the lockfile prefix Using this API, the prefix is not required everytime synchronized is used. Change-Id: I033fa1d2ed071651609ac2be0a5a16eb3fda6c33 --- diff --git a/cinder/openstack/common/lockutils.py b/cinder/openstack/common/lockutils.py index 0c6b26c41..f21f0d918 100644 --- a/cinder/openstack/common/lockutils.py +++ b/cinder/openstack/common/lockutils.py @@ -49,6 +49,10 @@ CONF = cfg.CONF CONF.register_opts(util_opts) +def set_defaults(lock_path): + cfg.set_defaults(util_opts, lock_path=lock_path) + + class _InterProcessLock(object): """Lock implementation which allows multiple locks, working around issues like bugs.debian.org/cgi-bin/bugreport.cgi?bug=632857 and does @@ -82,7 +86,7 @@ class _InterProcessLock(object): # to have a laughable 10 attempts "blocking" mechanism. self.trylock() return self - except IOError, e: + except IOError as e: if e.errno in (errno.EACCES, errno.EAGAIN): # external locks synchronise things like iptables # updates - give it some time to prevent busy spinning @@ -247,3 +251,28 @@ def synchronized(name, lock_file_prefix, external=False, lock_path=None): return retval return inner return wrap + + +def synchronized_with_prefix(lock_file_prefix): + """Partial object generator for the synchronization decorator. + + Redefine @synchronized in each project like so:: + + (in nova/utils.py) + from nova.openstack.common import lockutils + + synchronized = lockutils.synchronized_with_prefix('nova-') + + + (in nova/foo.py) + from nova import utils + + @utils.synchronized('mylock') + def bar(self, *args): + ... + + The lock_file_prefix argument is used to provide lock files on disk with a + meaningful prefix. The prefix should end with a hyphen ('-') if specified. + """ + + return functools.partial(synchronized, lock_file_prefix=lock_file_prefix) diff --git a/cinder/utils.py b/cinder/utils.py index 65128be4a..dc4e47e90 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -56,6 +56,7 @@ from cinder import exception from cinder import flags from cinder.openstack.common import excutils from cinder.openstack.common import importutils +from cinder.openstack.common import lockutils from cinder.openstack.common import log as logging from cinder.openstack.common import timeutils @@ -65,6 +66,8 @@ ISO_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S" PERFECT_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f" FLAGS = flags.FLAGS +synchronized = lockutils.synchronized_with_prefix('cinder-') + def find_config(config_path): """Find a configuration file using the given hint. diff --git a/cinder/volume/drivers/netapp/iscsi.py b/cinder/volume/drivers/netapp/iscsi.py index 32cefd366..d67a2b578 100644 --- a/cinder/volume/drivers/netapp/iscsi.py +++ b/cinder/volume/drivers/netapp/iscsi.py @@ -32,8 +32,8 @@ from suds import client from suds.sax import text from cinder import exception -from cinder.openstack.common import lockutils from cinder.openstack.common import log as logging +from cinder import utils from cinder.volume import driver from cinder.volume.drivers.netapp.api import NaApiError from cinder.volume.drivers.netapp.api import NaElement @@ -388,7 +388,7 @@ class NetAppISCSIDriver(driver.ISCSIDriver): self.discovered_datasets.append(ds) return ds - @lockutils.synchronized('netapp_dfm', 'cinder-', True) + @utils.synchronized('netapp_dfm', external=True) def _provision(self, name, description, project, ss_type, size): """Provision a LUN through provisioning manager. @@ -449,7 +449,7 @@ class NetAppISCSIDriver(driver.ISCSIDriver): return None return volume_type['name'] - @lockutils.synchronized('netapp_dfm', 'cinder-', True) + @utils.synchronized('netapp_dfm', external=True) def _remove_destroy(self, name, project): """Remove the LUN from the dataset, also destroying it. diff --git a/cinder/volume/drivers/san/hp/hp_3par_common.py b/cinder/volume/drivers/san/hp/hp_3par_common.py index bed8cc39d..88be95163 100644 --- a/cinder/volume/drivers/san/hp/hp_3par_common.py +++ b/cinder/volume/drivers/san/hp/hp_3par_common.py @@ -50,7 +50,6 @@ from oslo.config import cfg from cinder import context from cinder import exception -from cinder.openstack.common import lockutils from cinder.openstack.common import log as logging from cinder import utils from cinder.volume import volume_types @@ -597,7 +596,7 @@ exit return status - @lockutils.synchronized('3parclone', 'cinder-', True) + @utils.synchronized('3parclone', external=True) def create_cloned_volume(self, volume, src_vref, client): try: diff --git a/cinder/volume/drivers/san/hp/hp_3par_fc.py b/cinder/volume/drivers/san/hp/hp_3par_fc.py index d5af46d3e..7af686268 100644 --- a/cinder/volume/drivers/san/hp/hp_3par_fc.py +++ b/cinder/volume/drivers/san/hp/hp_3par_fc.py @@ -35,8 +35,8 @@ from hp3parclient import exceptions as hpexceptions from oslo.config import cfg from cinder import exception -from cinder.openstack.common import lockutils from cinder.openstack.common import log as logging +from cinder import utils import cinder.volume.driver from cinder.volume.drivers.san.hp import hp_3par_common as hpcommon from cinder.volume.drivers.san import san @@ -117,7 +117,7 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) """Returns an error if prerequisites aren't met.""" self._check_flags() - @lockutils.synchronized('3par-vol', 'cinder-', True) + @utils.synchronized('3par-vol', external=True) def create_volume(self, volume): metadata = self.common.create_volume(volume, self.client) return {'metadata': metadata} @@ -127,11 +127,11 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) self.client) return {'metadata': new_vol} - @lockutils.synchronized('3par-vol', 'cinder-', True) + @utils.synchronized('3par-vol', external=True) def delete_volume(self, volume): self.common.delete_volume(volume, self.client) - @lockutils.synchronized('3par-vol', 'cinder-', True) + @utils.synchronized('3par-vol', external=True) def create_volume_from_snapshot(self, volume, snapshot): """ Creates a volume from a snapshot. @@ -140,15 +140,15 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) """ self.common.create_volume_from_snapshot(volume, snapshot, self.client) - @lockutils.synchronized('3par-snap', 'cinder-', True) + @utils.synchronized('3par-snap', external=True) def create_snapshot(self, snapshot): self.common.create_snapshot(snapshot, self.client) - @lockutils.synchronized('3par-snap', 'cinder-', True) + @utils.synchronized('3par-snap', external=True) def delete_snapshot(self, snapshot): self.common.delete_snapshot(snapshot, self.client) - @lockutils.synchronized('3par-attach', 'cinder-', True) + @utils.synchronized('3par-attach', external=True) def initialize_connection(self, volume, connector): """Assigns the volume to a server. @@ -200,7 +200,7 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) 'target_wwn': ports['FC']}} return info - @lockutils.synchronized('3par-attach', 'cinder-', True) + @utils.synchronized('3par-attach', external=True) def terminate_connection(self, volume, connector, force): """ Driver entry point to unattach a volume from an instance. @@ -246,14 +246,14 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) return host - @lockutils.synchronized('3par-exp', 'cinder-', True) + @utils.synchronized('3par-exp', external=True) def create_export(self, context, volume): pass - @lockutils.synchronized('3par-exp', 'cinder-', True) + @utils.synchronized('3par-exp', external=True) def ensure_export(self, context, volume): pass - @lockutils.synchronized('3par-exp', 'cinder-', True) + @utils.synchronized('3par-exp', external=True) def remove_export(self, context, volume): pass diff --git a/cinder/volume/drivers/san/hp/hp_3par_iscsi.py b/cinder/volume/drivers/san/hp/hp_3par_iscsi.py index fca9c2006..f1b1b8355 100644 --- a/cinder/volume/drivers/san/hp/hp_3par_iscsi.py +++ b/cinder/volume/drivers/san/hp/hp_3par_iscsi.py @@ -34,8 +34,8 @@ from hp3parclient import client from hp3parclient import exceptions as hpexceptions from cinder import exception -from cinder.openstack.common import lockutils from cinder.openstack.common import log as logging +from cinder import utils import cinder.volume.driver from cinder.volume.drivers.san.hp import hp_3par_common as hpcommon from cinder.volume.drivers.san import san @@ -119,7 +119,7 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) """Returns an error if prerequisites aren't met.""" self._check_flags() - @lockutils.synchronized('3par-vol', 'cinder-', True) + @utils.synchronized('3par-vol', external=True) def create_volume(self, volume): metadata = self.common.create_volume(volume, self.client) @@ -137,11 +137,11 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) self.configuration.iscsi_port), 'metadata': new_vol} - @lockutils.synchronized('3par-vol', 'cinder-', True) + @utils.synchronized('3par-vol', external=True) def delete_volume(self, volume): self.common.delete_volume(volume, self.client) - @lockutils.synchronized('3par-vol', 'cinder-', True) + @utils.synchronized('3par-vol', external=True) def create_volume_from_snapshot(self, volume, snapshot): """ Creates a volume from a snapshot. @@ -150,15 +150,15 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) """ self.common.create_volume_from_snapshot(volume, snapshot, self.client) - @lockutils.synchronized('3par-snap', 'cinder-', True) + @utils.synchronized('3par-snap', external=True) def create_snapshot(self, snapshot): self.common.create_snapshot(snapshot, self.client) - @lockutils.synchronized('3par-snap', 'cinder-', True) + @utils.synchronized('3par-snap', external=True) def delete_snapshot(self, snapshot): self.common.delete_snapshot(snapshot, self.client) - @lockutils.synchronized('3par-attach', 'cinder-', True) + @utils.synchronized('3par-attach', external=True) def initialize_connection(self, volume, connector): """Assigns the volume to a server. @@ -205,7 +205,7 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) } return info - @lockutils.synchronized('3par-attach', 'cinder-', True) + @utils.synchronized('3par-attach', external=True) def terminate_connection(self, volume, connector, force): """ Driver entry point to unattach a volume from an instance. @@ -266,14 +266,14 @@ must be the same" % (cpg['domain'], self.configuration.hp3par_domain) return host - @lockutils.synchronized('3par-exp', 'cinder-', True) + @utils.synchronized('3par-exp', external=True) def create_export(self, context, volume): pass - @lockutils.synchronized('3par-exp', 'cinder-', True) + @utils.synchronized('3par-exp', external=True) def ensure_export(self, context, volume): pass - @lockutils.synchronized('3par-exp', 'cinder-', True) + @utils.synchronized('3par-exp', external=True) def remove_export(self, context, volume): pass diff --git a/cinder/volume/manager.py b/cinder/volume/manager.py index 5ae2b28f1..fecbffe22 100644 --- a/cinder/volume/manager.py +++ b/cinder/volume/manager.py @@ -50,11 +50,11 @@ from cinder.image import glance from cinder import manager from cinder.openstack.common import excutils from cinder.openstack.common import importutils -from cinder.openstack.common import lockutils from cinder.openstack.common import log as logging from cinder.openstack.common import timeutils from cinder.openstack.common import uuidutils from cinder import quota +from cinder import utils from cinder.volume.configuration import Configuration from cinder.volume import utils as volume_utils from cinder.volume import volume_types @@ -541,7 +541,7 @@ class VolumeManager(manager.SchedulerDependentManager): def attach_volume(self, context, volume_id, instance_uuid, mountpoint): """Updates db to show volume is attached""" - @lockutils.synchronized(volume_id, 'cinder-', external=True) + @utils.synchronized(volume_id, external=True) def do_attach(): # check the volume status before attaching volume = self.db.volume_get(context, volume_id)