Search in sources :

Example 6 with TcpDiscoveryAbstractMessage

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

the class TcpDiscoverySelfTest method testFailBeforeNodeAddedSent.

/**
     * @throws Exception If any error occurs.
     */
public void testFailBeforeNodeAddedSent() throws Exception {
    try {
        Ignite g1 = startGrid(1);
        final CountDownLatch joinCnt = new CountDownLatch(2);
        final CountDownLatch failCnt = new CountDownLatch(1);
        g1.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                if (evt.type() == EVT_NODE_JOINED)
                    joinCnt.countDown();
                else if (evt.type() == EVT_NODE_FAILED)
                    failCnt.countDown();
                else
                    assert false : "Unexpected event type: " + evt;
                return true;
            }
        }, EVT_NODE_JOINED, EVT_NODE_FAILED);
        final Ignite g = startGrid("FailBeforeNodeAddedSentSpi");
        discoMap.get(g.name()).addSendMessageListener(new IgniteInClosure<TcpDiscoveryAbstractMessage>() {

            @Override
            public void apply(TcpDiscoveryAbstractMessage msg) {
                if (msg instanceof TcpDiscoveryNodeAddedMessage) {
                    discoMap.get(g.name()).simulateNodeFailure();
                    throw new RuntimeException("Avoid message sending: " + msg.getClass());
                }
            }
        });
        startGrid(3);
        assert joinCnt.await(10, SECONDS);
        assert failCnt.await(10, SECONDS);
    } finally {
        stopAllGrids();
    }
}
Also used : TcpDiscoveryAbstractMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite) TcpDiscoveryNodeAddedMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryNodeAddedMessage) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 7 with TcpDiscoveryAbstractMessage

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

the class TcpDiscoverySelfTest method testFailBeforeNodeLeftSent.

/**
     * @throws Exception If any error occurs.
     */
public void testFailBeforeNodeLeftSent() throws Exception {
    try {
        startGrid(1);
        startGrid(2);
        final Ignite g = startGrid("FailBeforeNodeLeftSentSpi");
        discoMap.get(g.name()).addSendMessageListener(new IgniteInClosure<TcpDiscoveryAbstractMessage>() {

            @Override
            public void apply(TcpDiscoveryAbstractMessage msg) {
                if (msg instanceof TcpDiscoveryNodeLeftMessage) {
                    discoMap.get(g.name()).simulateNodeFailure();
                    throw new RuntimeException("Avoid message sending: " + msg.getClass());
                }
            }
        });
        Ignite g3 = startGrid(3);
        final CountDownLatch cnt = new CountDownLatch(1);
        g3.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                cnt.countDown();
                return true;
            }
        }, EVT_NODE_FAILED);
        stopGrid(1);
        assert cnt.await(20, SECONDS);
    } finally {
        stopAllGrids();
    }
}
Also used : TcpDiscoveryAbstractMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite) TcpDiscoveryNodeLeftMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryNodeLeftMessage) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with TcpDiscoveryAbstractMessage

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

the class CacheStartOnJoinTest method getConfiguration.

/** {@inheritDoc} */
@Override
protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(gridName);
    TcpDiscoverySpi testSpi = new TcpDiscoverySpi() {

        /** */
        private boolean delay = true;

        @Override
        protected void writeToSocket(Socket sock, OutputStream out, TcpDiscoveryAbstractMessage msg, long timeout) throws IOException, IgniteCheckedException {
            super.writeToSocket(sock, out, msg, timeout);
        }

        @Override
        protected void startMessageProcess(TcpDiscoveryAbstractMessage msg) {
            if (getTestIgniteInstanceName(0).equals(ignite.name())) {
                if (msg instanceof TcpDiscoveryJoinRequestMessage) {
                    TcpDiscoveryJoinRequestMessage msg0 = (TcpDiscoveryJoinRequestMessage) msg;
                    if (msg0.client() && delay) {
                        log.info("Delay join processing: " + msg0);
                        delay = false;
                        doSleep(5000);
                    }
                }
            }
            super.startMessageProcess(msg);
        }
    };
    testSpi.setIpFinder(ipFinder);
    testSpi.setJoinTimeout(60_000);
    cfg.setDiscoverySpi(testSpi);
    MemoryConfiguration memCfg = new MemoryConfiguration();
    memCfg.setPageSize(1024);
    memCfg.setDefaultMemoryPolicySize(50 * 1024 * 1024);
    cfg.setMemoryConfiguration(memCfg);
    cfg.setClientMode(client);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) TcpDiscoveryAbstractMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage) OutputStream(java.io.OutputStream) MemoryConfiguration(org.apache.ignite.configuration.MemoryConfiguration) Socket(java.net.Socket) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) TcpDiscoveryJoinRequestMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage)

