Avoid repeated full journal scans and fast ticks in Windows idle

This commit is contained in:
2026-11-19 10:04:26 +01:00
parent 01960477f8
commit 478ee1fc1a
5 changed files with 161 additions and 27 deletions
+23 -3
View File
@@ -101,6 +101,7 @@ pub struct ClientSync<V = DefaultVerifier> {
verifier: V,
helper_identity: HelperIdentity,
pending: Option<PreparedSync>,
integrity: Option<crate::ledger::IntegrityMonitor>,
channel: Option<Channel>,
gate: Option<Gate>,
}
@@ -117,6 +118,7 @@ impl<V: Ed25519Verifier> ClientSync<V> {
verifier,
helper_identity: HelperIdentity::Root,
pending: None,
integrity: None,
channel: None,
gate: None,
}
@@ -127,6 +129,11 @@ impl<V: Ed25519Verifier> ClientSync<V> {
self.helper_identity = identity;
self
}
/// Use with one protected SQLite connection retained for this instance.
pub fn with_periodic_idle_integrity_checks(mut self) -> Self {
self.integrity = Some(crate::ledger::IntegrityMonitor::default());
self
}
/// Invoke on every authenticated/fenced ServerHello or transport loss.
pub fn close(&mut self) {
self.gate = None;
@@ -606,7 +613,17 @@ impl<V: Ed25519Verifier> ClientSync<V> {
channel.connection_generation > 0 && !channel.boot_id.is_nil(),
"UNAUTHENTICATED_CHANNEL"
);
initialize(db)?;
if let Some(integrity) = &mut self.integrity {
let work_pending: bool = db.query_row(
"SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown'))",
[],
|row| row.get(0),
)?;
integrity.check(db, work_pending, "JOURNAL_INTEGRITY")?;
initialize_schema(db)?;
} else {
initialize(db)?;
}
let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?;
let meta = metadata(&tx)?;
ensure!(meta.fault.is_none(), "SYNC_MANUAL_RECOVERY_REQUIRED");
@@ -1147,8 +1164,11 @@ impl<V: Ed25519Verifier> ClientSync<V> {
}
pub fn initialize(db: &Connection) -> Result<()> {
let check: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?;
ensure!(check == "ok", "JOURNAL_INTEGRITY");
crate::ledger::check_integrity(db, "JOURNAL_INTEGRITY")?;
initialize_schema(db)
}
fn initialize_schema(db: &Connection) -> Result<()> {
db.execute_batch("PRAGMA synchronous=FULL;
CREATE TABLE IF NOT EXISTS reconcile_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,high_sequence INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,last_decision TEXT,pending_request TEXT,helper_id TEXT,helper_checkpoint INTEGER NOT NULL DEFAULT 0,fault TEXT);
CREATE TABLE IF NOT EXISTS reconcile_requests(id TEXT PRIMARY KEY,request BLOB NOT NULL,envelope BLOB NOT NULL,generation INTEGER NOT NULL,response BLOB);