Search in sources :

Example 1 with ConnectionResponseMessage

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

the class NodeClusterCoordinator method createConnectionResponse.

private ConnectionResponseMessage createConnectionResponse(final ConnectionRequest request, final NodeIdentifier resolvedNodeIdentifier, final DataFlow clusterDataFlow) {
    if (isBlockedByFirewall(resolvedNodeIdentifier.getSocketAddress())) {
        // if the socket address is not listed in the firewall, then return a null response
        logger.info("Firewall blocked connection request from node " + resolvedNodeIdentifier);
        final ConnectionResponse response = ConnectionResponse.createBlockedByFirewallResponse();
        final ConnectionResponseMessage responseMessage = new ConnectionResponseMessage();
        responseMessage.setConnectionResponse(response);
        return responseMessage;
    }
    if (clusterDataFlow == null) {
        final ConnectionResponseMessage responseMessage = new ConnectionResponseMessage();
        responseMessage.setConnectionResponse(new ConnectionResponse(5, "The cluster dataflow is not yet available"));
        return responseMessage;
    }
    // Set node's status to 'CONNECTING'
    NodeConnectionStatus status = getConnectionStatus(resolvedNodeIdentifier);
    if (status == null) {
        addNodeEvent(resolvedNodeIdentifier, "Connection requested from new node. Setting status to connecting.");
    } else {
        addNodeEvent(resolvedNodeIdentifier, "Connection requested from existing node. Setting status to connecting.");
    }
    status = new NodeConnectionStatus(resolvedNodeIdentifier, NodeConnectionState.CONNECTING, null, null, System.currentTimeMillis());
    updateNodeStatus(status);
    final ConnectionResponse response = new ConnectionResponse(resolvedNodeIdentifier, clusterDataFlow, instanceId, getConnectionStatuses(), revisionManager.getAllRevisions().stream().map(rev -> ComponentRevision.fromRevision(rev)).collect(Collectors.toList()));
    final ConnectionResponseMessage responseMessage = new ConnectionResponseMessage();
    responseMessage.setConnectionResponse(response);
    return responseMessage;
}
Also used : ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) ConnectionResponse(org.apache.nifi.cluster.protocol.ConnectionResponse)

Example 2 with ConnectionResponseMessage

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

the class TestNodeClusterCoordinator method testTryAgainIfNoFlowServiceSet.

@Test
public void testTryAgainIfNoFlowServiceSet() {
    final ClusterCoordinationProtocolSenderListener senderListener = Mockito.mock(ClusterCoordinationProtocolSenderListener.class);
    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 NodeIdentifier requestedNodeId = createNodeId(6);
    final ConnectionRequest request = new ConnectionRequest(requestedNodeId, new StandardDataFlow(new byte[0], new byte[0], new byte[0], new HashSet<>()));
    final ConnectionRequestMessage requestMsg = new ConnectionRequestMessage();
    requestMsg.setConnectionRequest(request);
    coordinator.setConnected(true);
    final ProtocolMessage protocolResponse = coordinator.handle(requestMsg);
    assertNotNull(protocolResponse);
    assertTrue(protocolResponse instanceof ConnectionResponseMessage);
    final ConnectionResponse response = ((ConnectionResponseMessage) protocolResponse).getConnectionResponse();
    assertNotNull(response);
    assertEquals(5, response.getTryLaterSeconds());
}
Also used : ConnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage) RevisionManager(org.apache.nifi.web.revision.RevisionManager) ConnectionResponse(org.apache.nifi.cluster.protocol.ConnectionResponse) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) ConnectionRequest(org.apache.nifi.cluster.protocol.ConnectionRequest) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) ClusterCoordinationProtocolSenderListener(org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener) EventReporter(org.apache.nifi.events.EventReporter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ConnectionResponseMessage

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

the class NodeClusterCoordinator method createFlowElectionInProgressResponse.

private ConnectionResponseMessage createFlowElectionInProgressResponse() {
    final ConnectionResponseMessage responseMessage = new ConnectionResponseMessage();
    final String statusDescription = flowElection.getStatusDescription();
    responseMessage.setConnectionResponse(new ConnectionResponse(5, "Cluster is still voting on which Flow is the correct flow for the cluster. " + statusDescription));
    return responseMessage;
}
Also used : ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) ConnectionResponse(org.apache.nifi.cluster.protocol.ConnectionResponse)

