Search in sources :

Example 6 with TcpDiscoveryJoinRequestMessage

use of org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage in project ignite by apache.

the class ServerImpl method sendMessageDirectly.

/**
     * Establishes connection to an address, sends message and returns the response (if any).
     *
     * @param msg Message to send.
     * @param addr Address to send message to.
     * @param timeoutHelper Operation timeout helper.
     * @return Response read from the recipient or {@code null} if no response is supposed.
     * @throws IgniteSpiException If an error occurs.
     */
@Nullable
private Integer sendMessageDirectly(TcpDiscoveryAbstractMessage msg, InetSocketAddress addr, IgniteSpiOperationTimeoutHelper timeoutHelper) throws IgniteSpiException {
    assert msg != null;
    assert addr != null;
    Collection<Throwable> errs = null;
    long ackTimeout0 = spi.getAckTimeout();
    int connectAttempts = 1;
    int sslConnectAttempts = 3;
    boolean joinReqSent;
    UUID locNodeId = getLocalNodeId();
    int reconCnt = 0;
    while (true) {
        // Need to set to false on each new iteration,
        // since remote node may leave in the middle of the first iteration.
        joinReqSent = false;
        boolean openSock = false;
        Socket sock = null;
        try {
            long tstamp = U.currentTimeMillis();
            sock = spi.openSocket(addr, timeoutHelper);
            openSock = true;
            TcpDiscoveryHandshakeRequest req = new TcpDiscoveryHandshakeRequest(locNodeId);
            // Handshake.
            spi.writeToSocket(sock, req, timeoutHelper.nextTimeoutChunk(spi.getSocketTimeout()));
            TcpDiscoveryHandshakeResponse res = spi.readMessage(sock, null, timeoutHelper.nextTimeoutChunk(ackTimeout0));
            if (msg instanceof TcpDiscoveryJoinRequestMessage) {
                boolean ignore = false;
                synchronized (failedNodes) {
                    for (TcpDiscoveryNode failedNode : failedNodes.keySet()) {
                        if (failedNode.id().equals(res.creatorNodeId())) {
                            if (log.isDebugEnabled())
                                log.debug("Ignore response from node from failed list: " + res);
                            ignore = true;
                            break;
                        }
                    }
                }
                if (ignore)
                    break;
            }
            if (locNodeId.equals(res.creatorNodeId())) {
                if (log.isDebugEnabled())
                    log.debug("Handshake response from local node: " + res);
                break;
            }
            spi.stats.onClientSocketInitialized(U.currentTimeMillis() - tstamp);
            // Send message.
            tstamp = U.currentTimeMillis();
            spi.writeToSocket(sock, msg, timeoutHelper.nextTimeoutChunk(spi.getSocketTimeout()));
            long tstamp0 = U.currentTimeMillis();
            if (debugMode)
                debugLog(msg, "Message has been sent directly to address [msg=" + msg + ", addr=" + addr + ", rmtNodeId=" + res.creatorNodeId() + ']');
            if (log.isDebugEnabled())
                log.debug("Message has been sent directly to address [msg=" + msg + ", addr=" + addr + ", rmtNodeId=" + res.creatorNodeId() + ']');
            // Connection has been established, but
            // join request may not be unmarshalled on remote host.
            // E.g. due to class not found issue.
            joinReqSent = msg instanceof TcpDiscoveryJoinRequestMessage;
            int receipt = spi.readReceipt(sock, timeoutHelper.nextTimeoutChunk(ackTimeout0));
            spi.stats.onMessageSent(msg, tstamp0 - tstamp);
            return receipt;
        } catch (ClassCastException e) {
            // on dedicated machines.
            if (log.isDebugEnabled())
                U.error(log, "Class cast exception on direct send: " + addr, e);
            onException("Class cast exception on direct send: " + addr, e);
            if (errs == null)
                errs = new ArrayList<>();
            errs.add(e);
        } catch (IOException | IgniteCheckedException e) {
            if (log.isDebugEnabled())
                log.error("Exception on direct send: " + e.getMessage(), e);
            onException("Exception on direct send: " + e.getMessage(), e);
            if (errs == null)
                errs = new ArrayList<>();
            errs.add(e);
            if (X.hasCause(e, SSLException.class)) {
                if (--sslConnectAttempts == 0)
                    throw new IgniteException("Unable to establish secure connection. " + "Was remote cluster configured with SSL? [rmtAddr=" + addr + ", errMsg=\"" + e.getMessage() + "\"]", e);
                continue;
            }
            if (X.hasCause(e, StreamCorruptedException.class)) {
                if (--sslConnectAttempts == 0)
                    throw new IgniteException("Unable to establish plain connection. " + "Was remote cluster configured with SSL? [rmtAddr=" + addr + ", errMsg=\"" + e.getMessage() + "\"]", e);
                continue;
            }
            if (timeoutHelper.checkFailureTimeoutReached(e))
                break;
            if (!spi.failureDetectionTimeoutEnabled() && ++reconCnt == spi.getReconnectCount())
                break;
            if (!openSock) {
                // Reconnect for the second time, if connection is not established.
                if (connectAttempts < 2) {
                    connectAttempts++;
                    continue;
                }
                // Don't retry if we can not establish connection.
                break;
            }
            if (!spi.failureDetectionTimeoutEnabled() && (e instanceof SocketTimeoutException || X.hasCause(e, SocketTimeoutException.class))) {
                ackTimeout0 *= 2;
                if (!checkAckTimeout(ackTimeout0))
                    break;
            }
        } finally {
            U.closeQuiet(sock);
        }
    }
    if (joinReqSent) {
        if (log.isDebugEnabled())
            log.debug("Join request has been sent, but receipt has not been read (returning RES_WAIT).");
        // however, warning on timed out join will be output.
        return RES_OK;
    }
    throw new IgniteSpiException("Failed to send message to address [addr=" + addr + ", msg=" + msg + ']', U.exceptionWithSuppressed("Failed to send message to address " + "[addr=" + addr + ", msg=" + msg + ']', errs));
}
Also used : TcpDiscoveryHandshakeRequest(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryHandshakeRequest) TcpDiscoveryHandshakeResponse(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryHandshakeResponse) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SocketTimeoutException(java.net.SocketTimeoutException) IgniteException(org.apache.ignite.IgniteException) StreamCorruptedException(java.io.StreamCorruptedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) UUID(java.util.UUID) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) TcpDiscoveryNode(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode) TcpDiscoveryJoinRequestMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

TcpDiscoveryJoinRequestMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage)6 TcpDiscoveryAbstractMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage)5 IOException (java.io.IOException)4 Socket (java.net.Socket)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 StreamCorruptedException (java.io.StreamCorruptedException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 SSLException (javax.net.ssl.SSLException)3 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)3 TcpDiscoveryNode (org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode)3 UUID (java.util.UUID)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)2 IgniteSpiOperationTimeoutHelper (org.apache.ignite.spi.IgniteSpiOperationTimeoutHelper)2 TcpDiscoverySpi (org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)2 DiscoveryDataPacket (org.apache.ignite.spi.discovery.tcp.internal.DiscoveryDataPacket)2 TcpDiscoveryHandshakeRequest (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryHandshakeRequest)2 TcpDiscoveryHandshakeResponse (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryHandshakeResponse)2 Nullable (org.jetbrains.annotations.Nullable)2 ObjectStreamException (java.io.ObjectStreamException)1