linkrs-sync 0.1.0

Synchronization primitives for Linkrs
Documentation
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#[cfg(feature = "fulltext")]
use std::collections::HashSet;
#[cfg(any(feature = "fulltext", feature = "vector"))]
use std::sync::Arc;
#[cfg(any(feature = "fulltext", feature = "vector"))]
use tokio::sync::{Mutex, RwLock};

use linkrs_core::types::CommitLsn;
#[cfg(feature = "fulltext")]
use linkrs_core::wal::IndexMutation;

pub struct ApplyReceipt {
    pub commit_lsn: CommitLsn,
    pub idempotency_key: String,
    pub applied: bool,
}

pub struct LateArrivalResult {
    pub accepted: bool,
    pub reason: String,
}

#[cfg(feature = "fulltext")]
pub struct FulltextReceiver {
    engine: Arc<dyn linkrs_fulltext::engine::FulltextSearchEngine>,
    receipts: Arc<RwLock<HashSet<String>>>,
    applied_lsn: Arc<RwLock<CommitLsn>>,
    apply_lock: Mutex<()>,
}

// FulltextReceiver keeps JSON because Tantivy commit payloads accept only UTF-8
// strings; this intentional exception is documented.
#[cfg(feature = "fulltext")]
#[derive(serde::Serialize, serde::Deserialize)]
struct FulltextCommitState {
    applied_lsn: u64,
    receipts: HashSet<String>,
}

#[cfg(feature = "fulltext")]
impl FulltextReceiver {
    pub fn new(engine: Arc<dyn linkrs_fulltext::engine::FulltextSearchEngine>) -> Self {
        let state = engine
            .commit_payload()
            .ok()
            .flatten()
            .and_then(|payload| serde_json::from_str::<FulltextCommitState>(&payload).ok())
            .unwrap_or_else(|| FulltextCommitState {
                applied_lsn: 0,
                receipts: HashSet::new(),
            });
        Self {
            engine,
            receipts: Arc::new(RwLock::new(state.receipts)),
            applied_lsn: Arc::new(RwLock::new(CommitLsn::new(state.applied_lsn))),
            apply_lock: Mutex::new(()),
        }
    }

    pub async fn apply_index_batch(
        &self,
        mutations: &[(&IndexMutation, CommitLsn)],
    ) -> Result<Vec<ApplyReceipt>, String> {
        let _apply_guard = self.apply_lock.lock().await;
        let mut deletes = Vec::new();
        let mut items = Vec::new();
        let mut receipts = Vec::new();
        let receipts_guard = self.receipts.read().await;

        for (mutation, lsn) in mutations {
            let idempotency_key = mutation.idempotency_key.as_str().to_string();
            if receipts_guard.contains(&idempotency_key) {
                receipts.push(ApplyReceipt {
                    commit_lsn: *lsn,
                    idempotency_key,
                    applied: true,
                });
                continue;
            }
            let applied_lsn = *self.applied_lsn.read().await;
            if *lsn < applied_lsn {
                return Err(format!(
                    "late fulltext mutation: commit LSN {} is below applied LSN {}",
                    lsn, applied_lsn
                ));
            }

            match mutation.operation {
                linkrs_core::wal::IndexOperation::Delete => {
                    let entity_id = match &mutation.entity_ref {
                        linkrs_core::wal::EntityRef::Vertex(vid) => vid.to_string(),
                        linkrs_core::wal::EntityRef::Edge { src, dst, .. } => {
                            format!("{}->{}", src, dst)
                        }
                    };
                    deletes.push(entity_id);
                }
                linkrs_core::wal::IndexOperation::Upsert => {
                    let entity_id = match &mutation.entity_ref {
                        linkrs_core::wal::EntityRef::Vertex(vid) => vid.to_string(),
                        linkrs_core::wal::EntityRef::Edge { src, dst, .. } => {
                            format!("{}->{}", src, dst)
                        }
                    };
                    let document = String::from_utf8(mutation.document_or_vector.clone())
                        .map_err(|e| format!("invalid utf-8 in fulltext document: {}", e))?;
                    items.push((entity_id, document));
                }
            }
            receipts.push(ApplyReceipt {
                commit_lsn: *lsn,
                idempotency_key,
                applied: false,
            });
        }
        drop(receipts_guard);

        if !deletes.is_empty() {
            let ids: Vec<&str> = deletes.iter().map(|s| s.as_str()).collect();
            self.engine
                .delete_batch(ids)
                .await
                .map_err(|e| format!("fulltext delete batch failed: {}", e))?;
        }

        if !items.is_empty() {
            self.engine
                .index_batch(items)
                .await
                .map_err(|e| format!("fulltext index batch failed: {}", e))?;
        }

        let mut persisted_receipts = self.receipts.read().await.clone();
        let mut max_lsn = *self.applied_lsn.read().await;
        for receipt in &mut receipts {
            if !receipt.applied {
                persisted_receipts.insert(receipt.idempotency_key.clone());
                receipt.applied = true;
            }
            if receipt.commit_lsn > max_lsn {
                max_lsn = receipt.commit_lsn;
            }
        }
        let payload = serde_json::to_string(&FulltextCommitState {
            applied_lsn: max_lsn.get(),
            receipts: persisted_receipts.clone(),
        })
        .map_err(|error| format!("serialize fulltext commit receipt: {error}"))?;
        self.engine
            .commit_with_payload(payload)
            .await
            .map_err(|e| format!("fulltext commit failed: {}", e))?;
        *self.receipts.write().await = persisted_receipts;
        *self.applied_lsn.write().await = max_lsn;

        Ok(receipts)
    }

