From 5163fc98542b565cd5040017d76706738d3513fd Mon Sep 17 00:00:00 2001 From: GloamXun Date: Thu, 19 Nov 2026 16:01:20 +0800 Subject: [PATCH] Overlap durable sync for interactive document drafts --- docs/P2_M1_PROGRESS.md | 6 +++ src/native.rs | 8 ++- src/native_job.rs | 107 +++++++++++++++++++++++++++++++++-------- 3 files changed, 101 insertions(+), 20 deletions(-) diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 77eeda3..8059aad 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -207,6 +207,12 @@ SHA-256 为 `a0c3b9929508e8bfbb8f33fe177fc662c4eca0cc7d5c41ac52503a39e337ab1b` 回执 `target/p4-parallel-draft-sync/interleaved-a3-3/receipt.json` 的 SHA-256 为 `cac9b9da9cf15032ff2c614093e0b01476bc139e99e3c6e3b6dc8216e99df9db`。 +并行同步已与 schema25 主线及流式失败报告合并。新版定向回归通过60项, +覆盖同步失败后原 PDF、日志、辅助文件和账本保留,以及共享会话取消、失败 +恢复和发布顺序。报告 `target/p4-parallel-sync-main-reconcile/directed.log` 的 +SHA-256 为 `2bb2dd465e5a7f1c94f834d305b2405a03efadb2382db5c64cdfc9b151fcfdfc`。 +上面的30次浏览器配对对应此前隔离构建,合并版仍需在固定条件下测量。 + 大规模错误报告按原 JSON 格式直接写入缓冲暂存文件,再完整同步并按新文件 发布。集成测试核对 11 种失败路径,包含原序列化字节、原有报告保护、错误 保留及暂存清理。真实 CVPR 大报告的峰值内存还需重新测量。 diff --git a/src/native.rs b/src/native.rs index 47f5f9e..03c0f3c 100644 --- a/src/native.rs +++ b/src/native.rs @@ -1099,7 +1099,13 @@ impl CompileJob { let inputs = files.input_snapshot(); let prepared = pdf .as_ref() - .map(|bytes| PreparedOutput::write(&output, bytes)) + .map(|bytes| { + if checkpoints.is_some() { + PreparedOutput::write_for_draft(&output, bytes) + } else { + PreparedOutput::write(&output, bytes) + } + }) .transpose()?; let receipt = prepared.as_ref().map(|p| p.version(&output)).transpose()?; let previous = self.compiler.latest(); diff --git a/src/native_job.rs b/src/native_job.rs index e78709c..edecbcf 100644 --- a/src/native_job.rs +++ b/src/native_job.rs @@ -1132,10 +1132,7 @@ impl Files { if !draft.finished { return Err("native draft must finish successfully before publication".into()); } - if let Some(log) = self.log.take() { - log.sync_all() - .map_err(|e| format!("sync native draft log: {e}"))?; - } + let log = self.log.take(); let initial = self .preparation .generated_initial @@ -1160,15 +1157,46 @@ impl Files { .map(|(path, version)| (path.clone(), version.clone())) .collect::>(); let mut artifacts: Vec<_> = draft.generated.into_values().collect(); - for artifact in &artifacts { - OpenOptions::new() - .write(true) - .open(&artifact.staged.path) - .and_then(|file| file.sync_all()) - .map_err(|e| { - format!("sync native draft {}: {e}", artifact.target.path.display()) + let sync_pdf = || -> Result<(), String> { + if let Some(file) = pdf.as_ref().and_then(|pdf| pdf.pending_sync.as_ref()) { + file.sync_all().map_err(|error| { + format!("sync native draft {}: {error}", draft.pdf.path.display()) })?; - } + } + Ok(()) + }; + let sync_log = || -> Result<(), String> { + if let Some(file) = &log { + file.sync_all() + .map_err(|error| format!("sync native draft log: {error}"))?; + } + Ok(()) + }; + // Flush independent files concurrently, then finish collecting all + // results before the revision gate can install any artifact. + std::thread::scope(|scope| { + let pdf_worker = pdf.as_ref().and_then(|pdf| pdf.pending_sync.as_ref()) + .and_then(|_| std::thread::Builder::new().spawn_scoped(scope, sync_pdf).ok()); + let log_worker = log.as_ref() + .and_then(|_| std::thread::Builder::new().spawn_scoped(scope, sync_log).ok()); + let generated = artifacts.iter().try_for_each(|artifact| { + OpenOptions::new() + .write(true) + .open(&artifact.staged.path) + .and_then(|file| file.sync_all()) + .map_err(|error| { + format!("sync native draft {}: {error}", artifact.target.path.display()) + }) + }); + let pdf_result = pdf_worker.map_or_else(sync_pdf, |worker| { + worker.join().map_err(|_| "native draft PDF sync thread panicked".to_owned())? + }); + let log_result = log_worker.map_or_else(sync_log, |worker| { + worker.join().map_err(|_| "native draft log sync thread panicked".to_owned())? + }); + pdf_result.and(log_result).and(generated) + })?; + drop(log); artifacts.push(draft.log); let untouched_ledger = if let Some(DraftLedger { file, staged }) = draft.ledger { let target = &draft.ledger_target.path; @@ -1195,7 +1223,8 @@ impl Files { } else { Some(draft.ledger_target) }; - if let Some(staged) = pdf { + if let Some(mut staged) = pdf { + drop(staged.pending_sync.take()); artifacts.push(DraftArtifact { target: draft.pdf, staged, @@ -1846,6 +1875,7 @@ fn restore_draft( /// Write complete bytes before publishing the output path. pub struct PreparedOutput { path: PathBuf, + pending_sync: Option, } impl PreparedOutput { @@ -1864,14 +1894,31 @@ impl PreparedOutput { output: &Path, write: impl FnOnce(&mut File) -> Result<(), String>, ) -> Result { - let (prepared, mut file) = Self::create(output)?; - let written = write(&mut file) - .and_then(|()| file.sync_all().map_err(|error| error.to_string())); + let (prepared, file) = Self::write_staged(output, write)?; + let synced = file.sync_all(); drop(file); - written.map_err(|error| format!("stage output {}: {error}", output.display()))?; + synced.map_err(|error| format!("stage output {}: {error}", output.display()))?; Ok(prepared) } + pub(crate) fn write_for_draft(output: &Path, bytes: &[u8]) -> Result { + let (mut prepared, file) = Self::write_staged(output, |file| { + file.write_all(bytes).map_err(|error| error.to_string()) + })?; + prepared.pending_sync = Some(file); + Ok(prepared) + } + + fn write_staged( + output: &Path, + write: impl FnOnce(&mut File) -> Result<(), String>, + ) -> Result<(Self, File), String> { + let (prepared, mut file) = Self::create(output)?; + write(&mut file) + .map_err(|error| format!("stage output {}: {error}", output.display()))?; + Ok((prepared, file)) + } + fn draft(output: &Path) -> Result<(Self, File), String> { let existing = match fs::symlink_metadata(output) { Ok(metadata) => { @@ -1932,7 +1979,7 @@ impl PreparedOutput { let path = output.with_file_name(format!(".texrra-{}-{id}.tmp", std::process::id())); if path != output { match create(&path) { - Ok(value) => return Ok((Self { path }, value)), + Ok(value) => return Ok((Self { path, pending_sync: None }, value)), Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue, Err(error) => { return Err(format!("stage output {}: {error}", output.display())); @@ -1967,6 +2014,7 @@ impl PreparedOutput { impl Drop for PreparedOutput { fn drop(&mut self) { + drop(self.pending_sync.take()); if !self.path.as_os_str().is_empty() { let _ = fs::remove_file(&self.path); } @@ -2107,7 +2155,28 @@ mod generated_file_tests { assert!(error.contains("native staged ledger changed"), "{error}"); assert!(!staged.exists()); assert_eq!(fs::read(&ledger_path).unwrap(), original_ledger); - assert_eq!(fs::read(generated).unwrap(), b"original"); + assert_eq!(fs::read(&generated).unwrap(), b"original"); + + #[cfg(unix)] + { + let output = root.join("job.pdf"); + let log = root.join("job.log"); + fs::write(&output, b"already published PDF").unwrap(); + let original_log = fs::read(&log).unwrap(); + let mut host = draft(); + write(&mut host, "actual.aux", "another draft"); + host.finish().unwrap(); + let mut pdf = PreparedOutput::write_for_draft(&output, b"new PDF").unwrap(); + let path = pdf.path.clone(); + pdf.pending_sync = Some(File::open("/dev/null").unwrap()); + let error = host.seal_draft(Some(pdf)).err().unwrap(); + assert!(error.contains("sync native draft") && error.contains("job.pdf"), "{error}"); + assert!(!path.exists()); + assert_eq!(fs::read(&output).unwrap(), b"already published PDF"); + assert_eq!(fs::read(&log).unwrap(), original_log); + assert_eq!(fs::read(&generated).unwrap(), b"original"); + assert_eq!(fs::read(&ledger_path).unwrap(), original_ledger); + } fs::remove_dir_all(root).unwrap(); }