Example 4 with ConnectionResponseMessage

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

the class TestNodeClusterCoordinator method testConnectionResponseIndicatesAllNodes.

@Test
public void testConnectionResponseIndicatesAllNodes() throws IOException {
    // Add a disconnected node
    coordinator.updateNodeStatus(new NodeConnectionStatus(createNodeId(1), DisconnectionCode.LACK_OF_HEARTBEAT));
    coordinator.updateNodeStatus(new NodeConnectionStatus(createNodeId(2), NodeConnectionState.DISCONNECTING));
    coordinator.updateNodeStatus(new NodeConnectionStatus(createNodeId(3), NodeConnectionState.CONNECTING));
    coordinator.updateNodeStatus(new NodeConnectionStatus(createNodeId(4), NodeConnectionState.CONNECTED));
    coordinator.updateNodeStatus(new NodeConnectionStatus(createNodeId(5), NodeConnectionState.CONNECTED));
    // Create a connection request message and send to the coordinator
    final NodeIdentifier requestedNodeId = createNodeId(6);
    final ProtocolMessage protocolResponse = requestConnection(requestedNodeId, coordinator);
    assertNotNull(protocolResponse);
    assertTrue(protocolResponse instanceof ConnectionResponseMessage);
    final ConnectionResponse response = ((ConnectionResponseMessage) protocolResponse).getConnectionResponse();
    assertNotNull(response);
    assertEquals(requestedNodeId, response.getNodeIdentifier());
    assertNull(response.getRejectionReason());
    final List<NodeConnectionStatus> statuses = response.getNodeConnectionStatuses();
    assertNotNull(statuses);
    assertEquals(6, statuses.size());
    final Map<NodeIdentifier, NodeConnectionStatus> statusMap = statuses.stream().collect(Collectors.toMap(status -> status.getNodeIdentifier(), status -> status));
    assertEquals(DisconnectionCode.LACK_OF_HEARTBEAT, statusMap.get(createNodeId(1)).getDisconnectCode());
    assertEquals(NodeConnectionState.DISCONNECTING, statusMap.get(createNodeId(2)).getState());
    assertEquals(NodeConnectionState.CONNECTING, statusMap.get(createNodeId(3)).getState());
    assertEquals(NodeConnectionState.CONNECTED, statusMap.get(createNodeId(4)).getState());
    assertEquals(NodeConnectionState.CONNECTED, statusMap.get(createNodeId(5)).getState());
    assertEquals(NodeConnectionState.CONNECTING, statusMap.get(createNodeId(6)).getState());
}
Also used : DataFlow(org.apache.nifi.cluster.protocol.DataFlow) ConnectionRequest(org.apache.nifi.cluster.protocol.ConnectionRequest) Arrays(java.util.Arrays) ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Assert.assertNotSame(org.junit.Assert.assertNotSame) HashMap(java.util.HashMap) FlowService(org.apache.nifi.services.FlowService) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IllegalNodeDisconnectionException(org.apache.nifi.cluster.manager.exception.IllegalNodeDisconnectionException) Map(java.util.Map) ReconnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ReconnectionRequestMessage) Before(org.junit.Before) ConnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) RevisionManager(org.apache.nifi.web.revision.RevisionManager) Collectors(java.util.stream.Collectors) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) NodeStatusChangeMessage(org.apache.nifi.cluster.protocol.message.NodeStatusChangeMessage) ClusterCoordinationProtocolSenderListener(org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener) Mockito(org.mockito.Mockito) List(java.util.List) FlowElection(org.apache.nifi.cluster.coordination.flow.FlowElection) Assert.assertNull(org.junit.Assert.assertNull) EventReporter(org.apache.nifi.events.EventReporter) NiFiProperties(org.apache.nifi.util.NiFiProperties) ConnectionResponse(org.apache.nifi.cluster.protocol.ConnectionResponse) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) ConnectionResponse(org.apache.nifi.cluster.protocol.ConnectionResponse) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) Test(org.junit.Test)

