mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 15:26:17 +00:00
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>
This commit is contained in:
1 parent
3266ddda3a
commit
9ce35f90ff
11 files changed
+433
-52
No files matched your search
@@ -1,10 +1,10 @@
|
||||
use crate::error::{Error, Result};
|
||||
use crate::protocol::{SyncEvent, WireOp, hlc_actor};
|
||||
use crate::protocol::{SyncEvent, WireOp, hlc_actor, is_poison};
|
||||
use crate::remote::{Remote, RemoteConfig};
|
||||
use crate::state::{self, Cursors, SyncState, SyncStatus};
|
||||
use futures_util::StreamExt;
|
||||
use std::collections::BTreeSet;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
use std::time::Duration;
|
||||
use synapse_core::{Graph, Op};
|
||||
@@ -58,6 +58,16 @@ struct Inner {
|
||||
wake: Notify,
|
||||
/// Some synced assets may lack their blob locally.
|
||||
blobs_dirty: AtomicBool,
|
||||
quarantined: AtomicU64,
|
||||
}
|
||||
|
||||
/// Per-cycle counters.
|
||||
#[derive(Debug, Default)]
|
||||
struct Cycle {
|
||||
pushed: u64,
|
||||
pulled: u64,
|
||||
skipped_assets: u64,
|
||||
failed_blobs: u64,
|
||||
}
|
||||
|
||||
/// Replicates a local [`Graph`] with a Synapse server. Cheap to clone.
|
||||
@@ -82,6 +92,7 @@ impl SyncClient {
|
||||
dirty: AtomicBool::new(true),
|
||||
wake: Notify::new(),
|
||||
blobs_dirty: AtomicBool::new(true),
|
||||
quarantined: AtomicU64::new(0),
|
||||
});
|
||||
Ok(Self { inner })
|
||||
}
|
||||
@@ -140,7 +151,7 @@ impl SyncClient {
|
||||
.await
|
||||
.unwrap_or(0);
|
||||
match &res {
|
||||
Ok((pushed, pulled)) => {
|
||||
Ok(c) => {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_millis() as i64)
|
||||
@@ -150,8 +161,11 @@ impl SyncClient {
|
||||
s.message = None;
|
||||
s.pending_ops = pending;
|
||||
s.last_sync_ms = Some(now);
|
||||
s.last_pushed = *pushed;
|
||||
s.last_pulled = *pulled;
|
||||
s.last_pushed = c.pushed;
|
||||
s.last_pulled = c.pulled;
|
||||
s.skipped_assets = c.skipped_assets;
|
||||
s.failed_blobs = c.failed_blobs;
|
||||
s.quarantined_ops = self.inner.quarantined.load(Ordering::SeqCst);
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -165,13 +179,14 @@ impl SyncClient {
|
||||
s.status = status;
|
||||
s.message = Some(e.to_string());
|
||||
s.pending_ops = pending;
|
||||
s.quarantined_ops = self.inner.quarantined.load(Ordering::SeqCst);
|
||||
});
|
||||
}
|
||||
}
|
||||
res.map(|_| ())
|
||||
}
|
||||
|
||||
async fn run_cycle(&self) -> Result<(u64, u64)> {
|
||||
async fn run_cycle(&self) -> Result<Cycle> {
|
||||
let remote = &self.inner.remote;
|
||||
let info = remote.ensure_graph().await?;
|
||||
let key = remote.key();
|
||||
@@ -180,26 +195,30 @@ impl SyncClient {
|
||||
.with_graph(move |g| Ok(state::load(g, &key, &server_id)?))
|
||||
.await?;
|
||||
|
||||
let pushed = self.push_all(&mut cur).await?;
|
||||
let mut stats = Cycle::default();
|
||||
self.push_all(&mut cur, &mut stats).await?;
|
||||
let (pulled, got_assets) = self.pull_all(&mut cur, &info.server_id).await?;
|
||||
stats.pulled = pulled;
|
||||
if got_assets {
|
||||
self.inner.blobs_dirty.store(true, Ordering::SeqCst);
|
||||
}
|
||||
if self.inner.cfg.blob_policy == BlobPolicy::Eager
|
||||
&& self.inner.blobs_dirty.load(Ordering::SeqCst)
|
||||
{
|
||||
self.fetch_missing_blobs().await?;
|
||||
self.inner.blobs_dirty.store(false, Ordering::SeqCst);
|
||||
stats.failed_blobs = self.fetch_missing_blobs().await?;
|
||||
// Failed blobs are retried on the next cycle.
|
||||
self.inner
|
||||
.blobs_dirty
|
||||
.store(stats.failed_blobs > 0, Ordering::SeqCst);
|
||||
}
|
||||
Ok((pushed, pulled))
|
||||
Ok(stats)
|
||||
}
|
||||
|
||||
/// Pushes ops originated by this replica. Ops that were *applied from the server* are
|
||||
/// re-logged locally by the core, but carry another actor in their HLC, so they are skipped
|
||||
/// here: that is what prevents echoing them back.
|
||||
async fn push_all(&self, cur: &mut Cursors) -> Result<u64> {
|
||||
async fn push_all(&self, cur: &mut Cursors, stats: &mut Cycle) -> Result<()> {
|
||||
let limit = self.inner.cfg.batch_size;
|
||||
let mut total = 0;
|
||||
loop {
|
||||
let after = cur.pushed_seq;
|
||||
let (batch, scanned_to, scanned) = self
|
||||
@@ -220,10 +239,13 @@ impl SyncClient {
|
||||
})
|
||||
.await?;
|
||||
if scanned == 0 {
|
||||
return Ok(total);
|
||||
return Ok(());
|
||||
}
|
||||
let mut batch = batch;
|
||||
if !batch.is_empty() {
|
||||
// Blobs first, so the server never knows an asset it cannot serve.
|
||||
// Blobs first, so the server never knows an asset it cannot serve. An asset
|
||||
// whose blob is missing locally can never be uploaded: skip its op (and warn)
|
||||
// instead of blocking every later op of this device.
|
||||
let hashes: BTreeSet<String> = batch
|
||||
.iter()
|
||||
.filter_map(|w| match &w.op {
|
||||
@@ -231,17 +253,38 @@ impl SyncClient {
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let mut missing = BTreeSet::new();
|
||||
for h in hashes {
|
||||
self.upload_blob(&h).await?;
|
||||
if !self.upload_blob(&h).await? {
|
||||
missing.insert(h);
|
||||
}
|
||||
}
|
||||
if !missing.is_empty() {
|
||||
let before = batch.len();
|
||||
batch.retain(|w| match &w.op {
|
||||
Op::PutAsset { asset } => !missing.contains(&asset.hash),
|
||||
_ => true,
|
||||
});
|
||||
stats.skipped_assets += (before - batch.len()) as u64;
|
||||
}
|
||||
if !batch.is_empty() {
|
||||
stats.pushed += batch.len() as u64;
|
||||
let resp = self.inner.remote.push(batch).await?;
|
||||
if !resp.rejected.is_empty() {
|
||||
self.inner
|
||||
.quarantined
|
||||
.fetch_add(resp.rejected.len() as u64, Ordering::SeqCst);
|
||||
for r in &resp.rejected {
|
||||
warn!(index = r.index, reason = %r.reason, "server rejected a local op");
|
||||
}
|
||||
}
|
||||
}
|
||||
total += batch.len() as u64;
|
||||
self.inner.remote.push(batch).await?;
|
||||
}
|
||||
self.with_graph(move |g| Ok(state::save_pushed(g, scanned_to)?))
|
||||
.await?;
|
||||
cur.pushed_seq = scanned_to;
|
||||
if (scanned as u32) < limit {
|
||||
return Ok(total);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -262,23 +305,39 @@ impl SyncClient {
|
||||
}
|
||||
let next = resp.next_cursor.max(cur.pull_cursor);
|
||||
let me2 = me.clone();
|
||||
let (applied, assets) = self
|
||||
let (applied, assets, quarantined) = self
|
||||
.with_graph(move |g| {
|
||||
let (mut applied, mut assets) = (0u64, false);
|
||||
let (mut applied, mut assets, mut quarantined) = (0u64, false, 0u64);
|
||||
for rec in resp.ops {
|
||||
// Defence in depth: never re-apply our own ops.
|
||||
if hlc_actor(&rec.hlc) == Some(me2.as_str()) {
|
||||
continue;
|
||||
}
|
||||
if g.apply_remote(&rec.hlc, &rec.op)? {
|
||||
applied += 1;
|
||||
assets |= matches!(rec.op, Op::PutAsset { .. });
|
||||
match g.apply_remote(&rec.hlc, &rec.op) {
|
||||
Ok(true) => {
|
||||
applied += 1;
|
||||
assets |= matches!(rec.op, Op::PutAsset { .. });
|
||||
}
|
||||
Ok(false) => {}
|
||||
// An op that can never apply must not wedge the pull forever:
|
||||
// park it and move on. Storage errors stay fatal (retried).
|
||||
Err(e) if is_poison(&e) => {
|
||||
warn!(seq = rec.seq, hlc = %rec.hlc, error = %e, "quarantined remote op");
|
||||
let payload = serde_json::to_string(&rec.op)
|
||||
.map_err(synapse_core::Error::from)?;
|
||||
state::quarantine(g, rec.seq, &rec.hlc, &payload, &e.to_string())?;
|
||||
quarantined += 1;
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
}
|
||||
state::save_pulled(g, next)?;
|
||||
Ok((applied, assets))
|
||||
Ok((applied, assets, quarantined))
|
||||
})
|
||||
.await?;
|
||||
self.inner
|
||||
.quarantined
|
||||
.fetch_add(quarantined, Ordering::SeqCst);
|
||||
cur.pull_cursor = next;
|
||||
applied_total += applied;
|
||||
got_assets |= assets;
|
||||
@@ -290,15 +349,16 @@ impl SyncClient {
|
||||
|
||||
// ----------------------------------------------------------------- blobs
|
||||
|
||||
async fn upload_blob(&self, hash: &str) -> Result<()> {
|
||||
/// Uploads the blob of a local asset. `Ok(false)` if the blob is missing locally.
|
||||
async fn upload_blob(&self, hash: &str) -> Result<bool> {
|
||||
let h = hash.to_string();
|
||||
let path = self.with_graph(move |g| Ok(g.blobs().path_of(&h))).await?;
|
||||
if !path.exists() {
|
||||
return Err(Error::Protocol(format!(
|
||||
"asset {hash} is registered but its blob is missing locally"
|
||||
)));
|
||||
warn!(%hash, "asset registered but its blob is missing locally; not pushing it");
|
||||
return Ok(false);
|
||||
}
|
||||
self.inner.remote.upload_blob(hash, &path).await
|
||||
self.inner.remote.upload_blob(hash, &path).await?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Downloads blob `hash` if it is not stored locally yet (resumes partial downloads).
|
||||
@@ -332,7 +392,10 @@ impl SyncClient {
|
||||
Err(last.unwrap())
|
||||
}
|
||||
|
||||
async fn fetch_missing_blobs(&self) -> Result<()> {
|
||||
/// Downloads every asset blob we lack. A blob that fails (e.g. 404) is logged and left
|
||||
/// for the next cycle; the others still download. Returns how many failed. Connectivity
|
||||
/// errors abort the cycle since every other blob would fail too.
|
||||
async fn fetch_missing_blobs(&self) -> Result<u64> {
|
||||
let missing = self
|
||||
.with_graph(|g| {
|
||||
let mut st = g.conn().prepare("SELECT hash FROM assets")?;
|
||||
@@ -341,15 +404,29 @@ impl SyncClient {
|
||||
.collect::<std::result::Result<Vec<_>, _>>()?;
|
||||
Ok(hashes
|
||||
.into_iter()
|
||||
.filter(|h| h.len() > 2 && !g.blobs().contains(h))
|
||||
.filter(|h| {
|
||||
let valid = crate::protocol::is_valid_hash(h);
|
||||
if !valid {
|
||||
warn!(hash = %h, "ignoring asset with an invalid blob hash");
|
||||
}
|
||||
valid && !g.blobs().contains(h)
|
||||
})
|
||||
.collect::<Vec<_>>())
|
||||
})
|
||||
.await?;
|
||||
let mut failed = 0;
|
||||
for h in missing {
|
||||
debug!(hash = %h, "downloading blob");
|
||||
self.ensure_blob(&h).await?;
|
||||
match self.ensure_blob(&h).await {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.is_offline() || matches!(e, Error::Unauthorized) => return Err(e),
|
||||
Err(e) => {
|
||||
warn!(hash = %h, error = %e, "blob download failed; will retry next cycle");
|
||||
failed += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
Ok(failed)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ background
|
||||
|
||||
@@ -38,6 +38,17 @@ pub struct PushResponse {
|
||||
/// Ops that changed server state (the rest lost LWW or were already known).
|
||||
pub applied: u32,
|
||||
pub last_seq: i64,
|
||||
/// Ops refused (invalid, or failing to apply). They are skipped, never stored; the rest of
|
||||
/// the batch is processed normally so one bad op cannot wedge a device.
|
||||
#[serde(default)]
|
||||
pub rejected: Vec<Rejection>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Rejection {
|
||||
/// Index of the op in the pushed batch.
|
||||
pub index: u32,
|
||||
pub reason: String,
|
||||
}
|
||||
|
||||
/// `GET /v1/graphs/{id}/ops?since=&limit=&exclude_actor=`
|
||||
@@ -103,6 +114,37 @@ pub fn is_valid_hash(h: &str) -> bool {
|
||||
h.len() == 64 && h.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
|
||||
}
|
||||
|
||||
/// Structural checks for an op received from a peer, before it touches the graph.
|
||||
/// `max_future_ms` bounds how far ahead of `now_ms` its HLC may be (clock-skew guard).
|
||||
pub fn validate_op(hlc: &str, op: &Op, now_ms: i64, max_future_ms: i64) -> Result<(), String> {
|
||||
let (ms, _) = synapse_core::hlc::decode(hlc).ok_or("malformed hlc")?;
|
||||
if hlc_actor(hlc).is_none() {
|
||||
return Err("hlc has no actor".into());
|
||||
}
|
||||
if ms > now_ms.saturating_add(max_future_ms) {
|
||||
return Err(format!(
|
||||
"hlc is {} s in the future (limit {} s)",
|
||||
(ms - now_ms) / 1000,
|
||||
max_future_ms / 1000
|
||||
));
|
||||
}
|
||||
if let Op::PutAsset { asset } = op
|
||||
&& !is_valid_hash(&asset.hash)
|
||||
{
|
||||
return Err("asset hash must be 64 lowercase hex chars".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Errors that make one op permanently unappliable (as opposed to transient storage errors).
|
||||
pub fn is_poison(e: &synapse_core::Error) -> bool {
|
||||
use synapse_core::Error as E;
|
||||
matches!(
|
||||
e,
|
||||
E::Invalid(_) | E::Json(_) | E::Conflict(_) | E::NotFound(_)
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -116,5 +158,21 @@ mod tests {
|
||||
assert!(!is_valid_graph_id(""));
|
||||
assert!(is_valid_hash(&"a".repeat(64)));
|
||||
assert!(!is_valid_hash("ab/../"));
|
||||
let op = Op::DeleteBlock {
|
||||
id: synapse_core::new_id(),
|
||||
};
|
||||
assert!(validate_op("0000000001000-00000-a", &op, 1000, 0).is_ok());
|
||||
assert!(validate_op("0000000999000-00000-a", &op, 1000, 60_000).is_err());
|
||||
assert!(validate_op("junk", &op, 1000, 0).is_err());
|
||||
let bad = Op::PutAsset {
|
||||
asset: synapse_core::Asset {
|
||||
hash: "../x".into(),
|
||||
size: 1,
|
||||
mime: None,
|
||||
original_name: None,
|
||||
created_at: 0,
|
||||
},
|
||||
};
|
||||
assert!(validate_op("0000000001000-00000-a", &bad, 1000, 0).is_err());
|
||||
}
|
||||
}
|
||||
@@ -186,10 +186,23 @@ impl Remote {
|
||||
/// has it and resuming a partial upload. The file is streamed, never loaded in memory.
|
||||
pub async fn upload_blob(&self, hash: &str, path: &Path) -> Result<()> {
|
||||
let size = tokio::fs::metadata(path).await?.len();
|
||||
for _ in 0..3 {
|
||||
// Each round resumes from what the server holds; a 202 (body ended short) or a 409
|
||||
// (stale offset) just means "ask again". Success is only a HEAD that finds the blob.
|
||||
let mut stalled = 0;
|
||||
let mut last_offset = None;
|
||||
loop {
|
||||
let Some(offset) = self.blob_upload_offset(hash).await? else {
|
||||
return Ok(());
|
||||
};
|
||||
if last_offset.is_some_and(|l| offset <= l) {
|
||||
stalled += 1;
|
||||
if stalled >= 3 {
|
||||
return Err(Error::Protocol(format!(
|
||||
"blob {hash}: upload makes no progress at offset {offset}"
|
||||
)));
|
||||
}
|
||||
}
|
||||
last_offset = Some(offset);
|
||||
let offset = if offset > size { 0 } else { offset };
|
||||
let mut file = tokio::fs::File::open(path).await?;
|
||||
file.seek(std::io::SeekFrom::Start(offset)).await?;
|
||||
@@ -205,15 +218,13 @@ impl Remote {
|
||||
.body(body)
|
||||
.send()
|
||||
.await?;
|
||||
if resp.status() == StatusCode::CONFLICT {
|
||||
continue; // offset out of date: HEAD again
|
||||
match resp.status() {
|
||||
StatusCode::CONFLICT | StatusCode::ACCEPTED => continue,
|
||||
_ => {
|
||||
Self::check(resp).await?;
|
||||
}
|
||||
}
|
||||
Self::check(resp).await?;
|
||||
return Ok(());
|
||||
}
|
||||
Err(Error::Protocol(format!(
|
||||
"blob {hash}: upload offset keeps changing"
|
||||
)))
|
||||
}
|
||||
|
||||
/// Downloads blob `hash` into `dest`, streaming through `part` (resumed if it exists), and
|
||||
|
||||
@@ -27,6 +27,13 @@ pub struct SyncState {
|
||||
/// 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 {
|
||||
@@ -38,6 +45,9 @@ impl Default for SyncState {
|
||||
last_sync_ms: None,
|
||||
last_pushed: 0,
|
||||
last_pulled: 0,
|
||||
quarantined_ops: 0,
|
||||
skipped_assets: 0,
|
||||
failed_blobs: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,10 +62,32 @@ pub struct Cursors {
|
||||
|
||||
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_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)
|
||||
|
||||
Reference in new issue
Block a user