Search in sources :

Example 11 with Connection

use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.

the class SqlErrorClientTest method testMissingHandler.

@Test
public void testMissingHandler() {
    instance1 = newHazelcastInstance(true);
    client = newClient();
    try {
        ClientMessage message = SqlExecute_reservedCodec.encodeRequest("SELECT * FROM table", Collections.emptyList(), 100L, 100);
        SqlClientService clientService = ((SqlClientService) client.getSql());
        Connection connection = clientService.getQueryConnection();
        clientService.invokeOnConnection(connection, message);
        fail("Must fail");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Unrecognized client message received"));
    }
}
Also used : Connection(com.hazelcast.internal.nio.Connection) SqlClientService(com.hazelcast.sql.impl.client.SqlClientService) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) IOException(java.io.IOException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with Connection

use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.

the class ClientICMPManager method start.

public static void start(ClientIcmpPingConfig clientIcmpPingConfig, TaskScheduler taskScheduler, ILogger logger, Collection<Connection> unmodifiableActiveConnections) {
    if (!clientIcmpPingConfig.isEnabled()) {
        return;
    }
    if (clientIcmpPingConfig.isEchoFailFastOnStartup()) {
        echoFailFast(logger);
    }
    int icmpTtl = clientIcmpPingConfig.getTtl();
    int icmpTimeoutMillis = clientIcmpPingConfig.getTimeoutMilliseconds();
    int icmpIntervalMillis = clientIcmpPingConfig.getIntervalMilliseconds();
    int icmpMaxAttempts = clientIcmpPingConfig.getMaxAttempts();
    if (icmpTimeoutMillis > icmpIntervalMillis) {
        throw new IllegalStateException("ICMP timeout is set to a value greater than the ICMP interval, " + "this is not allowed.");
    }
    if (icmpIntervalMillis < MIN_ICMP_INTERVAL_MILLIS) {
        throw new IllegalStateException("ICMP interval is set to a value less than the min allowed, " + MIN_ICMP_INTERVAL_MILLIS + "ms");
    }
    PingFailureDetector<Connection> failureDetector = new PingFailureDetector<>(icmpMaxAttempts);
    taskScheduler.scheduleWithRepetition(() -> {
        failureDetector.retainAttemptsForAliveEndpoints(unmodifiableActiveConnections);
        for (Connection connection : unmodifiableActiveConnections) {
            // we don't want an isReachable call to an address stopping us to check other addresses.
            // so we run each check in its own thread
            taskScheduler.execute(() -> ping(logger, failureDetector, connection, icmpTtl, icmpTimeoutMillis));
        }
    }, icmpIntervalMillis, icmpIntervalMillis, TimeUnit.MILLISECONDS);
}
Also used : PingFailureDetector(com.hazelcast.internal.cluster.fd.PingFailureDetector) Connection(com.hazelcast.internal.nio.Connection)

Example 13 with Connection

use of com.hazelcast.internal.nio.Connection 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 14 with Connection

use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.

the class HeartbeatManager method start.

public static void start(HazelcastClientInstanceImpl client, TaskScheduler taskScheduler, ILogger logger, long heartbeatIntervalMillis, long heartbeatTimeoutMillis, Collection<Connection> unmodifiableActiveConnections) {
    taskScheduler.scheduleWithRepetition(() -> {
        long now = Clock.currentTimeMillis();
        for (Connection connection : unmodifiableActiveConnections) {
            if (!connection.isAlive()) {
                return;
            }
            if (now - connection.lastReadTimeMillis() > heartbeatTimeoutMillis) {
                logger.warning("Heartbeat failed over the connection: " + connection);
                connection.close("Heartbeat timed out", new TargetDisconnectedException("Heartbeat timed out to connection " + connection));
                return;
            }
            if (now - connection.lastWriteTimeMillis() > heartbeatIntervalMillis) {
                ClientMessage request = ClientPingCodec.encodeRequest();
                ClientInvocation invocation = new ClientInvocation(client, request, null, connection);
                invocation.invokeUrgent();
            }
        }
    }, heartbeatIntervalMillis, heartbeatIntervalMillis, MILLISECONDS);
}
Also used : Connection(com.hazelcast.internal.nio.Connection) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 15 with Connection

use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.

the class ClientEngineImpl method accept.

public void accept(ClientMessage clientMessage) {
    Connection connection = clientMessage.getConnection();
    MessageTask messageTask = messageTaskFactory.create(clientMessage, connection);
    OperationServiceImpl operationService = nodeEngine.getOperationService();
    if (isUrgent(messageTask)) {
        operationService.execute((UrgentMessageTask) messageTask);
    } else if (messageTask instanceof AbstractPartitionMessageTask) {
        operationService.execute((AbstractPartitionMessageTask) messageTask);
    } else if (isQuery(messageTask)) {
        queryExecutor.execute(messageTask);
    } else if (messageTask instanceof TransactionalMessageTask) {
        blockingExecutor.execute(messageTask);
    } else if (messageTask instanceof BlockingMessageTask) {
        blockingExecutor.execute(messageTask);
    } else {
        executor.execute(messageTask);
    }
}
Also used : BlockingMessageTask(com.hazelcast.client.impl.protocol.task.BlockingMessageTask) UrgentMessageTask(com.hazelcast.client.impl.protocol.task.UrgentMessageTask) AbstractMapQueryMessageTask(com.hazelcast.client.impl.protocol.task.map.AbstractMapQueryMessageTask) SqlAbstractMessageTask(com.hazelcast.sql.impl.client.SqlAbstractMessageTask) AuthenticationBaseMessageTask(com.hazelcast.client.impl.protocol.task.AuthenticationBaseMessageTask) MessageTask(com.hazelcast.client.impl.protocol.task.MessageTask) TransactionalMessageTask(com.hazelcast.client.impl.protocol.task.TransactionalMessageTask) AbstractPartitionMessageTask(com.hazelcast.client.impl.protocol.task.AbstractPartitionMessageTask) ServerConnection(com.hazelcast.internal.server.ServerConnection) Connection(com.hazelcast.internal.nio.Connection) AbstractPartitionMessageTask(com.hazelcast.client.impl.protocol.task.AbstractPartitionMessageTask) BlockingMessageTask(com.hazelcast.client.impl.protocol.task.BlockingMessageTask) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl) TransactionalMessageTask(com.hazelcast.client.impl.protocol.task.TransactionalMessageTask)

Aggregations

Connection (com.hazelcast.internal.nio.Connection)41 Test (org.junit.Test)14 QuickTest (com.hazelcast.test.annotation.QuickTest)11 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)8 ServerConnection (com.hazelcast.internal.server.ServerConnection)7 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)7 ClientConnection (com.hazelcast.client.impl.connection.ClientConnection)6 Address (com.hazelcast.cluster.Address)6 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)5 Member (com.hazelcast.cluster.Member)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 ConnectionListener (com.hazelcast.internal.nio.ConnectionListener)5 ClientConnectionManager (com.hazelcast.client.impl.connection.ClientConnectionManager)4 ILogger (com.hazelcast.logging.ILogger)4 QueryId (com.hazelcast.sql.impl.QueryId)4 Map (java.util.Map)4 UUID (java.util.UUID)4 ClientInvocation (com.hazelcast.client.impl.spi.impl.ClientInvocation)3 TargetDisconnectedException (com.hazelcast.spi.exception.TargetDisconnectedException)3 IOException (java.io.IOException)3