Example 5 with ConnectionResponseMessage

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

the class TestNodeClusterCoordinator method testProposedIdentifierResolvedIfConflict.

@Test
public void testProposedIdentifierResolvedIfConflict() {
    final NodeIdentifier id1 = new NodeIdentifier("1234", "localhost", 8000, "localhost", 9000, "localhost", 10000, 11000, false);
    final NodeIdentifier conflictingId = new NodeIdentifier("1234", "localhost", 8001, "localhost", 9000, "localhost", 10000, 11000, false);
    final ConnectionRequest connectionRequest = new ConnectionRequest(id1, new StandardDataFlow(new byte[0], new byte[0], new byte[0], new HashSet<>()));
    final ConnectionRequestMessage crm = new ConnectionRequestMessage();
    crm.setConnectionRequest(connectionRequest);
    final ProtocolMessage response = coordinator.handle(crm);
    assertNotNull(response);
    assertTrue(response instanceof ConnectionResponseMessage);
    final ConnectionResponseMessage responseMessage = (ConnectionResponseMessage) response;
    final NodeIdentifier resolvedNodeId = responseMessage.getConnectionResponse().getNodeIdentifier();
    assertEquals(id1, resolvedNodeId);
    final ConnectionRequest conRequest2 = new ConnectionRequest(conflictingId, new StandardDataFlow(new byte[0], new byte[0], new byte[0], new HashSet<>()));
    final ConnectionRequestMessage crm2 = new ConnectionRequestMessage();
    crm2.setConnectionRequest(conRequest2);
    final ProtocolMessage conflictingResponse = coordinator.handle(crm2);
    assertNotNull(conflictingResponse);
    assertTrue(conflictingResponse instanceof ConnectionResponseMessage);
    final ConnectionResponseMessage conflictingResponseMessage = (ConnectionResponseMessage) conflictingResponse;
    final NodeIdentifier conflictingNodeId = conflictingResponseMessage.getConnectionResponse().getNodeIdentifier();
    assertNotSame(id1.getId(), conflictingNodeId.getId());
    assertEquals(conflictingId.getApiAddress(), conflictingNodeId.getApiAddress());
    assertEquals(conflictingId.getApiPort(), conflictingNodeId.getApiPort());
    assertEquals(conflictingId.getSiteToSiteAddress(), conflictingNodeId.getSiteToSiteAddress());
    assertEquals(conflictingId.getSiteToSitePort(), conflictingNodeId.getSiteToSitePort());
    assertEquals(conflictingId.getSocketAddress(), conflictingNodeId.getSocketAddress());
    assertEquals(conflictingId.getSocketPort(), conflictingNodeId.getSocketPort());
}
Also used : ConnectionRequest(org.apache.nifi.cluster.protocol.ConnectionRequest) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) ConnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ConnectionResponseMessage (org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage)7 ConnectionResponse (org.apache.nifi.cluster.protocol.ConnectionResponse)5 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)4 StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)4 ProtocolMessage (org.apache.nifi.cluster.protocol.message.ProtocolMessage)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 ConnectionRequest (org.apache.nifi.cluster.protocol.ConnectionRequest)3 ConnectionRequestMessage (org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage)3 IOException (java.io.IOException)2 DataFlow (org.apache.nifi.cluster.protocol.DataFlow)2 ClusterCoordinationProtocolSenderListener (org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener)2 EventReporter (org.apache.nifi.events.EventReporter)2 RevisionManager (org.apache.nifi.web.revision.RevisionManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Socket (java.net.Socket)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1