Search in sources :

Example 1 with ProtocolException

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

the class CuratorNodeProtocolSender method getServiceAddress.

@Override
protected synchronized InetSocketAddress getServiceAddress() throws IOException {
    if (coordinatorAddress != null) {
        return coordinatorAddress;
    }
    final RetryPolicy retryPolicy = new RetryNTimes(0, 0);
    final CuratorFramework curatorClient = CuratorFrameworkFactory.newClient(zkConfig.getConnectString(), zkConfig.getSessionTimeoutMillis(), zkConfig.getConnectionTimeoutMillis(), retryPolicy);
    curatorClient.start();
    try {
        // Get coordinator address and add watcher to change who we are heartbeating to if the value changes.
        final byte[] coordinatorAddressBytes = curatorClient.getData().usingWatcher(new Watcher() {

            @Override
            public void process(final WatchedEvent event) {
                coordinatorAddress = null;
            }
        }).forPath(coordinatorPath);
        if (coordinatorAddressBytes == null || coordinatorAddressBytes.length == 0) {
            throw new NoClusterCoordinatorException("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
        }
        final String address = new String(coordinatorAddressBytes, StandardCharsets.UTF_8);
        final String[] splits = address.split(":");
        if (splits.length != 2) {
            final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but this is not in the expected format of <hostname>:<port>", address);
            logger.error(message);
            throw new ProtocolException(message);
        }
        logger.info("Determined that Cluster Coordinator is located at {}; will use this address for sending heartbeat messages", address);
        final String hostname = splits[0];
        final int port;
        try {
            port = Integer.parseInt(splits[1]);
            if (port < 1 || port > 65535) {
                throw new NumberFormatException("Port must be in the range of 1 - 65535 but got " + port);
            }
        } catch (final NumberFormatException nfe) {
            final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but the port is not a valid port number", address);
            logger.error(message);
            throw new ProtocolException(message);
        }
        final InetSocketAddress socketAddress = InetSocketAddress.createUnresolved(hostname, port);
        coordinatorAddress = socketAddress;
        return socketAddress;
    } catch (final NoNodeException nne) {
        logger.info("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
        throw new NoClusterCoordinatorException("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
    } catch (final NoClusterCoordinatorException ncce) {
        throw ncce;
    } catch (Exception e) {
        throw new IOException("Unable to determine Cluster Coordinator from ZooKeeper", e);
    } finally {
        curatorClient.close();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) InetSocketAddress(java.net.InetSocketAddress) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) IOException(java.io.IOException) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) WatchedEvent(org.apache.zookeeper.WatchedEvent) CuratorFramework(org.apache.curator.framework.CuratorFramework) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) RetryPolicy(org.apache.curator.RetryPolicy)

Example 2 with ProtocolException

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

the class LeaderElectionNodeProtocolSender method getServiceAddress.

@Override
protected InetSocketAddress getServiceAddress() throws IOException {
    final String address = electionManager.getLeader(ClusterRoles.CLUSTER_COORDINATOR);
    if (StringUtils.isEmpty(address)) {
        throw new NoClusterCoordinatorException("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
    }
    final String[] splits = address.split(":");
    if (splits.length != 2) {
        final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but this is not in the expected format of <hostname>:<port>", address);
        logger.error(message);
        throw new ProtocolException(message);
    }
    logger.info("Determined that Cluster Coordinator is located at {}; will use this address for sending heartbeat messages", address);
    final String hostname = splits[0];
    final int port;
    try {
        port = Integer.parseInt(splits[1]);
        if (port < 1 || port > 65535) {
            throw new NumberFormatException("Port must be in the range of 1 - 65535 but got " + port);
        }
    } catch (final NumberFormatException nfe) {
        final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but the port is not a valid port number", address);
        logger.error(message);
        throw new ProtocolException(message);
    }
    final InetSocketAddress socketAddress = InetSocketAddress.createUnresolved(hostname, port);
    return socketAddress;
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with ProtocolException

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

the class SocketProtocolListener method dispatchRequest.

@Override
public void dispatchRequest(final Socket socket) {
    String hostname = null;
    try {
        final StopWatch stopWatch = new StopWatch(true);
        hostname = socket.getInetAddress().getHostName();
        final String requestId = UUID.randomUUID().toString();
        logger.debug("Received request {} from {}", requestId, hostname);
        String requestorDn = getRequestorDN(socket);
        // unmarshall message
        final ProtocolMessageUnmarshaller<ProtocolMessage> unmarshaller = protocolContext.createUnmarshaller();
        final ByteCountingInputStream countingIn = new ByteCountingInputStream(socket.getInputStream());
        InputStream wrappedInStream = countingIn;
        if (logger.isDebugEnabled()) {
            // don't buffer more than 1 MB of the message
            final int maxMsgBuffer = 1024 * 1024;
            final CopyingInputStream copyingInputStream = new CopyingInputStream(wrappedInStream, maxMsgBuffer);
            wrappedInStream = copyingInputStream;
        }
        final ProtocolMessage request;
        try {
            request = unmarshaller.unmarshal(wrappedInStream);
        } finally {
            if (logger.isDebugEnabled() && wrappedInStream instanceof CopyingInputStream) {
                final CopyingInputStream copyingInputStream = (CopyingInputStream) wrappedInStream;
                byte[] receivedMessage = copyingInputStream.getBytesRead();
                logger.debug("Received message: " + new String(receivedMessage));
            }
        }
        request.setRequestorDN(requestorDn);
        // dispatch message to handler
        ProtocolHandler desiredHandler = null;
        final Collection<ProtocolHandler> handlers = getHandlers();
        for (final ProtocolHandler handler : handlers) {
            if (handler.canHandle(request)) {
                desiredHandler = handler;
                break;
            }
        }
        // if no handler found, throw exception; otherwise handle request
        if (desiredHandler == null) {
            logger.error("Received request of type {} but none of the following Protocol Handlers were able to process the request: {}", request.getType(), handlers);
            throw new ProtocolException("No handler assigned to handle message type: " + request.getType());
        } else {
            final ProtocolMessage response = desiredHandler.handle(request);
            if (response != null) {
                try {
                    logger.debug("Sending response for request {}", requestId);
                    // marshal message to output stream
                    final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
                    marshaller.marshal(response, socket.getOutputStream());
                } catch (final IOException ioe) {
                    throw new ProtocolException("Failed marshalling protocol message in response to message type: " + request.getType() + " due to " + ioe, ioe);
                }
            }
        }
        stopWatch.stop();
        final NodeIdentifier nodeId = getNodeIdentifier(request);
        final String from = nodeId == null ? hostname : nodeId.toString();
        logger.info("Finished processing request {} (type={}, length={} bytes) from {} in {}", requestId, request.getType(), countingIn.getBytesRead(), from, stopWatch.getDuration());
    } catch (final IOException | ProtocolException e) {
        logger.warn("Failed processing protocol message from " + hostname + " due to " + e, e);
        if (bulletinRepository != null) {
            final Bulletin bulletin = BulletinFactory.createBulletin("Clustering", "WARNING", String.format("Failed to process protocol message from %s due to: %s", hostname, e.toString()));
            bulletinRepository.addBulletin(bulletin);
        }
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) ByteCountingInputStream(org.apache.nifi.stream.io.ByteCountingInputStream) InputStream(java.io.InputStream) ByteCountingInputStream(org.apache.nifi.stream.io.ByteCountingInputStream) IOException(java.io.IOException) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) StopWatch(org.apache.nifi.util.StopWatch) ProtocolHandler(org.apache.nifi.cluster.protocol.ProtocolHandler) Bulletin(org.apache.nifi.reporting.Bulletin) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

Example 4 with ProtocolException

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

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

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