Search in sources :

Example 1 with TcpDiscoveryPingResponse

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

the class ServerImpl method pingNode.

/**
     * Pings the node by its address to see if it's alive.
     *
     * @param addr Address of the node.
     * @param nodeId Node ID to ping. In case when client node ID is not null this node ID is an ID of the router node.
     * @param clientNodeId Client node ID.
     * @return ID of the remote node and "client exists" flag if node alive or {@code null} if the remote node has
     *         left a topology during the ping process.
     * @throws IgniteCheckedException If an error occurs.
     */
@Nullable
private IgniteBiTuple<UUID, Boolean> pingNode(InetSocketAddress addr, @Nullable UUID nodeId, @Nullable UUID clientNodeId) throws IgniteCheckedException {
    assert addr != null;
    UUID locNodeId = getLocalNodeId();
    IgniteSpiOperationTimeoutHelper timeoutHelper = new IgniteSpiOperationTimeoutHelper(spi, clientNodeId == null);
    if (F.contains(spi.locNodeAddrs, addr)) {
        if (clientNodeId == null)
            return F.t(getLocalNodeId(), false);
        ClientMessageWorker clientWorker = clientMsgWorkers.get(clientNodeId);
        if (clientWorker == null)
            return F.t(getLocalNodeId(), false);
        boolean clientPingRes;
        try {
            clientPingRes = clientWorker.ping(timeoutHelper);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IgniteInterruptedCheckedException(e);
        }
        return F.t(getLocalNodeId(), clientPingRes);
    }
    GridPingFutureAdapter<IgniteBiTuple<UUID, Boolean>> fut = new GridPingFutureAdapter<>();
    GridPingFutureAdapter<IgniteBiTuple<UUID, Boolean>> oldFut = pingMap.putIfAbsent(addr, fut);
    if (oldFut != null)
        return oldFut.get();
    else {
        Collection<Throwable> errs = null;
        try {
            Socket sock = null;
            int reconCnt = 0;
            boolean openedSock = false;
            while (true) {
                try {
                    if (addr.isUnresolved())
                        addr = new InetSocketAddress(InetAddress.getByName(addr.getHostName()), addr.getPort());
                    long tstamp = U.currentTimeMillis();
                    sock = spi.createSocket();
                    fut.sock = sock;
                    sock = spi.openSocket(sock, addr, timeoutHelper);
                    openedSock = true;
                    spi.writeToSocket(sock, new TcpDiscoveryPingRequest(locNodeId, clientNodeId), timeoutHelper.nextTimeoutChunk(spi.getSocketTimeout()));
                    TcpDiscoveryPingResponse res = spi.readMessage(sock, null, timeoutHelper.nextTimeoutChunk(spi.getAckTimeout()));
                    if (locNodeId.equals(res.creatorNodeId())) {
                        if (log.isDebugEnabled())
                            log.debug("Ping response from local node: " + res);
                        break;
                    }
                    spi.stats.onClientSocketInitialized(U.currentTimeMillis() - tstamp);
                    IgniteBiTuple<UUID, Boolean> t = F.t(res.creatorNodeId(), res.clientExists());
                    fut.onDone(t);
                    return t;
                } catch (IOException | IgniteCheckedException e) {
                    if (nodeId != null && !nodeAlive(nodeId)) {
                        if (log.isDebugEnabled())
                            log.debug("Failed to ping the node (has left or leaving topology): [nodeId=" + nodeId + ']');
                        fut.onDone((IgniteBiTuple<UUID, Boolean>) null);
                        return null;
                    }
                    if (errs == null)
                        errs = new ArrayList<>();
                    errs.add(e);
                    reconCnt++;
                    if (!openedSock && reconCnt == 2)
                        break;
                    if (timeoutHelper.checkFailureTimeoutReached(e))
                        break;
                    else if (!spi.failureDetectionTimeoutEnabled() && reconCnt == spi.getReconnectCount())
                        break;
                } finally {
                    U.closeQuiet(sock);
                }
            }
        } catch (Throwable t) {
            fut.onDone(t);
            if (t instanceof Error)
                throw t;
            throw U.cast(t);
        } finally {
            if (!fut.isDone())
                fut.onDone(U.exceptionWithSuppressed("Failed to ping node by address: " + addr, errs));
            boolean b = pingMap.remove(addr, fut);
            assert b;
        }
        return fut.get();
    }
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) InetSocketAddress(java.net.InetSocketAddress) TcpDiscoveryPingResponse(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryPingResponse) IOException(java.io.IOException) IgniteSpiOperationTimeoutHelper(org.apache.ignite.spi.IgniteSpiOperationTimeoutHelper) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UUID(java.util.UUID) TcpDiscoveryPingRequest(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryPingRequest) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 UUID (java.util.UUID)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)1 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)1 IgniteSpiOperationTimeoutHelper (org.apache.ignite.spi.IgniteSpiOperationTimeoutHelper)1 TcpDiscoveryPingRequest (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryPingRequest)1 TcpDiscoveryPingResponse (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryPingResponse)1 Nullable (org.jetbrains.annotations.Nullable)1