use of com.hazelcast.client.impl.clientside.CandidateClusterContext in project hazelcast by hazelcast.
the class ClientClusterDiscoveryServiceTest method test_continueFromWhereItleftOff.
@Test
public void test_continueFromWhereItleftOff() {
ArrayList<CandidateClusterContext> arrayList = new ArrayList<>();
int numberOfCandidates = 10;
for (int i = 0; i < numberOfCandidates; i++) {
arrayList.add(createContext(i));
}
ClusterDiscoveryService discoveryService = new ClusterDiscoveryService(arrayList, 1, lifecycleService);
getNextCluster(discoveryService);
assertEquals(arrayList.get(2), getNextCluster(discoveryService));
assertEquals(arrayList.get(3), getNextCluster(discoveryService));
}
use of com.hazelcast.client.impl.clientside.CandidateClusterContext in project hazelcast by hazelcast.
the class TcpClientConnectionManager method translate.
private Address translate(Address target) {
CandidateClusterContext currentContext = clusterDiscoveryService.current();
AddressProvider addressProvider = currentContext.getAddressProvider();
try {
Address translatedAddress = addressProvider.translate(target);
if (translatedAddress == null) {
throw new HazelcastException("Address Provider " + addressProvider.getClass() + " could not translate address " + target);
}
return translatedAddress;
} catch (Exception e) {
logger.warning("Failed to translate address " + target + " via address provider " + e.getMessage());
throw rethrow(e);
}
}
use of com.hazelcast.client.impl.clientside.CandidateClusterContext in project hazelcast by hazelcast.
the class TcpClientConnectionManager method encodeAuthenticationRequest.
private ClientMessage encodeAuthenticationRequest(Address toAddress) {
InternalSerializationService ss = client.getSerializationService();
byte serializationVersion = ss.getVersion();
CandidateClusterContext currentContext = clusterDiscoveryService.current();
Credentials credentials = currentContext.getCredentialsFactory().newCredentials(toAddress);
String clusterName = currentContext.getClusterName();
currentCredentials = credentials;
if (credentials instanceof PasswordCredentials) {
PasswordCredentials cr = (PasswordCredentials) credentials;
return ClientAuthenticationCodec.encodeRequest(clusterName, cr.getName(), cr.getPassword(), clientUuid, connectionType, serializationVersion, BuildInfoProvider.getBuildInfo().getVersion(), client.getName(), labels);
} else {
byte[] secretBytes;
if (credentials instanceof TokenCredentials) {
secretBytes = ((TokenCredentials) credentials).getToken();
} else {
secretBytes = ss.toData(credentials).toByteArray();
}
return ClientAuthenticationCustomCodec.encodeRequest(clusterName, secretBytes, clientUuid, connectionType, serializationVersion, BuildInfoProvider.getBuildInfo().getVersion(), client.getName(), labels);
}
}
use of com.hazelcast.client.impl.clientside.CandidateClusterContext in project hazelcast by hazelcast.
the class TcpClientConnectionManager method createSocketConnection.
@SuppressWarnings("unchecked")
protected TcpClientConnection createSocketConnection(Address target) {
CandidateClusterContext currentClusterContext = clusterDiscoveryService.current();
SocketChannel socketChannel = null;
try {
socketChannel = SocketChannel.open();
Socket socket = socketChannel.socket();
bindSocketToPort(socket);
Channel channel = networking.register(currentClusterContext.getChannelInitializer(), socketChannel, true);
channel.attributeMap().put(Address.class, target);
InetSocketAddress inetSocketAddress = new InetSocketAddress(target.getInetAddress(), target.getPort());
channel.connect(inetSocketAddress, connectionTimeoutMillis);
TcpClientConnection connection = new TcpClientConnection(client, connectionIdGen.incrementAndGet(), channel);
socketChannel.configureBlocking(true);
SocketInterceptor socketInterceptor = currentClusterContext.getSocketInterceptor();
if (socketInterceptor != null) {
socketInterceptor.onConnect(socket);
}
channel.start();
return connection;
} catch (Exception e) {
closeResource(socketChannel);
logger.finest(e);
throw rethrow(e);
}
}
use of com.hazelcast.client.impl.clientside.CandidateClusterContext in project hazelcast by hazelcast.
the class TcpClientConnectionManager method doConnectToCluster.
private void doConnectToCluster() {
CandidateClusterContext currentContext = clusterDiscoveryService.current();
logger.info("Trying to connect to cluster: " + currentContext.getClusterName());
// try the current cluster
if (doConnectToCandidateCluster(currentContext, false)) {
return;
}
synchronized (clientStateMutex) {
if (activeConnections.isEmpty()) {
clientState = ClientState.SWITCHING_CLUSTER;
} else {
// we don't need to switch cluster anymore.
return;
}
}
// try the next cluster
if (clusterDiscoveryService.tryNextCluster(this::destroyCurrentClusterConnectionAndTryNextCluster)) {
return;
}
// notify when no succeeded cluster connection is found
String msg = client.getLifecycleService().isRunning() ? "Unable to connect to any cluster." : "Client is being shutdown.";
throw new IllegalStateException(msg);
}
Aggregations