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;
}
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));
}
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;
}
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());
}
}
}
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());
}
}
Aggregations