Skip to content

kafka-out: the runtime writes a subject to a Kafka topic (ADR-0044 step 4) - #119

Merged
cpoder merged 7 commits into
masterfrom
feat/kafka-out
Dec 5, 2026
Merged

cpoder merged 7 commits into
masterfrom
feat/kafka-out

Conversation

@cpoder

@cpoder cpoder commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

ADR-0044 step 4, PR 1 of 2: the kafka-out driver. PR 2, after this one, moves the kafka_sink recipe onto it.

What it does

  • Consuming and acking. The sink reads its subject through the durable pull consumer its instances share (ADR-0020). The sdk gains Ctx::consume_batches, a batch loop that keeps the hop, DLQ and ack rules of consume_with in one place. kafka-out produces with acks=all and acks a message once the broker has answered for its record. A crash in between produces the record again when the bus delivers the message again.
  • Order. One produce request at a time per partition. A refusal that may clear holds the partition: it tries the same request again with a pause that grows from 200 ms to 5 s, before any other message is taken. Its messages are kept in progress (+WPI) for as long as they are held. A key's records keep their order within an instance. No idempotent producer.
  • Mapping, §5.
    • The payload becomes the value.
    • The key becomes the key, on the partition the Java client's default partitioner gives it. murmur2 is pinned by 15 keys and the bytes 0 to 255, computed by kafka-clients 4.4.0 itself, for 3, 7 and 100 partitions.
    • With no key, one partition per batch, in turn.
    • Headers carry over: a name set twice stays two headers, in the order they came. The sdk's Message::header_values gives the values apart, where the envelope joins them for a flow.
    • The time becomes the timestamp. With no time, the time of the produce is used, and the hop says · no time: the time of the produce.
    • Vejas-Emit-Id comes from the emit id, and traceparent and tracestate from the sink's span.
  • Settings. kafka-in's connection settings, TOPIC, SUBJECT (wildcards allowed), COMPRESSION (lz4 by default) and BATCH (500, at most 1000, the consumer's max_ack_pending). Secrets are handled as in kafka-in.
  • Refusals.
    • A refusal that may clear holds and retries.
    • A record refused for good is a dead letter with the broker's reason and no payload in the log. That covers MessageTooLarge, RecordListTooLarge, InvalidRecord, InvalidTimestamp and CorruptMessage. A request of several records refused that way is split in half, in order, until the record it is about stands alone; the others are written. The letter is counted and the message acked.
    • A refused login fails the unit, said once a start.
    • UnknownTopicOrPartition reads the metadata again. A topic that is gone fails the unit with a clear message. The topic is never created: a missing one is said at each start.
    • Partitions that grow are picked up when the metadata is read again, every 30 s.
  • Observability.
    • Metrics: vejas_kafka_records_out_total, vejas_kafka_bytes_out_total, vejas_kafka_produce_errors_total, plus vejas_kafka_auth_errors_total and vejas_kafka_records_refused_total shared with kafka-in.
    • A probe that connects, logs in, reads the topic's partitions and writes nothing.
    • The topic in /topology.
    • The hop carries the trace on.

Found on the way: rskafka wrote a throttled produce again

PartitionClient::produce treated an answer that carries a throttle time like an error and sent the request again after the throttle. The broker throttles after the records are in the log, so each throttled answer wrote them once more, for as long as the broker throttled.

  • Before the patch: under a quota of 4 kB/s on Kafka 4.4.0, 200 messages became 636 records in 66 s.
  • After the patch: the answer is read, then the client waits out the throttle. The same run writes 200 records in 16 s, each once.

The patch is in third_party/rskafka/src/client/partition.rs, written down in PATCHES.md.

