Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions codec/src/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,35 @@ pub fn build_batch_as(records: &[NewRecord], producer_id: i64) -> bytes::Bytes {
build_batch_full(records, producer_id, false)
}

/// Control record marker kind in a Kafka control batch (`0` = ABORT, `1` = COMMIT).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ControlBatchType {
Abort,
Commit,
}

impl RecordBatch {
/// For a control batch, decode its first record's key to determine whether it is a
/// COMMIT (1) or ABORT (0) marker.
pub fn control_type(&self) -> Option<ControlBatchType> {
if !self.is_control() {
return None;
}
let mut recs = self.records().ok()?;
let rec = recs.next()?.ok()?;
let key = rec.key.as_ref()?;
if key.len() < 4 {
return None;
}
let kind = i16::from_be_bytes([key[2], key[3]]);
match kind {
0 => Some(ControlBatchType::Abort),
1 => Some(ControlBatchType::Commit),
_ => None,
}
}
}

/// Bit 5 of the batch attributes: `isControl`.
pub const CONTROL_BATCH_FLAG: i16 = 0x20;

Expand Down
15 changes: 11 additions & 4 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ is direct:

There, a produce with `acks=all` is durable once the transaction's commit has reached
the standbys `synchronous_commit` asks for, and there is no separate replication
mechanism to configure or run.
mechanism to configure or run. With `synchronous_commit = off`, even a Postgres process
crash (`kill -9`) — not only a host crash — can lose acknowledged `acks=all` writes and
reuse their offsets.

The segment engine keeps the log in files, outside the WAL. By default `acks=all`
returns once the records are fsynced (`kafgres.fsync_before_ack`). With that off it
returns once they are in the page cache, as Kafka does with its default flush settings:
they survive a crash of Postgres but not of the host. A standby's copy is pulled
out of band (see below) and is always asynchronous, since `synchronous_standby_names`
does not cover files outside the WAL; a failover can lose the newest records, and the
leader epochs described next make that visible to consumers rather than silent.
leader epochs described next make that visible rather than silent to live consumers and to
clients that validate committed epochs on resume (such as the Java client; librdkafka 2.16
and kafka-python do not validate a group's committed epoch when resuming).

On the primary, `kafgres_produce()` still commits the row and the event together. The
loss is in replication: its row and commit marker travel in the WAL, which can be
Expand Down Expand Up @@ -139,8 +143,11 @@ The log stream and the WAL advance independently, so after a failover their tail
disagree. The log is the source of truth for the high watermark; Postgres keeps only the
slowly-changing metadata (topic configs, group offsets, producer state, transaction
state). A committed consumer offset that ends up ahead of the log tail after a failover
surfaces as `OFFSET_OUT_OF_RANGE`, a condition Kafka clients already handle through
`auto.offset.reset`.
surfaces as `OFFSET_OUT_OF_RANGE` while the new log remains below that offset, a condition
Kafka clients handle through `auto.offset.reset`. Once new writes grow the log past the
committed offset, clients that validate committed epochs (such as the Java client) detect
the truncation via `OffsetForLeaderEpoch`, whereas clients that do not (librdkafka,
kafka-python) resume at the offset silently.

## Process topology

Expand Down
4 changes: 2 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ covers.

| Setting | Reload | Default | Description |
|---|---|---|---|
| `kafgres.retention_check_interval_ms` | reload | `60000` | How often the broker applies retention and compaction, as Kafka's `log.retention.check.interval.ms`. |
| `kafgres.retention_check_interval_ms` | reload | `60000` | How often the broker applies retention and compaction, as Kafka's `log.retention.check.interval.ms`. (Stale transaction expiry runs on its own ~10 s timer.) |
| `kafgres.compaction_pass_bytes` | reload | `32 MiB` | Bytes compaction reads in the broker per retention check, across all partitions; a larger plan resumes at the next check. With the defaults the broker cleans up to about 0.5 MiB/s in total. Raising it lengthens the pause each check takes from serving clients. |

## CDC
Expand All @@ -73,7 +73,7 @@ covers.
| `kafgres.tls_ca_file` | reload | empty | PEM CA bundle that client certificates are verified against; enables mTLS. |
| `kafgres.tls_client_cert_required` | reload | off | Refuse the TLS handshake unless the client presents a certificate valid against `kafgres.tls_ca_file`. |
| `kafgres.sasl_required` | reload | off | Require SASL/SCRAM-SHA-256 authentication against `pg_authid` roles. |
| `kafgres.acls_enabled` | reload | off | Enforce the `kafgres_acls` table, for Kafka clients and for `kafgres_produce()` callers (as `User:<role>`). Off by default: with it on and no matching rule, the answer is refusal. |
| `kafgres.acls_enabled` | reload | off | Enforce the `kafgres_acls` table, for Kafka clients and for `kafgres_produce()` / `kafgres_read()` callers (as `User:<role>`). Off by default: with it on and no matching rule, the answer is refusal. ACL changes take up to ~1 s to apply due to the in-process ACL cache. |
| `kafgres.superusers` | reload | empty | Semicolon-separated principals that bypass every ACL check, for example `User:admin`. |

## Producers and limits
Expand Down
21 changes: 12 additions & 9 deletions docs/producing.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,22 @@ checks, replication), so each needs an explicit `GRANT EXECUTE`. The exceptions
`kafgres_follower_lag()`. On an install upgraded from 0.2.0 this takes effect at
`ALTER EXTENSION kafgres UPDATE`.

`kafgres_add_mapping` runs with the caller's privileges: it reads `kafgres_topics` and the
source table, and writes `kafgres_cdc_mappings`, so a role needs those grants as well as
`EXECUTE`. It also creates the CDC slot, which Postgres allows only to a superuser or a role
with the `REPLICATION` attribute. A role without that still saves its mapping, with a
warning, and the CDC worker creates the slot (see [CDC mappings](#cdc-mappings)). Use
`ALTER ROLE ... REPLICATION` for a role that should create it. `kafgres_slot_ready()` and
`kafgres_cdc_create_slot()` are administrative like the rest.
`kafgres_add_mapping` validates the expressions with the caller's privileges and records
the caller as the mapping's owner. The CDC worker renders each mapping (and any snapshot
of it) under that owner's privileges in a security-restricted context, so revoking
`SELECT` on a source or enrichment table stops rendering via `on_error`. Do not grant
direct `INSERT`, `UPDATE`, or `DELETE` on `kafgres_cdc_mappings`; `kafgres_add_mapping`
and `kafgres_remove_mapping` manage the table on the caller's behalf. Creating the CDC
slot still requires a superuser or a role with the `REPLICATION` attribute. A role without
that still saves its mapping, with a warning, and the CDC worker creates the slot (see
[CDC mappings](#cdc-mappings)). Use `ALTER ROLE ... REPLICATION` for a role that should
create it. `kafgres_slot_ready()` and `kafgres_cdc_create_slot()` are administrative like
the rest.

```sql
GRANT EXECUTE ON FUNCTION kafgres_add_mapping(text, text, text, text, text, text) TO cdc_admin;
GRANT SELECT ON kafgres_topics TO cdc_admin;
GRANT SELECT, INSERT, UPDATE ON kafgres_cdc_mappings TO cdc_admin;
GRANT SELECT ON public.orders TO cdc_admin; -- each source table it maps
GRANT SELECT ON public.orders TO cdc_admin; -- each source table it maps (and any enrichment tables)
ALTER ROLE cdc_admin REPLICATION; -- optional: lets kafgres_add_mapping create the slot itself
```

Expand Down
Loading
Loading