    pub async fn check_late_arrival(
        &self,
        commit_lsn: CommitLsn,
        idempotency_key: &str,
    ) -> LateArrivalResult {
        let receipts = self.receipts.read().await;
        if receipts.contains(idempotency_key) {
            return LateArrivalResult {
                accepted: false,
                reason: "duplicate idempotency key".to_string(),
            };
        }
        let applied_lsn = *self.applied_lsn.read().await;
        if commit_lsn < applied_lsn {
            return LateArrivalResult {
                accepted: false,
                reason: format!(
                    "late arrival: commit_lsn {} < applied_lsn {}",
                    commit_lsn, applied_lsn
                ),
            };
        }
        LateArrivalResult {
            accepted: true,
            reason: String::new(),
        }
    }

    pub async fn applied_lsn(&self) -> CommitLsn {
        *self.applied_lsn.read().await
    }

    pub async fn is_idempotent(&self, key: &str) -> bool {
        self.receipts.read().await.contains(key)
    }
}

#[cfg(feature = "vector")]
#[derive(Clone, serde::Serialize, serde::Deserialize)]
struct VectorCommitState {
    applied_lsn: u64,
    receipts: std::collections::HashMap<String, u64>,
}

#[cfg(feature = "vector")]
const VECTOR_RECEIPT_MAX_ENTRIES: usize = 8192;
#[cfg(feature = "vector")]
const VECTOR_RECEIPT_RETENTION_LSN_WINDOW: u64 = 100_000;

#[cfg(feature = "vector")]
pub struct VectorReceiver {
    state: Arc<RwLock<VectorCommitState>>,
    apply_lock: Mutex<()>,
    recovery_path: Arc<std::path::PathBuf>,
}

#[cfg(feature = "vector")]
impl Default for VectorReceiver {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "vector")]
impl VectorReceiver {
    pub fn new() -> Self {
        Self {
            state: Arc::new(RwLock::new(VectorCommitState {
                applied_lsn: CommitLsn::ZERO.get(),
                receipts: std::collections::HashMap::new(),
            })),
            apply_lock: Mutex::new(()),
            recovery_path: Arc::new(std::path::PathBuf::new()),
        }
    }

    pub fn with_recovery_path(mut self, path: impl Into<std::path::PathBuf>) -> Self {
        self.recovery_path = Arc::new(path.into());
        self
    }

    pub fn open(path: impl Into<std::path::PathBuf>) -> Self {
        let path: std::path::PathBuf = path.into();
        let state_path = Self::state_file_path(&path);
        let state = std::fs::read(&state_path)
            .ok()
            .and_then(|bytes| postcard::from_bytes::<VectorCommitState>(&bytes).ok())
            .unwrap_or(VectorCommitState {
                applied_lsn: 0,
                receipts: std::collections::HashMap::new(),
            });
        Self {
            state: Arc::new(RwLock::new(state)),
            apply_lock: Mutex::new(()),
            recovery_path: Arc::new(path),
        }
    }

    fn state_file_path(recovery_dir: &std::path::Path) -> std::path::PathBuf {
        recovery_dir.join("vector_receiver_state.bin")
    }

