Search in sources :

Example 1 with HeartbeatPayload

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

the class HeartbeatPayloadTest method testMarshalling.

@Test
public void testMarshalling() {
    payload.setActiveThreadCount(activeThreadCount);
    payload.setTotalFlowFileCount(totalFlowFileCount);
    HeartbeatPayload.marshal(payload, marshalledBytes);
    HeartbeatPayload newPayload = HeartbeatPayload.unmarshal(new ByteArrayInputStream(marshalledBytes.toByteArray()));
    assertEquals(activeThreadCount, newPayload.getActiveThreadCount());
    assertEquals(totalFlowFileCount, newPayload.getTotalFlowFileCount());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HeartbeatPayload(org.apache.nifi.cluster.protocol.HeartbeatPayload) Test(org.junit.Test)

Example 2 with HeartbeatPayload

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

the class ClusterProtocolHeartbeatMonitor method handleHeartbeat.

private ProtocolMessage handleHeartbeat(final HeartbeatMessage msg) {
    final HeartbeatMessage heartbeatMsg = msg;
    final Heartbeat heartbeat = heartbeatMsg.getHeartbeat();
    final NodeIdentifier nodeId = heartbeat.getNodeIdentifier();
    final NodeConnectionStatus connectionStatus = heartbeat.getConnectionStatus();
    final byte[] payloadBytes = heartbeat.getPayload();
    final HeartbeatPayload payload = HeartbeatPayload.unmarshal(payloadBytes);
    final int activeThreadCount = payload.getActiveThreadCount();
    final int flowFileCount = (int) payload.getTotalFlowFileCount();
    final long flowFileBytes = payload.getTotalFlowFileBytes();
    final long systemStartTime = payload.getSystemStartTime();
    final NodeHeartbeat nodeHeartbeat = new StandardNodeHeartbeat(nodeId, System.currentTimeMillis(), connectionStatus, flowFileCount, flowFileBytes, activeThreadCount, systemStartTime);
    heartbeatMessages.put(heartbeat.getNodeIdentifier(), nodeHeartbeat);
    logger.debug("Received new heartbeat from {}", nodeId);
    // Formulate a List of differences between our view of the cluster topology and the node's view
    // and send that back to the node so that it is in-sync with us
    List<NodeConnectionStatus> nodeStatusList = payload.getClusterStatus();
    if (nodeStatusList == null) {
        nodeStatusList = Collections.emptyList();
    }
    final List<NodeConnectionStatus> updatedStatuses = getUpdatedStatuses(nodeStatusList);
    final HeartbeatResponseMessage responseMessage = new HeartbeatResponseMessage();
    responseMessage.setUpdatedNodeStatuses(updatedStatuses);
    if (!getClusterCoordinator().isFlowElectionComplete()) {
        responseMessage.setFlowElectionMessage(getClusterCoordinator().getFlowElectionStatus());
    }
    return responseMessage;
}
Also used : HeartbeatResponseMessage(org.apache.nifi.cluster.protocol.message.HeartbeatResponseMessage) HeartbeatMessage(org.apache.nifi.cluster.protocol.message.HeartbeatMessage) Heartbeat(org.apache.nifi.cluster.protocol.Heartbeat) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) HeartbeatPayload(org.apache.nifi.cluster.protocol.HeartbeatPayload) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus)

Example 3 with HeartbeatPayload

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

the class HeartbeatPayloadTest method setup.

@Before
public void setup() {
    payload = new HeartbeatPayload();
    activeThreadCount = 15;
    totalFlowFileCount = 25;
    marshalledBytes = new ByteArrayOutputStream();
}
Also used : HeartbeatPayload(org.apache.nifi.cluster.protocol.HeartbeatPayload) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Before(org.junit.Before)

Example 4 with HeartbeatPayload

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

the class HeartbeatPayloadTest method testMarshallingWithNoInfo.

