Search in sources :

Example 1 with ReconnectionRequestMessage

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

the class TestNodeClusterCoordinator method testUnknownNodeAskedToConnectOnAttemptedConnectionComplete.

@Test(timeout = 5000)
public void testUnknownNodeAskedToConnectOnAttemptedConnectionComplete() throws IOException, InterruptedException {
    final ClusterCoordinationProtocolSenderListener senderListener = Mockito.mock(ClusterCoordinationProtocolSenderListener.class);
    final AtomicReference<ReconnectionRequestMessage> requestRef = new AtomicReference<>();
    Mockito.when(senderListener.requestReconnection(Mockito.any(ReconnectionRequestMessage.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            final ReconnectionRequestMessage msg = invocation.getArgumentAt(0, ReconnectionRequestMessage.class);
            requestRef.set(msg);
            return null;
        }
    });
    final EventReporter eventReporter = Mockito.mock(EventReporter.class);
    final RevisionManager revisionManager = Mockito.mock(RevisionManager.class);
    Mockito.when(revisionManager.getAllRevisions()).thenReturn(Collections.emptyList());
    final NodeClusterCoordinator coordinator = new NodeClusterCoordinator(senderListener, eventReporter, null, new FirstVoteWinsFlowElection(), null, revisionManager, createProperties(), null) {

        @Override
        void notifyOthersOfNodeStatusChange(NodeConnectionStatus updatedStatus, boolean notifyAllNodes, boolean waitForCoordinator) {
        }
    };
    final FlowService flowService = Mockito.mock(FlowService.class);
    final StandardDataFlow dataFlow = new StandardDataFlow(new byte[50], new byte[50], new byte[50], new HashSet<>());
    Mockito.when(flowService.createDataFlowFromController()).thenReturn(dataFlow);
    coordinator.setFlowService(flowService);
    coordinator.setConnected(true);
    final NodeIdentifier nodeId = createNodeId(1);
    coordinator.finishNodeConnection(nodeId);
    while (requestRef.get() == null) {
        Thread.sleep(10L);
    }
    final ReconnectionRequestMessage msg = requestRef.get();
    assertEquals(nodeId, msg.getNodeId());
    final StandardDataFlow df = msg.getDataFlow();
    assertNotNull(df);
    assertTrue(Arrays.equals(dataFlow.getFlow(), df.getFlow()));
    assertTrue(Arrays.equals(dataFlow.getSnippets(), df.getSnippets()));
}
Also used : RevisionManager(org.apache.nifi.web.revision.RevisionManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ReconnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ReconnectionRequestMessage) ClusterCoordinationProtocolSenderListener(org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener) EventReporter(org.apache.nifi.events.EventReporter) FlowService(org.apache.nifi.services.FlowService) Test(org.junit.Test)

Example 2 with ReconnectionRequestMessage

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

the class NodeClusterCoordinator method requestNodeConnect.

@Override
public void requestNodeConnect(final NodeIdentifier nodeId, final String userDn) {
    if (requireElection && !flowElection.isElectionComplete() && flowElection.isVoteCounted(nodeId)) {
        // If we receive a heartbeat from a node that we already know, we don't want to request that it reconnect
        // to the cluster because no flow has yet been elected. However, if the node has not yet voted, we want to send
        // a reconnect request because we want this node to cast its vote for the flow, and this happens on connection
        logger.debug("Received heartbeat for {} and node is not connected. Will not request node connect to cluster, " + "though, because the Flow Election is still in progress", nodeId);
        return;
    }
    if (userDn == null) {
        reportEvent(nodeId, Severity.INFO, "Requesting that node connect to cluster");
    } else {
        reportEvent(nodeId, Severity.INFO, "Requesting that node connect to cluster on behalf of " + userDn);
    }
    updateNodeStatus(new NodeConnectionStatus(nodeId, NodeConnectionState.CONNECTING, null, null, System.currentTimeMillis()));
    // create the request
    final ReconnectionRequestMessage request = new ReconnectionRequestMessage();
    request.setNodeId(nodeId);
    request.setInstanceId(instanceId);
    // If we still are requiring that an election take place, we do not want to include our local dataflow, because we don't
    // yet know what the cluster's dataflow looks like. However, if we don't require election, then we've connected to the
    // cluster, which means that our flow is correct.
    final boolean includeDataFlow = !requireElection;
    requestReconnectionAsynchronously(request, 10, 5, includeDataFlow);
}
Also used : ReconnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ReconnectionRequestMessage)

Aggregations

ReconnectionRequestMessage (org.apache.nifi.cluster.protocol.message.ReconnectionRequestMessage)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)1 StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)1 ClusterCoordinationProtocolSenderListener (org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener)1 EventReporter (org.apache.nifi.events.EventReporter)1 FlowService (org.apache.nifi.services.FlowService)1 RevisionManager (org.apache.nifi.web.revision.RevisionManager)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1