Search in sources :

Example 1 with AddressProvider

use of com.hazelcast.client.impl.connection.AddressProvider 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 2 with AddressProvider

use of com.hazelcast.client.impl.connection.AddressProvider in project hazelcast by hazelcast.

the class ClusterDiscoveryServiceBuilder method build.

public ClusterDiscoveryService build() {
    ArrayList<CandidateClusterContext> contexts = new ArrayList<>();
    for (ClientConfig config : configs) {
        ClientNetworkConfig networkConfig = config.getNetworkConfig();
        SocketInterceptor interceptor = initSocketInterceptor(networkConfig.getSocketInterceptorConfig());
        ICredentialsFactory credentialsFactory = initCredentialsFactory(config);
        if (credentialsFactory == null) {
            credentialsFactory = new StaticCredentialsFactory(new UsernamePasswordCredentials(null, null));
        }
        credentialsFactory.configure(new ClientCallbackHandler(config));
        DiscoveryService discoveryService = initDiscoveryService(config);
        AddressProvider provider;
        if (externalAddressProvider != null) {
            provider = externalAddressProvider;
        } else {
            provider = createAddressProvider(config, discoveryService);
        }
        final SSLConfig sslConfig = networkConfig.getSSLConfig();
        final SocketOptions socketOptions = networkConfig.getSocketOptions();
        contexts.add(new CandidateClusterContext(config.getClusterName(), provider, discoveryService, credentialsFactory, interceptor, clientExtension.createChannelInitializer(sslConfig, socketOptions)));
    }
    return new ClusterDiscoveryService(unmodifiableList(contexts), configsTryCount, lifecycleService);
}
Also used : SSLConfig(com.hazelcast.config.SSLConfig) SocketOptions(com.hazelcast.client.config.SocketOptions) ArrayList(java.util.ArrayList) UsernamePasswordCredentials(com.hazelcast.security.UsernamePasswordCredentials) DefaultAddressProvider(com.hazelcast.client.impl.spi.impl.DefaultAddressProvider) AddressProvider(com.hazelcast.client.impl.connection.AddressProvider) RemoteAddressProvider(com.hazelcast.client.impl.spi.impl.discovery.RemoteAddressProvider) ICredentialsFactory(com.hazelcast.security.ICredentialsFactory) SocketInterceptor(com.hazelcast.nio.SocketInterceptor) StaticCredentialsFactory(com.hazelcast.config.security.StaticCredentialsFactory) ClientConfig(com.hazelcast.client.config.ClientConfig) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) DiscoveryService(com.hazelcast.spi.discovery.integration.DiscoveryService) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig)

Example 3 with AddressProvider

use of com.hazelcast.client.impl.connection.AddressProvider in project hazelcast by hazelcast.

the class ClientRegressionWithRealNetworkTest method testOperationsContinueWhenClientDisconnected.

