Search in sources :

Example 1 with TraceableMessage

use of org.apache.ignite.internal.processors.tracing.messages.TraceableMessage in project ignite by apache.

the class ClientImpl method sendJoinRequest.

/**
 * @param recon {@code True} if reconnects.
 * @param addr Address.
 * @return Socket, connect response and client acknowledge support flag.
 */
@Nullable
private T3<SocketStream, Integer, Boolean> sendJoinRequest(boolean recon, InetSocketAddress addr) {
    assert addr != null;
    if (log.isDebugEnabled())
        log.debug("Send join request [addr=" + addr + ", reconnect=" + recon + ", locNodeId=" + getLocalNodeId() + ']');
    Collection<Throwable> errs = null;
    long ackTimeout0 = spi.getAckTimeout();
    int reconCnt = 0;
    int connectAttempts = 1;
    int sslConnectAttempts = 3;
    UUID locNodeId = getLocalNodeId();
    IgniteSpiOperationTimeoutHelper timeoutHelper = new IgniteSpiOperationTimeoutHelper(spi, true);
    DiscoveryDataPacket discoveryData = null;
    while (true) {
        boolean openSock = false;
        Socket sock = null;
        try {
            long tsNanos = System.nanoTime();
            sock = spi.openSocket(addr, timeoutHelper);
            openSock = true;
            TcpDiscoveryHandshakeRequest req = new TcpDiscoveryHandshakeRequest(locNodeId);
            req.client(true);
            spi.writeToSocket(sock, req, timeoutHelper.nextTimeoutChunk(spi.getSocketTimeout()));
            TcpDiscoveryHandshakeResponse res = spi.readMessage(sock, null, ackTimeout0);
            UUID rmtNodeId = res.creatorNodeId();
            assert rmtNodeId != null;
            assert !getLocalNodeId().equals(rmtNodeId);
            locNode.clientRouterNodeId(rmtNodeId);
            tsNanos = System.nanoTime();
            TcpDiscoveryAbstractMessage msg;
            if (!recon) {
                TcpDiscoveryNode node = locNode;
                if (locNode.order() > 0) {
                    node = locNode.clientReconnectNode(spi.locNodeAttrs);
                    marshalCredentials(node);
                }
                if (discoveryData == null) {
                    DiscoveryDataPacket dataPacket = new DiscoveryDataPacket(getLocalNodeId());
                    dataPacket.joiningNodeClient(true);
                    discoveryData = spi.collectExchangeData(dataPacket);
                }
                TcpDiscoveryJoinRequestMessage joinReqMsg = new TcpDiscoveryJoinRequestMessage(node, discoveryData);
                TcpDiscoveryNode nodef = node;
                joinReqMsg.spanContainer().span(tracing.create(TraceableMessagesTable.traceName(joinReqMsg.getClass())).addTag(SpanTags.tag(SpanTags.EVENT_NODE, SpanTags.ID), () -> nodef.id().toString()).addTag(SpanTags.tag(SpanTags.EVENT_NODE, SpanTags.CONSISTENT_ID), () -> nodef.consistentId().toString()).addLog(() -> "Created").end());
                msg = joinReqMsg;
                // The only way to know is passing flag directly with handshake response.
                if (!res.isDiscoveryDataPacketCompression())
                    ((TcpDiscoveryJoinRequestMessage) msg).gridDiscoveryData().unzipData(log);
            } else
                msg = new TcpDiscoveryClientReconnectMessage(getLocalNodeId(), rmtNodeId, lastMsgId);
            msg.client(true);
            if (msg instanceof TraceableMessage)
                tracing.messages().beforeSend((TraceableMessage) msg);
            spi.writeToSocket(sock, msg, timeoutHelper.nextTimeoutChunk(spi.getSocketTimeout()));
            spi.stats.onMessageSent(msg, U.millisSinceNanos(tsNanos));
            if (log.isDebugEnabled())
                log.debug("Message has been sent to address [msg=" + msg + ", addr=" + addr + ", rmtNodeId=" + rmtNodeId + ']');
            return new T3<>(new SocketStream(sock), spi.readReceipt(sock, timeoutHelper.nextTimeoutChunk(ackTimeout0)), res.clientAck());
        } catch (IOException | IgniteCheckedException e) {
            U.closeQuiet(sock);
            if (log.isDebugEnabled())
                log.error("Exception on joining: " + e.getMessage(), e);
            onException("Exception on joining: " + e.getMessage(), e);
            if (errs == null)
                errs = new ArrayList<>();
            errs.add(e);
            if (X.hasCause(e, SSLException.class)) {
                if (--sslConnectAttempts == 0)
                    throw new IgniteSpiException("Unable to establish secure connection. " + "Was remote cluster configured with SSL? [rmtAddr=" + addr + ", errMsg=\"" + e.getMessage() + "\"]", e);
                continue;
            }
            if (X.hasCause(e, StreamCorruptedException.class)) {
                // StreamCorruptedException could be caused by remote node failover
                if (connectAttempts < 2) {
                    connectAttempts++;
                    continue;
                }
                if (log.isDebugEnabled())
                    log.debug("Connect failed with StreamCorruptedException, skip address: " + addr);
                break;
            }
            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;
            }
        }
    }
    if (log.isDebugEnabled())
        log.debug("Failed to join to address [addr=" + addr + ", recon=" + recon + ", errs=" + errs + ']');
    return null;
}
Also used : SSLException(javax.net.ssl.SSLException) TcpDiscoveryClientReconnectMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryClientReconnectMessage) DiscoveryDataPacket(org.apache.ignite.spi.discovery.tcp.internal.DiscoveryDataPacket) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) StreamCorruptedException(java.io.StreamCorruptedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) UUID(java.util.UUID) TcpDiscoveryJoinRequestMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage) T3(org.apache.ignite.internal.util.typedef.T3) TcpDiscoveryHandshakeRequest(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryHandshakeRequest) TcpDiscoveryHandshakeResponse(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryHandshakeResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) IgniteSpiOperationTimeoutHelper(org.apache.ignite.spi.IgniteSpiOperationTimeoutHelper) TraceableMessage(org.apache.ignite.internal.processors.tracing.messages.TraceableMessage) SocketTimeoutException(java.net.SocketTimeoutException) TcpDiscoveryAbstractMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage) Socket(java.net.Socket) TcpDiscoveryNode(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 Socket (java.net.Socket)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UUID (java.util.UUID)1 SSLException (javax.net.ssl.SSLException)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 TraceableMessage (org.apache.ignite.internal.processors.tracing.messages.TraceableMessage)1 T3 (org.apache.ignite.internal.util.typedef.T3)1 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)1 IgniteSpiOperationTimeoutHelper (org.apache.ignite.spi.IgniteSpiOperationTimeoutHelper)1 DiscoveryDataPacket (org.apache.ignite.spi.discovery.tcp.internal.DiscoveryDataPacket)1 TcpDiscoveryNode (org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode)1 TcpDiscoveryAbstractMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage)1 TcpDiscoveryClientReconnectMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryClientReconnectMessage)1 TcpDiscoveryHandshakeRequest (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryHandshakeRequest)1 TcpDiscoveryHandshakeResponse (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryHandshakeResponse)1 TcpDiscoveryJoinRequestMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage)1 Nullable (org.jetbrains.annotations.Nullable)1