def update_progress(self): """Updates progress to lease. This call back to the lease is essential to keep the lease alive across long running read operations. :raises: VimException, VimFaultException, VimAttributeException, VimSessionOverLoadException, VimConnectionException """ percent = int(float(self._bytes_read) / self._vmdk_size * 100) LOG.debug( "Calling VIM API to update read progress of VMDK file" " with URL = %(url)s to %(percent)d%%.", { 'url': self._url, 'percent': percent }) try: self._session.invoke_api(self._session.vim, 'HttpNfcLeaseProgress', self._lease, percent=percent) LOG.debug( "Updated read progress of VMDK file with " "URL = %(url)s to %(percent)d%%.", { 'url': self._url, 'percent': percent }) except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception( _LE("Error occurred while updating the " "read progress of VMDK file with URL = %s."), self._url)
def update_progress(self): """Updates progress to lease. This call back to the lease is essential to keep the lease alive across long running read operations. :raises: VimException, VimFaultException, VimAttributeException, VimSessionOverLoadException, VimConnectionException """ percent = int(float(self._bytes_read) / self._vmdk_size * 100) LOG.debug("Calling VIM API to update read progress of VMDK file" " with URL = %(url)s to %(percent)d%%.", {'url': self._url, 'percent': percent}) try: self._session.invoke_api(self._session.vim, 'HttpNfcLeaseProgress', self._lease, percent=percent) LOG.debug("Updated read progress of VMDK file with " "URL = %(url)s to %(percent)d%%.", {'url': self._url, 'percent': percent}) except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Error occurred while updating the " "read progress of VMDK file with URL = %s."), self._url)
def _inner(): if initial_delay: greenthread.sleep(initial_delay) try: while self._running: start = timeutils.utcnow() self.f(*self.args, **self.kw) end = timeutils.utcnow() if not self._running: break delay = interval - timeutils.delta_seconds(start, end) if delay <= 0: LOG.warn(_LW('task run outlasted interval by %s sec'), -delay) greenthread.sleep(delay if delay > 0 else 0) except LoopingCallDone as e: self.stop() done.send(e.retvalue) except Exception: LOG.exception(_LE('in fixed duration looping call')) done.send_exception(*sys.exc_info()) return else: done.send(True)
def inner_func(*args, **kwargs): last_log_time = 0 last_exc_message = None exc_count = 0 while True: try: return infunc(*args, **kwargs) except Exception as exc: this_exc_message = six.u(str(exc)) if this_exc_message == last_exc_message: exc_count += 1 else: exc_count = 1 # Do not log any more frequently than once a minute unless # the exception message changes cur_time = int(time.time()) if (cur_time - last_log_time > 60 or this_exc_message != last_exc_message): logging.exception( _LE('Unexpected exception occurred %d time(s)... ' 'retrying.') % exc_count) last_log_time = cur_time last_exc_message = this_exc_message exc_count = 0 # This should be a very rare event. In case it isn't, do # a sleep. time.sleep(1)
def _func(*args, **kwargs): func_name = f.__name__ result = None try: LOG.debug( "Invoking %(func_name)s; retry count is " "%(retry_count)d.", { 'func_name': func_name, 'retry_count': self._retry_count }) result = f(*args, **kwargs) LOG.debug( "Function %(func_name)s returned successfully " "after %(retry_count)d retries.", { 'func_name': func_name, 'retry_count': self._retry_count }) except self._exceptions: with excutils.save_and_reraise_exception() as ctxt: LOG.warn(_LW("Exception which is in the suggested list of " "exceptions occurred while invoking function:" " %s."), func_name, exc_info=True) if (self._max_retry_count != -1 and self._retry_count >= self._max_retry_count): LOG.error( _LE("Cannot retry upon suggested exception " "since retry count (%(retry_count)d) " "reached max retry count " "(%(max_retry_count)d)."), { 'retry_count': self._retry_count, 'max_retry_count': self._max_retry_count }) else: ctxt.reraise = False self._retry_count += 1 self._sleep_time += self._inc_sleep_time return self._sleep_time except Exception: with excutils.save_and_reraise_exception(): LOG.exception( _LE("Exception which is not in the " "suggested list of exceptions occurred " "while invoking %s."), func_name) raise loopingcall.LoopingCallDone(result)
def __init__(self, message=None, **kwargs): self.kwargs = kwargs if not message: try: message = self.msg_fmt % kwargs except Exception: # kwargs doesn't match a variable in the message # log the issue and the kwargs LOG.exception(_LE('Exception in string format operation')) for name, value in six.iteritems(kwargs): LOG.error(_LE("%(name)s: %(value)s"), {'name': name, 'value': value}) # at least get the core message out if something happened message = self.msg_fmt super(VMwareDriverException, self).__init__(message)
def __exit__(self, exc_type, exc_val, exc_tb): if exc_type is not None: if self.reraise: logging.error(_LE('Original exception being dropped: %s'), traceback.format_exception(self.type_, self.value, self.tb)) return False if self.reraise: six.reraise(self.type_, self.value, self.tb)
def __init__(self, message=None, **kwargs): self.kwargs = kwargs if not message: try: message = self.msg_fmt % kwargs except Exception: # kwargs doesn't match a variable in the message # log the issue and the kwargs LOG.exception(_LE('Exception in string format operation')) for name, value in six.iteritems(kwargs): LOG.error(_LE("%(name)s: %(value)s"), { 'name': name, 'value': value }) # at least get the core message out if something happened message = self.msg_fmt super(VMwareDriverException, self).__init__(message)
def _func(*args, **kwargs): func_name = f.__name__ result = None try: LOG.debug("Invoking %(func_name)s; retry count is " "%(retry_count)d.", {'func_name': func_name, 'retry_count': self._retry_count}) result = f(*args, **kwargs) LOG.debug("Function %(func_name)s returned successfully " "after %(retry_count)d retries.", {'func_name': func_name, 'retry_count': self._retry_count}) except self._exceptions: with excutils.save_and_reraise_exception() as ctxt: LOG.warn(_LW("Exception which is in the suggested list of " "exceptions occurred while invoking function:" " %s."), func_name, exc_info=True) if (self._max_retry_count != -1 and self._retry_count >= self._max_retry_count): LOG.error(_LE("Cannot retry upon suggested exception " "since retry count (%(retry_count)d) " "reached max retry count " "(%(max_retry_count)d)."), {'retry_count': self._retry_count, 'max_retry_count': self._max_retry_count}) else: ctxt.reraise = False self._retry_count += 1 self._sleep_time += self._inc_sleep_time return self._sleep_time except Exception: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Exception which is not in the " "suggested list of exceptions occurred " "while invoking %s."), func_name) raise loopingcall.LoopingCallDone(result)
def logout(self): """Log out and terminate the current session.""" if self._session_id: LOG.info( _LI("Logging out and terminating the current session " "with ID = %s."), _trunc_id(self._session_id)) try: self.vim.Logout(self.vim.service_content.sessionManager) self._session_id = None except Exception: LOG.exception( _LE("Error occurred while logging out and " "terminating the current session with " "ID = %s."), _trunc_id(self._session_id)) else: LOG.debug("No session exists to log out.")
def logout(self): """Log out and terminate the current session.""" if self._session_id: LOG.info(_LI("Logging out and terminating the current session " "with ID = %s."), _trunc_id(self._session_id)) try: self.vim.Logout(self.vim.service_content.sessionManager) self._session_id = None except Exception: LOG.exception(_LE("Error occurred while logging out and " "terminating the current session with " "ID = %s."), _trunc_id(self._session_id)) else: LOG.debug("No session exists to log out.")
def _retrieve_properties_ex_fault_checker(response): """Checks the RetrievePropertiesEx API response for errors. Certain faults are sent in the SOAP body as a property of missingSet. This method raises VimFaultException when a fault is found in the response. :param response: response from RetrievePropertiesEx API call :raises: VimFaultException """ # TODO(rgerganov) this method doesn't belong here because this fault # checking is specific to the Vim service. We should come up with # a new design that allows extensible fault checking and have the # service specific parts in the corresponding child classes LOG.debug("Checking RetrievePropertiesEx API response for faults.") fault_list = [] if not response: # This is the case when the session has timed out. ESX SOAP # server sends an empty RetrievePropertiesExResponse. Normally # missingSet in the response objects has the specifics about # the error, but that's not the case with a timed out idle # session. It is as bad as a terminated session for we cannot # use the session. Therefore setting fault to NotAuthenticated # fault. LOG.debug( "RetrievePropertiesEx API response is empty; setting " "fault to %s.", exceptions.NOT_AUTHENTICATED) fault_list = [exceptions.NOT_AUTHENTICATED] else: for obj_cont in response.objects: if hasattr(obj_cont, 'missingSet'): for missing_elem in obj_cont.missingSet: fault_type = missing_elem.fault.fault.__class__ fault_list.append(fault_type.__name__) if fault_list: LOG.error( _LE("Faults %s found in RetrievePropertiesEx API " "response."), fault_list) raise exceptions.VimFaultException( fault_list, _("Error occurred while calling" " RetrievePropertiesEx.")) LOG.debug("No faults found in RetrievePropertiesEx API response.")
def _retrieve_properties_ex_fault_checker(response): """Checks the RetrievePropertiesEx API response for errors. Certain faults are sent in the SOAP body as a property of missingSet. This method raises VimFaultException when a fault is found in the response. :param response: response from RetrievePropertiesEx API call :raises: VimFaultException """ # TODO(rgerganov) this method doesn't belong here because this fault # checking is specific to the Vim service. We should come up with # a new design that allows extensible fault checking and have the # service specific parts in the corresponding child classes LOG.debug("Checking RetrievePropertiesEx API response for faults.") fault_list = [] if not response: # This is the case when the session has timed out. ESX SOAP # server sends an empty RetrievePropertiesExResponse. Normally # missingSet in the response objects has the specifics about # the error, but that's not the case with a timed out idle # session. It is as bad as a terminated session for we cannot # use the session. Therefore setting fault to NotAuthenticated # fault. LOG.debug("RetrievePropertiesEx API response is empty; setting " "fault to %s.", exceptions.NOT_AUTHENTICATED) fault_list = [exceptions.NOT_AUTHENTICATED] else: for obj_cont in response.objects: if hasattr(obj_cont, 'missingSet'): for missing_elem in obj_cont.missingSet: fault_type = missing_elem.fault.fault.__class__ fault_list.append(fault_type.__name__) if fault_list: LOG.error(_LE("Faults %s found in RetrievePropertiesEx API " "response."), fault_list) raise exceptions.VimFaultException(fault_list, _("Error occurred while calling" " RetrievePropertiesEx.")) LOG.debug("No faults found in RetrievePropertiesEx API response.")
def _poll_lease(self, lease): """Poll the state of the given lease. When the lease is ready, the event (param done) is notified. In case of any error, appropriate exception is set in the event. :param lease: lease whose state is to be polled """ LOG.debug("Invoking VIM API to read state of lease: %s.", lease) try: state = self.invoke_api(vim_util, 'get_object_property', self.vim, lease, 'state') except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Error occurred while checking " "state of lease: %s."), lease) else: if state == 'ready': LOG.debug("Lease: %s is ready.", lease) raise loopingcall.LoopingCallDone() elif state == 'initializing': LOG.debug("Lease: %s is initializing.", lease) elif state == 'error': LOG.debug("Invoking VIM API to read lease: %s error.", lease) error_msg = self._get_error_message(lease) excep_msg = _("Lease: %(lease)s is in error state. Details: " "%(error_msg)s.") % {'lease': lease, 'error_msg': error_msg} LOG.error(excep_msg) raise exceptions.VimException(excep_msg) else: # unknown state excep_msg = _("Unknown state: %(state)s for lease: " "%(lease)s.") % {'state': state, 'lease': lease} LOG.error(excep_msg) raise exceptions.VimException(excep_msg)
def _poll_lease(self, lease): """Poll the state of the given lease. When the lease is ready, the event (param done) is notified. In case of any error, appropriate exception is set in the event. :param lease: lease whose state is to be polled """ LOG.debug("Invoking VIM API to read state of lease: %s.", lease) try: state = self.invoke_api(vim_util, 'get_object_property', self.vim, lease, 'state') except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception( _LE("Error occurred while checking " "state of lease: %s."), lease) else: if state == 'ready': LOG.debug("Lease: %s is ready.", lease) raise loopingcall.LoopingCallDone() elif state == 'initializing': LOG.debug("Lease: %s is initializing.", lease) elif state == 'error': LOG.debug("Invoking VIM API to read lease: %s error.", lease) error_msg = self._get_error_message(lease) excep_msg = _("Lease: %(lease)s is in error state. Details: " "%(error_msg)s.") % { 'lease': lease, 'error_msg': error_msg } LOG.error(excep_msg) raise exceptions.VimException(excep_msg) else: # unknown state excep_msg = _("Unknown state: %(state)s for lease: " "%(lease)s.") % { 'state': state, 'lease': lease } LOG.error(excep_msg) raise exceptions.VimException(excep_msg)
def _poll_task(self, task): """Poll the given task until completion. If the task completes successfully, the method returns the task info using the input event (param done). In case of any error, appropriate exception is set in the event. :param task: managed object reference of the task """ LOG.debug("Invoking VIM API to read info of task: %s.", task) try: task_info = self.invoke_api(vim_util, 'get_object_property', self.vim, task, 'info') except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception( _LE("Error occurred while reading info of " "task: %s."), task) else: if task_info.state in ['queued', 'running']: if hasattr(task_info, 'progress'): LOG.debug("Task: %(task)s progress is %(progress)s%%.", { 'task': task, 'progress': task_info.progress }) elif task_info.state == 'success': LOG.debug("Task: %s status is success.", task) raise loopingcall.LoopingCallDone(task_info) else: error_msg = six.text_type(task_info.error.localizedMessage) excep_msg = _("Task: %(task)s failed with error: " "%(error)s.") % { 'task': task, 'error': error_msg } LOG.error(excep_msg) error = task_info.error name = error.fault.__class__.__name__ task_ex = exceptions.get_fault_class(name)(error_msg) raise task_ex
def _poll_task(self, task): """Poll the given task until completion. If the task completes successfully, the method returns the task info using the input event (param done). In case of any error, appropriate exception is set in the event. :param task: managed object reference of the task """ LOG.debug("Invoking VIM API to read info of task: %s.", task) try: task_info = self.invoke_api(vim_util, 'get_object_property', self.vim, task, 'info') except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Error occurred while reading info of " "task: %s."), task) else: if task_info.state in ['queued', 'running']: if hasattr(task_info, 'progress'): LOG.debug("Task: %(task)s progress is %(progress)s%%.", {'task': task, 'progress': task_info.progress}) elif task_info.state == 'success': LOG.debug("Task: %s status is success.", task) raise loopingcall.LoopingCallDone(task_info) else: error_msg = six.text_type(task_info.error.localizedMessage) excep_msg = _("Task: %(task)s failed with error: " "%(error)s.") % {'task': task, 'error': error_msg} LOG.error(excep_msg) error = task_info.error name = error.fault.__class__.__name__ task_ex = exceptions.get_fault_class(name)(error_msg) raise task_ex
def _inner(): if initial_delay: greenthread.sleep(initial_delay) try: while self._running: idle = self.f(*self.args, **self.kw) if not self._running: break if periodic_interval_max is not None: idle = min(idle, periodic_interval_max) LOG.debug('Dynamic looping call sleeping for %.02f ' 'seconds', idle) greenthread.sleep(idle) except LoopingCallDone as e: self.stop() done.send(e.retvalue) except Exception: LOG.exception(_LE('in dynamic looping call')) done.send_exception(*sys.exc_info()) return else: done.send(True)