def _set_synced_after(func, *args, **kwargs): call_args = inspect.getcallargs(func, *args, **kwargs) self = call_args['self'] sync_result = constants.ResourceSync.SUCCEED ret = None try: ret = func(*args, **kwargs) except Exception: sync_result = constants.ResourceSync.FAILED lock = coordination.Lock(self.storage_id) with lock: try: storage = db.storage_get(self.context, self.storage_id) except exception.StorageNotFound: LOG.warn('Storage %s not found when set synced' % self.storage_id) else: # One sync task done, sync status minus 1 # When sync status get to 0 # means all the sync tasks are completed if storage['sync_status'] != constants.SyncStatus.SYNCED: storage['sync_status'] -= sync_result db.storage_update(self.context, self.storage_id, {'sync_status': storage['sync_status']}) return ret
def create(self, req, body): """Register a new storage device.""" ctxt = req.environ['delfin.context'] access_info_dict = body # Lock to avoid synchronous creating. for access in constants.ACCESS_TYPE: if access_info_dict.get(access) is not None: host = access_info_dict.get(access).get('host') break lock_name = 'storage-create-' + host lock = coordination.Lock(lock_name) with lock: if self._storage_exist(ctxt, access_info_dict): raise exception.StorageAlreadyExists() storage = self.driver_api.discover_storage(ctxt, access_info_dict) # Registration success, sync resource collection for this storage try: self.sync(req, storage['id']) # Post registration, trigger alert sync self.task_rpcapi.sync_storage_alerts(ctxt, storage['id'], query_para=None) except Exception as e: # Unexpected error occurred, while syncing resources. msg = _('Failed to sync resources for storage: %(storage)s. ' 'Error: %(err)s') % {'storage': storage['id'], 'err': e} LOG.error(msg) return storage_view.build_storage(storage)
def create(self, req, body): """Register a new storage device.""" ctxt = req.environ['delfin.context'] access_info_dict = body # Lock to avoid synchronous creating. for access in constants.ACCESS_TYPE: if access_info_dict.get(access) is not None: host = access_info_dict.get(access).get('host') break lock_name = 'storage-create-' + host lock = coordination.Lock(lock_name) with lock: if self._storage_exist(ctxt, access_info_dict): raise exception.StorageAlreadyExists() storage = self.driver_api.discover_storage(ctxt, access_info_dict) # Registration success, sync resource collection for this storage try: self.sync(req, storage['id']) # Post registration, trigger alert sync self.task_rpcapi.sync_storage_alerts(ctxt, storage['id'], query_para=None) except Exception as e: # Unexpected error occurred, while syncing resources. msg = _('Failed to sync resources for storage: %(storage)s. ' 'Error: %(err)s') % { 'storage': storage['id'], 'err': e } LOG.error(msg) try: # Trigger Performance monitoring capabilities = self.driver_api.get_capabilities( context=ctxt, storage_id=storage['id']) validation.validate_capabilities(capabilities) perf_job_controller.create_perf_job(ctxt, storage['id'], capabilities) except exception.EmptyResourceMetrics: msg = _("Resource metric provided by capabilities is empty for " "storage: %s") % storage['id'] LOG.info(msg) except Exception as e: # Unexpected error occurred, while performance monitoring. msg = _('Failed to trigger performance monitoring for storage: ' '%(storage)s. Error: %(err)s') % { 'storage': storage['id'], 'err': six.text_type(e) } LOG.error(msg) return storage_view.build_storage(storage)
def test_lock(self, get_lock): with coordination.Lock('lock'): self.assertTrue(get_lock.called)