Class MessageSerializer<REQ extends MessageBody,​RESP extends MessageBody>

  • Type Parameters:
    REQ - Type of the requests of the protocol.
    RESP - Type of the responses of the protocol.

    @Internal
    public final class MessageSerializer<REQ extends MessageBody,​RESP extends MessageBody>
    extends Object
    Serialization and deserialization of messages exchanged between client and server.

    The binary messages have the following format:

                         <------ Frame ------------------------->
                        +----------------------------------------+
                        |        HEADER (8)      | PAYLOAD (VAR) |
     +------------------+----------------------------------------+
     | FRAME LENGTH (4) | VERSION (4) | TYPE (4) | CONTENT (VAR) |
     +------------------+----------------------------------------+
     

    The concrete content of a message depends on the MessageType.

    • Method Detail

      • serializeRequest

        public static <REQ extends MessageBody> org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf serializeRequest​(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufAllocator alloc,
                                                                                                                        long requestId,
                                                                                                                        REQ request)
        Serializes the request sent to the AbstractServerBase.
        Parameters:
        alloc - The ByteBufAllocator used to allocate the buffer to serialize the message into.
        requestId - The id of the request to which the message refers to.
        request - The request to be serialized.
        Returns:
        A ByteBuf containing the serialized message.
      • serializeResponse

        public static <RESP extends MessageBody> org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf serializeResponse​(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufAllocator alloc,
                                                                                                                          long requestId,
                                                                                                                          RESP response)
        Serializes the response sent to the Client.
        Parameters:
        alloc - The ByteBufAllocator used to allocate the buffer to serialize the message into.
        requestId - The id of the request to which the message refers to.
        response - The response to be serialized.
        Returns:
        A ByteBuf containing the serialized message.
      • serializeRequestFailure

        public static org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf serializeRequestFailure​(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufAllocator alloc,
                                                                                                     long requestId,
                                                                                                     Throwable cause)
                                                                                              throws IOException
        Serializes the exception containing the failure message sent to the Client in case of protocol related errors.
        Parameters:
        alloc - The ByteBufAllocator used to allocate the buffer to serialize the message into.
        requestId - The id of the request to which the message refers to.
        cause - The exception thrown at the server.
        Returns:
        A ByteBuf containing the serialized message.
        Throws:
        IOException
      • serializeServerFailure

        public static org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf serializeServerFailure​(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufAllocator alloc,
                                                                                                    Throwable cause)
                                                                                             throws IOException
        Serializes the failure message sent to the Client in case of server related errors.
        Parameters:
        alloc - The ByteBufAllocator used to allocate the buffer to serialize the message into.
        cause - The exception thrown at the server.
        Returns:
        The failure message.
        Throws:
        IOException
      • deserializeHeader

        public static MessageType deserializeHeader​(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf buf)
        De-serializes the header and returns the MessageType.
          The buffer is expected to be at the header position.
         
        Parameters:
        buf - The ByteBuf containing the serialized header.
        Returns:
        The message type.
        Throws:
        IllegalStateException - If unexpected message version or message type.
      • getRequestId

        public static long getRequestId​(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf buf)
        De-serializes the header and returns the MessageType.
          The buffer is expected to be at the request id position.
         
        Parameters:
        buf - The ByteBuf containing the serialized request id.
        Returns:
        The request id.
      • deserializeRequest

        public REQ deserializeRequest​(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf buf)
        De-serializes the request sent to the AbstractServerBase.
          The buffer is expected to be at the request position.
         
        Parameters:
        buf - The ByteBuf containing the serialized request.
        Returns:
        The request.
      • deserializeResponse

        public RESP deserializeResponse​(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf buf)
        De-serializes the response sent to the Client.
          The buffer is expected to be at the response position.
         
        Parameters:
        buf - The ByteBuf containing the serialized response.
        Returns:
        The response.
      • deserializeServerFailure

        public static Throwable deserializeServerFailure​(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf buf)
                                                  throws IOException,
                                                         ClassNotFoundException
        De-serializes the failure message sent to the Client in case of server related errors.
          The buffer is expected to be at the correct position.
         
        Parameters:
        buf - The ByteBuf containing the serialized failure message.
        Returns:
        The failure message.
        Throws:
        IOException
        ClassNotFoundException