Expand description
Byte-compatible reader/writer for Apache Kafka’s on-disk log format.
This crate is the append-only storage layer that the Krabka broker uses.
It reads and writes Kafka 4.x’s on-disk log format byte-for-byte:
20-digit zero-padded segment filenames, sparse .index and
.timeindex files, and append-only .log files that hold
[krabka_protocol::records::RecordBatch] v2 streams.
§What this crate does
- Open and recover existing log directories.
- Append
RecordBatches to the active segment. - Read sequentially from an absolute offset.
- Truncate the log to an offset, for replication and leader election.
- Time-based and size-based retention.
- Deliver-at-time visibility, where a batch becomes readable only once its activation time has passed.
§Scope and boundaries
This crate is the byte-compatible segment and index layer. It exposes
append, read, truncation, leader-epoch checkpoint, transaction-index,
retention, and compaction primitives over a single log directory.
krabka-broker applies broker-level policy above this storage layer. That
policy covers topic configuration, leader and follower ownership,
tiered-storage scheduling, transaction visibility rules, and write
serialization.
§Quick start
use {
krabka_ids::Offset,
krabka_log::{Log, LogConfig},
krabka_protocol::records::RecordBatch,
krabka_units::prelude::mebibytes,
};
let mut log = Log::open("/var/kafka/my-topic-0", LogConfig::default()).unwrap();
let mut batch = RecordBatch::default();
// ... fill the batch ...
let assigned_offset = log.append(&mut batch).unwrap();
let out = log.read(Offset(0), mebibytes(1)).unwrap();§Exporting a segment
use krabka_log::{Log, LogConfig};
let mut log = Log::open("/var/kafka/my-topic-0", LogConfig::default())?;
for segment in log.tierable_segments() {
println!(
"segment {} starts at offset {}",
segment.log_path.display(),
segment.base_offset
);
}Modules§
- name
- Names inside a Kafka log directory: the per-partition directory, and the segment files in it.
- topic_
name - The topic-name rules of Apache Kafka’s
org.apache.kafka.common.internals.Topic.
Structs§
- Aborted
Txn - Active
Producer - One producer as
DescribeProducersreports it. - Active
Segment Export - The active segment as tiered local retention measures it (KIP-405).
- Barrier
Marker - The contents of one barrier marker.
- Built
Compaction - A built
CompactionJob, forLog::finish_compactionto install. Dropping it instead deletes the files the build wrote. - Compaction
Context - Inputs to one
Log::compactpass that depend on broker-side state. - Compaction
Job - A compaction pass taken from a log by
Log::prepare_compaction, ready tobuildwithout the log. - Delivery
Advance - Result of
Log::advance_delivery_watermark. - DueDeletions
- Retired segment files whose deletion delay has passed, taken out of the
log by
Log::maintain_deferring_deletions. - Epoch
Entry - One leader epoch and the offset where it begins.
- Failed
Deletion - A
DueDeletions::deletethat stopped at a failure. - File
Region - A
[offset, offset+len)byte range within a segment.logfile. - Leader
Epoch - A partition leader epoch. KIP-320 wire type:
int32. - Leader
Epoch Checkpoint - Log
- A Kafka-format log: a sorted collection of
Segments plus a single active segment that accepts appends. - LogConfig
- Tunables for
Logbehavior. - Offset
- A record offset within a topic partition’s log. KIP wire type:
int64. - Producer
Batch Metadata - One data batch a producer appended: Kafka’s
BatchMetadata. - Producer
Id - A Kafka idempotent or transactional producer id. KIP-98 wire type:
int64. - Producer
Snapshot Entry - Durable producer metadata stored in Kafka’s
.snapshotsidecar format. - RawRead
- Verbatim, decode-free output of
Log::read_raw. - RawRead
Desc - Descriptor form of
Log::read_rawfor the zero-copysendfilefetch path, Increments D + E. - RawRead
Plan - A raw read planned under the log lock, to run without it.
- RawSegment
Desc - Descriptor form of
Segment::read_rawfor the zero-copy fetch path, Increments D + E. - RawSegment
Read - Verbatim, decode-free output of
Segment::read_raw. - Read
Output - Result of
Log::read: the absolute offset of the first batch returned and the batches themselves. - Recovered
Producer - One producer’s state as the log holds it: its snapshot entry, whose last batch is the producer’s last one, and the batches the producer appended before that one at the same epoch, oldest first.
- Remote
Tier Flags - KIP-950’s two tiered-storage disablement controls.
- Segment
- Segment
Export - A sealed segment described for tiered-storage offload (KIP-405).
- Stamp
Entry - One stamped offset range. The inclusive offsets
[base_offset, last_offset]all carrystamp. - Stamp
Index - Transactional
Batch - One batch as the transaction checks see it.
- TxnIndex
- Verbatim
Batch - A batch that the log appends verbatim, with no re-encode.
- Verification
Guard - A token for one verification of one producer on one partition.
Enums§
- Cleanup
Policy - Per-topic policy for what to do with old log segments.
- Compaction
Outcome - What
Log::finish_compactiondid with a built pass. - Delivery
Policy - Per-topic policy for when a durable record becomes visible to consumers.
- Filtered
Batch - What
filter_batchmade of a batch. - LogError
- Errors returned by
LogandSegment. - Schedule
Order - Whether a scheduled partition’s delivery times must not run backwards.
- Segment
Allocation - How a new segment’s
.logfile gets its disk blocks: Kafka’spreallocate. - Snapshot
Decode Error - Why
decoderejected a producer-state.snapshot. Each variant names the check that failed and carries what that check saw; itsDisplayis the reason the log reports inLogError::Corrupt. - Transaction
Append Refusal - Why a transactional produce may not start or continue its append.
Constants§
- BARRIER_
CONTROL_ TYPE - Control-record type of a krabka barrier marker.
- DEFAULT_
MAX_ MESSAGE_ SIZE - Kafka’s
max.message.bytesdefault: 1 MiB of records plus the 12-byteRecords.LOG_OVERHEADthat prefixes every batch on the wire. - DEFAULT_
READ_ AHEAD_ MAX - Default cap on how far past its own window a verbatim read asks the kernel to read ahead.
- DEFAULT_
TAIL_ CACHE_ SIZE - Default for
LogConfig::tail_cache_size. - DEFAULT_
WRITEBACK_ WINDOW - Default for
LogConfig::writeback_window. - NUM_
BATCHES_ TO_ RETAIN - Kafka’s
ProducerStateEntry.NUM_BATCHES_TO_RETAIN: the number of a producer’s most recent batches whose retry answers as a duplicate.
Traits§
- Stamp
Source - A source of monotonic internal stamps folded into a partition’s
.stampindex.
Functions§
- batch_
is_ deliverable - Apply the shared scheduled-delivery visibility rule to one batch.
- decode_
producer_ snapshot - Decode a Kafka v1 producer-state
.snapshottaken atsnapshot_offset, the exclusive log frontier its entries must lie strictly before. - epoch_
and_ offset_ for_ entries - Resolve a requested leader epoch to
(found_epoch, end_offset), exactly as Kafka’sLeaderEpochFileCache.endOffsetFordoes (seekafka_end_offset_forfor the case table). - epoch_
ms nowin epoch milliseconds. An instant before the epoch reads as0, and one pasti64::MAXmilliseconds saturates there.- filter_
batch - Drop the records of
batchfor whichkeepreturnsfalse. - parse_
barrier_ marker - Read the contents of a barrier marker back out of its record.
- write_
file_ atomic - Replace
pathwithbytesatomically and durably through real file I/O: writetmp, sync it, rename it overpath, then sync the parent directory.