use of org.apache.nifi.cluster.protocol.ProtocolHandler in project nifi by apache.
the class SocketProtocolListener method dispatchRequest.
@Override
public void dispatchRequest(final Socket socket) {
String hostname = null;
try {
final StopWatch stopWatch = new StopWatch(true);
hostname = socket.getInetAddress().getHostName();
final String requestId = UUID.randomUUID().toString();
logger.debug("Received request {} from {}", requestId, hostname);
String requestorDn = getRequestorDN(socket);
// unmarshall message
final ProtocolMessageUnmarshaller<ProtocolMessage> unmarshaller = protocolContext.createUnmarshaller();
final ByteCountingInputStream countingIn = new ByteCountingInputStream(socket.getInputStream());
InputStream wrappedInStream = countingIn;
if (logger.isDebugEnabled()) {
// don't buffer more than 1 MB of the message
final int maxMsgBuffer = 1024 * 1024;
final CopyingInputStream copyingInputStream = new CopyingInputStream(wrappedInStream, maxMsgBuffer);
wrappedInStream = copyingInputStream;
}
final ProtocolMessage request;
try {
request = unmarshaller.unmarshal(wrappedInStream);
} finally {
if (logger.isDebugEnabled() && wrappedInStream instanceof CopyingInputStream) {
final CopyingInputStream copyingInputStream = (CopyingInputStream) wrappedInStream;
byte[] receivedMessage = copyingInputStream.getBytesRead();
logger.debug("Received message: " + new String(receivedMessage));
}
}
request.setRequestorDN(requestorDn);
// dispatch message to handler
ProtocolHandler desiredHandler = null;
final Collection<ProtocolHandler> handlers = getHandlers();
for (final ProtocolHandler handler : handlers) {
if (handler.canHandle(request)) {
desiredHandler = handler;
break;
}
}
// if no handler found, throw exception; otherwise handle request
if (desiredHandler == null) {
logger.error("Received request of type {} but none of the following Protocol Handlers were able to process the request: {}", request.getType(), handlers);
throw new ProtocolException("No handler assigned to handle message type: " + request.getType());
} else {
final ProtocolMessage response = desiredHandler.handle(request);
if (response != null) {
try {
logger.debug("Sending response for request {}", requestId);
// marshal message to output stream
final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
marshaller.marshal(response, socket.getOutputStream());
} catch (final IOException ioe) {
throw new ProtocolException("Failed marshalling protocol message in response to message type: " + request.getType() + " due to " + ioe, ioe);
}
}
}
stopWatch.stop();
final NodeIdentifier nodeId = getNodeIdentifier(request);
final String from = nodeId == null ? hostname : nodeId.toString();
logger.info("Finished processing request {} (type={}, length={} bytes) from {} in {}", requestId, request.getType(), countingIn.getBytesRead(), from, stopWatch.getDuration());
} catch (final IOException | ProtocolException e) {
logger.warn("Failed processing protocol message from " + hostname + " due to " + e, e);
if (bulletinRepository != null) {
final Bulletin bulletin = BulletinFactory.createBulletin("Clustering", "WARNING", String.format("Failed to process protocol message from %s due to: %s", hostname, e.toString()));
bulletinRepository.addBulletin(bulletin);
}
}
}
Aggregations