Framing detection
A buffer may hold one message, many, or messages inside a transport’s frames. A framing is a rule that cuts the buffer into records; each record is then read as on How a reading is chosen.
singleback-to-backlength-prefix ×8sofh ×2aeron-frameExact message:skip-then 1 to 64 bytesscanExact, complete messages; then fewer unexplained bytes; then fewer records with evidence against; then the earlier row→one best: its records; a tie: AmbiguousThe framings
| Framing | Rule |
|---|---|
single | The whole buffer is one payload. Bytes after a message are read again as a nested reading |
back-to-back | Messages with headers, one after another; each length comes from its own walk. What follows the last message is one final record |
length-prefix | A 2- or 4-byte length, little- or big-endian, counting the payload alone or the payload plus the prefix (8 variants). The chain must land on the end of the buffer, except an incomplete last record, which is held as pending. A zero length ends the chain as a non-match |
sofh | FIX Simple Open Framing Header: a 4-byte length that counts the 6-byte header too, then a 2-byte encoding type, which must be SBE 1.0 (0x5BE0 big-endian or 0xEB50 little-endian). The header is big-endian by default; little-endian is tried too, as the standard allows by agreement |
aeron-frame | Aeron data frames as in a term buffer or an archive segment: a 32-byte little-endian header (frame_length first), frames aligned to 32 bytes, padding frames skipped (one whose body is not zeros is an unknown record), alignment bytes that are not zero an unknown record, a zero length ending the written part (the rest must be zeros, shown as a padding record, or pending with a message still being reassembled) |
skip-then | A leading header of 1 to 64 bytes (Limits::max_skip), then one of the framings above covering the rest exactly: real messages and no unexplained bytes |
scan | The last resort: slide forward to each offset where an Exact, complete message starts; the gaps are shown as text, padding or unknown bytes |
A caller that knows the framing fixes it with Inspector::with_framing and
skips detection.
Choosing a framing
Each framing that fits the bytes is scored, and the first difference decides:
| Order | Better is |
|---|---|
| 1 | more bytes inside Exact, complete messages |
| 2 | fewer bytes nothing explains |
| 3 | fewer records with evidence against |
| 4 | the earlier row in the table above |
Rows 1 and 4 mean a run of messages is back-to-back rather than single
(the messages found by reading a single message’s trailing bytes again do
not count in row 1), and one message alone is single. A tie inside one row,
such as two length-prefix variants landing on the same records, is
Ambiguous, both splits shown.
skip-then and scan always leave some bytes unexplained (the skipped
header, the gaps), so they are tried only while the best framing so far does
too, and never when a container framing (length-prefix, sofh or
aeron-frame) holds together over the whole buffer: its records tile every
byte (on a stream prefix, a held last record aside) and it holds at least one
Exact, complete message. scan takes any Exact, complete message it can
find, and a message header followed by zeros reads as one with empty groups
and empty var data; this rule stops such a chance match, inside an Aeron
frame’s header and alignment padding, from outscoring the frame chain that
shows a broken fragment honestly as unknown bytes.
Learning across buffers
An Inspector keeps the framing that won the last buffer. On the next buffer
it tries that framing first; if it explains every byte, it is kept and no
other framing is tried. Otherwise every framing is tried again. There are no
counters and no thresholds. Inspector::learnt_framing shows it.
Incomplete records
On a buffer cut from a stream, the last record may be incomplete: a
length-prefix or SOFH length that runs past the end, an Aeron frame still
being written (Aeron negates the length until the frame is done), or a
fragmented message whose last fragment has not arrived. Those bytes are kept
as pending and printed after the records (Pending(12) 0x..). By default
they count as unexplained, so stray bytes after a framing’s last record can
never hide there. An inspector made with with_stream_prefix(true) reads each
buffer as the start of a stream that goes on: there the pending bytes wait for
more input and do not count against the framing, once it holds at least one
Exact, complete message. On such a buffer an Aeron frame whose 32-byte
alignment padding has not arrived yet is pending too, so the padding is never
read as the start of the next frame.
Each record also gives its raw position: start is its first byte, framing
header included, and the records’ starts with pending_at (where the pending
bytes start) cut the buffer into contiguous spans, so a caller can copy any
record’s bytes unchanged. The sbe-inspect filter passes
unrecognised records through this way.
Fragmented Aeron messages
Aeron splits a message larger than one frame into fragments, marked with the
BEGIN (0x80) and END (0x40) flags. The inspector reassembles them before
decoding, as Aeron’s fragment assembler does: each fragment must start at the
term offset where the one before ended. A fragment that does not continue the
message, and a message whose END never comes before another BEGIN, are shown
as unknown bytes, never dropped. A reassembled record says how many fragments
it came from.
A term with a whole car message, then an Aeron cluster SessionEvent split
into two fragments, then unwritten zeros:
framing: aeron-frame, 3 records
record 0 at 32, 108 bytes:
Car Exact schema 1 v1 (uk.co.real_logic.sbe.benchmarks) template 1 wire v1 blockLength 41 108 bytes complete evidence: +enums valid, +chars printable, +text valid, +header ids found, +version equal, +ends exactly
serialNumber: 1234
modelYear: 2013
available: T
code: A
someNumbers: [10, 20, 30, 40, 50]
vehicleCode: "ABCDEF"
extras: sportsPack|cruiseControl
engine:
capacity: 2000
numCylinders: 4
maxRpm: 9000
manufacturerCode: "123"
fuel: "Petrol"
fuelFigures: 2 entries
[0]
speed: 30
mpg: 35.9
[1]
speed: 60
mpg: 25.0
performanceFigures: 1 entries
[0]
octaneRating: 95
acceleration: 2 entries
[0]
mph: 30
seconds: 4.0
[1]
mph: 60
seconds: 7.5
manufacturer: "Honda"
model: "Civic VTi"
record 1 at 192, 61 bytes, 2 fragments:
SessionEvent Exact schema 111 v18 (io.aeron.cluster.codecs) template 2 wire v18 blockLength 44 61 bytes complete evidence: +enums valid, +text valid, +header ids found, +version equal, +ends exactly
clusterSessionId: 77
correlationId: -1
leadershipTermId: 3
leaderMemberId: 2
code: REDIRECT
version: 66051
leaderHeartbeatTimeoutNs: null
detail: "moved"
record 2 at 288, 64 bytes:
Padding, 64 bytes
The same term in compact, one line per record:
Exact Car { serialNumber: 1234, modelYear: 2013, available: T, code: A, someNumbers: [10, 20, 30, 40, 50], vehicleCode: "ABCDEF", extras: sportsPack|cruiseControl, engine: { capacity: 2000, numCylinders: 4, maxRpm: 9000, manufacturerCode: "123", fuel: "Petrol" }, fuelFigures: [{ speed: 30, mpg: 35.9 }, { speed: 60, mpg: 25.0 }], performanceFigures: [{ octaneRating: 95, acceleration: [{ mph: 30, seconds: 4.0 }, { mph: 60, seconds: 7.5 }] }], manufacturer: "Honda", model: "Civic VTi" }
Exact SessionEvent { clusterSessionId: 77, correlationId: -1, leadershipTermId: 3, leaderMemberId: 2, code: REDIRECT, version: 66051, leaderHeartbeatTimeoutNs: null, detail: "moved" }
Padding(64)