Search in sources :

Example 1 with ClientNotAllowedInClusterException

use of com.hazelcast.client.ClientNotAllowedInClusterException in project hazelcast by hazelcast.

the class TcpClientConnectionManager method doConnectToCandidateCluster.

private boolean doConnectToCandidateCluster(CandidateClusterContext context, boolean switchingToNextCluster) {
    Set<Address> triedAddresses = new HashSet<>();
    try {
        waitStrategy.reset();
        do {
            Set<Address> triedAddressesPerAttempt = new HashSet<>();
            List<Member> memberList = new ArrayList<>(client.getClientClusterService().getMemberList());
            if (shuffleMemberList) {
                Collections.shuffle(memberList);
            }
            // try to connect to a member in the member list first
            for (Member member : memberList) {
                checkClientActive();
                triedAddressesPerAttempt.add(member.getAddress());
                Connection connection = connect(member, o -> getOrConnectToMember((Member) o, switchingToNextCluster));
                if (connection != null) {
                    return true;
                }
            }
            // try to connect to a member given via config(explicit config/discovery mechanisms)
            for (Address address : getPossibleMemberAddresses(context.getAddressProvider())) {
                checkClientActive();
                if (!triedAddressesPerAttempt.add(address)) {
                    // if we can not add it means that it is already tried to be connected with the member list
                    continue;
                }
                Connection connection = connect(address, o -> getOrConnectToAddress((Address) o, switchingToNextCluster));
                if (connection != null) {
                    return true;
                }
            }
            triedAddresses.addAll(triedAddressesPerAttempt);
            // and the lifecycle check is missing, hence we need to repeat the same check at this point.
            if (triedAddressesPerAttempt.isEmpty()) {
                checkClientActive();
            }
        } while (waitStrategy.sleep());
    } catch (ClientNotAllowedInClusterException | InvalidConfigurationException e) {
        logger.warning("Stopped trying on the cluster: " + context.getClusterName() + " reason: " + e.getMessage());
    }
    logger.info("Unable to connect to any address from the cluster with name: " + context.getClusterName() + ". The following addresses were tried: " + triedAddresses);
    return false;
}
Also used : Address(com.hazelcast.cluster.Address) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ClientNotAllowedInClusterException(com.hazelcast.client.ClientNotAllowedInClusterException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Connection(com.hazelcast.internal.nio.Connection) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) Member(com.hazelcast.cluster.Member) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException)

Example 2 with ClientNotAllowedInClusterException

use of com.hazelcast.client.ClientNotAllowedInClusterException in project hazelcast by hazelcast.

the class TcpClientConnectionManager method checkAuthenticationResponse.

/**
 * Checks the response from the server to see if authentication needs to be continued,
 * closes the connection and throws exception if the authentication needs to be cancelled.
 */
private void checkAuthenticationResponse(TcpClientConnection connection, ClientAuthenticationCodec.ResponseParameters response) {
    AuthenticationStatus authenticationStatus = AuthenticationStatus.getById(response.status);
    if (failoverConfigProvided && !response.failoverSupported) {
        logger.warning("Cluster does not support failover. This feature is available in Hazelcast Enterprise");
        authenticationStatus = NOT_ALLOWED_IN_CLUSTER;
    }
    switch(authenticationStatus) {
        case AUTHENTICATED:
            break;
        case CREDENTIALS_FAILED:
            AuthenticationException authException = new AuthenticationException("Authentication failed. The configured " + "cluster name on the client (see ClientConfig.setClusterName()) does not match the one configured " + "in the cluster or the credentials set in the Client security config could not be authenticated");
            connection.close("Failed to authenticate connection", authException);
            throw authException;
        case NOT_ALLOWED_IN_CLUSTER:
            ClientNotAllowedInClusterException notAllowedException = new ClientNotAllowedInClusterException("Client is not allowed in the cluster");
            connection.close("Failed to authenticate connection", notAllowedException);
            throw notAllowedException;
        default:
            AuthenticationException exception = new AuthenticationException("Authentication status code not supported. status: " + authenticationStatus);
            connection.close("Failed to authenticate connection", exception);
            throw exception;
    }
    ClientPartitionServiceImpl partitionService = (ClientPartitionServiceImpl) client.getClientPartitionService();
    if (!partitionService.checkAndSetPartitionCount(response.partitionCount)) {
        ClientNotAllowedInClusterException exception = new ClientNotAllowedInClusterException("Client can not work with this cluster" + " because it has a different partition count. " + "Expected partition count: " + partitionService.getPartitionCount() + ", Member partition count: " + response.partitionCount);
        connection.close("Failed to authenticate connection", exception);
        throw exception;
    }
}
Also used : AuthenticationStatus(com.hazelcast.client.impl.protocol.AuthenticationStatus) ClientNotAllowedInClusterException(com.hazelcast.client.ClientNotAllowedInClusterException) AuthenticationException(com.hazelcast.client.AuthenticationException) ClientPartitionServiceImpl(com.hazelcast.client.impl.spi.impl.ClientPartitionServiceImpl)

Aggregations

ClientNotAllowedInClusterException (com.hazelcast.client.ClientNotAllowedInClusterException)2 AuthenticationException (com.hazelcast.client.AuthenticationException)1 ClientConnection (com.hazelcast.client.impl.connection.ClientConnection)1 AuthenticationStatus (com.hazelcast.client.impl.protocol.AuthenticationStatus)1 ClientPartitionServiceImpl (com.hazelcast.client.impl.spi.impl.ClientPartitionServiceImpl)1 Address (com.hazelcast.cluster.Address)1 Member (com.hazelcast.cluster.Member)1 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)1 Connection (com.hazelcast.internal.nio.Connection)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1