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;
}
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;
}
}
Aggregations