@Override public void run() { try { handleOffloadRequest((OffloadMessage) request); } catch (InterruptedException e) { throw new ProtocolException("Could not complete offload request", e); } } }, "Offload Flow Files from Node");
public static void marshal(final HeartbeatPayload payload, final OutputStream os) throws ProtocolException { try { final Marshaller marshaller = JAXB_CONTEXT.createMarshaller(); marshaller.marshal(payload, os); } catch (final JAXBException je) { throw new ProtocolException(je); } }
public static void marshal(final BulletinsPayload payload, final OutputStream os) throws ProtocolException { try { final Marshaller marshaller = JAXB_CONTEXT.createMarshaller(); marshaller.marshal(payload, os); } catch (final JAXBException je) { throw new ProtocolException(je); } }
@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); } });
public static HeartbeatPayload unmarshal(final InputStream is) throws ProtocolException { try { final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller(); final XMLStreamReader xsr = XmlUtils.createSafeReader(is); return (HeartbeatPayload) unmarshaller.unmarshal(xsr); } catch (final JAXBException | XMLStreamException e) { throw new ProtocolException(e); } }
public static BulletinsPayload unmarshal(final InputStream is) throws ProtocolException { try { final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller(); final XMLStreamReader xsr = XmlUtils.createSafeReader(is); return (BulletinsPayload) unmarshaller.unmarshal(xsr); } catch (final JAXBException | XMLStreamException e) { throw new ProtocolException(e); } }
@Override public String getHeartbeatAddress() { final String heartbeatAddress = electionManager.getLeader(ClusterRoles.CLUSTER_COORDINATOR); if (heartbeatAddress == null) { throw new ProtocolException("Cannot send heartbeat because there is no Cluster Coordinator currently elected"); } return heartbeatAddress; }
private Socket createSocket() { InetSocketAddress socketAddress = null; try { // create a socket socketAddress = getServiceAddress(); return SocketUtils.createSocket(socketAddress, socketConfiguration); } catch (final IOException ioe) { if (socketAddress == null) { throw new ProtocolException("Failed to create socket due to: " + ioe, ioe); } else { throw new ProtocolException("Failed to create socket to " + socketAddress + " due to: " + ioe, ioe); } } }
private Socket createSocket(final String host, final int port, final boolean applyHandshakeTimeout) { try { // create a socket final Socket socket = SocketUtils.createSocket(InetSocketAddress.createUnresolved(host, port), socketConfiguration); if (applyHandshakeTimeout) { setConnectionHandshakeTimeoutOnSocket(socket); } return socket; } catch (final IOException ioe) { throw new ProtocolException("Failed to create socket due to: " + ioe, ioe); } }
@Override public ClusterWorkloadResponseMessage clusterWorkload(final ClusterWorkloadRequestMessage msg) throws ProtocolException { final InetSocketAddress serviceAddress; try { serviceAddress = getServiceAddress(); } catch (IOException e) { throw new ProtocolException("Failed to getServiceAddress due to " + e, e); } final ProtocolMessage responseMessage = sendProtocolMessage(msg, serviceAddress.getHostName(), serviceAddress.getPort()); if (MessageType.CLUSTER_WORKLOAD_RESPONSE == responseMessage.getType()) { return (ClusterWorkloadResponseMessage) responseMessage; } throw new ProtocolException("Expected message type '" + MessageType.CLUSTER_WORKLOAD_RESPONSE + "' but found '" + responseMessage.getType() + "'"); }
@Override protected InetSocketAddress getServiceAddress() throws IOException { final String address = electionManager.getLeader(ClusterRoles.CLUSTER_COORDINATOR); if (StringUtils.isEmpty(address)) { throw new NoClusterCoordinatorException("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet."); } final String[] splits = address.split(":"); if (splits.length != 2) { final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but this is not in the expected format of <hostname>:<port>", address); logger.error(message); throw new ProtocolException(message); } logger.info("Determined that Cluster Coordinator is located at {}; will use this address for sending heartbeat messages", address); final String hostname = splits[0]; final int port; try { port = Integer.parseInt(splits[1]); if (port < 1 || port > 65535) { throw new NumberFormatException("Port must be in the range of 1 - 65535 but got " + port); } } catch (final NumberFormatException nfe) { final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but the port is not a valid port number", address); logger.error(message); throw new ProtocolException(message); } final InetSocketAddress socketAddress = InetSocketAddress.createUnresolved(hostname, port); return socketAddress; }
private FlowResponseMessage handleFlowRequest(final FlowRequestMessage request) throws ProtocolException { readLock.lock(); try { logger.info("Received flow request message from cluster coordinator."); // 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(); } }
@Override public HeartbeatResponseMessage heartbeat(final HeartbeatMessage msg, final String address) throws ProtocolException { final String hostname; final int port; try { final String[] parts = address.split(":"); hostname = parts[0]; port = Integer.parseInt(parts[1]); } catch (final Exception e) { throw new IllegalArgumentException("Cannot send heartbeat to address [" + address + "]. Address must be in <hostname>:<port> format"); } final ProtocolMessage responseMessage = sendProtocolMessage(msg, hostname, port); if (MessageType.HEARTBEAT_RESPONSE == responseMessage.getType()) { return (HeartbeatResponseMessage) responseMessage; } throw new ProtocolException("Expected message type '" + MessageType.HEARTBEAT_RESPONSE + "' but found '" + responseMessage.getType() + "'"); }
msgBytes = baos.toByteArray(); } catch (final IOException e) { throw new ProtocolException("Failed to marshal NodeStatusChangeMessage", e); executor.awaitTermination(10, TimeUnit.DAYS); } catch (final InterruptedException ie) { throw new ProtocolException(ie);
msgBytes = baos.toByteArray(); } catch (final IOException e) { throw new ProtocolException("Failed to marshal NodeIdentifierRequestMessage", e); response = unmarshaller.unmarshal(socket.getInputStream()); } catch (final IOException ioe) { throw new ProtocolException("Failed unmarshalling '" + MessageType.RECONNECTION_RESPONSE + "' protocol message due to: " + ioe, ioe); return ((NodeConnectionStatusResponseMessage) response).getNodeConnectionStatus(); } else { throw new ProtocolException("Expected message type '" + MessageType.NODE_CONNECTION_STATUS_RESPONSE + "' but found '" + response.getType() + "'"); throw new ProtocolException("Failed to request Node Identifer from " + hostname + ":" + port, ioe);
private ProtocolMessage sendProtocolMessage(final ProtocolMessage msg, final String hostname, final int port) { Socket socket = null; try { try { socket = SocketUtils.createSocket(new InetSocketAddress(hostname, port), socketConfiguration); } catch (IOException e) { throw new ProtocolException("Failed to send message to Cluster Coordinator due to: " + e, e); } try { // marshal message to output stream 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.CONNECTION_RESPONSE + "' protocol message from " + socket.getRemoteSocketAddress() + " due to: " + ioe, ioe); } return response; } finally { SocketUtils.closeQuietly(socket); } }
+ "that address is %s, but this is not in the expected format of <hostname>:<port>", address); logger.error(message); throw new ProtocolException(message); + "that address is %s, but the port is not a valid port number", address); logger.error(message); throw new ProtocolException(message);
marshaller.marshal(msg, socket.getOutputStream()); } catch (final IOException ioe) { throw new ProtocolException("Failed marshalling '" + msg.getType() + "' protocol message due to: " + ioe, ioe); response = unmarshaller.unmarshal(socket.getInputStream()); } catch (final IOException ioe) { throw new ProtocolException("Failed unmarshalling '" + MessageType.CONNECTION_RESPONSE + "' protocol message from " + socket.getRemoteSocketAddress() + " due to: " + ioe, ioe); return connectionResponse; } else { throw new ProtocolException("Expected message type '" + MessageType.CONNECTION_RESPONSE + "' but found '" + response.getType() + "'");
throw new ProtocolException("Handler cannot handle message type: " + request.getType());
@Override public ProtocolMessage handle(final ProtocolMessage msg, Set<String> nodeIds) throws ProtocolException { switch (msg.getType()) { case HEARTBEAT: return handleHeartbeat((HeartbeatMessage) msg); case CLUSTER_WORKLOAD_REQUEST: return handleClusterWorkload((ClusterWorkloadRequestMessage) msg); default: throw new ProtocolException("Cannot handle message of type " + msg.getType()); } }