Search in sources :

Example 6 with CandidateClusterContext

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));
}
Also used : CandidateClusterContext(com.hazelcast.client.impl.clientside.CandidateClusterContext) ArrayList(java.util.ArrayList) ClusterDiscoveryService(com.hazelcast.client.impl.clientside.ClusterDiscoveryService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with CandidateClusterContext

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);
    }
}
Also used : CandidateClusterContext(com.hazelcast.client.impl.clientside.CandidateClusterContext) AddressProvider(com.hazelcast.client.impl.connection.AddressProvider) HazelcastException(com.hazelcast.core.HazelcastException) Address(com.hazelcast.cluster.Address) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) HazelcastException(com.hazelcast.core.HazelcastException) HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException) IOException(java.io.IOException) AuthenticationException(com.hazelcast.client.AuthenticationException) HazelcastClientOfflineException(com.hazelcast.client.HazelcastClientOfflineException) EOFException(java.io.EOFException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClientNotAllowedInClusterException(com.hazelcast.client.ClientNotAllowedInClusterException) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException)

Example 8 with CandidateClusterContext

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);
    }
}
Also used : CandidateClusterContext(com.hazelcast.client.impl.clientside.CandidateClusterContext) PasswordCredentials(com.hazelcast.security.PasswordCredentials) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) TokenCredentials(com.hazelcast.security.TokenCredentials) PasswordCredentials(com.hazelcast.security.PasswordCredentials) Credentials(com.hazelcast.security.Credentials) TokenCredentials(com.hazelcast.security.TokenCredentials)

Example 9 with CandidateClusterContext

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);
    }
}
Also used : CandidateClusterContext(com.hazelcast.client.impl.clientside.CandidateClusterContext) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(java.nio.channels.SocketChannel) Channel(com.hazelcast.internal.networking.Channel) SocketInterceptor(com.hazelcast.nio.SocketInterceptor) Socket(java.net.Socket) HazelcastException(com.hazelcast.core.HazelcastException) HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException) IOException(java.io.IOException) AuthenticationException(com.hazelcast.client.AuthenticationException) HazelcastClientOfflineException(com.hazelcast.client.HazelcastClientOfflineException) EOFException(java.io.EOFException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClientNotAllowedInClusterException(com.hazelcast.client.ClientNotAllowedInClusterException) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException)

Example 10 with CandidateClusterContext

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);
}
Also used : CandidateClusterContext(com.hazelcast.client.impl.clientside.CandidateClusterContext)

Aggregations

CandidateClusterContext (com.hazelcast.client.impl.clientside.CandidateClusterContext)13 ClusterDiscoveryService (com.hazelcast.client.impl.clientside.ClusterDiscoveryService)9 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)9 MutableInteger (com.hazelcast.internal.util.MutableInteger)3 HashSet (java.util.HashSet)3 AuthenticationException (com.hazelcast.client.AuthenticationException)2 ClientNotAllowedInClusterException (com.hazelcast.client.ClientNotAllowedInClusterException)2 HazelcastClientNotActiveException (com.hazelcast.client.HazelcastClientNotActiveException)2 HazelcastClientOfflineException (com.hazelcast.client.HazelcastClientOfflineException)2 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)2 HazelcastException (com.hazelcast.core.HazelcastException)2 TargetDisconnectedException (com.hazelcast.spi.exception.TargetDisconnectedException)2 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 AddressProvider (com.hazelcast.client.impl.connection.AddressProvider)1