예제 #1
0
    def _on_auth(self, response, connection, address):
        if response.is_success():
            response = client_authentication_codec.decode_response(
                response.result())
            status = response["status"]
            if status == _AuthenticationStatus.AUTHENTICATED:
                return self._handle_successful_auth(response, connection,
                                                    address)

            if status == _AuthenticationStatus.CREDENTIALS_FAILED:
                err = AuthenticationError(
                    "Authentication failed. The configured cluster name on "
                    "the client does not match the one configured in the cluster."
                )
            elif status == _AuthenticationStatus.NOT_ALLOWED_IN_CLUSTER:
                err = ClientNotAllowedInClusterError(
                    "Client is not allowed in the cluster")
            elif status == _AuthenticationStatus.SERIALIZATION_VERSION_MISMATCH:
                err = IllegalStateError(
                    "Server serialization version does not match to client")
            else:
                err = AuthenticationError(
                    "Authentication status code not supported. status: %s" %
                    status)

            connection.close("Failed to authenticate connection", err)
            raise err
        else:
            e = response.exception()
            # This will set the exception for the pending connection future
            connection.close("Failed to authenticate connection", e)
            six.reraise(e.__class__, e, response.traceback())
    def _on_auth(self, response, connection):
        try:
            response = client_authentication_codec.decode_response(
                response.result())
        except Exception as err:
            connection.close("Failed to authenticate connection", err)
            raise err

        status = response["status"]
        if status == _AuthenticationStatus.AUTHENTICATED:
            return self._handle_successful_auth(response, connection)

        if status == _AuthenticationStatus.CREDENTIALS_FAILED:
            err = AuthenticationError(
                "Authentication failed. Check cluster name and credentials.")
        elif status == _AuthenticationStatus.NOT_ALLOWED_IN_CLUSTER:
            err = ClientNotAllowedInClusterError(
                "Client is not allowed in the cluster")
        elif status == _AuthenticationStatus.SERIALIZATION_VERSION_MISMATCH:
            err = IllegalStateError(
                "Server serialization version does not match to client")
        else:
            err = AuthenticationError(
                "Authentication status code not supported. status: %s" %
                status)

        connection.close("Failed to authenticate connection", err)
        raise err
예제 #3
0
    def _sync_connect_to_cluster(self):
        tried_addresses = set()
        self._wait_strategy.reset()
        try:
            while True:
                for address in self._get_possible_addresses():
                    self._check_client_active()
                    tried_addresses.add(address)
                    connection = self._connect(address)
                    if connection:
                        return
                # If the address providers load no addresses (which seems to be possible),
                # then the above loop is not entered and the lifecycle check is missing,
                # hence we need to repeat the same check at this point.
                self._check_client_active()
                if not self._wait_strategy.sleep():
                    break
        except (ClientNotAllowedInClusterError, InvalidConfigurationError):
            cluster_name = self._client.config.cluster_name
            _logger.exception("Stopped trying on cluster %s", cluster_name)

        cluster_name = self._client.config.cluster_name
        _logger.info(
            "Unable to connect to any address from the cluster with name: %s. "
            "The following addresses were tried: %s", cluster_name,
            tried_addresses)
        if self._lifecycle_service.running:
            msg = "Unable to connect to any cluster"
        else:
            msg = "Client is being shutdown"
        raise IllegalStateError(msg)
