Search in sources :

Example 1 with ClusterWorkloadResponseMessage

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

the class NodeClusterCoordinator method getClusterWorkload.

@Override
public Map<NodeIdentifier, NodeWorkload> getClusterWorkload() throws IOException {
    final ClusterWorkloadRequestMessage request = new ClusterWorkloadRequestMessage();
    final ClusterWorkloadResponseMessage response = nodeProtocolSender.clusterWorkload(request);
    return response.getNodeWorkloads();
}
Also used : ClusterWorkloadResponseMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage) ClusterWorkloadRequestMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadRequestMessage)

Example 2 with ClusterWorkloadResponseMessage

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

the class AbstractNodeProtocolSender method clusterWorkload.

@Override
public ClusterWorkloadResponseMessage clusterWorkload(final ClusterWorkloadRequestMessage msg) throws ProtocolException {
    final InetSocketAddress serviceAddress;
    try {
        serviceAddress = getServiceAddress();
    } catch (IOException e) {
        throw new ProtocolException("Failed to getServiceAddress due to " + e, e);
    }
    final ProtocolMessage responseMessage = sendProtocolMessage(msg, serviceAddress.getHostName(), serviceAddress.getPort());
    if (MessageType.CLUSTER_WORKLOAD_RESPONSE == responseMessage.getType()) {
        return (ClusterWorkloadResponseMessage) responseMessage;
    }
    throw new ProtocolException("Expected message type '" + MessageType.CLUSTER_WORKLOAD_RESPONSE + "' but found '" + responseMessage.getType() + "'");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ClusterWorkloadResponseMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage) IOException(java.io.IOException) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage)

Example 3 with ClusterWorkloadResponseMessage

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

the class TestJaxbProtocolUtils method testRoundTripClusterWorkloadResponse.

@Test
public void testRoundTripClusterWorkloadResponse() throws JAXBException {
    final ClusterWorkloadResponseMessage msg = new ClusterWorkloadResponseMessage();
    final Map<NodeIdentifier, NodeWorkload> expectedNodeWorkloads = new HashMap<>();
    IntStream.range(1, 4).forEach(i -> {
        final String hostname = "node" + i;
        final NodeIdentifier nodeId = new NodeIdentifier(hostname, hostname, 8080, hostname, 8081, hostname, 8082, 8083, false);
        final NodeWorkload workload = new NodeWorkload();
        workload.setReportedTimestamp(System.currentTimeMillis() - 1000);
        workload.setSystemStartTime(System.currentTimeMillis());
        workload.setActiveThreadCount(i);
        workload.setFlowFileCount(i * 10);
        workload.setFlowFileBytes(i * 10 * 1024);
        expectedNodeWorkloads.put(nodeId, workload);
    });
    msg.setNodeWorkloads(expectedNodeWorkloads);
    // Marshall.
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JaxbProtocolUtils.JAXB_CONTEXT.createMarshaller().marshal(msg, baos);
    // Un-marshall.
    final Object unmarshalled = JaxbProtocolUtils.JAXB_CONTEXT.createUnmarshaller().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
    assertTrue(unmarshalled instanceof ClusterWorkloadResponseMessage);
    // Assert result.
    final ClusterWorkloadResponseMessage response = (ClusterWorkloadResponseMessage) unmarshalled;
    assertEquals(expectedNodeWorkloads.size(), response.getNodeWorkloads().size());
    response.getNodeWorkloads().entrySet().stream().forEach(entry -> {
        assertTrue(expectedNodeWorkloads.containsKey(entry.getKey()));
        final NodeWorkload w = entry.getValue();
        NodeWorkload expectedW = expectedNodeWorkloads.get(entry.getKey());
        assertEquals(expectedW.getActiveThreadCount(), w.getActiveThreadCount());
        assertEquals(expectedW.getReportedTimestamp(), w.getReportedTimestamp());
        assertEquals(expectedW.getSystemStartTime(), w.getSystemStartTime());
        assertEquals(expectedW.getFlowFileBytes(), w.getFlowFileBytes());
        assertEquals(expectedW.getFlowFileCount(), w.getFlowFileCount());
    });
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ClusterWorkloadResponseMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeWorkload(org.apache.nifi.cluster.coordination.node.NodeWorkload) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 4 with ClusterWorkloadResponseMessage

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

ClusterWorkloadResponseMessage (org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage)4 HashMap (java.util.HashMap)2 NodeWorkload (org.apache.nifi.cluster.coordination.node.NodeWorkload)2 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)2 ClusterWorkloadRequestMessage (org.apache.nifi.cluster.protocol.message.ClusterWorkloadRequestMessage)2 ProtocolMessage (org.apache.nifi.cluster.protocol.message.ProtocolMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ClusterCoordinator (org.apache.nifi.cluster.coordination.ClusterCoordinator)1