Search in sources :

Example 6 with ProtocolException

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

use of org.apache.nifi.cluster.protocol.ProtocolException 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)

Example 8 with ProtocolException

use of org.apache.nifi.cluster.protocol.ProtocolException in project nifi by apache.

the class StandardFlowService method handleFlowRequest.

private FlowResponseMessage handleFlowRequest(final FlowRequestMessage request) throws ProtocolException {
    readLock.lock();
    try {
        logger.info("Received flow request message from manager.");
        // create the response
        final FlowResponseMessage response = new FlowResponseMessage();
        response.setDataFlow(createDataFlowFromController());
        return response;
    } catch (final Exception ex) {
        throw new ProtocolException("Failed serializing flow controller state for flow request due to: " + ex, ex);
    } finally {
        readLock.unlock();
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) FlowResponseMessage(org.apache.nifi.cluster.protocol.message.FlowResponseMessage) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) ConnectionException(org.apache.nifi.cluster.ConnectionException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) IOException(java.io.IOException) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException)

Example 9 with ProtocolException

use of org.apache.nifi.cluster.protocol.ProtocolException in project nifi by apache.

the class StandardClusterCoordinationProtocolSender method requestNodeConnectionStatus.

@Override
public NodeConnectionStatus requestNodeConnectionStatus(final String hostname, final int port) {
    Objects.requireNonNull(hostname);
    final NodeConnectionStatusRequestMessage msg = new NodeConnectionStatusRequestMessage();
    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 NodeIdentifierRequestMessage", e);
    }
    try (final Socket socket = createSocket(hostname, port, true)) {
        // marshal message to output stream
        socket.getOutputStream().write(msgBytes);
        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.NODE_CONNECTION_STATUS_RESPONSE == response.getType()) {
            return ((NodeConnectionStatusResponseMessage) response).getNodeConnectionStatus();
        } else {
            throw new ProtocolException("Expected message type '" + MessageType.NODE_CONNECTION_STATUS_RESPONSE + "' but found '" + response.getType() + "'");
        }
    } catch (final IOException ioe) {
        throw new ProtocolException("Failed to request Node Identifer from " + hostname + ":" + port, ioe);
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NodeConnectionStatusResponseMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NodeConnectionStatusRequestMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusRequestMessage) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) Socket(java.net.Socket)

Example 10 with ProtocolException

use of org.apache.nifi.cluster.protocol.ProtocolException in project nifi by apache.

the class DelayedProtocolHandler method handle.

@Override
public ProtocolMessage handle(ProtocolMessage msg) throws ProtocolException {
    try {
        messages.add(msg);
        Thread.sleep(delay);
        return null;
    } catch (final InterruptedException ie) {
        throw new ProtocolException(ie);
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException)

Aggregations

ProtocolException (org.apache.nifi.cluster.protocol.ProtocolException)12 IOException (java.io.IOException)7 ProtocolMessage (org.apache.nifi.cluster.protocol.message.ProtocolMessage)5 Socket (java.net.Socket)4 NoClusterCoordinatorException (org.apache.nifi.cluster.exception.NoClusterCoordinatorException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 JAXBException (javax.xml.bind.JAXBException)2 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)2 ReconnectionResponseMessage (org.apache.nifi.cluster.protocol.message.ReconnectionResponseMessage)2 InputStream (java.io.InputStream)1 ExecutorService (java.util.concurrent.ExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 RetryPolicy (org.apache.curator.RetryPolicy)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1