Ankusa.Sink.Kafka (ankusa_kafka v0.7.0)

Copy Markdown View Source

Produces delivered hooks to a Kafka topic (any Kafka-API broker, e.g. Redpanda). The value is Ankusa.Sink.Message, byte-identical to what Ankusa.Sink.RabbitMQ publishes: inline up to :inline_max_bytes, a claim ticket above it.

Each record:

  • key — "tenant_id/source_id" by default. It picks the partition, so it is the ordering scope: records with the same key are consumed in the order they were produced. A sink entry with ordered: true (Ankusa.Ordering) sends the hook's lane key instead, whatever :key says, and Ankusa hands a lane's hooks to this sink one at a time, each after the previous was acknowledged.
  • headers — ankusa_id, ankusa_source_id, ankusa_tenant_id, ankusa_message_version, content_type (application/json) and ankusa_idempotency_key (the key consumers dedupe on, the message's idempotency_key), plus ankusa_dedupe_key, ankusa_replay_id and, on a hook of an ordered sink, ankusa_ordering_key when the hook carries them. Underscores, not hyphens, so they are usable unquoted as Redpanda Connect metadata and SQS attribute names.
  • timestamp — env.received_at (event time, not dispatch time).

deliver/3 returns :ok only after every in-sync replica has the record (required_acks: -1, not configurable). A record the broker will never take is {:error, {:permanent, reason}}, dead-lettered at once (see "Error classes" in Ankusa.Sink): a value plus key over :max_record_bytes is refused before it is produced (a body that cannot fit at all; a message whose inline base64 would not fit is checked in instead, see Ankusa.Sink.Message), and a produce the broker answers with message_too_large, invalid_message or invalid_record is permanent too. Anything else — unreachable broker, unknown topic, timeout, failed claim check — returns {:error, reason} into the source's Ankusa.RetryPolicy, then the DLQ.

A produce that times out may still land: brod cannot cancel one in flight, and its producer is not idempotent, so a produce retried after a lost ack can write the record twice. Delivery is at-least-once anyway; consumers dedupe on the idempotency_key.

The sink never creates topics. Partition count decides which keys share a partition and can only grow, remapping keys when it does — that belongs to whoever operates the topic, not to the first hook that happens to be delivered. An unknown topic is an error, never an auto-created single-partition topic.

Keys are hashed with brod's :hash partitioner (erlang:phash2/1), which is not the Java client's murmur2: the same key can land on a different partition than a Java producer would pick. That only matters if another producer writes the same topic or a consumer relies on co-partitioning.

Process model

The first deliver/3 for an {instance, :client} pair starts a brod client under this package's DynamicSupervisor; brod handles reconnects, leader changes, and per-topic producers from there. brod requires the client id to be a registered atom, so it is :"ankusa_kafka.<instance>.<client>": both parts are config atoms, so the atom count is bounded and two instances never share a client. The client's brokers and ssl/sasl settings are fixed by the first delivery that starts it; use a different :client for a different cluster.

opts

  • :brokers — required; "host:port" strings or {host, port} tuples
  • :topic — required
  • :key — a string, or (Envelope.t() -> String.t());
                        default `"#{tenant_id}/#{source_id}"`
  • :inline_max_bytes — default 64 KiB (65,536), configurable
  • :max_record_bytes — the largest value + key produced; default
                        `1_000_000` (the broker's `message.max.bytes`
                        default, 1 MiB, less batch overhead). Raise it
                        with the topic's `max.message.bytes`
  • :produce_timeout_ms — broker ack timeout and the longest deliver/3
                        waits for it; default `5_000`
  • :client — atom naming the brod client; default :default
  • :idle_timeout_ms — stop the brod client after this long without a
                        delivery through it, default `600_000` (10 min);
                        `0` never. Never under `:produce_timeout_ms` plus
                        1 s. See `Ankusa.Sink.Reaper`
  • :ssl, :sasl — passed to brod's client config when given