Search in sources :

Example 26 with TcpDiscoveryNode

use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode in project ignite by apache.

the class ClientImpl method pingNode.

/** {@inheritDoc} */
@Override
public boolean pingNode(@NotNull final UUID nodeId) {
    if (nodeId.equals(getLocalNodeId()))
        return true;
    TcpDiscoveryNode node = rmtNodes.get(nodeId);
    if (node == null || !node.visible())
        return false;
    GridFutureAdapter<Boolean> fut = pingFuts.get(nodeId);
    if (fut == null) {
        fut = new GridFutureAdapter<>();
        GridFutureAdapter<Boolean> oldFut = pingFuts.putIfAbsent(nodeId, fut);
        if (oldFut != null)
            fut = oldFut;
        else {
            State state = this.state;
            if (spi.getSpiContext().isStopping() || state == STOPPED || state == SEGMENTED) {
                if (pingFuts.remove(nodeId, fut))
                    fut.onDone(false);
                return false;
            } else if (state == DISCONNECTED) {
                if (pingFuts.remove(nodeId, fut))
                    fut.onDone(new IgniteClientDisconnectedCheckedException(null, "Failed to ping node, client node disconnected."));
            } else {
                final GridFutureAdapter<Boolean> finalFut = fut;
                timer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        if (pingFuts.remove(nodeId, finalFut)) {
                            if (ClientImpl.this.state == DISCONNECTED)
                                finalFut.onDone(new IgniteClientDisconnectedCheckedException(null, "Failed to ping node, client node disconnected."));
                            else
                                finalFut.onDone(false);
                        }
                    }
                }, spi.netTimeout);
                sockWriter.sendMessage(new TcpDiscoveryClientPingRequest(getLocalNodeId(), nodeId));
            }
        }
    }
    try {
        return fut.get();
    } catch (IgniteInterruptedCheckedException ignored) {
        return false;
    } catch (IgniteCheckedException e) {
        throw new IgniteSpiException(e);
    }
}
Also used : TcpDiscoveryClientPingRequest(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryClientPingRequest) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TimerTask(java.util.TimerTask) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) TcpDiscoveryNode(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode)

Example 27 with TcpDiscoveryNode

use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode in project ignite by apache.

the class ServerImpl method spiStop0.

/**
     * Stops SPI finally or stops SPI for restart.
     *
     * @param disconnect {@code True} if SPI is being disconnected.
     * @throws IgniteSpiException If failed.
     */