Tests

  • Unit tests (kafka::out):
    • the settings;
    • the murmur2 vectors;
    • the §5 table;
    • the refusal classes;
    • with a fake producer: requests per partition bounded at 1 MB, one in flight per partition, a held request sent again before the next, a record refused for good found by splitting, a refused login told apart from an unknown partition, and a message with no time or a wrong one.
  • The sdk's batch loop against a real NATS (sink::live, now a CI step in connector-crate):
    • each message is settled once, as its delivery says;
    • the messages left are nak'ed and come first at the restart;
    • a batch held 3.5 s with ack_wait 1 s is not delivered again;
    • a lasting failure is dead-lettered at once.
  • e2e/kafka.sh phase 10 passes in CI on Apache Kafka 3.7.2 and 4.4.0 (run 47505358120, on 3ed6862), and locally on 4.4.0:
    • a round trip through kafka-out then kafka-in: the key, a header named twice in order, the time, the bytes (UTF-8, 0 to 255, empty) and the trace;
    • in Kafka, checked with kcat: Vejas-Emit-Id, traceparent continuing the trace, tracestate, the timestamp, and each key on its partition;
    • the hop of the message with no time says so; the topology shows the topic;
    • 40 keys written by the Java console producer and by kafka-out land on the same partitions of a 7-partition topic;
    • under a 4 kB/s quota, 200 records, each once, each key in order;
    • a record too large between two others: the others are written in order, the dead letter carries MessageTooLarge, no payload is in the log, and the refused counter is 1;
    • a missing topic is said at each start and not created, and the 3 messages that waited are written once it exists;
    • a wrong password: each start is refused and said once, the auth counter matches the broker's refusals, and the probe says it once;
    • the probes write nothing, and no secret is in the log;
    • kill -9 after about 4,000 of 20,000 messages: no gap, and copies only of what was in hand (0 to 52 locally; 166 on 3.7.2 and 6 on 4.4.0 in CI, with batches of 200), each with its message's id;
    • two instances share the subject: 4,000 records, each once, about half written by each;
    • partitions added to the topic are found and written to (6, 3 and 3 records by partition);
    • a topic deleted under the sink is said, and the 50 messages it held are written once the topic is back.

Mutants

Each mutant was applied alone, run against the tests named, then undone.

Mutant Rule Result
unsigned_abs() in place of & 0x7fffffff in the partitioner partitioner killed by the murmur2 vectors, and 4 other tests
the seed 0x9747b28d in place of 0x9747b28c partitioner killed by the murmur2 vectors, and 4 other tests
a request's records in reverse order order killed by 4 tests
a held request sent after the next one of its partition order lived at first, because the test held the last request of its partition. The test now holds the second of three, and kills it (bddb170)
a record with no answer settled as sent ack after answer killed by a_refused_login_and_an_unknown_partition_are_told_apart
the sdk acks a batch before delivering it ack after answer killed by sink::live, and by the deleted-topic check of phase 10: 10 of 50 records were written. The kill -9 check did not kill it in one run: the kill landed outside a produce
the sdk's keeper sends no +WPI while a batch is held in progress killed by sink::live
rskafka as it was, which sent a throttled produce again copies the quota check fails: 636 records for 200 messages

After the review of 3ed6862 (9fd713f, 5ec01cf)

  • H1. A pull round that failed once messages were in hand returned with them unsettled. The batch now ends there: what is in hand is delivered and settled, then the unit fails. sink::live::a_round_that_fails_mid_batch_settles_what_is_in_hand fails the second round of a 70-message backlog: the 64 in hand are delivered and acked, 6 stay pending, and the error comes back.
  • M2. A topic made again with fewer partitions now fails the unit, which gives back what it holds and starts again on the new count. rskafka's Could not find metadata for partition reads the metadata again. A unit test covers both, and a phase 10 check deletes a 3-partition topic and makes it again with 1: 30 messages, each once, each key in order.
  • L1. A record with no time takes the time of each try.
  • L7. The partition count's messages name the driver.
  • M1, L10, L11, L12 are in the module doc, the book, and Amendment C.
  • Mutants of the fixes, each killed:
Mutant Killed by
a failed round returns at once, as before H1 sink::live::a_round_that_fails_mid_batch_settles_what_is_in_hand
fewer partitions keep the old count fewer_partitions_stop_the_unit_and_more_are_taken
the missing-partition answer classed as may clear fewer_partitions_stop_the_unit_and_more_are_taken
a record with no time stamped once, at the batch a_record_with_no_time_takes_the_time_of_each_try

