Search in sources :

Example 6 with ProtocolMessage

use of org.apache.nifi.cluster.protocol.message.ProtocolMessage 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 7 with ProtocolMessage

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

the class StandardClusterCoordinationProtocolSender method disconnect.

/**
 * Requests a node to disconnect from the cluster. The configured value for
 * handshake timeout is applied to the socket before making the request.
 *
 * @param msg a message
 * @throws ProtocolException if the message failed to be sent
 */
@Override
public void disconnect(final DisconnectMessage 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);
        }
    } finally {
        SocketUtils.closeQuietly(socket);
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) IOException(java.io.IOException) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) Socket(java.net.Socket)

Example 8 with ProtocolMessage

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

the class StandardClusterCoordinationProtocolSender method notifyNodeStatusChange.

@Override
public void notifyNodeStatusChange(final Set<NodeIdentifier> nodesToNotify, final NodeStatusChangeMessage msg) {
    if (nodesToNotify.isEmpty()) {
        return;
    }
    final int numThreads = Math.min(nodesToNotify.size(), maxThreadsPerRequest);
    final byte[] msgBytes;
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
        marshaller.marshal(msg, baos);
        msgBytes = baos.toByteArray();
    } catch (final IOException e) {
        throw new ProtocolException("Failed to marshal NodeStatusChangeMessage", e);
    }
    final ExecutorService executor = Executors.newFixedThreadPool(numThreads, new ThreadFactory() {

        private final AtomicInteger counter = new AtomicInteger(0);

        @Override
        public Thread newThread(final Runnable r) {
            final Thread thread = Executors.defaultThreadFactory().newThread(r);
            thread.setDaemon(true);
            thread.setName("Notify Cluster of Node Status Change-" + counter.incrementAndGet());
            return thread;
        }
    });
    for (final NodeIdentifier nodeId : nodesToNotify) {
        executor.submit(new Runnable() {

            @Override
            public void run() {
                try (final Socket socket = createSocket(nodeId, true)) {
                    // marshal message to output stream
                    socket.getOutputStream().write(msgBytes);
                } catch (final IOException ioe) {
                    throw new ProtocolException("Failed to send Node Status Change message to " + nodeId, ioe);
                }
                logger.debug("Notified {} of status change {}", nodeId, msg);
            }
        });
    }
    executor.shutdown();
    try {
        executor.awaitTermination(10, TimeUnit.DAYS);
    } catch (final InterruptedException ie) {
        throw new ProtocolException(ie);
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) ThreadFactory(java.util.concurrent.ThreadFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ExecutorService(java.util.concurrent.ExecutorService) Socket(java.net.Socket)

Example 9 with ProtocolMessage

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

the class SocketProtocolListenerTest method testDelayedRequest.

@Test
public void testDelayedRequest() throws Exception {
    ProtocolMessage msg = new PingMessage();
    DelayedProtocolHandler handler = new DelayedProtocolHandler(2000);
    listener.addHandler(handler);
    // marshal message to output stream
    marshaller.marshal(msg, socket.getOutputStream());
    try {
        socket.getInputStream().read();
        fail("Socket timeout not received.");
    } catch (SocketTimeoutException ste) {
    }
    assertEquals(1, handler.getMessages().size());
    assertEquals(msg.getType(), handler.getMessages().get(0).getType());
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) DelayedProtocolHandler(org.apache.nifi.cluster.protocol.impl.testutils.DelayedProtocolHandler) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) PingMessage(org.apache.nifi.cluster.protocol.message.PingMessage) Test(org.junit.Test)

Example 10 with ProtocolMessage

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

the class SocketProtocolListenerTest method testRequest.

@Test
public void testRequest() throws Exception {
    ProtocolMessage msg = new PingMessage();
    ReflexiveProtocolHandler handler = new ReflexiveProtocolHandler();
    listener.addHandler(handler);
    // marshal message to output stream
    marshaller.marshal(msg, socket.getOutputStream());
    // unmarshall response and return
    ProtocolMessage response = unmarshaller.unmarshal(socket.getInputStream());
    assertEquals(msg.getType(), response.getType());
    assertEquals(1, handler.getMessages().size());
    assertEquals(msg.getType(), handler.getMessages().get(0).getType());
}
Also used : ReflexiveProtocolHandler(org.apache.nifi.cluster.protocol.impl.testutils.ReflexiveProtocolHandler) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) PingMessage(org.apache.nifi.cluster.protocol.message.PingMessage) Test(org.junit.Test)

Aggregations

ProtocolMessage (org.apache.nifi.cluster.protocol.message.ProtocolMessage)18 IOException (java.io.IOException)11 Socket (java.net.Socket)6 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)6 ProtocolException (org.apache.nifi.cluster.protocol.ProtocolException)6 Test (org.junit.Test)5 ConnectionResponseMessage (org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage)4 HashSet (java.util.HashSet)3 ConnectionRequest (org.apache.nifi.cluster.protocol.ConnectionRequest)3 StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)3 ClusterCoordinationProtocolSenderListener (org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener)3 JaxbProtocolContext (org.apache.nifi.cluster.protocol.jaxb.JaxbProtocolContext)3 ConnectionRequestMessage (org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage)3 EventReporter (org.apache.nifi.events.EventReporter)3 ServerSocketConfiguration (org.apache.nifi.io.socket.ServerSocketConfiguration)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2