Search in sources :

Example 6 with NodeConnectionStatus

use of org.apache.nifi.cluster.coordination.node.NodeConnectionStatus in project nifi by apache.

the class TestJaxbProtocolUtils method testRoundTripConnectionStatusResponse.

@Test
public void testRoundTripConnectionStatusResponse() throws JAXBException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final NodeConnectionStatusResponseMessage msg = new NodeConnectionStatusResponseMessage();
    final NodeIdentifier nodeId = new NodeIdentifier("id", "localhost", 8000, "localhost", 8001, "localhost", 8002, 8003, true);
    final NodeConnectionStatus nodeStatus = new NodeConnectionStatus(nodeId, DisconnectionCode.NOT_YET_CONNECTED);
    msg.setNodeConnectionStatus(nodeStatus);
    JaxbProtocolUtils.JAXB_CONTEXT.createMarshaller().marshal(msg, baos);
    final Object unmarshalled = JaxbProtocolUtils.JAXB_CONTEXT.createUnmarshaller().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
    assertTrue(unmarshalled instanceof NodeConnectionStatusResponseMessage);
    final NodeConnectionStatusResponseMessage unmarshalledMsg = (NodeConnectionStatusResponseMessage) unmarshalled;
    final NodeConnectionStatus unmarshalledStatus = unmarshalledMsg.getNodeConnectionStatus();
    assertEquals(nodeStatus, unmarshalledStatus);
}
Also used : NodeConnectionStatusResponseMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) Test(org.junit.Test)

Example 7 with NodeConnectionStatus

use of org.apache.nifi.cluster.coordination.node.NodeConnectionStatus in project nifi by apache.

the class AbstractHeartbeatMonitor method processHeartbeat.

private void processHeartbeat(final NodeHeartbeat heartbeat) {
    final NodeIdentifier nodeId = heartbeat.getNodeIdentifier();
    // Do not process heartbeat if it's blocked by firewall.
    if (clusterCoordinator.isBlockedByFirewall(nodeId.getSocketAddress())) {
        clusterCoordinator.reportEvent(nodeId, Severity.WARNING, "Firewall blocked received heartbeat. Issuing disconnection request.");
        // request node to disconnect
        clusterCoordinator.requestNodeDisconnect(nodeId, DisconnectionCode.BLOCKED_BY_FIREWALL, "Blocked by Firewall");
        removeHeartbeat(nodeId);
        return;
    }
    final NodeConnectionStatus connectionStatus = clusterCoordinator.getConnectionStatus(nodeId);
    if (connectionStatus == null) {
        // Unknown node. Issue reconnect request
        clusterCoordinator.reportEvent(nodeId, Severity.INFO, "Received heartbeat from unknown node. Removing heartbeat and requesting that node connect to cluster.");
        removeHeartbeat(nodeId);
        clusterCoordinator.requestNodeConnect(nodeId, null);
        return;
    }
    final NodeConnectionState connectionState = connectionStatus.getState();
    if (heartbeat.getConnectionStatus().getState() != NodeConnectionState.CONNECTED && connectionState == NodeConnectionState.CONNECTED) {
        // Cluster Coordinator believes that node is connected, but node does not believe so.
        clusterCoordinator.reportEvent(nodeId, Severity.WARNING, "Received heartbeat from node that thinks it is not yet part of the cluster," + "though the Cluster Coordinator thought it was (node claimed state was " + heartbeat.getConnectionStatus().getState() + "). Marking as Disconnected and requesting that Node reconnect to cluster");
        clusterCoordinator.requestNodeConnect(nodeId, null);
        return;
    }
    if (NodeConnectionState.DISCONNECTED == connectionState) {
        // ignore heartbeats from nodes disconnected by means other than lack of heartbeat, unless it is
        // the only node. We allow it if it is the only node because if we have a one-node cluster, then
        // we cannot manually reconnect it.
        final DisconnectionCode disconnectionCode = connectionStatus.getDisconnectCode();
        // Determine whether or not the node should be allowed to be in the cluster still, depending on its reason for disconnection.
        switch(disconnectionCode) {
            case LACK_OF_HEARTBEAT:
            case UNABLE_TO_COMMUNICATE:
            case NOT_YET_CONNECTED:
            case STARTUP_FAILURE:
                {
                    clusterCoordinator.reportEvent(nodeId, Severity.INFO, "Received heartbeat from node previously " + "disconnected due to " + disconnectionCode + ". Issuing reconnection request.");
                    clusterCoordinator.requestNodeConnect(nodeId, null);
                    break;
                }
            default:
                {
                    // disconnected nodes should not heartbeat, so we need to issue a disconnection request.
                    logger.info("Ignoring received heartbeat from disconnected node " + nodeId + ".  Issuing disconnection request.");
                    clusterCoordinator.requestNodeDisconnect(nodeId, disconnectionCode, connectionStatus.getDisconnectReason());
                    removeHeartbeat(nodeId);
                    break;
                }
        }
        return;
    }
    if (NodeConnectionState.DISCONNECTING == connectionStatus.getState()) {
        // ignore spurious heartbeat
        removeHeartbeat(nodeId);
        return;
    }
    // first heartbeat causes status change from connecting to connected
    if (NodeConnectionState.CONNECTING == connectionState) {
        final Long connectionRequestTime = connectionStatus.getConnectionRequestTime();
        if (connectionRequestTime != null && heartbeat.getTimestamp() < connectionRequestTime) {
            clusterCoordinator.reportEvent(nodeId, Severity.INFO, "Received heartbeat but ignoring because it was reported before the node was last asked to reconnect.");
            removeHeartbeat(nodeId);
            return;
        }
        // connection complete
        clusterCoordinator.finishNodeConnection(nodeId);
        clusterCoordinator.reportEvent(nodeId, Severity.INFO, "Received first heartbeat from connecting node. Node connected.");
    }
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) DisconnectionCode(org.apache.nifi.cluster.coordination.node.DisconnectionCode) NodeConnectionState(org.apache.nifi.cluster.coordination.node.NodeConnectionState) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus)

