Search in sources :

Example 1 with NodeInformation

use of org.apache.nifi.remote.cluster.NodeInformation in project nifi by apache.

the class SocketFlowFileServerProtocol method sendPeerList.

@Override
public void sendPeerList(final Peer peer, final Optional<ClusterNodeInformation> clusterNodeInfo, final NodeInformation self) throws IOException {
    if (!handshakeCompleted) {
        throw new IllegalStateException("Handshake has not been completed");
    }
    if (shutdown) {
        throw new IllegalStateException("Protocol is shutdown");
    }
    logger.debug("{} Sending Peer List to {}", this, peer);
    final CommunicationsSession commsSession = peer.getCommunicationsSession();
    final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream());
    logger.debug("{} Advertising Remote Input host name {}", this, peer);
    List<NodeInformation> nodeInfos;
    if (clusterNodeInfo.isPresent()) {
        nodeInfos = new ArrayList<>(clusterNodeInfo.get().getNodeInformation());
    } else {
        nodeInfos = Collections.singletonList(self);
    }
    // determine how many nodes have Site-to-site enabled
    int numPeers = 0;
    for (final NodeInformation nodeInfo : nodeInfos) {
        if (nodeInfo.getSiteToSitePort() != null) {
            numPeers++;
        }
    }
    dos.writeInt(numPeers);
    for (final NodeInformation nodeInfo : nodeInfos) {
        if (nodeInfo.getSiteToSitePort() == null) {
            continue;
        }
        dos.writeUTF(nodeInfo.getSiteToSiteHostname());
        dos.writeInt(nodeInfo.getSiteToSitePort());
        dos.writeBoolean(nodeInfo.isSiteToSiteSecure());
        dos.writeInt(nodeInfo.getTotalFlowFiles());
    }
    logger.info("Sending list of {} peers back to client {}", numPeers, peer);
    dos.flush();
}
Also used : NodeInformation(org.apache.nifi.remote.cluster.NodeInformation) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) DataOutputStream(java.io.DataOutputStream) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession)

Example 2 with NodeInformation

use of org.apache.nifi.remote.cluster.NodeInformation in project nifi by apache.

the class ClusterCoordinatorNodeInformant method getNodeInformation.

@Override
public ClusterNodeInformation getNodeInformation() {
    final List<NodeInformation> nodeInfoCollection;
    try {
        nodeInfoCollection = clusterCoordinator.getClusterWorkload().entrySet().stream().map(entry -> {
            final NodeIdentifier nodeId = entry.getKey();
            final NodeInformation nodeInfo = new NodeInformation(nodeId.getSiteToSiteAddress(), nodeId.getSiteToSitePort(), nodeId.getSiteToSiteHttpApiPort(), nodeId.getApiPort(), nodeId.isSiteToSiteSecure(), entry.getValue().getFlowFileCount());
            return nodeInfo;
        }).collect(Collectors.toList());
    } catch (IOException e) {
        throw new RuntimeException("Failed to retrieve cluster workload due to " + e, e);
    }
    final ClusterNodeInformation nodeInfo = new ClusterNodeInformation();
    nodeInfo.setNodeInformation(nodeInfoCollection);
    return nodeInfo;
}
Also used : NodeInformation(org.apache.nifi.remote.cluster.NodeInformation) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) IOException(java.io.IOException)

Example 3 with NodeInformation

use of org.apache.nifi.remote.cluster.NodeInformation in project nifi by apache.

the class SocketRemoteSiteListener method handleRequest.

private void handleRequest(final ServerProtocol protocol, final Peer peer, final RequestType requestType) throws IOException, NotAuthorizedException, BadRequestException, RequestExpiredException {
    LOG.debug("Request type from {} is {}", protocol, requestType);
    switch(requestType) {
        case NEGOTIATE_FLOWFILE_CODEC:
            protocol.negotiateCodec(peer);
            break;
        case RECEIVE_FLOWFILES:
            // peer wants to receive FlowFiles, so we will transfer FlowFiles.
            protocol.getPort().transferFlowFiles(peer, protocol);
            break;
        case SEND_FLOWFILES:
            // Peer wants to send FlowFiles, so we will receive.
            protocol.getPort().receiveFlowFiles(peer, protocol);
            break;
        case REQUEST_PEER_LIST:
            final Optional<ClusterNodeInformation> nodeInfo = (nodeInformant == null) ? Optional.empty() : Optional.of(nodeInformant.getNodeInformation());
            String remoteInputHostVal = nifiProperties.getRemoteInputHost();
            if (remoteInputHostVal == null) {
                remoteInputHostVal = InetAddress.getLocalHost().getHostName();
            }
            final Boolean isSiteToSiteSecure = nifiProperties.isSiteToSiteSecure();
            final Integer apiPort = isSiteToSiteSecure ? nifiProperties.getSslPort() : nifiProperties.getPort();
            final NodeInformation self = new NodeInformation(remoteInputHostVal, nifiProperties.getRemoteInputPort(), nifiProperties.getRemoteInputHttpPort(), // Avoid potential NullPointerException.
            apiPort != null ? apiPort : 0, isSiteToSiteSecure, // TotalFlowFiles doesn't matter if it's a standalone NiFi.
            0);
            protocol.sendPeerList(peer, nodeInfo, self);
            break;
        case SHUTDOWN:
            protocol.shutdown(peer);
            break;
    }
}
Also used : NodeInformation(org.apache.nifi.remote.cluster.NodeInformation) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 4 with NodeInformation

use of org.apache.nifi.remote.cluster.NodeInformation in project nifi by apache.

the class TestSocketRemoteSiteListener method testRequestPeerList.

