Search in sources :

Example 11 with ProtocolMessage

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

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

Example 13 with ProtocolMessage

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

the class Node method createNodeProtocolSender.

@SuppressWarnings("unchecked")
private NodeProtocolSender createNodeProtocolSender() {
    final SocketConfiguration socketConfig = new SocketConfiguration();
    socketConfig.setSocketTimeout(3000);
    socketConfig.setReuseAddress(true);
    final ProtocolContext<ProtocolMessage> protocolContext = new JaxbProtocolContext<>(JaxbProtocolUtils.JAXB_CONTEXT);
    final NodeProtocolSender protocolSender = new LeaderElectionNodeProtocolSender(socketConfig, protocolContext, electionManager);
    return protocolSender;
}
Also used : LeaderElectionNodeProtocolSender(org.apache.nifi.cluster.coordination.node.LeaderElectionNodeProtocolSender) NodeProtocolSender(org.apache.nifi.cluster.protocol.NodeProtocolSender) LeaderElectionNodeProtocolSender(org.apache.nifi.cluster.coordination.node.LeaderElectionNodeProtocolSender) SocketConfiguration(org.apache.nifi.io.socket.SocketConfiguration) ServerSocketConfiguration(org.apache.nifi.io.socket.ServerSocketConfiguration) JaxbProtocolContext(org.apache.nifi.cluster.protocol.jaxb.JaxbProtocolContext) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage)

Example 14 with ProtocolMessage

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

the class Node method createClusterCoordinator.

@SuppressWarnings("unchecked")
private NodeClusterCoordinator createClusterCoordinator() {
    final EventReporter eventReporter = new EventReporter() {

        @Override
        public void reportEvent(Severity severity, String category, String message) {
            reportedEvents.add(new ReportedEvent(nodeId, severity, message));
        }
    };
    final ServerSocketConfiguration serverSocketConfiguration = new ServerSocketConfiguration();
    serverSocketConfiguration.setSocketTimeout(5000);
    final ProtocolContext<ProtocolMessage> protocolContext = new JaxbProtocolContext<>(JaxbProtocolUtils.JAXB_CONTEXT);
    protocolListener = new SocketProtocolListener(3, Integer.parseInt(nodeProperties.getProperty(NiFiProperties.CLUSTER_NODE_PROTOCOL_PORT)), serverSocketConfiguration, protocolContext);
    try {
        protocolListener.start();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    final ClusterCoordinationProtocolSenderListener protocolSenderListener = new ClusterCoordinationProtocolSenderListener(createCoordinatorProtocolSender(), protocolListener);
    return new NodeClusterCoordinator(protocolSenderListener, eventReporter, electionManager, flowElection, null, revisionManager, nodeProperties, protocolSender);
}
Also used : SocketProtocolListener(org.apache.nifi.cluster.protocol.impl.SocketProtocolListener) NodeClusterCoordinator(org.apache.nifi.cluster.coordination.node.NodeClusterCoordinator) Severity(org.apache.nifi.reporting.Severity) IOException(java.io.IOException) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) ReportedEvent(org.apache.nifi.cluster.ReportedEvent) ServerSocketConfiguration(org.apache.nifi.io.socket.ServerSocketConfiguration) JaxbProtocolContext(org.apache.nifi.cluster.protocol.jaxb.JaxbProtocolContext) ClusterCoordinationProtocolSenderListener(org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener) EventReporter(org.apache.nifi.events.EventReporter)

Example 15 with ProtocolMessage

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

the class AbstractNodeProtocolSender method heartbeat.

@Override
public HeartbeatResponseMessage heartbeat(final HeartbeatMessage msg, final String address) throws ProtocolException {
    final String hostname;
    final int port;
    try {
        final String[] parts = address.split(":");
        hostname = parts[0];
        port = Integer.parseInt(parts[1]);
    } catch (final Exception e) {
        throw new IllegalArgumentException("Cannot send heartbeat to address [" + address + "]. Address must be in <hostname>:<port> format");
    }
    final ProtocolMessage responseMessage = sendProtocolMessage(msg, hostname, port);
    if (MessageType.HEARTBEAT_RESPONSE == responseMessage.getType()) {
        return (HeartbeatResponseMessage) responseMessage;
    }
    throw new ProtocolException("Expected message type '" + MessageType.HEARTBEAT_RESPONSE + "' but found '" + responseMessage.getType() + "'");
}
Also used : HeartbeatResponseMessage(org.apache.nifi.cluster.protocol.message.HeartbeatResponseMessage) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) IOException(java.io.IOException)

Aggregations

ProtocolMessage (org.apache.nifi.cluster.protocol.message.ProtocolMessage)18 IOException (java.io.IOException)11 Socket (java.net.Socket)6 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)6 ProtocolException (org.apache.nifi.cluster.protocol.ProtocolException)6 Test (org.junit.Test)5 ConnectionResponseMessage (org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage)4 HashSet (java.util.HashSet)3 ConnectionRequest (org.apache.nifi.cluster.protocol.ConnectionRequest)3 StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)3 ClusterCoordinationProtocolSenderListener (org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener)3 JaxbProtocolContext (org.apache.nifi.cluster.protocol.jaxb.JaxbProtocolContext)3 ConnectionRequestMessage (org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage)3 EventReporter (org.apache.nifi.events.EventReporter)3 ServerSocketConfiguration (org.apache.nifi.io.socket.ServerSocketConfiguration)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2