def shutdown(self): if not self.live: return self.live = False if self._connect_all_members_timer: self._connect_all_members_timer.cancel() self._heartbeat_manager.shutdown() with self._lock: for connection_future in six.itervalues(self._pending_connections): connection_future.set_exception( HazelcastClientNotActiveError( "Hazelcast client is shutting down")) # Need to create copy of connection values to avoid modification errors on runtime for connection in list(six.itervalues(self.active_connections)): connection.close("Hazelcast client is shutting down", None) self.active_connections.clear() self._addresses_to_connections.clear() self._pending_connections.clear() del self._connection_listeners[:]
def _handle_exception(self, invocation, error, traceback=None): if self.logger.isEnabledFor(logging.DEBUG): self.logger.debug("Got exception for request %s, error: %s" % (invocation.request, error), extra=self._logger_extras) if not self._client.lifecycle_service.is_running(): invocation.set_exception(HazelcastClientNotActiveError(), traceback) self._pending.pop(invocation.request.get_correlation_id(), None) return if not self._should_retry(invocation, error): invocation.set_exception(error, traceback) self._pending.pop(invocation.request.get_correlation_id(), None) return if invocation.timeout < time.time(): self.logger.debug( "Error will not be retried because invocation timed out: %s", error, extra=self._logger_extras) invocation.set_exception( HazelcastTimeoutError( "Request timed out because an error occurred after " "invocation timeout: %s" % error, traceback)) self._pending.pop(invocation.request.get_correlation_id(), None) return invoke_func = functools.partial(self.invoke, invocation) self._reactor.add_timer(self._invocation_retry_pause, invoke_func)
def _notify_error(self, invocation, error): _logger.debug("Got exception for request %s, error: %s", invocation.request, error) if not self._client.lifecycle_service.is_running(): self._complete_with_error(invocation, HazelcastClientNotActiveError()) return if not self._should_retry(invocation, error): self._complete_with_error(invocation, error) return if invocation.timeout < time.time(): _logger.debug( "Error will not be retried because invocation timed out: %s", error) error = OperationTimeoutError( "Request timed out because an error occurred " "after invocation timeout: %s" % error) self._complete_with_error(invocation, error) return invoke_func = functools.partial(self._do_invoke, invocation) self._reactor.add_timer(self._invocation_retry_pause, invoke_func)
def shutdown(self): if self._shutdown: return self._shutdown = True if self._clean_resources_timer: self._clean_resources_timer.cancel() for invocation in list(six.itervalues(self._pending)): self._notify_error(invocation, HazelcastClientNotActiveError())
def _get_or_create_session(self, group_id): with self._lock: if self._shutdown: error = HazelcastClientNotActiveError("Session manager is already shut down!") return ImmediateExceptionFuture(error) session = self._sessions.get(group_id, None) if session is None or not session.is_valid(): with self._mutex(group_id): session = self._sessions.get(group_id) if session is None or not session.is_valid(): return self._create_new_session(group_id) return ImmediateFuture(session)
def get_or_create_unique_thread_id(self, group_id): with self._lock: if self._shutdown: error = HazelcastClientNotActiveError( "Session manager is already shut down!") return ImmediateExceptionFuture(error) key = (group_id, thread_id()) global_thread_id = self._thread_ids.get(key) if global_thread_id: return ImmediateFuture(global_thread_id) return self._request_generate_thread_id(group_id).continue_with( lambda t_id: self._thread_ids.setdefault(key, t_id.result()))
def _send(self, invocation, connection): if self._shutdown: raise HazelcastClientNotActiveError() if self._backup_ack_to_client_enabled: invocation.request.set_backup_aware_flag() message = invocation.request correlation_id = message.get_correlation_id() self._pending[correlation_id] = invocation if invocation.event_handler: self._listener_service.add_event_handler(correlation_id, invocation.event_handler) if not connection.send_message(message): if invocation.event_handler: self._listener_service.remove_event_handler(correlation_id) return False invocation.sent_connection = connection return True
def _send(self, invocation, connection): if self._shutdown: raise HazelcastClientNotActiveError() correlation_id = self._next_correlation_id.get_and_increment() message = invocation.request message.set_correlation_id(correlation_id) message.set_partition_id(invocation.partition_id) self._pending[correlation_id] = invocation if invocation.event_handler: self._listener_service.add_event_handler(correlation_id, invocation.event_handler) self.logger.debug("Sending %s to %s", message, connection, extra=self._logger_extras) if not connection.send_message(message): if invocation.event_handler: self._listener_service.remove_event_handler(correlation_id) return False return True
def _check_client_active(self): if not self._lifecycle_service.running: raise HazelcastClientNotActiveError()
def test_read_many_on_client_not_active_error(self): self.error_on_first_read_many( HazelcastClientNotActiveError("expected")) self.topic.add_listener(no_op) self.assertEqual(1, self.ringbuffer.read_many.call_count) self.assertEqual(0, len(self.topic._runners))
def shutdown(self): self._shutdown = True for invocation in list(six.itervalues(self._pending)): self._handle_exception(invocation, HazelcastClientNotActiveError())