use of org.apache.nifi.cluster.protocol.ProtocolException 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.ProtocolException in project nifi by apache.
the class StandardFlowService method handle.
@Override
public ProtocolMessage handle(final ProtocolMessage request) throws ProtocolException {
final long startNanos = System.nanoTime();
try {
switch(request.getType()) {
case FLOW_REQUEST:
return handleFlowRequest((FlowRequestMessage) request);
case RECONNECTION_REQUEST:
{
// Suspend heartbeats until we've reconnected. Otherwise,
// we may send a heartbeat while we are still in the process of
// connecting, which will cause the Cluster Manager to mark us
// as "Connected," which becomes problematic as the FlowController's lock
// may still be held, causing this node to take a long time to respond to requests.
controller.suspendHeartbeats();
final Thread t = new Thread(new Runnable() {
@Override
public void run() {
handleReconnectionRequest((ReconnectionRequestMessage) request);
}
}, "Reconnect to Cluster");
t.setDaemon(true);
t.start();
return new ReconnectionResponseMessage();
}
case DISCONNECTION_REQUEST:
{
final Thread t = new Thread(new Runnable() {
@Override
public void run() {
handleDisconnectionRequest((DisconnectMessage) request);
}
}, "Disconnect from Cluster");
t.setDaemon(true);
t.start();
return null;
}
default:
throw new ProtocolException("Handler cannot handle message type: " + request.getType());
}
} finally {
if (logger.isDebugEnabled()) {
final long procNanos = System.nanoTime() - startNanos;
final long procMillis = TimeUnit.MILLISECONDS.convert(procNanos, TimeUnit.NANOSECONDS);
logger.debug("Finished Processing Protocol Message of type {} in {} millis", request.getType(), procMillis);
}
}
}
use of org.apache.nifi.cluster.protocol.ProtocolException in project nifi by apache.
the class StandardFlowService method handleFlowRequest.
private FlowResponseMessage handleFlowRequest(final FlowRequestMessage request) throws ProtocolException {
readLock.lock();
try {
logger.info("Received flow request message from manager.");
// create the response
final FlowResponseMessage response = new FlowResponseMessage();
response.setDataFlow(createDataFlowFromController());
return response;
} catch (final Exception ex) {
throw new ProtocolException("Failed serializing flow controller state for flow request due to: " + ex, ex);
} finally {
readLock.unlock();
}
}
use of org.apache.nifi.cluster.protocol.ProtocolException in project nifi by apache.
the class StandardClusterCoordinationProtocolSender method requestNodeConnectionStatus.
@Override
public NodeConnectionStatus requestNodeConnectionStatus(final String hostname, final int port) {
Objects.requireNonNull(hostname);
final NodeConnectionStatusRequestMessage msg = new NodeConnectionStatusRequestMessage();
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 NodeIdentifierRequestMessage", e);
}
try (final Socket socket = createSocket(hostname, port, true)) {
// marshal message to output stream
socket.getOutputStream().write(msgBytes);
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.NODE_CONNECTION_STATUS_RESPONSE == response.getType()) {
return ((NodeConnectionStatusResponseMessage) response).getNodeConnectionStatus();
} else {
throw new ProtocolException("Expected message type '" + MessageType.NODE_CONNECTION_STATUS_RESPONSE + "' but found '" + response.getType() + "'");
}
} catch (final IOException ioe) {
throw new ProtocolException("Failed to request Node Identifer from " + hostname + ":" + port, ioe);
}
}
use of org.apache.nifi.cluster.protocol.ProtocolException in project nifi by apache.
the class DelayedProtocolHandler method handle.
@Override
public ProtocolMessage handle(ProtocolMessage msg) throws ProtocolException {
try {
messages.add(msg);
Thread.sleep(delay);
return null;
} catch (final InterruptedException ie) {
throw new ProtocolException(ie);
}
}
Aggregations