@Test
public void testMarshallingWithNoInfo() {
    HeartbeatPayload.marshal(payload, marshalledBytes);
    HeartbeatPayload newPayload = HeartbeatPayload.unmarshal(new ByteArrayInputStream(marshalledBytes.toByteArray()));
    assertEquals(0, newPayload.getActiveThreadCount());
    assertEquals(0, newPayload.getTotalFlowFileCount());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HeartbeatPayload(org.apache.nifi.cluster.protocol.HeartbeatPayload) Test(org.junit.Test)

Example 5 with HeartbeatPayload

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

the class ClusterProtocolHeartbeater method send.

@Override
public synchronized void send(final HeartbeatMessage heartbeatMessage) throws IOException {
    final long sendStart = System.nanoTime();
    final String heartbeatAddress = getHeartbeatAddress();
    final HeartbeatResponseMessage responseMessage = protocolSender.heartbeat(heartbeatMessage, heartbeatAddress);
    final byte[] payloadBytes = heartbeatMessage.getHeartbeat().getPayload();
    final HeartbeatPayload payload = HeartbeatPayload.unmarshal(payloadBytes);
    final List<NodeConnectionStatus> nodeStatusList = payload.getClusterStatus();
    final Map<NodeIdentifier, Long> updateIdMap = nodeStatusList.stream().collect(Collectors.toMap(status -> status.getNodeIdentifier(), status -> status.getUpdateIdentifier()));
    final List<NodeConnectionStatus> updatedStatuses = responseMessage.getUpdatedNodeStatuses();
    if (updatedStatuses != null) {
        for (final NodeConnectionStatus updatedStatus : updatedStatuses) {
            final NodeIdentifier nodeId = updatedStatus.getNodeIdentifier();
            final Long updateId = updateIdMap.get(nodeId);
            final boolean updated = clusterCoordinator.resetNodeStatus(updatedStatus, updateId == null ? -1L : updateId);
            if (updated) {
                logger.info("After receiving heartbeat response, updated status of {} to {}", updatedStatus.getNodeIdentifier(), updatedStatus);
            } else {
                logger.debug("After receiving heartbeat response, did not update status of {} to {} because the update is out-of-date", updatedStatus.getNodeIdentifier(), updatedStatus);
            }
        }
    }
    final long sendNanos = System.nanoTime() - sendStart;
    final long sendMillis = TimeUnit.NANOSECONDS.toMillis(sendNanos);
    final DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US);
    final String flowElectionMessage = responseMessage.getFlowElectionMessage();
    final String formattedElectionMessage = flowElectionMessage == null ? "" : "; " + flowElectionMessage;
    logger.info("Heartbeat created at {} and sent to {} at {}; send took {} millis{}", dateFormatter.format(new Date(heartbeatMessage.getHeartbeat().getCreatedTimestamp())), heartbeatAddress, dateFormatter.format(new Date()), sendMillis, formattedElectionMessage);
}
Also used : HeartbeatResponseMessage(org.apache.nifi.cluster.protocol.message.HeartbeatResponseMessage) ClusterRoles(org.apache.nifi.cluster.coordination.node.ClusterRoles) NodeProtocolSender(org.apache.nifi.cluster.protocol.NodeProtocolSender) HeartbeatResponseMessage(org.apache.nifi.cluster.protocol.message.HeartbeatResponseMessage) LeaderElectionManager(org.apache.nifi.controller.leader.election.LeaderElectionManager) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Logger(org.slf4j.Logger) Date(java.util.Date) HeartbeatPayload(org.apache.nifi.cluster.protocol.HeartbeatPayload) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) HeartbeatMessage(org.apache.nifi.cluster.protocol.message.HeartbeatMessage) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) Locale(java.util.Locale) Map(java.util.Map) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) DateFormat(java.text.DateFormat) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) Date(java.util.Date) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) HeartbeatPayload(org.apache.nifi.cluster.protocol.HeartbeatPayload) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

HeartbeatPayload (org.apache.nifi.cluster.protocol.HeartbeatPayload)8 Heartbeat (org.apache.nifi.cluster.protocol.Heartbeat)4 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)4 HeartbeatMessage (org.apache.nifi.cluster.protocol.message.HeartbeatMessage)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 NodeConnectionStatus (org.apache.nifi.cluster.coordination.node.NodeConnectionStatus)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HeartbeatResponseMessage (org.apache.nifi.cluster.protocol.message.HeartbeatResponseMessage)2 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 ClusterCoordinator (org.apache.nifi.cluster.coordination.ClusterCoordinator)1 ClusterRoles (org.apache.nifi.cluster.coordination.node.ClusterRoles)1