object_log/sequencer.rs
1//! The sequencing seam: offset assignment, the offset→location index, and
2//! retention. The engine owns storage + buffering; a `Sequencer` owns offsets.
3
4use crate::ObjectLogError;
5use serde::{Deserialize, Serialize};
6use std::collections::{HashMap, HashSet};
7use std::sync::Mutex;
8
9/// Opaque, engine-visible identifier for an independent offset stream (one dense,
10/// monotonic offset space). A Kafka broker maps `(topic, partition)` onto one of
11/// these; a WAL maps a shard. object-log treats it as an opaque key.
12#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
13pub struct PartitionKey(pub String);
14
15impl PartitionKey {
16 /// Borrow the key as a string slice.
17 pub fn as_str(&self) -> &str {
18 &self.0
19 }
20}
21
22/// Where a batch's bytes live inside its object. **Authored by the engine** (it
23/// owns object layout); the sequencer stores it in the index and returns it from
24/// [`Sequencer::lookup`].
25#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
26pub struct BatchLocation {
27 /// Key of the object holding the batch.
28 pub object_id: String,
29 /// Byte offset of the batch within the object.
30 pub byte_start: u32,
31 /// Byte length of the batch.
32 pub byte_len: u32,
33}
34
35/// One batch presented to [`Sequencer::commit`]. The engine fills `partition`,
36/// `record_count`, and `location` (all engine-visible); `meta` is forwarded
37/// **uninterpreted** — only the sequencer reads it.
38pub struct CommitBatch<'a, M> {
39 /// The offset stream this batch belongs to.
40 pub partition: PartitionKey,
41 /// Number of records in the batch (offsets advance by this).
42 pub record_count: i32,
43 /// Where the batch lives in its (already-durable) object.
44 pub location: BatchLocation,
45 /// Sequencer-private metadata (e.g. idempotent-producer identity). Opaque to
46 /// the engine.
47 pub meta: &'a M,
48}
49
50/// Per-batch result of [`Sequencer::commit`].
51#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
52pub enum CommitOutcome {
53 /// A fresh, contiguous offset range was assigned starting at `base_offset`.
54 Assigned {
55 /// First offset assigned to the batch.
56 base_offset: i64,
57 /// Number of records committed.
58 record_count: i32,
59 },
60 /// A retried batch was recognized as already committed (idempotent no-op);
61 /// the original `base_offset` is returned.
62 Duplicate {
63 /// The originally assigned first offset.
64 base_offset: i64,
65 },
66}
67
68/// An index entry resolving an offset range to its bytes.
69#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
70pub struct IndexEntry {
71 /// Where the batch's bytes live.
72 pub location: BatchLocation,
73 /// First offset in the batch.
74 pub base_offset: i64,
75 /// Number of records in the batch.
76 pub record_count: i32,
77}
78
79/// The linearization point: assigns offsets to durably-stored batches and owns
80/// the offset→location index.
81///
82/// **Synchronous on purpose** — a lin-point is a critical section, not async I/O.
83/// The engine calls it from its flush worker (a dedicated thread / blocking
84/// task), so a blocking implementation (a `Mutex`, a SQL transaction) is fine.
85///
86/// Contract: [`commit`](Sequencer::commit) is **atomic across the whole slice**
87/// (all batches in one object commit together or not at all), and the engine
88/// presents batches for any single [`PartitionKey`] in arrival order and never
89/// splits one partition across concurrent `commit` calls.
90pub trait Sequencer: Send + Sync {
91 /// Sequencer-private per-batch metadata, forwarded uninterpreted by the
92 /// engine. object-log's default sequencers use `()`; a Kafka coordinator uses
93 /// its producer-identity fields.
94 type Meta: Send + Sync;
95
96 /// Assign offsets to a just-PUT object's batches and persist the index.
97 /// Returns one [`CommitOutcome`] per input batch, in order. Atomic: on `Err`,
98 /// nothing is committed.
99 fn commit(
100 &self,
101 batches: &[CommitBatch<'_, Self::Meta>],
102 ) -> Result<Vec<CommitOutcome>, ObjectLogError>;
103
104 /// Resolve `fetch_offset` to the ordered index entries covering it onward.
105 fn lookup(
106 &self,
107 partition: &PartitionKey,
108 fetch_offset: i64,
109 ) -> Result<Vec<IndexEntry>, ObjectLogError>;
110
111 /// The next offset to be assigned (index-only; no object reads).
112 fn high_watermark(&self, partition: &PartitionKey) -> Result<i64, ObjectLogError>;
113
114 /// The first readable offset (advances on [`truncate_before`](Sequencer::truncate_before)).
115 fn log_start_offset(&self, partition: &PartitionKey) -> Result<i64, ObjectLogError>;
116
117 /// Retention MECHANISM (not policy): drop index entries below `offset` and
118 /// return object ids that now have **no** live references from **any**
119 /// partition (objects are multiplexed and shared), for the engine to delete.
120 fn truncate_before(
121 &self,
122 partition: &PartitionKey,
123 offset: i64,
124 ) -> Result<Vec<String>, ObjectLogError>;
125}
126
127/// Per-partition in-memory index state.
128#[derive(Default)]
129struct PartState {
130 next_offset: i64,
131 log_start: i64,
132 entries: Vec<IndexEntry>,
133}
134
135/// A single-node, in-memory [`Sequencer`] (`Meta = ()`): assigns dense offsets and
136/// keeps the offset→location index in memory.
137///
138/// The log *bytes* live durably in the `BlobStore`, but this index does not
139/// survive a restart — use it for tests and single-process work, or persist the
140/// index yourself for a crash-durable standalone log.
141#[derive(Default)]
142pub struct InMemorySequencer {
143 state: Mutex<HashMap<PartitionKey, PartState>>,
144}
145
146impl InMemorySequencer {
147 /// Create an empty sequencer.
148 pub fn new() -> Self {
149 Self::default()
150 }
151}
152
153impl Sequencer for InMemorySequencer {
154 type Meta = ();
155
156 fn commit(
157 &self,
158 batches: &[CommitBatch<'_, Self::Meta>],
159 ) -> Result<Vec<CommitOutcome>, ObjectLogError> {
160 let mut state = self.state.lock().expect("poisoned");
161 let mut outcomes = Vec::with_capacity(batches.len());
162 for cb in batches {
163 let ps = state.entry(cb.partition.clone()).or_default();
164 let base = ps.next_offset;
165 ps.entries.push(IndexEntry {
166 location: cb.location.clone(),
167 base_offset: base,
168 record_count: cb.record_count,
169 });
170 ps.next_offset = base + cb.record_count as i64;
171 outcomes.push(CommitOutcome::Assigned {
172 base_offset: base,
173 record_count: cb.record_count,
174 });
175 }
176 Ok(outcomes)
177 }
178
179 fn lookup(
180 &self,
181 partition: &PartitionKey,
182 fetch_offset: i64,
183 ) -> Result<Vec<IndexEntry>, ObjectLogError> {
184 let state = self.state.lock().expect("poisoned");
185 let Some(ps) = state.get(partition) else {
186 return Ok(Vec::new());
187 };
188 Ok(ps
189 .entries
190 .iter()
191 .filter(|e| e.base_offset + e.record_count as i64 > fetch_offset)
192 .cloned()
193 .collect())
194 }
195
196 fn high_watermark(&self, partition: &PartitionKey) -> Result<i64, ObjectLogError> {
197 Ok(self
198 .state
199 .lock()
200 .expect("poisoned")
201 .get(partition)
202 .map_or(0, |ps| ps.next_offset))
203 }
204
205 fn log_start_offset(&self, partition: &PartitionKey) -> Result<i64, ObjectLogError> {
206 Ok(self
207 .state
208 .lock()
209 .expect("poisoned")
210 .get(partition)
211 .map_or(0, |ps| ps.log_start))
212 }
213
214 fn truncate_before(
215 &self,
216 partition: &PartitionKey,
217 offset: i64,
218 ) -> Result<Vec<String>, ObjectLogError> {
219 let mut state = self.state.lock().expect("poisoned");
220 let mut dropped: Vec<String> = Vec::new();
221 match state.get_mut(partition) {
222 Some(ps) => {
223 for e in ps.entries.iter() {
224 if e.base_offset + e.record_count as i64 <= offset {
225 dropped.push(e.location.object_id.clone());
226 }
227 }
228 ps.entries
229 .retain(|e| e.base_offset + e.record_count as i64 > offset);
230 if offset > ps.log_start {
231 ps.log_start = offset.min(ps.next_offset);
232 }
233 }
234 None => return Ok(Vec::new()),
235 }
236 // An object is reclaimable only when no entry in ANY partition references it.
237 let mut live: HashSet<String> = HashSet::new();
238 for ps in state.values() {
239 for e in &ps.entries {
240 live.insert(e.location.object_id.clone());
241 }
242 }
243 let mut dead: Vec<String> = Vec::new();
244 let mut seen: HashSet<String> = HashSet::new();
245 for oid in dropped {
246 if !live.contains(&oid) && seen.insert(oid.clone()) {
247 dead.push(oid);
248 }
249 }
250 Ok(dead)
251 }
252}