-
Notifications
You must be signed in to change notification settings - Fork 891
Data Structures
To support the transport of messages from senders to receivers a number of data structures are required. These data structures need to conform to a number of design principles for the common transport path, exceptional cases can be afforded an exceptional path, e.g. large messages:
- Copy-Free: The send and receive buffer is directly used.
- Allocation-Free: The send and receive buffer is pre-allocated so that the transport path is free from the allocation or reclamation of memory.
- Lock-Free: Lock-free techniques are employed to manage concurrent access.
- Wait-Free: Concurrent algorithms will complete within a finite number of steps.
- Persistent/Immutable: Message buffers for send and receive record immutable history to allow replay and resend semantics.
- O(1) Cost: All operations are constant time regardless of buffer size, senders, receivers, contention, etc.
The source needs to be able to support the efficient retransmission of messages. Messages may need to be retransmitted to cope with loss or late joiners to a communications stream. Each archive represents a term in the communication history from the source.
Source Archive - Memory Mapped File +-------------------------------------------+---------------------+---------------+ | Message buffer (64-byte aligned size) | Index (msg offsets) | State Trailer | +-------------------------------------------+---------------------+---------------+
Messages are stored to the buffer in preparation for transmission as a sequential stream on a FIFO basis. The next n bytes of the buffer are claimed by performing an atomic increment of the tail counter stored in the state trailer.
The producers of message call send(bytes)
on a source object passing the bytes to be sent. The send call then follows the following algorithm:
- IF message is bigger than transmission frame size THEN
- Send in chunks
- END IF
- WHILE next capacity claim < message size
- Mark claimed capacity with tombstone
- Move on to next term
- END WHILE
- Copy in message
- Mark record in archive as complete
A transmission thread observes the send buffer and repeatedly performs the following algorithm:
- Assign message sequence
- Update index for message sequence offset
- Send message on underlying network layer
Each archive contains an index of messages that can be used for the fast lookup of messages from sequence numbers.