1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// SPDX-License-Identifier: BUSL-1.1
//! Sync-producer registry catalog ops for the `_system.sync_producer_hwm` and
//! `_system.sync_producers` tables.
//!
//! Two tables live here:
//!
//! * `_system.sync_producer_hwm` — singleton `"global"` → `u64` high-watermark
//! for the monotonic producer-id allocator. Mirrors the layout of
//! `_system.surrogate_hwm` (which uses `u32`).
//!
//! * `_system.sync_producers` — `lite_id (str)` → MessagePack-serialized
//! `StoredProducerRegistration`. One row per registered Lite client.
//!
//! All writes go through single-statement redb write transactions for
//! crash-safety, matching the pattern used by every other `_system.*` table.
use super::types::{SystemCatalog, catalog_err};
// ── Table definitions ─────────────────────────────────────────────────────────
/// Singleton high-watermark for the producer-id allocator.
///
/// Key: `"global"` (the only row).
/// Value: highest `producer_id` ever issued (0 = no allocations yet).
pub const SYNC_PRODUCER_HWM: redb::TableDefinition<&str, u64> =
redb::TableDefinition::new("_system.sync_producer_hwm");
/// Per-Lite-client producer registration rows.
///
/// Key: `lite_id` (opaque string from the Lite handshake; typically a
/// UUID or device fingerprint).
/// Value: MessagePack-serialized [`StoredProducerRegistration`].
pub const SYNC_PRODUCERS: redb::TableDefinition<&str, &[u8]> =
redb::TableDefinition::new("_system.sync_producers");
/// Singleton row key used in `_system.sync_producer_hwm`.
const HWM_KEY: &str = "global";
// ── Catalog record ────────────────────────────────────────────────────────────
/// Persisted state for a single Lite client's sync producer.
///
/// Handshake wiring (Stage 4) and Raft replication of `register` / `fence`
/// (Stage 5) are explicit follow-ups; this record is the persistence layer
/// only.
#[derive(zerompk::ToMessagePack, zerompk::FromMessagePack, Debug, Clone, PartialEq)]
#[msgpack(map, allow_unknown_fields)]
pub struct StoredProducerRegistration {
/// Stable, monotonic, per-database u64 identity for this Lite client's
/// write stream. Allocated from `_system.sync_producer_hwm` and never
/// reused.
pub producer_id: u64,
/// Fencing epoch, advanced by `fence()` calls. Any token issued with a
/// lower epoch is considered stale and must be rejected. Starts at 0 on
/// first registration.
pub current_epoch: u64,
/// Internal tenant that owns this registration.
pub tenant_id: u64,
/// Unix-millisecond timestamp when this registration was first created.
pub created_ms: i64,
}
// ── Hwm catalog ops ────────────────────────────────────────────────────────────
impl SystemCatalog {
/// Persist the producer-id allocator high-watermark. Overwrites the
/// singleton row.
pub fn put_producer_hwm(&self, hwm: u64) -> crate::Result<()> {
let txn = self
.db
.begin_write()
.map_err(|e| catalog_err("producer_hwm write txn", e))?;
{
let mut table = txn
.open_table(SYNC_PRODUCER_HWM)
.map_err(|e| catalog_err("open sync_producer_hwm", e))?;
table
.insert(HWM_KEY, hwm)
.map_err(|e| catalog_err("insert sync_producer_hwm", e))?;
}
txn.commit()
.map_err(|e| catalog_err("sync_producer_hwm commit", e))
}
/// Load the persisted producer-id hwm, or `0` if none recorded yet
/// (fresh database).
pub fn get_producer_hwm(&self) -> crate::Result<u64> {
let txn = self
.db
.begin_read()
.map_err(|e| catalog_err("producer_hwm read txn", e))?;
let table = txn
.open_table(SYNC_PRODUCER_HWM)
.map_err(|e| catalog_err("open sync_producer_hwm", e))?;
match table
.get(HWM_KEY)
.map_err(|e| catalog_err("get sync_producer_hwm", e))?
{
Some(v) => Ok(v.value()),
None => Ok(0),
}
}
}
// ── Producer registration catalog ops ────────────────────────────────────────
impl SystemCatalog {
/// Persist a producer registration row, creating or overwriting it.
///
/// Idempotent: re-inserting the same `lite_id` with the same record
/// overwrites the existing row on disk (no-op at the application layer).
pub fn put_producer_registration(
&self,
lite_id: &str,
reg: &StoredProducerRegistration,
) -> crate::Result<()> {
let bytes = zerompk::to_msgpack_vec(reg)
.map_err(|e| catalog_err("serialize producer_registration", e))?;
let txn = self
.db
.begin_write()
.map_err(|e| catalog_err("sync_producers write txn", e))?;
{
let mut table = txn
.open_table(SYNC_PRODUCERS)
.map_err(|e| catalog_err("open sync_producers", e))?;
table
.insert(lite_id, bytes.as_slice())
.map_err(|e| catalog_err("insert sync_producers", e))?;
}
txn.commit()
.map_err(|e| catalog_err("sync_producers commit", e))
}
/// Load the registration row for `lite_id`, or `None` if not found.
pub fn get_producer_registration(
&self,
lite_id: &str,
) -> crate::Result<Option<StoredProducerRegistration>> {
let txn = self
.db
.begin_read()
.map_err(|e| catalog_err("sync_producers read txn", e))?;
let table = txn
.open_table(SYNC_PRODUCERS)
.map_err(|e| catalog_err("open sync_producers", e))?;
match table
.get(lite_id)
.map_err(|e| catalog_err("get sync_producers", e))?
{
None => Ok(None),
Some(v) => {
let reg: StoredProducerRegistration = zerompk::from_msgpack(v.value())
.map_err(|e| catalog_err("deserialize producer_registration", e))?;
Ok(Some(reg))
}
}
}
}
// ── Tests ─────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
fn open() -> (tempfile::TempDir, SystemCatalog) {
let dir = tempfile::tempdir().unwrap();
let cat = SystemCatalog::open(&dir.path().join("system.redb")).unwrap();
(dir, cat)
}
// ── hwm tests ──
#[test]
fn fresh_hwm_returns_zero() {
let (_dir, cat) = open();
assert_eq!(cat.get_producer_hwm().unwrap(), 0);
}
#[test]
fn put_hwm_then_get_roundtrip() {
let (_dir, cat) = open();
cat.put_producer_hwm(42).unwrap();
assert_eq!(cat.get_producer_hwm().unwrap(), 42);
cat.put_producer_hwm(1_000_000_000).unwrap();
assert_eq!(cat.get_producer_hwm().unwrap(), 1_000_000_000);
}
#[test]
fn hwm_persists_across_reopen() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("system.redb");
{
let cat = SystemCatalog::open(&path).unwrap();
cat.put_producer_hwm(7777).unwrap();
}
let cat = SystemCatalog::open(&path).unwrap();
assert_eq!(cat.get_producer_hwm().unwrap(), 7777);
}
// ── registration tests ──
fn reg(producer_id: u64, epoch: u64, tenant_id: u64) -> StoredProducerRegistration {
StoredProducerRegistration {
producer_id,
current_epoch: epoch,
tenant_id,
created_ms: 1_700_000_000_000,
}
}
#[test]
fn put_then_get_roundtrip() {
let (_dir, cat) = open();
let r = reg(1, 0, 99);
cat.put_producer_registration("device-abc", &r).unwrap();
let got = cat
.get_producer_registration("device-abc")
.unwrap()
.unwrap();
assert_eq!(got, r);
}
#[test]
fn missing_lite_id_returns_none() {
let (_dir, cat) = open();
assert!(cat.get_producer_registration("nobody").unwrap().is_none());
}
#[test]
fn put_is_idempotent_overwrite() {
let (_dir, cat) = open();
cat.put_producer_registration("dev", ®(1, 0, 1)).unwrap();
cat.put_producer_registration("dev", ®(1, 1, 1)).unwrap();
let got = cat.get_producer_registration("dev").unwrap().unwrap();
assert_eq!(got.current_epoch, 1);
}
#[test]
fn registrations_persist_across_reopen() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("system.redb");
{
let cat = SystemCatalog::open(&path).unwrap();
cat.put_producer_registration("dev-1", ®(10, 3, 5))
.unwrap();
}
let cat = SystemCatalog::open(&path).unwrap();
let got = cat.get_producer_registration("dev-1").unwrap().unwrap();
assert_eq!(got.producer_id, 10);
assert_eq!(got.current_epoch, 3);
assert_eq!(got.tenant_id, 5);
}
}