@Override public boolean canHandle(final ProtocolMessage msg) { switch (msg.getType()) { case RECONNECTION_REQUEST: case OFFLOAD_REQUEST: case DISCONNECTION_REQUEST: case FLOW_REQUEST: return true; default: return false; } }
@Override public MessageType getType() { if (protocolMessage == null) { return null; } return protocolMessage.getType(); }
@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() + "'"); }
@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()); } }
@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() + "'"); }
final long startNanos = System.nanoTime(); try { switch (request.getType()) { case FLOW_REQUEST: return handleFlowRequest((FlowRequestMessage) request); throw new ProtocolException("Handler cannot handle message type: " + request.getType()); 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);
@Override public ProtocolMessage handle(final ProtocolMessage protocolMessage, final Set<String> nodeIdentities) throws ProtocolException { switch (protocolMessage.getType()) { case CONNECTION_REQUEST: return handleConnectionRequest((ConnectionRequestMessage) protocolMessage, nodeIdentities); case NODE_STATUS_CHANGE: handleNodeStatusChange((NodeStatusChangeMessage) protocolMessage); return null; case NODE_CONNECTION_STATUS_REQUEST: return handleNodeConnectionStatusRequest(); default: throw new ProtocolException("Cannot handle Protocol Message " + protocolMessage + " because it is not of the correct type"); } }
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() + "'");
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, nodeIdentities); 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); 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 (MessageType.CONNECTION_RESPONSE == response.getType()) { final ConnectionResponseMessage connectionResponse = (ConnectionResponseMessage) response; return connectionResponse; } else { throw new ProtocolException("Expected message type '" + MessageType.CONNECTION_RESPONSE + "' but found '" + response.getType() + "'");
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); } }
private NodeIdentifier getNodeIdentifier(final ProtocolMessage message) { if (message == null) { return null; } switch (message.getType()) { case CONNECTION_REQUEST: return ((ConnectionRequestMessage) message).getConnectionRequest().getProposedNodeIdentifier(); case HEARTBEAT: return ((HeartbeatMessage) message).getHeartbeat().getNodeIdentifier(); case OFFLOAD_REQUEST: return ((OffloadMessage) message).getNodeId(); case DISCONNECTION_REQUEST: return ((DisconnectMessage) message).getNodeId(); case FLOW_REQUEST: return ((FlowRequestMessage) message).getNodeId(); case RECONNECTION_REQUEST: return ((ReconnectionRequestMessage) message).getNodeId(); default: return null; } }
@Override public MessageType getType() { if (protocolMessage == null) { return null; } return protocolMessage.getType(); }
@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() + "'"); }
@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() + "'"); }
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); } }
private NodeIdentifier getNodeIdentifier(final ProtocolMessage message) { if (message == null) { return null; } switch (message.getType()) { case CONNECTION_REQUEST: return ((ConnectionRequestMessage) message).getConnectionRequest().getProposedNodeIdentifier(); case HEARTBEAT: return ((HeartbeatMessage) message).getHeartbeat().getNodeIdentifier(); case OFFLOAD_REQUEST: return ((OffloadMessage) message).getNodeId(); case DISCONNECTION_REQUEST: return ((DisconnectMessage) message).getNodeId(); case FLOW_REQUEST: return ((FlowRequestMessage) message).getNodeId(); case RECONNECTION_REQUEST: return ((ReconnectionRequestMessage) message).getNodeId(); default: return null; } }