    async fn persist_state(&self, state: &VectorCommitState) -> Result<(), String> {
        let path = Self::state_file_path(&self.recovery_path);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
        }
        let temporary = path.with_extension("tmp");
        let bytes = postcard::to_allocvec(state).map_err(|e| e.to_string())?;
        std::fs::write(&temporary, &bytes).map_err(|e| e.to_string())?;
        std::fs::File::open(&temporary)
            .and_then(|f| f.sync_all())
            .map_err(|e| e.to_string())?;
        std::fs::rename(&temporary, &path).map_err(|e| e.to_string())?;
        if let Some(parent) = path.parent() {
            std::fs::File::open(parent)
                .and_then(|f| f.sync_all())
                .map_err(|e| e.to_string())?;
        }
        Ok(())
    }

    pub async fn check_late_arrival(
        &self,
        commit_lsn: CommitLsn,
        idempotency_key: &str,
    ) -> LateArrivalResult {
        let state = self.state.read().await;
        if state.receipts.contains_key(idempotency_key) {
            return LateArrivalResult {
                accepted: false,
                reason: "duplicate idempotency key".to_string(),
            };
        }

        let current = CommitLsn::new(state.applied_lsn);
        if commit_lsn < current {
            // Below the water-level: considered late. Even if the key was
            // pruned from the LRU window, the SQLite idempotency table still
            // protects against re-application (see SqliteOutbox::materialize).
            return LateArrivalResult {
                accepted: false,
                reason: format!(
                    "late arrival: commit_lsn {} < applied_lsn {}",
                    commit_lsn, current
                ),
            };
        }

        LateArrivalResult {
            accepted: true,
            reason: String::new(),
        }
    }

    /// Persist an applied mutation receipt before exposing it to later deliveries.
    ///
    /// The caller must invoke this only after the vector mutation itself has
    /// completed.  A persistence failure is returned so delivery remains
    /// retryable instead of silently losing the idempotency receipt on restart.
    pub async fn record_application(
        &self,
        commit_lsn: CommitLsn,
        idempotency_key: &str,
    ) -> Result<(), String> {
        let _apply_guard = self.apply_lock.lock().await;
        let mut next = self.state.read().await.clone();
        next.receipts
            .insert(idempotency_key.to_string(), commit_lsn.get());
        next.applied_lsn = next.applied_lsn.max(commit_lsn.get());
        // LRU + water-level pruning: keep receipt set bounded. Entries whose
        // LSN is far below the current water-level are dropped; the SQLite
        // outbox `idempotency` table still guards against replay after a
        // restart for those older LSNs.
        if next.receipts.len() > VECTOR_RECEIPT_MAX_ENTRIES {
            let water_level = next
                .applied_lsn
                .saturating_sub(VECTOR_RECEIPT_RETENTION_LSN_WINDOW);
            next.receipts.retain(|_, lsn| *lsn >= water_level);
            // If still over capacity (e.g. bursty LSNs within window), drop
            // the oldest entries by LSN until under the limit.
            if next.receipts.len() > VECTOR_RECEIPT_MAX_ENTRIES {
                let mut entries: Vec<(String, u64)> =
                    next.receipts.iter().map(|(k, v)| (k.clone(), *v)).collect();
                entries.sort_by_key(|(_, lsn)| *lsn);
                let to_remove = entries.len() - VECTOR_RECEIPT_MAX_ENTRIES;
                for (key, _) in entries.into_iter().take(to_remove) {
                    next.receipts.remove(&key);
                }
            }
        }
        if !self.recovery_path.as_os_str().is_empty() {
            self.persist_state(&next).await?;
        }
        *self.state.write().await = next;
        Ok(())
    }

    pub async fn applied_lsn(&self) -> CommitLsn {
        CommitLsn::new(self.state.read().await.applied_lsn)
    }
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "vector")]
    use crate::VectorReceiver;
    #[cfg(feature = "vector")]
    use linkrs_core::types::CommitLsn;
    #[tokio::test]
    #[cfg(feature = "vector")]
    async fn vector_receiver_rejects_late_arrival() {
        let receiver = VectorReceiver::new();
        receiver
            .record_application(CommitLsn::new(100), "key-1")
            .await
            .expect("record receipt");

        let result = receiver
            .check_late_arrival(CommitLsn::new(50), "key-2")
            .await;
        assert!(!result.accepted);
        assert!(result.reason.contains("late arrival"));

        let result = receiver
            .check_late_arrival(CommitLsn::new(100), "key-3")
            .await;
        assert!(result.accepted);
        receiver
            .record_application(CommitLsn::new(100), "key-3")
            .await
            .expect("record receipt");

        let result = receiver
            .check_late_arrival(CommitLsn::new(200), "key-3")
            .await;
        assert!(!result.accepted);
        assert!(result.reason.contains("duplicate"));
    }

    #[tokio::test]
    #[cfg(feature = "vector")]
    async fn vector_receiver_restores_persisted_receipts() {
        let directory = tempfile::tempdir().expect("create temporary recovery directory");
        let receiver = VectorReceiver::open(directory.path());
        receiver
            .record_application(CommitLsn::new(100), "key-1")
            .await
            .expect("persist receipt");

        let recovered = VectorReceiver::open(directory.path());
        assert_eq!(recovered.applied_lsn().await, CommitLsn::new(100));
        let duplicate = recovered
            .check_late_arrival(CommitLsn::new(100), "key-1")
            .await;
        assert!(!duplicate.accepted);
    }
}