private void spiStop0(boolean disconnect) throws IgniteSpiException {
    if (log.isDebugEnabled()) {
        if (disconnect)
            log.debug("Disconnecting SPI.");
        else
            log.debug("Preparing to start local node stop procedure.");
    }
    if (disconnect) {
        synchronized (mux) {
            spiState = DISCONNECTING;
        }
    }
    if (msgWorker != null && msgWorker.isAlive() && !disconnect) {
        // Send node left message only if it is final stop.
        msgWorker.addMessage(new TcpDiscoveryNodeLeftMessage(locNode.id()));
        synchronized (mux) {
            long timeout = spi.netTimeout;
            long threshold = U.currentTimeMillis() + timeout;
            while (spiState != LEFT && timeout > 0) {
                try {
                    mux.wait(timeout);
                    timeout = threshold - U.currentTimeMillis();
                } catch (InterruptedException ignored) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
            if (spiState == LEFT) {
                if (log.isDebugEnabled())
                    log.debug("Verification for local node leave has been received from coordinator" + " (continuing stop procedure).");
            } else if (log.isInfoEnabled()) {
                log.info("No verification for local node leave has been received from coordinator" + " (will stop node anyway).");
            }
        }
    }
    U.interrupt(tcpSrvr);
    U.join(tcpSrvr, log);
    tcpSrvr = null;
    Collection<SocketReader> tmp;
    synchronized (mux) {
        tmp = U.arrayList(readers);
    }
    U.interrupt(tmp);
    U.joinThreads(tmp, log);
    U.interrupt(ipFinderCleaner);
    U.join(ipFinderCleaner, log);
    U.interrupt(msgWorker);
    U.join(msgWorker, log);
    for (ClientMessageWorker clientWorker : clientMsgWorkers.values()) {
        U.interrupt(clientWorker);
        U.join(clientWorker, log);
    }
    clientMsgWorkers.clear();
    IgniteUtils.shutdownNow(ServerImpl.class, utilityPool, log);
    U.interrupt(statsPrinter);
    U.join(statsPrinter, log);
    Collection<TcpDiscoveryNode> rmts = null;
    Collection<TcpDiscoveryNode> nodes = null;
    if (!disconnect)
        spi.printStopInfo();
    else {
        spi.getSpiContext().deregisterPorts();
        nodes = ring.visibleNodes();
        rmts = F.view(nodes, F.remoteNodes(locNode.id()));
    }
    long topVer = ring.topologyVersion();
    ring.clear();
    if (rmts != null && !rmts.isEmpty()) {
        // This is restart/disconnection and remote nodes are not empty.
        // We need to fire FAIL event for each.
        DiscoverySpiListener lsnr = spi.lsnr;
        if (lsnr != null) {
            Collection<ClusterNode> processed = new HashSet<>();
            for (TcpDiscoveryNode n : rmts) {
                assert n.visible();
                processed.add(n);
                List<ClusterNode> top = U.arrayList(nodes, F.notIn(processed));
                topVer++;
                Map<Long, Collection<ClusterNode>> hist = updateTopologyHistory(topVer, Collections.unmodifiableList(top));
                lsnr.onDiscovery(EVT_NODE_FAILED, topVer, n, top, hist, null);
            }
        }
    }
    printStatistics();
    spi.stats.clear();
    synchronized (mux) {
        // Clear stored data.
        leavingNodes.clear();
        failedNodes.clear();
        spiState = DISCONNECTED;
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) DiscoverySpiListener(org.apache.ignite.spi.discovery.DiscoverySpiListener) Collection(java.util.Collection) TcpDiscoveryNodeLeftMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryNodeLeftMessage) TcpDiscoveryNode(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode) GridBoundedLinkedHashSet(org.apache.ignite.internal.util.GridBoundedLinkedHashSet) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 28 with TcpDiscoveryNode

use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode in project ignite by apache.

the class ServerImpl method forceNextNodeFailure.

/**
     * <strong>FOR TEST ONLY!!!</strong>
     * <p>
     * Simulates situation when next node is still alive but is bypassed
     * since it has been excluded from the ring, possibly, due to short time
     * network problems.
     * <p>
     * This method is intended for test purposes only.
     */
void forceNextNodeFailure() {
    U.warn(log, "Next node will be forcibly failed (if any).");
    TcpDiscoveryNode next;
    synchronized (mux) {
        next = ring.nextNode(failedNodes.keySet());
    }
    if (next != null)
        msgWorker.addMessage(new TcpDiscoveryNodeFailedMessage(getLocalNodeId(), next.id(), next.internalOrder()));
}
Also used : TcpDiscoveryNodeFailedMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryNodeFailedMessage) TcpDiscoveryNode(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode)

Example 29 with TcpDiscoveryNode

use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode 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)

Example 30 with TcpDiscoveryNode

use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode in project ignite by apache.

the class ServerImpl method pingNode.

/** {@inheritDoc} */
@Override
public boolean pingNode(UUID nodeId) {
    assert nodeId != null;
    if (log.isDebugEnabled())
        log.debug("Pinging node: " + nodeId + "]");
    if (nodeId == getLocalNodeId())
        return true;
    TcpDiscoveryNode node = ring.node(nodeId);
    if (node == null)
        return false;
    if (!nodeAlive(nodeId))
        return false;
    boolean res = pingNode(node);
    if (!res && !node.isClient() && nodeAlive(nodeId)) {
        LT.warn(log, "Failed to ping node (status check will be initiated): " + nodeId);
        msgWorker.addMessage(new TcpDiscoveryStatusCheckMessage(locNode, node.id()));
    }
    return res;
}
Also used : TcpDiscoveryStatusCheckMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryStatusCheckMessage) TcpDiscoveryNode(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode)

Aggregations

TcpDiscoveryNode (org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode)31 ClusterNode (org.apache.ignite.cluster.ClusterNode)9 Ignite (org.apache.ignite.Ignite)8 UUID (java.util.UUID)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 IOException (java.io.IOException)6 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)5 ArrayList (java.util.ArrayList)4 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)4 TcpDiscoveryVmIpFinder (org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder)4 Nullable (org.jetbrains.annotations.Nullable)4 Socket (java.net.Socket)3 SocketTimeoutException (java.net.SocketTimeoutException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 IgniteException (org.apache.ignite.IgniteException)3 Event (org.apache.ignite.events.Event)3 TcpDiscoveryJoinRequestMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage)3 StreamCorruptedException (java.io.StreamCorruptedException)2 InetSocketAddress (java.net.InetSocketAddress)2 Collection (java.util.Collection)2