Example 9 with TcpDiscoveryAbstractMessage

use of org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage 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);
    while (true) {
        boolean openSock = false;
        Socket sock = null;
        try {
            long tstamp = U.currentTimeMillis();
            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);
            spi.stats.onClientSocketInitialized(U.currentTimeMillis() - tstamp);
            locNode.clientRouterNodeId(rmtNodeId);
            tstamp = U.currentTimeMillis();
            TcpDiscoveryAbstractMessage msg;
            if (!recon) {
                TcpDiscoveryNode node = locNode;
                if (locNode.order() > 0) {
                    node = locNode.clientReconnectNode(spi.spiCtx.nodeAttributes());
                    marshalCredentials(node);
                }
                msg = new TcpDiscoveryJoinRequestMessage(node, spi.collectExchangeData(new DiscoveryDataPacket(getLocalNodeId())));
            } else
                msg = new TcpDiscoveryClientReconnectMessage(getLocalNodeId(), rmtNodeId, lastMsgId);
            msg.client(true);
            spi.writeToSocket(sock, msg, timeoutHelper.nextTimeoutChunk(spi.getSocketTimeout()));
            spi.stats.onMessageSent(msg, U.currentTimeMillis() - tstamp);
            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)) {
                if (--sslConnectAttempts == 0)
                    throw new IgniteSpiException("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;
            }
        }
    }
    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) IOException(java.io.IOException) IgniteSpiOperationTimeoutHelper(org.apache.ignite.spi.IgniteSpiOperationTimeoutHelper) 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)

Example 10 with TcpDiscoveryAbstractMessage

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

the class ServerImpl method sendJoinRequestMessage.

/**
     * Tries to send join request message to a random node presenting in topology.
     * Address is provided by {@link org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder} and message is
     * sent to first node connection succeeded to.
     *
     * @return {@code true} if send succeeded.
     * @throws IgniteSpiException If any error occurs.
     */
