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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! Durable outbox administration: configuration, snapshots, dead letters,
//! degraded ranges and retention.
use super::*;
use crate::checkpoint_manifest::CheckpointManifestManager;
use crate::sqlite_outbox::OutboxSnapshot;
use linkrs_core::types::CommitLsn;
use std::path::Path;
fn latest_manifest_outbox_snapshot(work_dir: &Path) -> Result<Option<OutboxSnapshot>, String> {
    let manifest_manager =
        CheckpointManifestManager::new(work_dir.join("checkpoint").join("manifests"));
    let Some(manifest) = manifest_manager.load_latest()? else {
        return Ok(None);
    };
    Ok(manifest.outbox_snapshot.map(|snapshot| OutboxSnapshot {
        path: snapshot.path,
        size_bytes: snapshot.size_bytes,
        checksum: snapshot.checksum,
        materialized_lsn: snapshot.materialized_lsn,
    }))
}
fn restore_outbox_from_candidates(
    live_path: &Path,
    snapshot_dir: &Path,
    preferred_snapshot: Option<&OutboxSnapshot>,
) -> Result<Option<CommitLsn>, String> {
    let mut preferred_error = None;
    if let Some(snapshot) = preferred_snapshot {
        match crate::restore_snapshot_sync(snapshot, live_path) {
            Ok(()) => return Ok(Some(snapshot.materialized_lsn)),
            Err(error) => {
                log::warn!(
                    "Failed to restore manifest-referenced outbox snapshot {}: {}",
                    snapshot.path.display(),
                    error
                );
                preferred_error = Some(error);
            }
        }
    }

    if snapshot_dir.is_dir() {
        return crate::restore_latest_snapshot(live_path, snapshot_dir).map(Some);
    }

    preferred_error.map_or(Ok(None), Err)
}
#[cfg_attr(
    not(any(feature = "fulltext", feature = "vector")),
    allow(unused_variables)
)]
impl super::SyncManager {
    pub fn configure_outbox(&mut self, path: impl AsRef<std::path::Path>) -> Result<(), SyncError> {
        let path = path.as_ref();
        let sqlite_path = if path
            .extension()
            .is_some_and(|extension| extension == "sqlite")
        {
            path.to_path_buf()
        } else {
            path.with_extension("sqlite")
        };

        let database_parent = sqlite_path.parent().unwrap_or(Path::new("."));
        let snapshot_dir = database_parent
            .parent()
            .unwrap_or(database_parent)
            .join("outbox_snapshots");
        let work_dir = database_parent.parent().unwrap_or(database_parent);
        let preferred_snapshot =
            latest_manifest_outbox_snapshot(work_dir).map_err(SyncError::PersistenceError)?;

        // Validate the live database before opening it. A file can exist while
        // still being an incomplete or corrupt SQLite projection after a crash.
        // Restore the snapshot referenced by the latest valid combined
        // checkpoint first, then fall back to the directory-wide snapshot scan.
        let live_is_healthy =
            self.execute_sync(|| async { Ok(crate::verify_live_database(&sqlite_path).await) })?;
        if !live_is_healthy {
            match restore_outbox_from_candidates(
                &sqlite_path,
                &snapshot_dir,
                preferred_snapshot.as_ref(),
            ) {
                Ok(Some(lsn)) => {
                    log::info!("Recovered outbox from snapshot at LSN {}", lsn.get());
                }
                Ok(None) => {}
                Err(error) => {
                    log::warn!(
                        "Outbox recovery attempted but failed: {}. Starting fresh.",
                        error
                    );
                }
            }
        }

        let open_outbox = || {
            self.execute_sync(|| async {
                SqliteOutbox::open(&sqlite_path)
                    .await
                    .map_err(SyncError::PersistenceError)
            })
        };
        let outbox = match open_outbox() {
            Ok(outbox) => outbox,
            Err(error) if snapshot_dir.is_dir() => {
                log::warn!(
                    "Failed to open outbox {}: {}. Restoring the latest snapshot.",
                    sqlite_path.display(),
                    error
                );
                restore_outbox_from_candidates(
                    &sqlite_path,
                    &snapshot_dir,
                    preferred_snapshot.as_ref(),
                )
                .map_err(SyncError::PersistenceError)?
                .ok_or_else(|| {
                    SyncError::PersistenceError(format!(
                        "No valid outbox snapshot found in {}",
                        snapshot_dir.display()
                    ))
                })?;
                open_outbox()?
            }
            Err(error) => return Err(error),
        };
        let outbox_arc = Arc::new(outbox);
        self.sqlite_outbox = Some(outbox_arc.clone());
        #[cfg(feature = "vector")]
        {
            self.vector_receiver = Some(Arc::new(crate::VectorReceiver::open(
                work_dir.join("vector_receiver"),
            )));
            if let Some(coord) = self.vector_coordinator.as_ref() {
                coord.set_outbox(outbox_arc.clone());
            }
        }
        Ok(())
    }