Left out

  • Tombstones. §5 says nothing of them: a message with an empty payload is written as an empty value, never as a null one.
  • The broker's own error message for one record (KIP-467's record_errors). The patched client gives the error code, which the letter carries.
  • Order between instances that share a subject. Each keeps a key's order in what it takes.
  • No idempotent producer, as briefed. A retry after a timeout can write a request twice.
  • A hold on a real broker. On Kafka 4.4.0 a topic of one replica took acks=all writes with min.insync.replicas=2, so a one-broker cluster could not refuse that way. The hold is covered by the unit tests and the sdk's live test.
  • ADR-0044 Amendment C records the decisions of step 4 and the limits left as they are (L2 to L6, L8, L9, L12 of the review).

🤖 Generated with Claude Code

https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT

cpoder and others added 5 commits December 5, 2026 09:49
…s delivery says

Ctx::consume_batches gathers up to N messages (or a byte budget) from the
durable pull consumer, hands them to the driver at once, and settles each as
consume_carrying settles one: a hop, then an ack, a nak or a dead letter.
While a batch is in hand a thread keeps its messages in progress every third
of the ack wait. A message left when the unit stops or fails is given back
at once, to come first. consume_with now shares its taking and settling with
it. Puller::round_within asks the server to hold a round briefly, for the
rounds that fill a batch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT
…ep 4)

The sink consumes its subject in batches through the shared durable
consumer, produces with acks=all, one request at a time per partition, and
acks each message once the broker answered for its record. A refusal that
may clear holds the partition, tried again with a growing pause before any
other message is taken. A record refused for good is a dead letter with the
broker's reason; a request of several refused so is split until it stands
alone. Keys go where the Java client's default partitioner puts them
(murmur2, pinned with vectors the Java client computed); keyless messages
take one partition per batch, in turn. Metrics, a probe that writes
nothing, the topic in the topology. e2e/kafka.sh phase 10.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT
…t sent again

The envelope joins a repeated header for a flow (ADR-0042). A sink that
writes headers one by one reads them from Message::header_values, which
gives each value apart, in the order it came; kafka-out writes them so, as
ADR-0044 point 5 says.

rskafka sent a produce again when its answer carried a throttle time. The
broker throttles after the records are in the log, so each throttled answer
wrote them once more: under a quota of 4 kB/s on Kafka 4.4.0, 200 messages
became 636 records. The answer is read now, then the client waits out the
throttle. e2e/kafka.sh checks a quota where it checked min.insync.replicas,
which Kafka 4.4.0 does not enforce on a topic of one replica.

The sdk's batch test runs in CI; the changelog and the book describe
kafka-out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT
A mutant that sends a held request after the next one of its partition
lived: the test held the last request. It holds the second of three now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT
…d when its topic is deleted

Two checks in phase 10. Partitions added to the topic are written to once
the unit reads the metadata again. A topic deleted under the sink is said,
and the 50 messages it held are written once the topic is back: with the
sdk acking a batch before its delivery, 40 of them were lost. The topic
list is piped into matches, as the pipefail check asks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT
@cpoder
cpoder marked this pull request as ready for review December 5, 2026 10:03
cpoder and others added 2 commits December 5, 2026 11:42
…itions restart the unit

H1. A pull round that failed once messages were in hand returned with
them unsettled: no ack, no nak, and their +WPI stopped. The restarted unit
wrote newer messages first, and these came back after their ack wait. The
batch now ends there: what is in hand is delivered and settled, then the
unit fails. sink::live checks it with a round that fails mid-batch.

M2. A topic made again with fewer partitions kept the old count, and the
batch held for good on a partition that no longer existed. A smaller count
now fails the unit, which gives back what it holds and starts again on the
new count; rskafka's missing-partition answer reads the metadata again.
e2e/kafka.sh checks it.

L1. A record with no time takes the time of each try, so a hold does not
age it. L7. The partition count's messages name the driver that reads it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT
…4 Amendment C

The module doc and the book say that each retry of a held request can
write it again, with no fixed count, and that a try given up after 45 s
can land after the next request; that a key keeps its order while the
instance runs; that the sink opens one connection and one login per
partition. Amendment C records the decisions of step 4 and the limits
left as they are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT
@cpoder
cpoder merged commit 73e8336 into master Dec 5, 2026
7 checks passed
@cpoder
cpoder deleted the feat/kafka-out branch December 5, 2026 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant