Search in sources :

Example 16 with ProtocolMessage

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

the class AbstractNodeProtocolSender method requestConnection.

@Override
public ConnectionResponseMessage requestConnection(final ConnectionRequestMessage msg) throws ProtocolException, UnknownServiceAddressException {
    Socket socket = null;
    try {
        socket = createSocket();
        try {
            // marshal message to output stream
            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.CONNECTION_RESPONSE + "' protocol message from " + socket.getRemoteSocketAddress() + " due to: " + ioe, ioe);
        }
        if (MessageType.CONNECTION_RESPONSE == response.getType()) {
            final ConnectionResponseMessage connectionResponse = (ConnectionResponseMessage) response;
            return connectionResponse;
        } else {
            throw new ProtocolException("Expected message type '" + MessageType.CONNECTION_RESPONSE + "' but found '" + response.getType() + "'");
        }
    } finally {
        SocketUtils.closeQuietly(socket);
    }
}
Also used : ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) IOException(java.io.IOException) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) Socket(java.net.Socket)

Example 17 with ProtocolMessage

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

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

the class ClusterProtocolHeartbeatMonitor method handleClusterWorkload.

private ProtocolMessage handleClusterWorkload(final ClusterWorkloadRequestMessage msg) {
    final ClusterWorkloadResponseMessage response = new ClusterWorkloadResponseMessage();
    final Map<NodeIdentifier, NodeWorkload> workloads = new HashMap<>();
    getLatestHeartbeats().values().stream().filter(hb -> NodeConnectionState.CONNECTED.equals(hb.getConnectionStatus().getState())).forEach(hb -> {
        NodeWorkload wl = new NodeWorkload();
        wl.setReportedTimestamp(hb.getTimestamp());
        wl.setSystemStartTime(hb.getSystemStartTime());
        wl.setActiveThreadCount(hb.getActiveThreadCount());
        wl.setFlowFileCount(hb.getFlowFileCount());
        wl.setFlowFileBytes(hb.getFlowFileBytes());
        workloads.put(hb.getNodeIdentifier(), wl);
    });
    response.setNodeWorkloads(workloads);
    return response;
}
Also used : ClusterWorkloadResponseMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) HeartbeatPayload(org.apache.nifi.cluster.protocol.HeartbeatPayload) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Function(java.util.function.Function) HeartbeatMessage(org.apache.nifi.cluster.protocol.message.HeartbeatMessage) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) MessageType(org.apache.nifi.cluster.protocol.message.ProtocolMessage.MessageType) NodeConnectionState(org.apache.nifi.cluster.coordination.node.NodeConnectionState) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) ProtocolHandler(org.apache.nifi.cluster.protocol.ProtocolHandler) HeartbeatResponseMessage(org.apache.nifi.cluster.protocol.message.HeartbeatResponseMessage) Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Heartbeat(org.apache.nifi.cluster.protocol.Heartbeat) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NiFiProperties(org.apache.nifi.util.NiFiProperties) ClusterWorkloadRequestMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadRequestMessage) NodeWorkload(org.apache.nifi.cluster.coordination.node.NodeWorkload) ProtocolListener(org.apache.nifi.cluster.protocol.ProtocolListener) Collections(java.util.Collections) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterWorkloadResponseMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeWorkload(org.apache.nifi.cluster.coordination.node.NodeWorkload)

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