use of org.apache.nifi.cluster.protocol.message.ProtocolMessage in project nifi by apache.
the class StandardClusterCoordinationProtocolSender method requestReconnection.
/**
* Requests a node to reconnect to the cluster. The configured value for
* handshake timeout is applied to the socket before making the request.
*
* @param msg a message
* @return the response
* @throws ProtocolException if the message failed to be sent or the
* response was malformed
*/
@Override
public ReconnectionResponseMessage requestReconnection(final ReconnectionRequestMessage msg) throws ProtocolException {
Socket socket = null;
try {
socket = createSocket(msg.getNodeId(), true);
// marshal message to output stream
try {
final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
marshaller.marshal(msg, socket.getOutputStream());
} catch (final IOException ioe) {
throw new ProtocolException("Failed marshalling '" + msg.getType() + "' protocol message due to: " + ioe, ioe);
}
final ProtocolMessage response;
try {
// unmarshall response and return
final ProtocolMessageUnmarshaller<ProtocolMessage> unmarshaller = protocolContext.createUnmarshaller();
response = unmarshaller.unmarshal(socket.getInputStream());
} catch (final IOException ioe) {
throw new ProtocolException("Failed unmarshalling '" + MessageType.RECONNECTION_RESPONSE + "' protocol message due to: " + ioe, ioe);
}
if (MessageType.RECONNECTION_RESPONSE == response.getType()) {
return (ReconnectionResponseMessage) response;
} else {
throw new ProtocolException("Expected message type '" + MessageType.FLOW_RESPONSE + "' but found '" + response.getType() + "'");
}
} finally {
SocketUtils.closeQuietly(socket);
}
}
use of org.apache.nifi.cluster.protocol.message.ProtocolMessage in project nifi by apache.
the class StandardClusterCoordinationProtocolSender method disconnect.
/**
* Requests a node to disconnect from the cluster. The configured value for
* handshake timeout is applied to the socket before making the request.
*
* @param msg a message
* @throws ProtocolException if the message failed to be sent
*/
@Override
public void disconnect(final DisconnectMessage msg) throws ProtocolException {
Socket socket = null;
try {
socket = createSocket(msg.getNodeId(), true);
// marshal message to output stream
try {
final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
marshaller.marshal(msg, socket.getOutputStream());
} catch (final IOException ioe) {
throw new ProtocolException("Failed marshalling '" + msg.getType() + "' protocol message due to: " + ioe, ioe);
}
} finally {
SocketUtils.closeQuietly(socket);
}
}
use of org.apache.nifi.cluster.protocol.message.ProtocolMessage in project nifi by apache.
the class StandardClusterCoordinationProtocolSender method notifyNodeStatusChange.
@Override
public void notifyNodeStatusChange(final Set<NodeIdentifier> nodesToNotify, final NodeStatusChangeMessage msg) {
if (nodesToNotify.isEmpty()) {
return;
}
final int numThreads = Math.min(nodesToNotify.size(), maxThreadsPerRequest);
final byte[] msgBytes;
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
marshaller.marshal(msg, baos);
msgBytes = baos.toByteArray();
} catch (final IOException e) {
throw new ProtocolException("Failed to marshal NodeStatusChangeMessage", e);
}
final ExecutorService executor = Executors.newFixedThreadPool(numThreads, new ThreadFactory() {
private final AtomicInteger counter = new AtomicInteger(0);
@Override
public Thread newThread(final Runnable r) {
final Thread thread = Executors.defaultThreadFactory().newThread(r);
thread.setDaemon(true);
thread.setName("Notify Cluster of Node Status Change-" + counter.incrementAndGet());
return thread;
}
});
for (final NodeIdentifier nodeId : nodesToNotify) {
executor.submit(new Runnable() {
@Override
public void run() {
try (final Socket socket = createSocket(nodeId, true)) {
// marshal message to output stream
socket.getOutputStream().write(msgBytes);
} catch (final IOException ioe) {
throw new ProtocolException("Failed to send Node Status Change message to " + nodeId, ioe);
}
logger.debug("Notified {} of status change {}", nodeId, msg);
}
});
}
executor.shutdown();
try {
executor.awaitTermination(10, TimeUnit.DAYS);
} catch (final InterruptedException ie) {
throw new ProtocolException(ie);
}
}
use of org.apache.nifi.cluster.protocol.message.ProtocolMessage in project nifi by apache.
the class SocketProtocolListenerTest method testDelayedRequest.
@Test
public void testDelayedRequest() throws Exception {
ProtocolMessage msg = new PingMessage();
DelayedProtocolHandler handler = new DelayedProtocolHandler(2000);
listener.addHandler(handler);
// marshal message to output stream
marshaller.marshal(msg, socket.getOutputStream());
try {
socket.getInputStream().read();
fail("Socket timeout not received.");
} catch (SocketTimeoutException ste) {
}
assertEquals(1, handler.getMessages().size());
assertEquals(msg.getType(), handler.getMessages().get(0).getType());
}
use of org.apache.nifi.cluster.protocol.message.ProtocolMessage in project nifi by apache.
the class SocketProtocolListenerTest method testRequest.
@Test
public void testRequest() throws Exception {
ProtocolMessage msg = new PingMessage();
ReflexiveProtocolHandler handler = new ReflexiveProtocolHandler();
listener.addHandler(handler);
// marshal message to output stream
marshaller.marshal(msg, socket.getOutputStream());
// unmarshall response and return
ProtocolMessage response = unmarshaller.unmarshal(socket.getInputStream());
assertEquals(msg.getType(), response.getType());
assertEquals(1, handler.getMessages().size());
assertEquals(msg.getType(), handler.getMessages().get(0).getType());
}
Aggregations