Reconcile exact browser Actions migration retries in one transaction

This commit is contained in:
yoriswind committed 2026-11-03 20:55:34 -08:00
1 parent 13a830c6c1
commit 3c9bc2d588
4 files changed
+234 -41

No files matched your search

+56 -35
View File
@@ -12,6 +12,8 @@ const DATABASE: &str = "photocraft.presets";
const FILES: &str = "files";
const TIMEOUT_MS: f64 = 10_000.0;
mod migration;
fn js(e: JsValue) -> String {
if let Some(exception) = e.dyn_ref::<web_sys::DomException>() {
format!("{}: {}", exception.name(), exception.message())
@@ -231,47 +233,57 @@ async fn read(db: &IdbDatabase, deadline: f64) -> Result<(Bridge, Vec<String>),
Ok((bridge, warnings))
}
pub async fn write(db: &IdbDatabase, batch: &Batch) -> Result<(), String> {
/// Queue only while the transaction is active (creation or an IDB request callback).
fn queue_batch(store: &web_sys::IdbObjectStore, batch: &Batch, committed: bool) -> Result<(), String> {
for (name, bytes) in batch {
if committed && matches!(name.as_str(), ACTIONS_FILE | LEGACY_ACTIONS_FILE | MIGRATED_ACTIONS_FILE) {
continue;
}
let key = JsValue::from_str(name);
if let Some(bytes) = bytes {
if let Some(migration) = &batch.migration
&& name == ACTIONS_FILE
&& migration.published
{
continue;
}
let blob = Blob::new_with_u8_array_sequence(&Array::of1(&Uint8Array::from(bytes.as_ref()))).map_err(js)?;
let exclusive = batch.migration.as_ref().is_some_and(|migration| name == MIGRATED_ACTIONS_FILE || (name == ACTIONS_FILE && !migration.published));
if exclusive {
store.add_with_key(&blob, &key).map_err(js)?;
} else {
store.put_with_key(&blob, &key).map_err(js)?;
}
} else {
store.delete(&key).map_err(js)?;
}
}
Ok(())
}
pub async fn write(db: &std::rc::Rc<std::cell::RefCell<IdbDatabase>>, batch: &Batch) -> Result<(), String> {
if let Some(migration) = &batch.migration {
if !matches!(batch.get(LEGACY_ACTIONS_FILE), Some(None))
|| batch.get(MIGRATED_ACTIONS_FILE).and_then(Option::as_ref).is_none_or(|bytes| bytes.as_ref() != migration.legacy.as_ref())
|| batch.get(ACTIONS_FILE).and_then(Option::as_ref).is_none()
|| batch.get(ACTIONS_FILE).and_then(Option::as_ref).is_none_or(|bytes| bytes.as_ref() != migration.atn.as_ref())
{
return Err("incomplete Actions migration transaction".into());
}
}
let tx = db.transaction_with_str_and_mode(FILES, IdbTransactionMode::Readwrite).map_err(js)?;
let store = tx.object_store(FILES).map_err(js)?;
let queued = (|| -> Result<(), String> {
for (name, bytes) in batch {
let key = JsValue::from_str(name);
if let Some(bytes) = bytes {
if let Some(migration) = &batch.migration {
if name == ACTIONS_FILE && migration.published && bytes.as_ref() == migration.atn.as_ref() {
continue;
}
if name == MIGRATED_ACTIONS_FILE && bytes.as_ref() != migration.legacy.as_ref() {
return Err("migration backup changed while staged".into());
}
}
let blob = Blob::new_with_u8_array_sequence(&Array::of1(&Uint8Array::from(bytes.as_ref()))).map_err(js)?;
let exclusive =
batch.migration.as_ref().is_some_and(|migration| name == MIGRATED_ACTIONS_FILE || (name == ACTIONS_FILE && !migration.published));
if exclusive {
store.add_with_key(&blob, &key).map_err(js)?;
} else {
store.put_with_key(&blob, &key).map_err(js)?;
}
} else {
store.delete(&key).map_err(js)?;
}
let deadline = js_sys::Date::now() + TIMEOUT_MS;
// A versionchange handler closes the old connection. Retry replaces only that
// connection; it must not rehydrate over the live Bridge's newer unsaved bytes.
let connection = db.borrow().clone();
let tx = match connection.transaction_with_str_and_mode(FILES, IdbTransactionMode::Readwrite) {
Ok(tx) => tx,
Err(error) if error.dyn_ref::<web_sys::DomException>().is_some_and(|error| error.name() == "InvalidStateError") => {
let connection = open(deadline).await?;
*db.borrow_mut() = connection.clone();
connection.transaction_with_str_and_mode(FILES, IdbTransactionMode::Readwrite).map_err(js)?
}
Ok(())
})();
if let Err(e) = queued {
let _ = tx.abort();
return Err(e);
}
Err(error) => return Err(js(error)),
};
let store = tx.object_store(FILES).map_err(js)?;
let mut listeners = Listeners::new();
let promise = Promise::new(&mut |resolve, reject| {
if let Err(e) = listeners.add(&tx, "complete", move |_| {
@@ -291,9 +303,18 @@ pub async fn write(db: &IdbDatabase, batch: &Batch) -> Result<(), String> {
}
}
});
let result = before(&promise, js_sys::Date::now() + TIMEOUT_MS).await.map(|_| ());
let queued =
if batch.migration.is_some() { migration::start(&tx, &store, batch, deadline).map(Some) } else { queue_batch(&store, batch, false).map(|_| None) };
let scope = match queued {
Ok(scope) => scope,
Err(error) => {
let _ = tx.abort();
return Err(error);
}
};
let result = before(&promise, deadline).await.map(|_| ());
if result.is_err() {
let _ = tx.abort();
}
result
scope.as_ref().and_then(migration::Scope::error).map_or(result, Err)
}
@@ -0,0 +1,122 @@
//! Migration-only transaction liveness while immutable Blob bytes are compared.
//! Blob futures publish a decision; only an active IDB request callback queues mutations.
use std::{cell::RefCell, rc::Rc};
use super::*;
type Callback = Rc<RefCell<Option<Closure<dyn FnMut(Event)>>>>;
#[derive(Default)]
struct Decision {
ready: Option<Result<bool, String>>,
stopped: bool,
error: Option<String>,
}
pub(super) struct Scope {
decision: Rc<RefCell<Decision>>,
current: Rc<RefCell<Option<IdbRequest>>>,
callback: Callback,
}
impl Scope {
pub(super) fn error(&self) -> Option<String> {
self.decision.borrow().error.clone()
}
}
impl Drop for Scope {
fn drop(&mut self) {
self.decision.borrow_mut().stopped = true;
if let Some(request) = self.current.borrow_mut().take() {
request.set_onsuccess(None);
}
self.callback.borrow_mut().take();
}
}
fn next(store: &web_sys::IdbObjectStore, current: &Rc<RefCell<Option<IdbRequest>>>, callback: &Callback) -> Result<(), String> {
let request = store.count().map_err(js)?;
let borrowed = callback.borrow();
let handler = borrowed.as_ref().ok_or("migration callback was released")?;
request.set_onsuccess(Some(handler.as_ref().unchecked_ref()));
if let Some(previous) = current.borrow_mut().replace(request) {
previous.set_onsuccess(None);
}
Ok(())
}
async fn bytes(value: JsValue, deadline: f64) -> Result<Option<Vec<u8>>, String> {
if value.is_undefined() {
return Ok(None);
}
let blob = value.dyn_into::<Blob>().map_err(|_| "migration record is not a Blob")?;
if blob.size() > photocraft_engine::preset_store::MAX_ACTIONS_BYTES as f64 {
return Err("migration record exceeds the Actions file limit".into());
}
let buffer = before(&blob.array_buffer(), deadline).await?;
Ok(Some(Uint8Array::new(&buffer).to_vec()))
}
pub(super) fn start(tx: &web_sys::IdbTransaction, store: &web_sys::IdbObjectStore, batch: &Batch, deadline: f64) -> Result<Scope, String> {
let migration = batch.migration.clone().ok_or("missing migration identity")?;
let mut reads = Vec::new();
let promises = Array::new();
for name in [ACTIONS_FILE, LEGACY_ACTIONS_FILE, MIGRATED_ACTIONS_FILE] {
let pending = request(&store.get(&JsValue::from_str(name)).map_err(js)?);
promises.push(&pending.promise);
reads.push(pending);
}
let decision = Rc::new(RefCell::new(Decision::default()));
let current = Rc::new(RefCell::new(None));
let callback: Callback = Rc::new(RefCell::new(None));
let weak = Rc::downgrade(&callback);
let state = decision.clone();
let active = current.clone();
let transaction = tx.clone();
let files = store.clone();
let owned = batch.clone();
*callback.borrow_mut() = Some(Closure::new(move |_: Event| {
let result = {
let mut state = state.borrow_mut();
if state.stopped {
return;
}
if js_sys::Date::now() >= deadline { Some(Err("browser preset storage timed out".into())) } else { state.ready.take() }
};
let result = match result {
Some(result) => {
// This callback is an active IDB request event. No Blob future issues writes.
state.borrow_mut().stopped = true;
result.and_then(|committed| queue_batch(&files, &owned, committed))
}
None => weak.upgrade().ok_or_else(|| "migration callback was released".to_string()).and_then(|callback| next(&files, &active, &callback)),
};
if let Err(error) = result {
let mut state = state.borrow_mut();
state.stopped = true;
state.error = Some(error);
let _ = transaction.abort();
}
}));
let scope = Scope { decision: decision.clone(), current, callback };
next(store, &scope.current, &scope.callback)?;
wasm_bindgen_futures::spawn_local(async move {
let result = async {
let values = before(&Promise::all(&promises), deadline).await?;
let values = Array::from(&values);
let atn = bytes(values.get(0), deadline).await?;
let legacy = bytes(values.get(1), deadline).await?;
let backup = bytes(values.get(2), deadline).await?;
migration.already_committed(atn.as_deref(), legacy.as_deref(), backup.as_deref())
}
.await;
// A timeout or abort may have finished the attempt. Late reads have no IDB effects.
let mut state = decision.borrow_mut();
if !state.stopped {
state.ready = Some(result);
}
drop(reads);
});
Ok(scope)
}
+53 -3
View File
@@ -15,6 +15,19 @@ pub struct Migration {
pub atn: Arc<[u8]>,
pub published: bool,
}
impl Migration {
/// Called only with bytes read under the writer's single readwrite transaction.
pub fn already_committed(&self, atn: Option<&[u8]>, legacy: Option<&[u8]>, backup: Option<&[u8]>) -> Result<bool, String> {
if atn == Some(self.atn.as_ref()) && legacy.is_none() && backup == Some(self.legacy.as_ref()) {
return Ok(true);
}
if legacy == Some(self.legacy.as_ref()) && backup.is_none() && atn == self.published.then_some(self.atn.as_ref()) {
return Ok(false);
}
Err("Actions migration records changed; original, ATN and backup retained unchanged".into())
}
}
#[derive(Clone)]
pub struct Batch {
pub files: Files,
pub migration: Option<Migration>,
@@ -105,13 +118,23 @@ impl Bridge {
return None;
}
s.writing = true;
let files = std::mem::take(&mut s.dirty)
let mut files: Files = std::mem::take(&mut s.dirty)
.into_iter()
.map(|name| {
let bytes = s.files.get(&name).cloned();
(name, bytes)
})
.collect();
if let Some(migration) = s.migration.clone() {
// Every ambiguous retry has the same exact identity. Newer live edits wait
// for this migration's acknowledgement, then get their own ordinary batch.
if s.files.get(ACTIONS_FILE).map(AsRef::as_ref) != Some(migration.atn.as_ref()) {
s.dirty.insert(ACTIONS_FILE.into());
}
files.insert(ACTIONS_FILE.into(), Some(migration.atn));
files.insert(MIGRATED_ACTIONS_FILE.into(), Some(migration.legacy));
files.insert(LEGACY_ACTIONS_FILE.into(), None);
}
Some(Batch { files, migration: s.migration.clone() })
}
@@ -279,11 +302,13 @@ mod tests {
}
#[test]
fn actions_migration_abort_requeues_whole_group_with_latest_atn() {
fn actions_migration_abort_retries_fixed_bytes_before_the_latest_atn() {
let bridge = Bridge::default();
bridge.load(LEGACY_ACTIONS_FILE, b"legacy".to_vec()).unwrap();
bridge.migrate_actions(b"legacy", b"first ATN", false).unwrap();
bridge.write(ACTIONS_FILE, b"edited before begin").unwrap();
let batch = bridge.begin().unwrap();
assert_eq!(batch.get(ACTIONS_FILE).unwrap().as_deref(), Some(&b"first ATN"[..]));
bridge.write(ACTIONS_FILE, b"edited ATN").unwrap();
bridge.finish(batch, Err("transaction aborted".into()));
assert!(bridge.unsaved());
@@ -292,12 +317,37 @@ mod tests {
let batch = bridge.begin().unwrap();
assert!(batch.migration.is_some());
assert_eq!(batch.len(), 3);
assert_eq!(batch.get(ACTIONS_FILE).unwrap().as_deref(), Some(&b"edited ATN"[..]));
assert_eq!(batch.get(ACTIONS_FILE).unwrap().as_deref(), Some(&b"first ATN"[..]));
assert_eq!(batch.get(MIGRATED_ACTIONS_FILE).unwrap().as_deref(), Some(&b"legacy"[..]));
bridge.finish(batch, Ok(()));
assert!(bridge.unsaved());
let next = bridge.begin().unwrap();
assert!(next.migration.is_none());
assert_eq!(next.len(), 1);
assert_eq!(next.get(ACTIONS_FILE).unwrap().as_deref(), Some(&b"edited ATN"[..]));
bridge.finish(next, Ok(()));
assert!(!bridge.unsaved());
}
#[test]
fn migration_recognizes_only_the_exact_fresh_or_committed_tuple() {
for published in [false, true] {
let migration = Migration { legacy: Arc::from(&b"legacy"[..]), atn: Arc::from(&b"ATN0"[..]), published };
assert!(!migration.already_committed(published.then_some(&b"ATN0"[..]), Some(b"legacy"), None).unwrap());
assert!(migration.already_committed(Some(b"ATN0"), None, Some(b"legacy")).unwrap());
for (atn, legacy, backup) in [
(Some(&b"ATN1"[..]), None, Some(&b"legacy"[..])),
(Some(&b"ATN0"[..]), Some(&b"change"[..]), None),
(Some(&b"ATN0"[..]), None, Some(&b"change"[..])),
(Some(&b"ATN0"[..]), Some(&b"legacy"[..]), Some(&b"legacy"[..])),
(None, None, Some(&b"legacy"[..])),
(Some(&b""[..]), None, Some(&b"legacy"[..])),
] {
assert!(migration.already_committed(atn, legacy, backup).is_err());
}
}
}
#[test]
fn actions_migration_validates_all_bindings_and_limits_before_mutation() {
let bridge = Bridge::default();
+3 -3
View File
@@ -1,6 +1,6 @@
//! The browser shell: web `Services`, drag-and-drop, and the eframe web runner.
use std::cell::Cell;
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
@@ -67,7 +67,7 @@ pub fn start() {
// Hydrate before constructing an interactive session: a late load must never undo a
// user deletion/reorder. Storage denial or timeout still opens a usable session.
let (database, presets, mut preset_warnings) = match crate::indexed_presets::load().await {
Ok((db, bridge, warnings)) => (Some(db), bridge, warnings),
Ok((db, bridge, warnings)) => (Some(Rc::new(RefCell::new(db))), bridge, warnings),
Err(e) => {
let bridge = crate::preset_bridge::Bridge::default();
bridge.unavailable(e);
@@ -271,7 +271,7 @@ struct WebShell {
/// Read by the `beforeunload` listener ([`guard_unload`]).
unsaved: Arc<AtomicBool>,
served: Served,
database: Option<web_sys::IdbDatabase>,
database: Option<Rc<RefCell<web_sys::IdbDatabase>>>,
presets: crate::preset_bridge::Bridge,
/// The interface language whose served script font was last asked for.
ui_language: &'static str,