예제 #4
0
 def _check_acquire_response(self, response):
     try:
         return response.result()
     except WaitKeyCancelledError:
         error = IllegalStateError("Semaphore(\"%s\") not acquired because the acquire call on the "
                                   "CP group is cancelled, possibly because of another indeterminate "
                                   "call from the same thread." % self._object_name)
         raise error
    def _sync_connect_to_cluster(self):
        tried_addresses = set()
        self._wait_strategy.reset()
        try:
            while True:
                tried_addresses_per_attempt = set()
                members = self._cluster_service.get_members()
                if self._shuffle_member_list:
                    random.shuffle(members)

                for member in members:
                    self._check_client_active()
                    tried_addresses_per_attempt.add(member.address)
                    connection = self._connect(member,
                                               self._get_or_connect_to_member)
                    if connection:
                        return

                for address in self._get_possible_addresses():
                    self._check_client_active()
                    if address in tried_addresses_per_attempt:
                        # We already tried this address on from the member list
                        continue

                    tried_addresses_per_attempt.add(address)
                    connection = self._connect(address,
                                               self._get_or_connect_to_address)
                    if connection:
                        return

                tried_addresses.update(tried_addresses_per_attempt)

                # If the address providers load no addresses (which seems to be possible),
                # then the above loop is not entered and the lifecycle check is missing,
                # hence we need to repeat the same check at this point.
                if not tried_addresses_per_attempt:
                    self._check_client_active()

                if not self._wait_strategy.sleep():
                    break
        except (ClientNotAllowedInClusterError, InvalidConfigurationError):
            cluster_name = self._config.cluster_name
            _logger.exception("Stopped trying on cluster %s", cluster_name)

        cluster_name = self._config.cluster_name
        _logger.info(
            "Unable to connect to any address from the cluster with name: %s. "
            "The following addresses were tried: %s",
            cluster_name,
            tried_addresses,
        )
        if self._lifecycle_service.running:
            msg = "Unable to connect to any cluster"
        else:
            msg = "Client is being shutdown"
        raise IllegalStateError(msg)
예제 #6
0
    def wait_initial_member_list_fetched(self):
        """
        Blocks until the initial member list is fetched from the cluster.
        If it is not received within the timeout, an error is raised.

        :raises IllegalStateError: If the member list could not be fetched
        """
        fetched = self._initial_list_fetched.wait(_INITIAL_MEMBERS_TIMEOUT_SECONDS)
        if not fetched:
            raise IllegalStateError("Could not get initial member list from cluster!")
    def _connect(self):
        connection_manager = self._context.connection_manager
        for count in range(0, RETRY_COUNT):
            connection = connection_manager.get_random_connection()
            if connection:
                return connection

            _logger.debug(
                "Could not get a connection for the transaction. Attempt %d of %d",
                count,
                RETRY_COUNT,
                exc_info=True)
            if count + 1 == RETRY_COUNT:
                raise IllegalStateError("No active connection is found")
예제 #8
0
 def check_response(response):
     try:
         response.result()
     except SessionExpiredError:
         self._invalidate_session(session_id)
         return self._do_acquire(current_thread_id, invocation_uuid, permits)
     except WaitKeyCancelledError:
         self._release_session(session_id, permits)
         error = IllegalStateError("Semaphore(\"%s\") not acquired because the acquire call on the CP "
                                   "group is cancelled, possibly because of another indeterminate call "
                                   "from the same thread." % self._object_name)
         raise error
     except Exception as e:
         self._release_session(session_id, permits)
         raise e
예제 #9
0
    def _create_address_provider(self):
        network_config = self.config.network
        address_list_provided = len(network_config.addresses) != 0
        cloud_config = network_config.cloud
        cloud_enabled = cloud_config.enabled or cloud_config.discovery_token != ""
        if address_list_provided and cloud_enabled:
            raise IllegalStateError(
                "Only one discovery method can be enabled at a time. "
                "Cluster members given explicitly: %s, Hazelcast Cloud enabled: %s"
                % (address_list_provided, cloud_enabled))

        cloud_address_provider = self._init_cloud_address_provider(
            cloud_config)
        if cloud_address_provider:
            return cloud_address_provider

        return DefaultAddressProvider(network_config.addresses)
예제 #10
0
    def _create_address_provider(self):
        config = self._config
        cluster_members = config.cluster_members
        address_list_provided = len(cluster_members) > 0
        cloud_discovery_token = config.cloud_discovery_token
        cloud_enabled = cloud_discovery_token is not None
        if address_list_provided and cloud_enabled:
            raise IllegalStateError(
                "Only one discovery method can be enabled at a time. "
                "Cluster members given explicitly: %s, Hazelcast Cloud enabled: %s"
                % (address_list_provided, cloud_enabled))

        if cloud_enabled:
            connection_timeout = self._get_connection_timeout(config)
            return HazelcastCloudAddressProvider(cloud_discovery_token,
                                                 connection_timeout)

        return DefaultAddressProvider(cluster_members)
예제 #11
0
 def _new_illegal_state_error(self, cause=None):
     error = IllegalStateError("Semaphore[\"%s\"] has no valid session!" % self._object_name, cause)
     return error
예제 #12
0
 def result_fnc(f):
     if f.result():
         return True
     raise IllegalStateError("Queue is full!")