    pub fn configure_outbox_consumer(&mut self, config: OutboxConsumerConfig) {
        self.outbox_consumer = Arc::new(OutboxConsumerConfig {
            consumer_id: if config.consumer_id.is_empty() {
                OutboxConsumerConfig::default().consumer_id
            } else {
                config.consumer_id
            },
            batch_size: config.batch_size.max(1),
            lease_duration_ms: config.lease_duration_ms.max(1_000),
            max_retries: config.max_retries.max(1),
            max_concurrency: config.max_concurrency.max(1),
        });
    }

    /// Inject a backend-aware delivery policy (Local vs Qdrant) from startup.
    ///
    /// The policy replaces the raw `OutboxConsumerConfig` with backend-tuned
    /// values and records the chosen policy for observability.
    #[cfg(feature = "vector")]
    pub fn configure_backend_policy(&mut self, policy: crate::backend::BackendDeliveryPolicy) {
        self.backend_policy = Some(Arc::new(policy.clone()));
        self.configure_outbox_consumer(policy.to_consumer_config());
        // Align the per-target concurrency with the policy.
        let mut consumer = (*self.outbox_consumer).clone();
        consumer.max_concurrency = policy.max_concurrency.max(1);
        self.outbox_consumer = Arc::new(consumer);
    }

    /// Override the backpressure limits (defaults: 10k per txn, 100k total).
    pub fn configure_backpressure(&mut self, config: OutboxBackpressureConfig) {
        self.backpressure = Arc::new(OutboxBackpressureConfig {
            max_pending_per_txn: config.max_pending_per_txn.max(1),
            max_pending_total: config.max_pending_total.max(1),
        });
    }

    /// Current in-memory staged intent count across all transactions.
    pub fn staged_pending_len(&self) -> usize {
        self.pending_intents.iter().map(|e| e.value().len()).sum()
    }

    /// Expose the current durable + staged pending depth for storage-layer
    /// flow control. Returns `0` when no outbox is configured.
    pub fn outbox_pending(&self) -> usize {
        let staged = self.staged_pending_len();
        if let Some(outbox) = self.sqlite_outbox.clone() {
            let durable = crate::runtime::block_on_ambient(async move {
                outbox.stats().await.ok().map(|s| s.pending).unwrap_or(0)
            })
            .unwrap_or(0);
            staged + durable
        } else {
            staged
        }
    }

    pub fn outbox_stats(&self) -> crate::OutboxStats {
        let Some(outbox) = self.sqlite_outbox.clone() else {
            return crate::OutboxStats {
                pending: 0,
                ..Default::default()
            };
        };
        match crate::runtime::block_on_ambient(async move { outbox.stats().await }) {
            Ok(Ok(stats)) => stats,
            _ => crate::OutboxStats {
                pending: 0,
                ..Default::default()
            },
        }
    }

