Search in sources :

Example 1 with HandshakeProperties

use of org.apache.nifi.remote.protocol.HandshakeProperties in project nifi by apache.

the class StandardHttpFlowFileServerProtocol method doHandshake.

@Override
protected HandshakeProperties doHandshake(Peer peer) throws IOException, HandshakeException {
    HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    final String transactionId = commsSession.getTransactionId();
    HandshakeProperties confirmed = null;
    if (!StringUtils.isEmpty(transactionId)) {
        // If handshake is already done, use it.
        confirmed = transactionManager.getHandshakenProperties(transactionId);
    }
    if (confirmed == null) {
        // If it's not, then do handshake.
        confirmed = new HandshakeProperties();
        confirmed.setCommsIdentifier(transactionId);
        validateHandshakeRequest(confirmed, peer, commsSession.getHandshakeParams());
    }
    logger.debug("{} Done handshake, confirmed={}", this, confirmed);
    return confirmed;
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) HandshakeProperties(org.apache.nifi.remote.protocol.HandshakeProperties)

Example 2 with HandshakeProperties

use of org.apache.nifi.remote.protocol.HandshakeProperties in project nifi by apache.

the class TestHttpRemoteSiteListener method testNormalTransactionProgress.

@Test
public void testNormalTransactionProgress() {
    HttpRemoteSiteListener transactionManager = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    String transactionId = transactionManager.createTransaction();
    assertTrue("Transaction should be active.", transactionManager.isTransactionActive(transactionId));
    ProcessSession processSession = Mockito.mock(ProcessSession.class);
    FlowFileTransaction transaction = new FlowFileTransaction(processSession, null, null, 0, null, null);
    transactionManager.holdTransaction(transactionId, transaction, new HandshakeProperties());
    assertNotNull(transactionManager.getHandshakenProperties(transactionId));
    transaction = transactionManager.finalizeTransaction(transactionId);
    assertNotNull(transaction);
    assertFalse("Transaction should not be active anymore.", transactionManager.isTransactionActive(transactionId));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFileTransaction(org.apache.nifi.remote.protocol.FlowFileTransaction) HandshakeProperties(org.apache.nifi.remote.protocol.HandshakeProperties) Test(org.junit.Test)

Example 3 with HandshakeProperties

use of org.apache.nifi.remote.protocol.HandshakeProperties in project nifi by apache.

the class SocketFlowFileServerProtocol method doHandshake.

@Override
protected HandshakeProperties doHandshake(Peer peer) throws IOException, HandshakeException {
    HandshakeProperties confirmed = new HandshakeProperties();
    final CommunicationsSession commsSession = peer.getCommunicationsSession();
    final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream());
    final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream());
    confirmed.setCommsIdentifier(dis.readUTF());
    if (versionNegotiator.getVersion() >= 3) {
        String transitUriPrefix = dis.readUTF();
        if (!transitUriPrefix.endsWith("/")) {
            transitUriPrefix = transitUriPrefix + "/";
        }
        confirmed.setTransitUriPrefix(transitUriPrefix);
    }
    final Map<String, String> properties = new HashMap<>();
    final int numProperties = dis.readInt();
    for (int i = 0; i < numProperties; i++) {
        final String propertyName = dis.readUTF();
        final String propertyValue = dis.readUTF();
        properties.put(propertyName, propertyValue);
    }
    // evaluate the properties received
    boolean responseWritten = false;
    try {
        validateHandshakeRequest(confirmed, peer, properties);
    } catch (HandshakeException e) {
        ResponseCode handshakeResult = e.getResponseCode();
        if (handshakeResult.containsMessage()) {
            handshakeResult.writeResponse(dos, e.getMessage());
        } else {
            handshakeResult.writeResponse(dos);
        }
        switch(handshakeResult) {
            case UNAUTHORIZED:
            case PORT_NOT_IN_VALID_STATE:
            case PORTS_DESTINATION_FULL:
                responseWritten = true;
                break;
            default:
                throw e;
        }
    }
    // send "OK" response
    if (!responseWritten) {
        ResponseCode.PROPERTIES_OK.writeResponse(dos);
    }
    return confirmed;
}
Also used : ResponseCode(org.apache.nifi.remote.protocol.ResponseCode) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession) DataInputStream(java.io.DataInputStream) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) HandshakeProperties(org.apache.nifi.remote.protocol.HandshakeProperties)

Example 4 with HandshakeProperties

use of org.apache.nifi.remote.protocol.HandshakeProperties 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)

Example 5 with HandshakeProperties

use of org.apache.nifi.remote.protocol.HandshakeProperties in project nifi by apache.

the class TestSocketFlowFileServerProtocol method testSendPeerListStandalone.

@Test
public void testSendPeerListStandalone() throws Exception {
    final SocketFlowFileServerProtocol protocol = getDefaultSocketFlowFileServerProtocol();
    final Optional<ClusterNodeInformation> clusterNodeInfo = Optional.empty();
    final String siteToSiteHostname = "node1.example.com";
    final Integer siteToSitePort = 8081;
    final Integer siteToSiteHttpPort = null;
    final int apiPort = 8080;
    final boolean isSiteToSiteSecure = true;
    final int numOfQueuedFlowFiles = 100;
    final NodeInformation self = new NodeInformation(siteToSiteHostname, siteToSitePort, siteToSiteHttpPort, apiPort, isSiteToSiteSecure, numOfQueuedFlowFiles);
    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(1, numPeers);
        assertEquals(siteToSiteHostname, dis.readUTF());
        assertEquals(siteToSitePort.intValue(), dis.readInt());
        assertEquals(isSiteToSiteSecure, dis.readBoolean());
        assertEquals(numOfQueuedFlowFiles, dis.readInt());
    }
}
Also used : NodeInformation(org.apache.nifi.remote.cluster.NodeInformation) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) Peer(org.apache.nifi.remote.Peer) 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

HandshakeProperties (org.apache.nifi.remote.protocol.HandshakeProperties)5 DataInputStream (java.io.DataInputStream)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Peer (org.apache.nifi.remote.Peer)2 ClusterNodeInformation (org.apache.nifi.remote.cluster.ClusterNodeInformation)2 NodeInformation (org.apache.nifi.remote.cluster.NodeInformation)2 Response (org.apache.nifi.remote.protocol.Response)2 DataOutputStream (java.io.DataOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ProcessSession (org.apache.nifi.processor.ProcessSession)1 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)1 HttpServerCommunicationsSession (org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)1 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)1 FlowFileTransaction (org.apache.nifi.remote.protocol.FlowFileTransaction)1 ResponseCode (org.apache.nifi.remote.protocol.ResponseCode)1