private void testOperationsContinueWhenClientDisconnected(ClientConnectionStrategyConfig.ReconnectMode reconnectMode) {
    HazelcastInstance instance1 = Hazelcast.newHazelcastInstance();
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getConnectionStrategyConfig().setReconnectMode(reconnectMode);
    AtomicBoolean waitFlag = new AtomicBoolean();
    CountDownLatch testFinished = new CountDownLatch(1);
    AddressProvider addressProvider = new AddressProvider() {

        @Override
        public Addresses loadAddresses() {
            if (waitFlag.get()) {
                try {
                    testFinished.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return AddressHelper.getSocketAddresses("127.0.0.1");
        }

        @Override
        public Address translate(Address address) {
            return address;
        }
    };
    clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
    clientConfig.setProperty(ClientProperty.INVOCATION_TIMEOUT_SECONDS.getName(), "3");
    HazelcastInstance client = HazelcastClientUtil.newHazelcastClient(addressProvider, clientConfig);
    HazelcastInstance instance2 = Hazelcast.newHazelcastInstance();
    warmUpPartitions(instance1, instance2);
    String keyOwnedBy2 = generateKeyOwnedBy(instance2);
    makeSureConnectedToServers(client, 2);
    IMap<Object, Object> clientMap = client.getMap("test");
    // we are closing a connection and making sure It is not established ever again
    waitFlag.set(true);
    UUID memberUUID = instance1.getLocalEndpoint().getUuid();
    instance1.shutdown();
    makeSureDisconnectedFromServer(client, memberUUID);
    // we expect these operations to run without throwing exception, since they are done on live instance.
    clientMap.put(keyOwnedBy2, 1);
    assertEquals(1, clientMap.get(keyOwnedBy2));
    testFinished.countDown();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AddressProvider(com.hazelcast.client.impl.connection.AddressProvider) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Address(com.hazelcast.cluster.Address) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 4 with AddressProvider

use of com.hazelcast.client.impl.connection.AddressProvider in project hazelcast by hazelcast.

the class TestHazelcastFactory method createAddressProvider.

private AddressProvider createAddressProvider(ClientConfig config) {
    boolean discoveryEnabled = new HazelcastProperties(config.getProperties()).getBoolean(ClientProperty.DISCOVERY_SPI_ENABLED);
    List<DiscoveryStrategyConfig> aliasedDiscoveryConfigs = ClientAliasedDiscoveryConfigUtils.createDiscoveryStrategyConfigs(config);
    List<String> userConfiguredAddresses = config.getNetworkConfig().getAddresses();
    boolean isAtLeastAProviderConfigured = discoveryEnabled || !aliasedDiscoveryConfigs.isEmpty() || !userConfiguredAddresses.isEmpty();
    if (isAtLeastAProviderConfigured) {
        // address providers or addresses are configured explicitly, don't add more addresses
        return null;
    }
    return new AddressProvider() {

        @Override
        public Addresses loadAddresses() {
            Addresses possibleAddresses = new Addresses();
            for (Address address : getKnownAddresses()) {
                Addresses addresses = AddressHelper.getPossibleSocketAddresses(address.getPort(), address.getHost(), 1);
                possibleAddresses.addAll(addresses);
            }
            return possibleAddresses;
        }

        @Override
        public Address translate(Address address) {
            return address;
        }
    };
}
Also used : Addresses(com.hazelcast.client.impl.connection.Addresses) HazelcastProperties(com.hazelcast.spi.properties.HazelcastProperties) AddressProvider(com.hazelcast.client.impl.connection.AddressProvider) Address(com.hazelcast.cluster.Address) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig)

Aggregations

AddressProvider (com.hazelcast.client.impl.connection.AddressProvider)4 Address (com.hazelcast.cluster.Address)3 ClientConfig (com.hazelcast.client.config.ClientConfig)2 AuthenticationException (com.hazelcast.client.AuthenticationException)1 ClientNotAllowedInClusterException (com.hazelcast.client.ClientNotAllowedInClusterException)1 HazelcastClientNotActiveException (com.hazelcast.client.HazelcastClientNotActiveException)1 HazelcastClientOfflineException (com.hazelcast.client.HazelcastClientOfflineException)1 ClientNetworkConfig (com.hazelcast.client.config.ClientNetworkConfig)1 SocketOptions (com.hazelcast.client.config.SocketOptions)1 CandidateClusterContext (com.hazelcast.client.impl.clientside.CandidateClusterContext)1 Addresses (com.hazelcast.client.impl.connection.Addresses)1 DefaultAddressProvider (com.hazelcast.client.impl.spi.impl.DefaultAddressProvider)1 RemoteAddressProvider (com.hazelcast.client.impl.spi.impl.discovery.RemoteAddressProvider)1 DiscoveryStrategyConfig (com.hazelcast.config.DiscoveryStrategyConfig)1 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)1 SSLConfig (com.hazelcast.config.SSLConfig)1 StaticCredentialsFactory (com.hazelcast.config.security.StaticCredentialsFactory)1 HazelcastException (com.hazelcast.core.HazelcastException)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 SocketInterceptor (com.hazelcast.nio.SocketInterceptor)1