use std::sync::Arc;
use anyhow::Context;
use axum::Json;
use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use second_brain_core::schema::SyncOp;
use second_brain_core::sqlite_store::BelowFloor;
use second_brain_sync::crypto::seal_line;
use second_brain_sync::keyring::{IdentityMismatch, Keyring};
use second_brain_sync::protocol::{
Hello, SyncHeader, VersionError, check_store_header, store_header,
};
use serde::{Deserialize, Serialize};
use super::internal_error;
use crate::AppState;
use crate::clock_gate::is_untrusted;
use crate::snapshot_merge::{MergeRunning, merge_spooled, merging, receive, write_snapshot};
use crate::sync_hello::{HelloRejected, accept_handshake, check_identity, hello_for};
use crate::write_gate::clock_held;
fn sync_error(e: anyhow::Error) -> axum::response::Response {
if is_untrusted(&e) {
return clock_held();
}
if let Some(below) = e.downcast_ref::<BelowFloor>() {
return (
StatusCode::CONFLICT,
Json(serde_json::json!({
"error": e.to_string(),
"below_floor": true,
"after_seq": below.after_seq,
"log_floor": below.log_floor,
})),
)
.into_response();
}
if let Some(MergeRunning(merging)) = e.downcast_ref::<MergeRunning>() {
return (
StatusCode::CONFLICT,
Json(serde_json::json!({
"error": e.to_string(),
"merging": true,
"snapshot": merging,
})),
)
.into_response();
}
if e.downcast_ref::<VersionError>().is_none()
&& e.downcast_ref::<HelloRejected>().is_none()
&& e.downcast_ref::<IdentityMismatch>().is_none()
{
return internal_error(e);
}
let mut body = serde_json::json!({"error": e.to_string()});
if e.downcast_ref::<IdentityMismatch>().is_some() {
body["identity"] = serde_json::Value::Bool(true);
}
(StatusCode::CONFLICT, Json(body)).into_response()
}
pub(super) async fn sync_version(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let sqlite = state.sqlite.clone();
match tokio::task::spawn_blocking(move || store_header(&sqlite)).await {
Ok(Ok(header)) => Json(header).into_response(),
Ok(Err(e)) => sync_error(e),
Err(e) => internal_error(e),
}
}
#[derive(Deserialize)]
pub(super) struct SyncExportRequest {
after_seq: Option<u64>,
limit: Option<usize>,
header: Option<SyncHeader>,
hello: Option<Hello>,
peer_id: Option<String>,
}
#[derive(Serialize)]
struct SyncExportResponse {
lines: Vec<String>,
max_seq: u64,
#[serde(skip_serializing_if = "Option::is_none")]
hello: Option<Hello>,
}
pub(super) async fn sync_export(
State(state): State<Arc<AppState>>,
Json(req): Json<SyncExportRequest>,
) -> impl IntoResponse {
let after_seq = req.after_seq.unwrap_or(0);
let limit = req.limit;
let store = state.store.clone();
let sqlite = state.sqlite.clone();
let result = tokio::task::spawn_blocking(move || -> anyhow::Result<SyncExportResponse> {
check_store_header(&sqlite, req.header.as_ref(), "the calling sb")?;
let peer = req
.hello
.as_ref()
.map(|puller| puller.machine_id.clone())
.or(req.peer_id.clone())
.context("a sync export names the peer it is sealed for")?;
let hello = match &req.hello {
Some(puller) => Some(greet_puller(&state, puller, after_seq)?),
None => None,
};
let sealer = Keyring::load(&sqlite, &state.data_dir)?.sealer_for(
&peer,
req.hello
.as_ref()
.and_then(|h| h.keys.as_ref())
.map(|k| k.epochs.as_slice()),
)?;
let seal = |records: &[serde_json::Value]| -> anyhow::Result<Vec<String>> {
records
.iter()
.map(|record| seal_line(&sealer, &serde_json::to_vec(record)?))
.collect()
};
if let Some(limit) = limit {
let (records, max_seq) =
second_brain_sync::export::export_page(store.as_ref(), after_seq, limit)?;
Ok(SyncExportResponse {
lines: seal(&records)?,
max_seq,
hello,
})
} else {
let mut buf = Vec::new();
let max_seq =
second_brain_sync::export::export_changes(store.as_ref(), after_seq, &mut buf)?;
let jsonl = String::from_utf8(buf)?;
let records: Vec<serde_json::Value> = jsonl
.lines()
.filter(|l| !l.trim().is_empty())
.filter_map(|l| serde_json::from_str(l).ok())
.collect();
Ok(SyncExportResponse {
lines: seal(&records)?,
max_seq,
hello,
})
}
})
.await;
match result {
Ok(Ok(resp)) => Json(resp).into_response(),
Ok(Err(e)) => sync_error(e),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
}
}
fn greet_puller(state: &AppState, puller: &Hello, after_seq: u64) -> anyhow::Result<Hello> {
check_identity(&state.sqlite, &state.data_dir, puller)?;
if let Some(_slot) = state.gate.try_enter() {
if let Some(cursor) = puller.cursor {
let applied = puller.applied.unwrap_or(cursor).min(cursor);
state.sqlite.record_peer_ack(
&puller.machine_id,
applied.min(after_seq),
state.clock.wall_micros(),
)?;
}
let hello = Hello {
cursor: Some(puller.cursor.unwrap_or(0).max(after_seq)),
..puller.clone()
};
accept_handshake(
&state.sqlite,
&state.clock,
&state.health,
&state.device_access,
&state.data_dir,
&hello,
)?;
}
hello_for(
&state.sqlite,
&state.clock,
&state.data_dir,
&puller.machine_id,
puller.keys.as_ref().map(|k| k.epochs.as_slice()),
)
}
#[derive(Deserialize)]
pub(super) struct SyncImportRequest {
header: Option<SyncHeader>,
lines: Vec<String>,
}
#[derive(Serialize)]
struct SyncImportResponse {
created: u64,
updated: u64,
deleted: u64,
skipped: u64,
#[serde(default)]
skipped_by_reason: std::collections::BTreeMap<String, u64>,
errors: u64,
max_seq: u64,
reached: u64,
}
pub(super) async fn sync_import(
State(state): State<Arc<AppState>>,
Json(req): Json<SyncImportRequest>,
) -> impl IntoResponse {
let sqlite = state.sqlite.clone();
let result = tokio::task::spawn_blocking(move || -> anyhow::Result<SyncImportResponse> {
check_store_header(&sqlite, req.header.as_ref(), "the sender")?;
if let Some(hello) = req.header.as_ref().and_then(|h| h.hello.as_ref()) {
accept_handshake(
&sqlite,
&state.clock,
&state.health,
&state.device_access,
&state.data_dir,
hello,
)?;
}
let sender = req.header.as_ref().and_then(|h| h.hello.as_ref());
let records = Keyring::load(&sqlite, &state.data_dir)?
.open_lines(sender.map(|h| h.machine_id.as_str()), &req.lines)?
.iter()
.map(|opened| Ok(serde_json::from_slice(opened)?))
.collect::<anyhow::Result<Vec<serde_json::Value>>>()?;
let source = req
.header
.as_ref()
.and_then(|h| h.hello.as_ref())
.map(|h| h.machine_id.as_str());
let applied = crate::sync_import::import_from(&sqlite, source, &records)?;
if source.is_some()
&& let Err(e) =
second_brain_sync::peers::record_success(&state.data_dir, chrono::Utc::now())
{
tracing::warn!("cannot record the push from {source:?} as a sync: {e:#}");
}
let stats = applied.stats;
Ok(SyncImportResponse {
created: stats.created,
updated: stats.updated,
deleted: stats.deleted,
skipped: stats.skipped,
skipped_by_reason: stats.skipped_by_reason,
errors: stats.errors,
max_seq: applied.max_seq,
reached: applied.reached,
})
})
.await;
match result {
Ok(Ok(resp)) => Json(resp).into_response(),
Ok(Err(e)) => sync_error(e),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
}
}
#[derive(Serialize)]
struct SyncPeerStatus {
peer_id: String,
last_seq: u64,
last_sync_at: String,
}
pub(super) async fn sync_status(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let store = state.store.clone();
let result = tokio::task::spawn_blocking(move || store.get_all_sync_states()).await;
match result {
Ok(Ok(states)) => {
let peers: Vec<SyncPeerStatus> = states
.into_iter()
.map(|s| SyncPeerStatus {
peer_id: s.peer_id,
last_seq: s.last_seq,
last_sync_at: s.last_sync_at.to_rfc3339(),
})
.collect();
Json(serde_json::json!({"peers": peers})).into_response()
}
Ok(Err(e)) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
}
}
pub(super) async fn sync_prune_relations(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let store = state.store.clone();
let result = tokio::task::spawn_blocking(move || store.prune_dangling_relations()).await;
match result {
Ok(Ok(pruned)) => Json(serde_json::json!({"pruned": pruned})).into_response(),
Ok(Err(e)) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
}
}
#[derive(Deserialize)]
pub(super) struct SyncCompactRequest {
ops: Vec<SyncOp>,
#[serde(default = "default_true")]
dry_run: bool,
}
fn default_true() -> bool {
true
}
pub(super) async fn sync_compact(
State(state): State<Arc<AppState>>,
Json(req): Json<SyncCompactRequest>,
) -> impl IntoResponse {
let store = state.store.clone();
let result =
tokio::task::spawn_blocking(move || store.compact_sync_log(&req.ops, req.dry_run)).await;
match result {
Ok(Ok(stats)) => Json(serde_json::json!({
"groups_with_duplicates": stats.groups_with_duplicates,
"rows_deleted": stats.rows_deleted,
"max_local_seq_before": stats.max_local_seq_before,
"max_local_seq_after": stats.max_local_seq_after,
}))
.into_response(),
Ok(Err(e)) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
}
}
#[derive(Deserialize)]
pub(super) struct SetSyncStateRequest {
peer_id: String,
last_seq: u64,
}
pub(super) async fn set_sync_state(
State(state): State<Arc<AppState>>,
Json(req): Json<SetSyncStateRequest>,
) -> impl IntoResponse {
let store = state.store.clone();
let peer_id = req.peer_id;
let last_seq = req.last_seq;
let result = tokio::task::spawn_blocking(move || {
use second_brain_core::schema::SyncState;
store.set_sync_state(&SyncState {
peer_id,
last_seq,
last_sync_at: chrono::Utc::now(),
})
})
.await;
match result {
Ok(Ok(_)) => StatusCode::OK.into_response(),
Ok(Err(e)) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({"error": e.to_string()})),
)
.into_response(),
}
}
#[derive(Deserialize)]
pub(super) struct SyncHelloRequest {
peer_id: String,
}
pub(super) async fn sync_hello(
State(state): State<Arc<AppState>>,
Json(req): Json<SyncHelloRequest>,
) -> impl IntoResponse {
let result = tokio::task::spawn_blocking(move || {
hello_for(
&state.sqlite,
&state.clock,
&state.data_dir,
&req.peer_id,
None,
)
})
.await;
match result {
Ok(Ok(hello)) => Json(hello).into_response(),
Ok(Err(e)) => sync_error(e),
Err(e) => internal_error(e),
}
}
#[derive(Deserialize)]
pub(super) struct SyncAcceptHelloRequest {
hello: Hello,
}
pub(super) async fn sync_accept_hello(
State(state): State<Arc<AppState>>,
Json(req): Json<SyncAcceptHelloRequest>,
) -> impl IntoResponse {
let result = tokio::task::spawn_blocking(move || {
accept_handshake(
&state.sqlite,
&state.clock,
&state.health,
&state.device_access,
&state.data_dir,
&req.hello,
)
})
.await;
match result {
Ok(Ok(())) => StatusCode::OK.into_response(),
Ok(Err(e)) => sync_error(e),
Err(e) => internal_error(e),
}
}
#[derive(Deserialize)]
pub(super) struct SyncAckRequest {
peer_id: String,
acked_seq: u64,
}
pub(super) async fn sync_ack(
State(state): State<Arc<AppState>>,
Json(req): Json<SyncAckRequest>,
) -> impl IntoResponse {
let result = tokio::task::spawn_blocking(move || {
state
.sqlite
.record_peer_ack(&req.peer_id, req.acked_seq, state.clock.wall_micros())
})
.await;
match result {
Ok(Ok(())) => StatusCode::OK.into_response(),
Ok(Err(e)) => sync_error(e),
Err(e) => internal_error(e),
}
}
#[derive(Deserialize)]
pub(super) struct SyncPeerInRequest {
peer_id: String,
seq: u64,
}
pub(super) async fn sync_peer_in(
State(state): State<Arc<AppState>>,
Json(req): Json<SyncPeerInRequest>,
) -> impl IntoResponse {
let result =
tokio::task::spawn_blocking(move || state.sqlite.record_peer_in(&req.peer_id, req.seq))
.await;
match result {
Ok(Ok(())) => StatusCode::OK.into_response(),
Ok(Err(e)) => sync_error(e),
Err(e) => internal_error(e),
}
}
#[derive(Deserialize)]
pub(super) struct PushReportQuery {
peer_id: String,
}
pub(super) async fn sync_push_report(
State(state): State<Arc<AppState>>,
Query(query): Query<PushReportQuery>,
) -> impl IntoResponse {
let result =
tokio::task::spawn_blocking(move || state.sqlite.push_report(&query.peer_id)).await;
match result {
Ok(Ok(reported)) => Json(serde_json::json!({"reported": reported})).into_response(),
Ok(Err(e)) => sync_error(e),
Err(e) => internal_error(e),
}
}
#[derive(Deserialize)]
pub(super) struct RecordPushReportRequest {
peer_id: String,
reported: Option<u64>,
}
pub(super) async fn record_push_report(
State(state): State<Arc<AppState>>,
Json(req): Json<RecordPushReportRequest>,
) -> impl IntoResponse {
let result = tokio::task::spawn_blocking(move || {
state.sqlite.record_push_report(&req.peer_id, req.reported)
})
.await;
match result {
Ok(Ok(())) => StatusCode::OK.into_response(),
Ok(Err(e)) => sync_error(e),
Err(e) => internal_error(e),
}
}
#[derive(Deserialize)]
pub(super) struct SnapshotRequest {
hello: Hello,
}
pub(super) async fn sync_snapshot(
State(state): State<Arc<AppState>>,
Json(req): Json<SnapshotRequest>,
) -> axum::response::Response {
let checked = {
let state = state.clone();
let hello = req.hello.clone();
tokio::task::spawn_blocking(move || check_identity(&state.sqlite, &state.data_dir, &hello))
.await
};
match checked {
Ok(Ok(())) => {}
Ok(Err(e)) => return sync_error(e),
Err(e) => return internal_error(e),
}
let (tx, rx) = tokio::sync::mpsc::channel::<std::io::Result<bytes::Bytes>>(SNAPSHOT_CHANNEL);
tokio::task::spawn_blocking(move || {
let out = std::io::BufWriter::with_capacity(
SNAPSHOT_WRITE_BUFFER,
ChannelWriter { tx: tx.clone() },
);
if let Err(e) = write_snapshot(&state.sqlite, &state.data_dir, &state.gate, &req.hello, out)
{
tracing::warn!(
"streaming a snapshot to {} failed: {e:#}",
req.hello.machine_id
);
let _ = tx.blocking_send(Err(std::io::Error::other(format!("{e:#}"))));
}
});
let stream = futures_util::stream::unfold(rx, |mut rx| async move {
rx.recv().await.map(|item| (item, rx))
});
(
[(axum::http::header::CONTENT_TYPE, "application/octet-stream")],
axum::body::Body::from_stream(stream),
)
.into_response()
}
const SNAPSHOT_CHANNEL: usize = 4;
const SNAPSHOT_WRITE_BUFFER: usize = 1024 * 1024;
struct ChannelWriter {
tx: tokio::sync::mpsc::Sender<std::io::Result<bytes::Bytes>>,
}
impl std::io::Write for ChannelWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.tx
.blocking_send(Ok(bytes::Bytes::copy_from_slice(buf)))
.map_err(|_| std::io::Error::new(std::io::ErrorKind::BrokenPipe, "the puller left"))?;
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
struct BodyReader {
rx: tokio::sync::mpsc::Receiver<std::io::Result<bytes::Bytes>>,
chunk: bytes::Bytes,
}
impl std::io::Read for BodyReader {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
while self.chunk.is_empty() {
match self.rx.blocking_recv() {
Some(Ok(chunk)) => self.chunk = chunk,
Some(Err(e)) => return Err(e),
None => return Ok(0),
}
}
let n = buf.len().min(self.chunk.len());
buf[..n].copy_from_slice(&self.chunk.split_to(n));
Ok(n)
}
}
#[derive(Deserialize)]
pub(super) struct ApplyQuery {
exporter: Option<String>,
}
pub(super) async fn sync_snapshot_apply(
State(state): State<Arc<AppState>>,
axum::extract::Query(query): axum::extract::Query<ApplyQuery>,
body: axum::body::Body,
) -> axum::response::Response {
use futures_util::StreamExt;
let (tx, rx) = tokio::sync::mpsc::channel::<std::io::Result<bytes::Bytes>>(SNAPSHOT_CHANNEL);
tokio::spawn(async move {
let mut stream = body.into_data_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(std::io::Error::other);
if tx.send(chunk).await.is_err() {
break;
}
}
});
let spooled = {
let state = state.clone();
tokio::task::spawn_blocking(move || {
receive(
&state.sqlite,
&state.data_dir,
BodyReader {
rx,
chunk: bytes::Bytes::new(),
},
query.exporter.as_deref(),
)
})
.await
};
match spooled {
Ok(Ok(merging)) => {
tokio::task::spawn_blocking(move || {
if let Err(e) = merge_spooled(&state.sqlite, &state.data_dir, &state.gate) {
tracing::warn!("merging the pushed snapshot failed: {e:#}");
}
});
Json(merging).into_response()
}
Ok(Err(e)) => sync_error(e),
Err(e) => internal_error(e),
}
}
pub(super) async fn sync_snapshot_status(State(state): State<Arc<AppState>>) -> impl IntoResponse {
match merging(&state.data_dir) {
Ok(merging) => Json(serde_json::json!({ "merging": merging })).into_response(),
Err(e) => internal_error(e),
}
}