@SuppressWarnings({ "BusyWait" })
private boolean sendJoinRequestMessage() throws IgniteSpiException {
    TcpDiscoveryAbstractMessage joinReq = new TcpDiscoveryJoinRequestMessage(locNode, spi.collectExchangeData(new DiscoveryDataPacket(getLocalNodeId())));
    // Time when it has been detected, that addresses from IP finder do not respond.
    long noResStart = 0;
    while (true) {
        Collection<InetSocketAddress> addrs = spi.resolvedAddresses();
        if (F.isEmpty(addrs))
            return false;
        boolean retry = false;
        Collection<Exception> errs = new ArrayList<>();
        for (InetSocketAddress addr : addrs) {
            try {
                IgniteSpiOperationTimeoutHelper timeoutHelper = new IgniteSpiOperationTimeoutHelper(spi, true);
                Integer res;
                try {
                    SecurityUtils.serializeVersion(1);
                    res = sendMessageDirectly(joinReq, addr, timeoutHelper);
                } finally {
                    SecurityUtils.restoreDefaultSerializeVersion();
                }
                assert res != null;
                noResAddrs.remove(addr);
                // Address is responsive, reset period start.
                noResStart = 0;
                switch(res) {
                    case RES_WAIT:
                        // Concurrent startup, try sending join request again or wait if no success.
                        retry = true;
                        break;
                    case RES_OK:
                        if (log.isDebugEnabled())
                            log.debug("Join request message has been sent to address [addr=" + addr + ", req=" + joinReq + ']');
                        // Join request sending succeeded, wait for response from topology.
                        return true;
                    default:
                        // Concurrent startup, try next node.
                        if (res == RES_CONTINUE_JOIN) {
                            if (!fromAddrs.contains(addr))
                                retry = true;
                        } else {
                            if (log.isDebugEnabled())
                                log.debug("Unexpected response to join request: " + res);
                            retry = true;
                        }
                        break;
                }
            } catch (IgniteSpiException e) {
                errs.add(e);
                if (log.isDebugEnabled()) {
                    IOException ioe = X.cause(e, IOException.class);
                    log.debug("Failed to send join request message [addr=" + addr + ", msg=" + (ioe != null ? ioe.getMessage() : e.getMessage()) + ']');
                    onException("Failed to send join request message [addr=" + addr + ", msg=" + (ioe != null ? ioe.getMessage() : e.getMessage()) + ']', ioe);
                }
                noResAddrs.add(addr);
            }
        }
        if (retry) {
            if (log.isDebugEnabled())
                log.debug("Concurrent discovery SPI start has been detected (local node should wait).");
            try {
                U.sleep(2000);
            } catch (IgniteInterruptedCheckedException e) {
                throw new IgniteSpiException("Thread has been interrupted.", e);
            }
        } else if (!spi.ipFinder.isShared() && !ipFinderHasLocAddr) {
            IgniteCheckedException e = null;
            if (!errs.isEmpty()) {
                e = new IgniteCheckedException("Multiple connection attempts failed.");
                for (Exception err : errs) e.addSuppressed(err);
            }
            if (e != null && X.hasCause(e, ConnectException.class)) {
                LT.warn(log, "Failed to connect to any address from IP finder " + "(make sure IP finder addresses are correct and firewalls are disabled on all host machines): " + toOrderedList(addrs), true);
            }
            if (spi.joinTimeout > 0) {
                if (noResStart == 0)
                    noResStart = U.currentTimeMillis();
                else if (U.currentTimeMillis() - noResStart > spi.joinTimeout)
                    throw new IgniteSpiException("Failed to connect to any address from IP finder within join timeout " + "(make sure IP finder addresses are correct, and operating system firewalls are disabled " + "on all host machines, or consider increasing 'joinTimeout' configuration property): " + addrs, e);
            }
            try {
                U.sleep(2000);
            } catch (IgniteInterruptedCheckedException ex) {
                throw new IgniteSpiException("Thread has been interrupted.", ex);
            }
        } else
            break;
    }
    return false;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StreamCorruptedException(java.io.StreamCorruptedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SSLException(javax.net.ssl.SSLException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ObjectStreamException(java.io.ObjectStreamException) IgniteException(org.apache.ignite.IgniteException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) NoSuchElementException(java.util.NoSuchElementException) IgniteSystemProperties.getInteger(org.apache.ignite.IgniteSystemProperties.getInteger) IgniteSpiOperationTimeoutHelper(org.apache.ignite.spi.IgniteSpiOperationTimeoutHelper) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) DiscoveryDataPacket(org.apache.ignite.spi.discovery.tcp.internal.DiscoveryDataPacket) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TcpDiscoveryAbstractMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) TcpDiscoveryJoinRequestMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage)

Aggregations

TcpDiscoveryAbstractMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage)10 TcpDiscoveryJoinRequestMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IOException (java.io.IOException)3 Socket (java.net.Socket)3 Ignite (org.apache.ignite.Ignite)3 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)3 TcpDiscoveryNode (org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode)3 StreamCorruptedException (java.io.StreamCorruptedException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 ArrayList (java.util.ArrayList)2 SSLException (javax.net.ssl.SSLException)2 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)2 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 Event (org.apache.ignite.events.Event)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