    /// Return durable outbox delivery and index-generation diagnostics.
    pub fn sync_diagnostics(&self) -> Result<crate::SyncDiagnostics, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        #[allow(unused_mut)]
        let mut diagnostics = self.execute_sync(|| async {
            outbox
                .diagnostics()
                .await
                .map_err(SyncError::PersistenceError)
        })?;
        #[cfg(feature = "vector")]
        if let Some(coord) = self.vector_coordinator.as_ref() {
            diagnostics.vector_disabled_skips = coord.disabled_skip_count();
        }
        Ok(diagnostics)
    }

    /// Create a crash-safe immutable snapshot of the SQLite projection.
    pub fn create_outbox_snapshot(
        &self,
        destination: impl AsRef<Path>,
    ) -> Result<crate::OutboxSnapshot, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        let destination = destination.as_ref().to_path_buf();
        self.execute_sync(|| async {
            outbox
                .create_snapshot(destination)
                .await
                .map_err(SyncError::PersistenceError)
        })
    }

    pub fn wait_for_minimum_lsn(
        &self,
        target: &linkrs_core::types::TargetId,
        index_id: u64,
        generation: u64,
        minimum_lsn: linkrs_core::types::CommitLsn,
        timeout_ms: u64,
    ) -> Result<bool, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        self.execute_sync(|| async {
            outbox
                .wait_for_minimum_lsn(target, index_id, generation, minimum_lsn, timeout_ms)
                .await
                .map_err(SyncError::PersistenceError)
        })
    }

    pub fn create_checkpoint_outbox_snapshot(&self) -> Result<crate::OutboxSnapshot, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        let materialized_lsn = self.execute_sync(|| async {
            outbox
                .materialized_lsn()
                .await
                .map_err(SyncError::PersistenceError)
        })?;
        let database_parent = outbox.path().parent().ok_or_else(|| {
            SyncError::PersistenceError("SQLite outbox path has no parent".to_string())
        })?;
        let work_dir = database_parent.parent().unwrap_or(database_parent);
        let destination = work_dir
            .join("outbox_snapshots")
            .join(format!("outbox_snapshot_{}.sqlite", materialized_lsn.get()));
        self.create_outbox_snapshot(destination)
    }

    pub fn verify_outbox_snapshot(snapshot: &crate::OutboxSnapshot) -> Result<(), SyncError> {
        crate::SqliteOutbox::verify_snapshot(snapshot).map_err(SyncError::PersistenceError)
    }

    pub fn requeue_dead_letter(&self, event_id: i64) -> Result<bool, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        self.execute_sync(|| async {
            outbox
                .requeue_dead_letter(event_id)
                .await
                .map_err(SyncError::PersistenceError)
        })
    }

    pub fn requeue_dead_letters_batch(
        &self,
        target: Option<&linkrs_core::types::TargetId>,
        index_id: Option<u64>,
        generation: Option<u64>,
        limit: usize,
    ) -> Result<usize, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        let outbox = outbox.clone();
        let target = target.cloned();
        self.execute_sync(move || {
            let outbox = outbox.clone();
            let target = target.clone();
            async move {
                outbox
                    .requeue_dead_letters_batch(target.as_ref(), index_id, generation, limit)
                    .await
                    .map_err(SyncError::PersistenceError)
            }
        })
    }

    pub fn list_dead_letters(
        &self,
        target: Option<&linkrs_core::types::TargetId>,
        index_id: Option<u64>,
        generation: Option<u64>,
        limit: usize,
        offset: usize,
    ) -> Result<Vec<crate::DeadLetterRow>, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        let outbox = outbox.clone();
        let target = target.cloned();
        self.execute_sync(move || {
            let outbox = outbox.clone();
            let target = target.clone();
            async move {
                outbox
                    .list_dead_letters(target.as_ref(), index_id, generation, limit, offset)
                    .await
                    .map_err(SyncError::PersistenceError)
            }
        })
    }

    pub fn list_degraded_ranges(
        &self,
        target: Option<&linkrs_core::types::TargetId>,
        index_id: Option<u64>,
        generation: Option<u64>,
    ) -> Result<Vec<crate::DegradedRangeRow>, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        let outbox = outbox.clone();
        let target = target.cloned();
        self.execute_sync(move || {
            let outbox = outbox.clone();
            let target = target.clone();
            async move {
                outbox
                    .list_degraded_ranges(target.as_ref(), index_id, generation)
                    .await
                    .map_err(SyncError::PersistenceError)
            }
        })
    }

    pub fn clear_degraded_range(
        &self,
        target: &linkrs_core::types::TargetId,
        index_id: u64,
        generation: u64,
        start_lsn: linkrs_core::types::CommitLsn,
        end_lsn: linkrs_core::types::CommitLsn,
    ) -> Result<bool, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        let outbox = outbox.clone();
        let target = target.clone();
        self.execute_sync(move || {
            let outbox = outbox.clone();
            let target = target.clone();
            async move {
                outbox
                    .clear_degraded_range(&target, index_id, generation, start_lsn, end_lsn)
                    .await
                    .map_err(SyncError::PersistenceError)
            }
        })
    }

    pub fn retention_lsn(&self) -> Result<linkrs_core::types::CommitLsn, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        self.execute_sync(|| async {
            outbox
                .retention_lsn()
                .await
                .map_err(SyncError::PersistenceError)
        })
    }

    pub fn update_retention_lsn(
        &self,
        retention_lsn: linkrs_core::types::CommitLsn,
    ) -> Result<(), SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Err(SyncError::PersistenceError(
                "SQLite outbox is not configured".to_string(),
            ));
        };
        let outbox = outbox.clone();
        self.execute_sync(move || {
            let outbox = outbox.clone();
            async move {
                outbox
                    .update_retention_lsn(retention_lsn)
                    .await
                    .map_err(SyncError::PersistenceError)
            }
        })
    }

    pub fn prune_applied_events(
        &self,
        retention_lsn: linkrs_core::types::CommitLsn,
    ) -> Result<u64, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Ok(0);
        };
        let outbox = outbox.clone();
        self.execute_sync(move || {
            let outbox = outbox.clone();
            async move {
                outbox
                    .prune_applied_events(retention_lsn)
                    .await
                    .map_err(SyncError::PersistenceError)
            }
        })
    }

    pub fn archive_dead_letters(
        &self,
        retention_lsn: linkrs_core::types::CommitLsn,
    ) -> Result<u64, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Ok(0);
        };
        let outbox = outbox.clone();
        self.execute_sync(move || {
            let outbox = outbox.clone();
            async move {
                outbox
                    .archive_dead_letters(retention_lsn)
                    .await
                    .map_err(SyncError::PersistenceError)
            }
        })
    }

    pub fn prune_degraded_ranges(&self, max_age_ms: u64) -> Result<u64, SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Ok(0);
        };
        let outbox = outbox.clone();
        self.execute_sync(move || {
            let outbox = outbox.clone();
            async move {
                outbox
                    .prune_degraded_ranges(max_age_ms)
                    .await
                    .map_err(SyncError::PersistenceError)
            }
        })
    }

    /// Run one retention cycle: prune applied, archive dead letters, update retention watermark.
    pub fn run_retention_once(
        &self,
        grace_lsn_distance: u64,
        max_age_ms: u64,
    ) -> Result<(u64, u64, u64), SyncError> {
        let Some(outbox) = &self.sqlite_outbox else {
            return Ok((0, 0, 0));
        };
        let outbox = outbox.clone();
        self.execute_sync(move || {
            let outbox = outbox.clone();
            async move {
                let safe = outbox
                    .compute_safe_retention_lsn(grace_lsn_distance)
                    .await
                    .map_err(SyncError::PersistenceError)?;
                let pruned = outbox
                    .prune_applied_events(safe)
                    .await
                    .map_err(SyncError::PersistenceError)?;
                let archived = outbox
                    .archive_dead_letters(safe)
                    .await
                    .map_err(SyncError::PersistenceError)?;
                outbox
                    .update_retention_lsn(safe)
                    .await
                    .map_err(SyncError::PersistenceError)?;
                let _ = outbox
                    .prune_degraded_ranges(max_age_ms)
                    .await
                    .map_err(SyncError::PersistenceError)?;
                Ok((pruned, archived, safe.get()))
            }
        })
    }
}