Class Message
Every message (request or response) flowing between nodes or between client and server uses this format. The fixed 16-byte header is parsed first, then the variable-length body is read based on bodyLength.
Wire format (16-byte header): [magic 1B][opcode 1B][corrId 4B][bodyLen 4B][flags 1B][keyLen 2B][status 1B][mapNameLen 1B][reserved 1B]
Body layout: [mapName bytes][key bytes][value bytes] - mapNameLen in header tells how many bytes of mapName prefix the body - keyLen in header tells how many bytes of key follow the mapName - remaining bytes are the value
Backward compatible: old messages have mapNameLen=0 (was reserved/zero).
Thread-safety warning: Message instances are mutable. The withXxx()
builder methods mutate and return this for chaining during construction.
Once a Message has been sent or shared across threads, it must not be further mutated.
Use copy() to create an independent copy if you need to modify a shared message.
Correlation ID enables request-response multiplexing on a single TCP connection — the client sends requests with incrementing IDs and matches responses by ID, allowing pipelining (send multiple requests before waiting for responses).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byteTransport-only flag: body bytes are LZ4-compressed after a successful PROTOCOL_HELLO negotiation.static final bytestatic final byteTransport-only flag: body bytes are zstd-compressed after a successful PROTOCOL_HELLO negotiation.static final byteTransport-only flag: compressed body bytes include the v2 integrity checksum envelope.static final byteFlag bit indicating the message body carries a length-prefixed senderId between the mapName and key regions.static final intstatic final byte -
Method Summary
Modifier and TypeMethodDescriptionintGet the body length.copy()Create a deep copy of this message.intGet the correlation ID.booleanbyteflags()Get the message flags.inthashCode()byte[]key()Get the key bytes.shortGet the key length.@Nullable StringmapName()Get the map/structure name.byteGet the map name length.static Messagerequest(MessageType type) Create a request message.static Messageresponse(MessageType type, int correlationId) Create a response message.static Messageresponse(MessageType type, int correlationId, byte status) Create a response message with status.@Nullable StringsenderId()Get the sender node ID.bytestatus()Get the status code.longGet the TTL in milliseconds.@Nullable MessageTypetype()Get the message type.byte[]value()Get the value bytes.withFlags(byte flags) Set message flags.Create a copy of this message with a fresh correlation ID.withKey(byte @Nullable [] key) withMapName(@Nullable String mapName) Set the target data structure name (map, queue, or topic).withSenderId(@Nullable String senderId) Set the sender node ID.withStatus(byte status) Set the response status code.withTtl(long ttlMillis) Set the TTL for put operations.withValue(byte @Nullable [] value) Set the message value.
-
Field Details
-
MAGIC
public static final byte MAGIC- See Also:
-
HEADER_SIZE
public static final int HEADER_SIZE- See Also:
-
FLAG_HAS_SENDER_ID
public static final byte FLAG_HAS_SENDER_IDFlag bit indicating the message body carries a length-prefixed senderId between the mapName and key regions. Used for write-retry idempotency: the client stamps a stable dedup ID viawithSenderId(String)and the server's IdempotencyManager uses it to deduplicate retried mutations.- See Also:
-
FLAG_COMPRESSED_LZ4
public static final byte FLAG_COMPRESSED_LZ4Transport-only flag: body bytes are LZ4-compressed after a successful PROTOCOL_HELLO negotiation. Cleared before a decoded Message is exposed.- See Also:
-
FLAG_COMPRESSED_ZSTD
public static final byte FLAG_COMPRESSED_ZSTDTransport-only flag: body bytes are zstd-compressed after a successful PROTOCOL_HELLO negotiation. Cleared before a decoded Message is exposed.- See Also:
-
FLAG_COMPRESSION_INTEGRITY
public static final byte FLAG_COMPRESSION_INTEGRITYTransport-only flag: compressed body bytes include the v2 integrity checksum envelope. Cleared before a decoded Message is exposed.- See Also:
-
FLAG_COMPRESSED_MASK
public static final byte FLAG_COMPRESSED_MASK- See Also:
-
-
Method Details
-
request
Create a request message.- Parameters:
type- the message type (must not be null)- Returns:
- a new request message
-
response
Create a response message.- Parameters:
type- the message type (must not be null)correlationId- the correlation ID from the request- Returns:
- a new response message
-
response
Create a response message with status.- Parameters:
type- the message type (must not be null)correlationId- the correlation ID from the requeststatus- the response status code- Returns:
- a new response message
-
withFreshCorrelationId
Create a copy of this message with a fresh correlation ID. Used for retries to prevent delayed responses from matching the wrong future.- Returns:
- a new Message with the same type/key/value/mapName/flags but a new correlationId
-
withKey
-
withValue
Set the message value.- Parameters:
value- the value bytes (may be null)- Returns:
- this message for chaining
-
withMapName
Set the target data structure name (map, queue, or topic). The name length is stored as an unsigned byte in the wire header, so the maximum encoded size is 255 bytes.- Parameters:
mapName- the structure name (may be null)- Returns:
- this message for chaining
- Throws:
IllegalArgumentException- if encoded name exceeds 255 bytes
-
withTtl
Set the TTL for put operations.- Parameters:
ttlMillis- TTL in milliseconds- Returns:
- this message for chaining
-
withSenderId
-
withFlags
Set message flags.- Parameters:
flags- the flag bits- Returns:
- this message for chaining
-
withStatus
Set the response status code.- Parameters:
status- the status code- Returns:
- this message for chaining
-
type
Get the message type.- Returns:
- the message type, or null if not set
-
correlationId
public int correlationId()Get the correlation ID.- Returns:
- the correlation ID for request-response matching
-
bodyLength
public int bodyLength()Get the body length.- Returns:
- the length of the message body in bytes
-
flags
public byte flags()Get the message flags.- Returns:
- the flag bits
-
keyLength
public short keyLength()Get the key length.- Returns:
- the length of the key in bytes
-
status
public byte status()Get the status code.- Returns:
- the response status code
-
mapNameLength
public byte mapNameLength()Get the map name length.- Returns:
- the length of the map name in the body
-
key
public byte[] key()Get the key bytes.- Returns:
- the key data (never null; empty array if not set)
-
value
public byte[] value()Get the value bytes.- Returns:
- the value data (never null; empty array if not set)
-
mapName
Get the map/structure name.- Returns:
- the structure name (map, queue, or topic), or null if not set
-
ttlMillis
public long ttlMillis()Get the TTL in milliseconds.- Returns:
- TTL in milliseconds for put operations
-
senderId
Get the sender node ID.- Returns:
- the originating node ID, or null if not set
-
copy
Create a deep copy of this message. Use this when you need to modify a message that may be shared across threads.- Returns:
- a new Message with the same field values (byte arrays are cloned)
-
equals
-
hashCode
-