Search in sources :

Example 1 with ReconnectionResponseMessage

use of org.apache.nifi.cluster.protocol.message.ReconnectionResponseMessage in project nifi by apache.

the class StandardClusterCoordinationProtocolSender method requestReconnection.

/**
 * Requests a node to reconnect to the cluster. The configured value for
 * handshake timeout is applied to the socket before making the request.
 *
 * @param msg a message
 * @return the response
 * @throws ProtocolException if the message failed to be sent or the
 * response was malformed
 */
@Override
public ReconnectionResponseMessage requestReconnection(final ReconnectionRequestMessage msg) throws ProtocolException {
    Socket socket = null;
    try {
        socket = createSocket(msg.getNodeId(), true);
        // marshal message to output stream
        try {
            final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
            marshaller.marshal(msg, socket.getOutputStream());
        } catch (final IOException ioe) {
            throw new ProtocolException("Failed marshalling '" + msg.getType() + "' protocol message due to: " + ioe, ioe);
        }
        final ProtocolMessage response;
        try {
            // unmarshall response and return
            final ProtocolMessageUnmarshaller<ProtocolMessage> unmarshaller = protocolContext.createUnmarshaller();
            response = unmarshaller.unmarshal(socket.getInputStream());
        } catch (final IOException ioe) {
            throw new ProtocolException("Failed unmarshalling '" + MessageType.RECONNECTION_RESPONSE + "' protocol message due to: " + ioe, ioe);
        }
        if (MessageType.RECONNECTION_RESPONSE == response.getType()) {
            return (ReconnectionResponseMessage) response;
        } else {
            throw new ProtocolException("Expected message type '" + MessageType.FLOW_RESPONSE + "' but found '" + response.getType() + "'");
        }
    } finally {
        SocketUtils.closeQuietly(socket);
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) ReconnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ReconnectionResponseMessage) IOException(java.io.IOException) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) Socket(java.net.Socket)

Example 2 with ReconnectionResponseMessage

use of org.apache.nifi.cluster.protocol.message.ReconnectionResponseMessage in project nifi by apache.

the class StandardFlowService method handle.

@Override
public ProtocolMessage handle(final ProtocolMessage request) throws ProtocolException {
    final long startNanos = System.nanoTime();
    try {
        switch(request.getType()) {
            case FLOW_REQUEST:
                return handleFlowRequest((FlowRequestMessage) request);
            case RECONNECTION_REQUEST:
                {
                    // Suspend heartbeats until we've reconnected. Otherwise,
                    // we may send a heartbeat while we are still in the process of
                    // connecting, which will cause the Cluster Manager to mark us
                    // as "Connected," which becomes problematic as the FlowController's lock
                    // may still be held, causing this node to take a long time to respond to requests.
                    controller.suspendHeartbeats();
                    final Thread t = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            handleReconnectionRequest((ReconnectionRequestMessage) request);
                        }
                    }, "Reconnect to Cluster");
                    t.setDaemon(true);
                    t.start();
                    return new ReconnectionResponseMessage();
                }
            case DISCONNECTION_REQUEST:
                {
                    final Thread t = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            handleDisconnectionRequest((DisconnectMessage) request);
                        }
                    }, "Disconnect from Cluster");
                    t.setDaemon(true);
                    t.start();
                    return null;
                }
            default:
                throw new ProtocolException("Handler cannot handle message type: " + request.getType());
        }
    } finally {
        if (logger.isDebugEnabled()) {
            final long procNanos = System.nanoTime() - startNanos;
            final long procMillis = TimeUnit.MILLISECONDS.convert(procNanos, TimeUnit.NANOSECONDS);
            logger.debug("Finished Processing Protocol Message of type {} in {} millis", request.getType(), procMillis);
        }
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) ReconnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ReconnectionResponseMessage)

Aggregations

ProtocolException (org.apache.nifi.cluster.protocol.ProtocolException)2 ReconnectionResponseMessage (org.apache.nifi.cluster.protocol.message.ReconnectionResponseMessage)2 IOException (java.io.IOException)1 Socket (java.net.Socket)1 ProtocolMessage (org.apache.nifi.cluster.protocol.message.ProtocolMessage)1