@Test
public void testRequestPeerList() throws Exception {
    Method method = SocketRemoteSiteListener.class.getDeclaredMethod("handleRequest", ServerProtocol.class, Peer.class, RequestType.class);
    method.setAccessible(true);
    final NiFiProperties nifiProperties = spy(NiFiProperties.class);
    final int apiPort = 8080;
    final int remoteSocketPort = 8081;
    final String remoteInputHost = "node1.example.com";
    when(nifiProperties.getPort()).thenReturn(apiPort);
    when(nifiProperties.getRemoteInputHost()).thenReturn(remoteInputHost);
    when(nifiProperties.getRemoteInputPort()).thenReturn(remoteSocketPort);
    // Even if HTTP transport is disabled, RAW should work.
    when(nifiProperties.getRemoteInputHttpPort()).thenReturn(null);
    when(nifiProperties.isSiteToSiteHttpEnabled()).thenReturn(false);
    when(nifiProperties.isSiteToSiteSecure()).thenReturn(false);
    final SocketRemoteSiteListener listener = new SocketRemoteSiteListener(remoteSocketPort, null, nifiProperties);
    final ServerProtocol serverProtocol = mock(ServerProtocol.class);
    doAnswer(invocation -> {
        final NodeInformation self = invocation.getArgumentAt(2, NodeInformation.class);
        // Listener should inform about itself properly:
        assertEquals(remoteInputHost, self.getSiteToSiteHostname());
        assertEquals(remoteSocketPort, self.getSiteToSitePort().intValue());
        assertNull(self.getSiteToSiteHttpApiPort());
        assertEquals(apiPort, self.getAPIPort());
        return null;
    }).when(serverProtocol).sendPeerList(any(Peer.class), any(Optional.class), any(NodeInformation.class));
    final Peer peer = null;
    method.invoke(listener, serverProtocol, peer, RequestType.REQUEST_PEER_LIST);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) NodeInformation(org.apache.nifi.remote.cluster.NodeInformation) Optional(java.util.Optional) Method(java.lang.reflect.Method) ServerProtocol(org.apache.nifi.remote.protocol.ServerProtocol) Test(org.junit.Test)

Example 5 with NodeInformation

use of org.apache.nifi.remote.cluster.NodeInformation in project nifi by apache.

the class TestSocketFlowFileServerProtocol method testSendPeerListCluster.

@Test
public void testSendPeerListCluster() throws Exception {
    final SocketFlowFileServerProtocol protocol = getDefaultSocketFlowFileServerProtocol();
    final List<NodeInformation> nodeInfoList = new ArrayList<>();
    final ClusterNodeInformation clusterNodeInformation = new ClusterNodeInformation();
    clusterNodeInformation.setNodeInformation(nodeInfoList);
    final Optional<ClusterNodeInformation> clusterNodeInfo = Optional.of(clusterNodeInformation);
    for (int i = 0; i < 3; i++) {
        final String siteToSiteHostname = String.format("node%d.example.com", i);
        final Integer siteToSitePort = 8081;
        final Integer siteToSiteHttpPort = null;
        final int apiPort = 8080;
        final boolean isSiteToSiteSecure = true;
        final int numOfQueuedFlowFiles = 100 + i;
        final NodeInformation nodeInformation = new NodeInformation(siteToSiteHostname, siteToSitePort, siteToSiteHttpPort, apiPort, isSiteToSiteSecure, numOfQueuedFlowFiles);
        nodeInfoList.add(nodeInformation);
    }
    final NodeInformation self = nodeInfoList.get(0);
    final HandshakeProperties handshakeProperties = new HandshakeProperties();
    handshakeProperties.setCommsIdentifier("communication-identifier");
    handshakeProperties.setTransitUriPrefix("uri-prefix");
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final Peer peer = getDefaultPeer(handshakeProperties, outputStream);
    protocol.handshake(peer);
    protocol.sendPeerList(peer, clusterNodeInfo, self);
    try (final DataInputStream dis = new DataInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
        final Response handshakeResponse = Response.read(dis);
        assertEquals(ResponseCode.PROPERTIES_OK, handshakeResponse.getCode());
        final int numPeers = dis.readInt();
        assertEquals(nodeInfoList.size(), numPeers);
        for (int i = 0; i < nodeInfoList.size(); i++) {
            final NodeInformation node = nodeInfoList.get(i);
            assertEquals(node.getSiteToSiteHostname(), dis.readUTF());
            assertEquals(node.getSiteToSitePort().intValue(), dis.readInt());
            assertEquals(node.isSiteToSiteSecure(), dis.readBoolean());
            assertEquals(node.getTotalFlowFiles(), dis.readInt());
        }
    }
}
Also used : NodeInformation(org.apache.nifi.remote.cluster.NodeInformation) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) Peer(org.apache.nifi.remote.Peer) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Response(org.apache.nifi.remote.protocol.Response) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) ByteArrayInputStream(java.io.ByteArrayInputStream) HandshakeProperties(org.apache.nifi.remote.protocol.HandshakeProperties) Test(org.junit.Test)

Aggregations

NodeInformation (org.apache.nifi.remote.cluster.NodeInformation)6 ClusterNodeInformation (org.apache.nifi.remote.cluster.ClusterNodeInformation)5 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 Peer (org.apache.nifi.remote.Peer)2 HandshakeProperties (org.apache.nifi.remote.protocol.HandshakeProperties)2 Response (org.apache.nifi.remote.protocol.Response)2 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)1 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)1 ServerProtocol (org.apache.nifi.remote.protocol.ServerProtocol)1 NiFiProperties (org.apache.nifi.util.NiFiProperties)1