Search in sources :

Example 1 with Connection

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

the class ClientTestSupport method getAllEventHandlers.

protected Map<Long, EventHandler> getAllEventHandlers(HazelcastInstance client) {
    ClientConnectionManager connectionManager = getHazelcastClientInstanceImpl(client).getConnectionManager();
    Collection<Connection> activeConnections = connectionManager.getActiveConnections();
    HashMap<Long, EventHandler> map = new HashMap<>();
    for (Connection activeConnection : activeConnections) {
        map.putAll(((ClientConnection) activeConnection).getEventHandlers());
    }
    return map;
}
Also used : HashMap(java.util.HashMap) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) Connection(com.hazelcast.internal.nio.Connection) EventHandler(com.hazelcast.client.impl.spi.EventHandler) ClientConnectionManager(com.hazelcast.client.impl.connection.ClientConnectionManager)

Example 2 with Connection

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

the class ClusterJoinManager method handleMasterResponse.

/**
 * Set master address, if required.
 *
 * @param masterAddress address of cluster's master, as provided in {@link MasterResponseOp}
 * @param callerAddress address of node that sent the {@link MasterResponseOp}
 * @see MasterResponseOp
 */
public void handleMasterResponse(Address masterAddress, Address callerAddress) {
    clusterServiceLock.lock();
    try {
        if (logger.isFineEnabled()) {
            logger.fine(format("Handling master response %s from %s", masterAddress, callerAddress));
        }
        if (clusterService.isJoined()) {
            if (logger.isFineEnabled()) {
                logger.fine(format("Ignoring master response %s from %s, this node is already joined", masterAddress, callerAddress));
            }
            return;
        }
        if (node.getThisAddress().equals(masterAddress)) {
            logger.warning("Received my address as master address from " + callerAddress);
            return;
        }
        Address currentMaster = clusterService.getMasterAddress();
        if (currentMaster == null || currentMaster.equals(masterAddress)) {
            setMasterAndJoin(masterAddress);
            return;
        }
        if (currentMaster.equals(callerAddress)) {
            logger.warning(format("Setting master to %s since %s says it is not master anymore", masterAddress, currentMaster));
            setMasterAndJoin(masterAddress);
            return;
        }
        Connection conn = node.getServer().getConnectionManager(MEMBER).get(currentMaster);
        if (conn != null && conn.isAlive()) {
            logger.info(format("Ignoring master response %s from %s since this node has an active master %s", masterAddress, callerAddress, currentMaster));
            sendJoinRequest(currentMaster);
        } else {
            logger.warning(format("Ambiguous master response! Received master response %s from %s. " + "This node has a master %s, but does not have an active connection to it. " + "Master field will be unset now.", masterAddress, callerAddress, currentMaster));
            clusterService.setMasterAddress(null);
        }
    } finally {
        clusterServiceLock.unlock();
    }
}
Also used : Address(com.hazelcast.cluster.Address) ServerConnection(com.hazelcast.internal.server.ServerConnection) Connection(com.hazelcast.internal.nio.Connection)

Example 3 with Connection

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

the class ClusterServiceImpl method suspectAddressIfNotConnected.

public void suspectAddressIfNotConnected(Address address) {
    lock.lock();
    try {
        MemberImpl member = getMember(address);
        if (member == null) {
            if (logger.isFineEnabled()) {
                logger.fine("Cannot suspect " + address + ", since it's not a member.");
            }
            return;
        }
        Connection conn = node.getServer().getConnectionManager(MEMBER).get(address);
        if (conn != null && conn.isAlive()) {
            if (logger.isFineEnabled()) {
                logger.fine("Cannot suspect " + member + ", since there's a live connection -> " + conn);
            }
            return;
        }
        suspectMember(member, "No connection", false);
    } finally {
        lock.unlock();
    }
}
Also used : MemberImpl(com.hazelcast.cluster.impl.MemberImpl) Connection(com.hazelcast.internal.nio.Connection)

Example 4 with Connection

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

the class TcpIpJoiner method joinViaTargetMember.

private void joinViaTargetMember(Address targetAddress, long maxJoinMillis) {
    try {
        if (targetAddress == null) {
            throw new IllegalArgumentException("Invalid target address: NULL");
        }
        if (logger.isFineEnabled()) {
            logger.fine("Joining over target member " + targetAddress);
        }
        if (targetAddress.equals(node.getThisAddress()) || isLocalAddress(targetAddress)) {
            clusterJoinManager.setThisMemberAsMaster();
            return;
        }
        long joinStartTime = Clock.currentTimeMillis();
        Connection connection;
        while (shouldRetry() && (Clock.currentTimeMillis() - joinStartTime < maxJoinMillis)) {
            ServerConnectionManager connectionManager = node.getServer().getConnectionManager(MEMBER);
            connection = connectionManager.getOrConnect(targetAddress);
            if (connection == null) {
                connectionManager.blockOnConnect(targetAddress, JOIN_RETRY_WAIT_TIME, 0);
                continue;
            }
            if (logger.isFineEnabled()) {
                logger.fine("Sending joinRequest " + targetAddress);
            }
            clusterJoinManager.sendJoinRequest(targetAddress);
            if (!clusterService.isJoined()) {
                clusterService.blockOnJoin(JOIN_RETRY_WAIT_TIME);
            }
        }
    } catch (final Exception e) {
        logger.warning(e);
    }
}
Also used : ServerConnectionManager(com.hazelcast.internal.server.ServerConnectionManager) Connection(com.hazelcast.internal.nio.Connection) InvalidAddressException(com.hazelcast.internal.util.AddressUtil.InvalidAddressException) UnknownHostException(java.net.UnknownHostException)

Example 5 with Connection

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

the class TcpServerConnectionManagerBase method send.

protected boolean send(Packet packet, Address target, SendTask sendTask, int streamId) {
    UUID targetUuid = addressRegistry.uuidOf(target);
    if (targetUuid == serverContext.getThisUuid()) {
        logger.warning("Packet send task is rejected. Target is this node! Target[uuid=" + targetUuid + ", address=" + target + "]");
        return false;
    }
    Connection connection = get(targetUuid, streamId);
    if (connection != null) {
        return connection.write(packet);
    }
    if (sendTask == null) {
        sendTask = new SendTask(packet, target, streamId);
    }
    int retries = sendTask.retries;
    if (retries < RETRY_NUMBER && serverContext.isNodeActive()) {
        getOrConnect(target, true, streamId);
        try {
            server.scheduleDeferred(sendTask, (retries + 1) * DELAY_FACTOR, TimeUnit.MILLISECONDS);
            return true;
        } catch (RejectedExecutionException e) {
            if (server.isLive()) {
                throw e;
            }
            if (logger.isFinestEnabled()) {
                logger.finest("Packet send task is rejected. Packet cannot be sent to " + target);
            }
        }
    }
    return false;
}
Also used : ServerConnection(com.hazelcast.internal.server.ServerConnection) Connection(com.hazelcast.internal.nio.Connection) UUID(java.util.UUID) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

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