Previous: 2. Architecture. Next: 4. Connections.
apache-pulsar-protocol is the sans-IO core: the protocol types, framing, and the connection and producer/consumer protocol state machines (2 §2). It does no I/O and has no runtime.
PulsarApi.proto is vendored from a pinned apache/pulsar commit. The commit is recorded next to the file.protoc.BaseCommand fields 70–82 (wire-protocol §1).Decided: buffa, with the conditions below. A spike on the real PulsarApi.proto compared buffa 0.9.2 with prost 0.14. It ran on an Apple M5 Pro, in release builds, with an allocation-counting allocator.
| Decode | ns/op | Allocations |
|---|---|---|
| BaseCommand + MessageMetadata: buffa view | 200 | 3 |
BaseCommand + MessageMetadata: buffa OwnedView (Send + 'static) | 234 | 3 |
| BaseCommand + MessageMetadata: prost | 495 | 9 |
| BaseCommand only: buffa view | 97 | 2 |
| BaseCommand only: prost | 206 | 0 |
| Encode a SEND frame, messages already built | ns/op | Allocations |
|---|---|---|
| buffa | 100 | 0 |
| prost | 115 | 0 |
What decides it. The views decode BaseCommand and MessageMetadata borrowing from the frame buffer: strings like producer_name point into it. That is what the receive path wants (§5). Both libraries encode into a caller's buffer with a size pass first, which framing needs. In buffa that is compute_size then write_to, which sizes once. The frames both produce are byte for byte the same.
Conditions:
[default = …] to absent optional fields: partition comes back None, where prost returns −1. The proto has 12 optional fields with non-zero defaults, for example partition = -1, CommandSend.num_messages = 1 and durable = true, and 4 enum fields whose default is their first value. The protocol crate wraps all 16 in accessors that apply the default, and a test reads every [default] in the vendored proto and checks that it has one.BaseCommand.type into the first variant (CONNECT). Codegen uses open_enums_in for that field, so an unknown command is seen as unknown.required field: an absent ledgerId decodes as 0. The decoder checks presence (has_* on views) where it matters.Costs:
MessageFieldView, buffa issue #429), hence the 2–3 allocations above. Lazy views allocate nothing for BaseCommand, but have no OwnedView.The alternative not taken. prost handles proto2 defaults by itself and is far more widely used (about 723M downloads, against about 12M), at about twice the decode cost. Only the protocol crate sees generated types, so the choice can be revisited without touching the rest of the client.
simple command [total_size u32][command_size u32][BaseCommand]
payload command [total_size u32][command_size u32][BaseCommand]
[magic 0x0e01 u16][crc32c u32][metadata_size u32][MessageMetadata][payload]
BrokerEntryMetadata section (magic 0x0e02) before the checksum section. It is parsed when present.max_message_size from CommandConnected, plus the frame overhead.Init ──CONNECT──► AwaitConnected ──CONNECTED──► Ready ──close / error──► Closed
│ ▲ │ ▲
AUTH_CHALLENGE AUTH_CHALLENGE (refresh)
▼ │ ▼ │
AwaitAuthData AwaitAuthData
CONNECT advertises the latest protocol version the client implements (v21 today), the client version, the auth data and, through a proxy, proxy_to_broker_url (4 §2). CONNECTED brings the broker's protocol version, max_message_size and FeatureFlags.
Requests: each connection allocates request ids and keeps a table of pending requests with deadlines. Responses (CommandSuccess, CommandError, CommandProducerSuccess, CommandLookupTopicResponse, and so on) complete them by request id.
Pushed commands come out of the connection state machine as they are, and the layer above routes them to the object registered for their id:
| Id | Commands |
|---|---|
producer_id | CommandSendReceipt, CommandSendError, CommandCloseProducer |
consumer_id | CommandMessage, CommandCloseConsumer, CommandActiveConsumerChange, CommandReachedEndOfTopic, CommandScalableTopicAssignmentUpdate |
session_id | CommandScalableTopicUpdate |
watch_id | CommandWatchScalableTopicsUpdate, CommandWatchTcAssignmentsUpdate |
The ids of each kind come from one allocator per connection. Segment consumers and assignment sessions share the consumer_id space, so they can never collide.
Keepalive: the connection answers the broker's pings, and pings after an idle interval. A missed pong closes the connection.
Feature gating: a connection keeps the broker's FeatureFlags. It refuses to send a scalable-topic command without supports_scalable_topics, with UnsupportedBroker (C15). supports_tc_metadata_discovery and supports_auth_refresh gate their features the same way.
The producer and consumer state machines on top of this (5) live in the same crate. They track registration, pending sends and receipts, flow permits and acks per id, and they produce commands for the connection to frame.
BytesMut, and each complete frame is split off as Bytes, without a copy. A message's payload is a slice of its frame, so delivering it costs no copy either.| Codec | Crate | Notes |
|---|---|---|
| LZ4 | lz4_flex | Pure Rust. A block without a frame, as Java's. |
| ZLIB | flate2 with miniz_oxide | Pure Rust. Java flushes its stream and never finishes it, so it has no final block or checksum: decode the recorded size and no further, as Java's Inflater does (spec gap 22). The producer writes a finished, standard stream, at the default level, as the C++ and Go clients do: the Java, C++ and Go readers stop at the recorded size, so they read it (J15). |
| Snappy | snap | Pure Rust. A block without a frame, as Java's. |
| ZSTD | zstd | Binds the C libzstd. There is no mature pure-Rust encoder; ruzstd only decodes well. A standard frame, at level 3, as Java's. |
A producer compresses each entry‘s payload whole: a batch with every message’s own metadata (5 §2). The metadata records the codec and the uncompressed size, which a consumer decompresses to and checks.
Each codec sits behind a feature of apache-pulsar-protocol, all on by default, and apache-pulsar forwards them: lz4, zlib, snappy and zstd. ZSTD binds the C libzstd, so building it needs a C compiler. A producer configured with a codec its build leaves out fails create with InvalidConfiguration, and a consumer leaves an entry compressed with one for a consumer that can read it.
CommandTopicMigrated (protocol v20), the redirect a blue-green migration sends: handle it in the first connection milestone, or later?