Files
Synapse/crates/synapse-sync/src/state.rs
T
dasemuandClaude Sonnet 5.5 9ce35f90ff fix(sync): robust blob handling, op validation and quarantine
- upload_blob resumes until the server confirms the blob (202/409 are not success)
- missing local blob skips its PutAsset instead of blocking all later ops
- failed blob downloads are counted and retried, not fatal to the cycle
- server validates pushed ops (hash, hlc, clock skew) and returns per-op rejections
- pulled ops that can never apply are quarantined; storage errors stay fatal
- SYNAPSE_MAX_CLOCK_SKEW_MINUTES; document Arc<Mutex<Graph>> handle for the FFI

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 14:38:58 +02:00

158 lines
5.2 KiB
Rust

//! Persistent sync cursors (table `sync_state`) and the UI-facing state.
use rusqlite::{Connection, OptionalExtension, params};
use serde::Serialize;
use synapse_core::Graph;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SyncStatus {
Idle,
Syncing,
/// Server unreachable; local edits are kept and pushed once it is back.
Offline,
/// The server answered but something failed (auth, protocol, disk...). See `message`.
Error,
}
/// What the UI shows about sync.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct SyncState {
pub status: SyncStatus,
pub message: Option<String>,
/// Local ops not yet pushed to the server.
pub pending_ops: u64,
/// Wall-clock millis of the last fully successful cycle.
pub last_sync_ms: Option<i64>,
/// Ops pushed / pulled(applied) in the last successful cycle.
pub last_pushed: u64,
pub last_pulled: u64,
/// Ops refused by the server or skipped locally because they can never apply (this
/// session). Details are in the log and, for pulled ops, in table `sync_quarantine`.
pub quarantined_ops: u64,
/// Local assets whose blob is missing: their `PutAsset` op was not pushed.
pub skipped_assets: u64,
/// Blobs that could not be downloaded in the last cycle (retried next cycle).
pub failed_blobs: u64,
}
impl Default for SyncState {
fn default() -> Self {
Self {
status: SyncStatus::Idle,
message: None,
pending_ops: 0,
last_sync_ms: None,
last_pushed: 0,
last_pulled: 0,
quarantined_ops: 0,
skipped_assets: 0,
failed_blobs: 0,
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Cursors {
/// Highest local op seq already pushed (or skipped because it came from the server).
pub pushed_seq: i64,
/// Highest server op seq already pulled.
pub pull_cursor: i64,
}
pub(crate) fn init(conn: &Connection) -> rusqlite::Result<()> {
conn.execute_batch(
"CREATE TABLE IF NOT EXISTS sync_state (key TEXT PRIMARY KEY, value TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS sync_quarantine (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_seq INTEGER NOT NULL,
hlc TEXT NOT NULL,
payload TEXT NOT NULL,
reason TEXT NOT NULL
);",
)
}
/// Records a pulled op that can never be applied, so it is skipped instead of blocking sync.
pub(crate) fn quarantine(
g: &Graph,
server_seq: i64,
hlc: &str,
payload: &str,
reason: &str,
) -> rusqlite::Result<()> {
g.conn().execute(
"INSERT INTO sync_quarantine(server_seq, hlc, payload, reason) VALUES (?1, ?2, ?3, ?4)",
params![server_seq, hlc, payload, reason],
)?;
Ok(())
}
fn get(conn: &Connection, key: &str) -> rusqlite::Result<Option<String>> {
conn.query_row("SELECT value FROM sync_state WHERE key=?1", [key], |r| {
r.get(0)
})
.optional()
}
fn set(conn: &Connection, key: &str, value: &str) -> rusqlite::Result<()> {
conn.execute(
"INSERT INTO sync_state(key, value) VALUES (?1, ?2)
ON CONFLICT(key) DO UPDATE SET value=?2",
params![key, value],
)?;
Ok(())
}
/// Loads the cursors for `(remote_key, server_id)`. If the graph is now synced with another
/// server/graph, or the server graph was recreated, cursors restart from zero (re-pushing is
/// safe: the server merges with last-writer-wins and ignores what it already has).
pub(crate) fn load(g: &Graph, remote_key: &str, server_id: &str) -> rusqlite::Result<Cursors> {
let c = g.conn();
init(c)?;
let same = get(c, "remote_key")?.as_deref() == Some(remote_key)
&& get(c, "server_id")?.as_deref() == Some(server_id);
if !same {
set(c, "remote_key", remote_key)?;
set(c, "server_id", server_id)?;
set(c, "pushed_seq", "0")?;
set(c, "pull_cursor", "0")?;
return Ok(Cursors::default());
}
let num =
|k| -> rusqlite::Result<i64> { Ok(get(c, k)?.and_then(|v| v.parse().ok()).unwrap_or(0)) };
Ok(Cursors {
pushed_seq: num("pushed_seq")?,
pull_cursor: num("pull_cursor")?,
})
}
pub(crate) fn save_pushed(g: &Graph, seq: i64) -> rusqlite::Result<()> {
set(g.conn(), "pushed_seq", &seq.to_string())
}
pub(crate) fn save_pulled(g: &Graph, cursor: i64) -> rusqlite::Result<()> {
set(g.conn(), "pull_cursor", &cursor.to_string())
}
/// Number of local-origin ops after `pushed_seq`.
pub(crate) fn pending(g: &Graph, pushed_seq: i64) -> rusqlite::Result<u64> {
let pattern = format!("%-{}", g.actor());
g.conn()
.query_row(
"SELECT count(*) FROM ops WHERE seq > ?1 AND hlc LIKE ?2",
params![pushed_seq, pattern],
|r| r.get::<_, i64>(0),
)
.map(|n| n as u64)
}
/// Pending count using the persisted cursor (works before the first successful sync too).
pub(crate) fn pending_now(g: &Graph) -> rusqlite::Result<u64> {
init(g.conn())?;
let pushed = get(g.conn(), "pushed_seq")?
.and_then(|v| v.parse().ok())
.unwrap_or(0);
pending(g, pushed)
}