Example 8 with NodeConnectionStatus

use of org.apache.nifi.cluster.coordination.node.NodeConnectionStatus 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 9 with NodeConnectionStatus

use of org.apache.nifi.cluster.coordination.node.NodeConnectionStatus in project nifi by apache.

the class StandardNiFiServiceFacade method getNode.

private NodeDTO getNode(final NodeIdentifier nodeId) {
    final NodeConnectionStatus nodeStatus = clusterCoordinator.getConnectionStatus(nodeId);
    final List<NodeEvent> events = clusterCoordinator.getNodeEvents(nodeId);
    final Set<String> roles = getRoles(nodeId);
    final NodeHeartbeat heartbeat = heartbeatMonitor.getLatestHeartbeat(nodeId);
    return dtoFactory.createNodeDTO(nodeId, nodeStatus, heartbeat, events, roles);
}
Also used : NodeEvent(org.apache.nifi.cluster.event.NodeEvent) NodeHeartbeat(org.apache.nifi.cluster.coordination.heartbeat.NodeHeartbeat) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus)

Example 10 with NodeConnectionStatus

use of org.apache.nifi.cluster.coordination.node.NodeConnectionStatus in project nifi by apache.

the class TestThreadPoolRequestReplicator method createClusterCoordinator.

private ClusterCoordinator createClusterCoordinator() {
    final ClusterCoordinator coordinator = mock(ClusterCoordinator.class);
    when(coordinator.getConnectionStatus(Mockito.any(NodeIdentifier.class))).thenAnswer(new Answer<NodeConnectionStatus>() {

        @Override
        public NodeConnectionStatus answer(InvocationOnMock invocation) throws Throwable {
            return new NodeConnectionStatus(invocation.getArgumentAt(0, NodeIdentifier.class), NodeConnectionState.CONNECTED);
        }
    });
    return coordinator;
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus)

Aggregations

NodeConnectionStatus (org.apache.nifi.cluster.coordination.node.NodeConnectionStatus)16 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)12 ClusterCoordinator (org.apache.nifi.cluster.coordination.ClusterCoordinator)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ProtocolException (org.apache.nifi.cluster.protocol.ProtocolException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 NodeConnectionState (org.apache.nifi.cluster.coordination.node.NodeConnectionState)3 DataFlow (org.apache.nifi.cluster.protocol.DataFlow)3 HeartbeatPayload (org.apache.nifi.cluster.protocol.HeartbeatPayload)3 StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)3 HeartbeatMessage (org.apache.nifi.cluster.protocol.message.HeartbeatMessage)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2