Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(storage): update storage interfaces and lease migration

Steven Enamakel2d637aad2aee
diff --git a/crates/openhuman-core/src/profiles/mod.rs b/crates/openhuman-core/src/profiles/mod.rs
index 70546d5b43..1b456458b7 100644
--- a/crates/openhuman-core/src/profiles/mod.rs
+++ b/crates/openhuman-core/src/profiles/mod.rs
@@ -38,7 +38,7 @@ pub mod ops;
mod recovery;
pub mod registry;
mod storage_schema;
-pub(crate) use storage_schema::{collection_specs, upgrade_records};
+pub(crate) use storage_schema::{collection_specs, upgrade_lease_records, upgrade_records};
pub mod schemas;
pub mod surface;
pub mod tools;
diff --git a/crates/openhuman-core/src/profiles/registry.rs b/crates/openhuman-core/src/profiles/registry.rs
index e25431c70a..ba8801d553 100644
--- a/crates/openhuman-core/src/profiles/registry.rs
+++ b/crates/openhuman-core/src/profiles/registry.rs
@@ -16,9 +16,9 @@ use std::path::PathBuf;
use std::sync::Arc;
use tinystoragedrivers::{
- CollectionSpec, DocumentStoreExt, ErrorKind, Precondition, Query, Scope, ScopedStorage,
- Versioned, WriteOptions,
+ CollectionSpec, ErrorKind, Precondition, Query, Scope, Versioned, WriteOptions,
};
+use tinystoragedrivers_api::DocumentStore;
use super::layout::{self, ProfileLayout};
use super::storage_schema::migrate_cluster;
@@ -26,7 +26,7 @@ pub use super::storage_schema::REGISTRY_COLLECTION;
use super::types::{ProfileId, ProfileMeta};
use crate::storage::lease::CLUSTER_SCOPE;
use crate::storage::schema_support::key_of;
-use crate::storage::{DocumentStore, StorageBackend};
+use crate::storage::{DocumentStoreExt, Storage, StorageBackend};
/// The profile a registry document holds: the body's fields, and the creation
/// time (Unix seconds) from the record's metadata.
@@ -50,7 +50,7 @@ pub enum ProfileRegistry {
/// One document per profile in the shared backend.
Documents {
docs: Arc<dyn DocumentStore>,
- scoped: ScopedStorage,
+ scoped: Storage,
declared: tokio::sync::OnceCell<()>,
},
/// `profile.toml` under each profile's directory below `root`.
diff --git a/crates/openhuman-core/src/profiles/storage_schema.rs b/crates/openhuman-core/src/profiles/storage_schema.rs
index 49c74d2c5e..9f2246e17d 100644
--- a/crates/openhuman-core/src/profiles/storage_schema.rs
+++ b/crates/openhuman-core/src/profiles/storage_schema.rs
@@ -4,14 +4,13 @@
//! | v1 | v2 | scope |
//! |---|---|---|
//! | `profiles` (`{profile_id, created_at (seconds), layout_version}`) | `profiles.registry` (`{profile_id, layout_version}`; creation time in tsd metadata) | cluster |
-//! | `leases` (`{owner, endpoint, epoch, expires_at_ms, released}`) | `tsd.leases`, owner `profiles` (`{holder, epoch, expires_ms, endpoint?, released?}`) | cluster |
-//!
-//! The lease move is not a document step (a step writes only its own
-//! owner's collections), so [`migrate_legacy_leases`] does it after the steps.
+//! | `leases` (`{owner, endpoint, epoch, expires_at_ms, released}`) | host lease collection (`{holder, epoch, expires_ms, endpoint?, released?}`) | cluster |
//!
//! The cluster scope is not a tenant, so boot's per-tenant migration never
-//! visits it; [`migrate_cluster`] runs this owner's steps over it, the first
-//! time a registry opens the shared backend.
+//! visits it; [`migrate_cluster`] ensures this owner's declaration when a
+//! registry opens the shared backend. Legacy lease conversion is exposed as a
+//! typed import transform for the host importer; opening a profile registry
+//! never mutates a legacy collection.
use anyhow::Result;
use serde_json::Value;
@@ -82,86 +81,47 @@ pub async fn migrate_cluster(scoped: &Storage) -> Result<()> {
Ok(())
}
-/// The v1 lease collection.
-pub(super) const LEGACY_LEASE_COLLECTION: &str = "leases";
-
-/// The `tsd.leases` body of a v1 lease record, or `None` for a malformed one.
-fn lease_v2(old: &Value) -> Option<Value> {
- let mut body = serde_json::Map::new();
- body.insert("holder".into(), old.get("owner")?.as_str()?.into());
- body.insert("epoch".into(), old.get("epoch")?.as_u64()?.into());
- body.insert(
- "expires_ms".into(),
- i64::try_from(old.get("expires_at_ms")?.as_u64()?)
- .unwrap_or(i64::MAX)
- .into(),
- );
- if let Some(endpoint) = old.get("endpoint").and_then(Value::as_str) {
- body.insert("endpoint".into(), endpoint.into());
- }
- if old.get("released").and_then(Value::as_bool) == Some(true) {
- body.insert("released".into(), true.into());
- }
- Some(Value::Object(body))
-}
-
-/// Moves the v1 `leases` records of the cluster scope into `tsd.leases`
-/// (owner `profiles`), then drops the collection. A record already in
-/// `tsd.leases` wins: it is newer. Epochs carry over, so a holder fenced at
-/// the old epoch stays fenced. Returns the records moved.
-///
-/// This is a stop-the-world cutover, like the rest of schema v2 (the registry
-/// in this same scope moves the same way): every node of a deployment must
-/// run this version, because an old node would acquire in a recreated
-/// `leases` collection that new nodes never read.
-///
-/// # Errors
-///
-/// A storage error; the records not yet moved stay for the next open.
-pub async fn migrate_legacy_leases(scoped: &Storage) -> Result<usize> {
- use tinystoragedrivers::{Capability, DocumentStoreExt, Key, Precondition, Query};
- let docs = scoped.documents();
- // A driver that cannot list collections is asked for the records
- // directly, so a live legacy lease is never silently ignored.
- if docs
- .capabilities()
- .contains(Capability::CollectionEnumeration)
- && !docs
- .collections("")
- .await?
- .iter()
- .any(|name| name == LEGACY_LEASE_COLLECTION)
- {
- return Ok(0);
- }
- tinystoragedrivers::primitives::ensure(scoped).await?;
- let mut moved = 0;
- for legacy in docs
- .query_all(LEGACY_LEASE_COLLECTION, &Query::all())
- .await?
- {
- let Some(body) = lease_v2(&legacy.doc) else {
- log::warn!("[profiles][lease] skipping a malformed legacy lease record");
- continue;
- };
- let id = Key::of([crate::storage::lease::LEASE_OWNER, legacy.id.as_str()])?;
- match docs
- .put(
- tinystoragedrivers::primitives::LEASES,
- id.as_str(),
- body,
- Precondition::Absent,
- )
- .await
- {
- Ok(_) => moved += 1,
- Err(error) if error.kind() == tinystoragedrivers::ErrorKind::Conflict => {}
- Err(error) => return Err(error),
- }
- }
- docs.drop_collection(LEGACY_LEASE_COLLECTION).await?;
- log::info!("[profiles][lease] moved {moved} legacy lease records to tsd.leases");
- Ok(moved)
+/// Converts legacy lease documents to the host's typed lease import records.
+/// Invalid source rows fail closed so the importer cannot silently discard a
+/// live lease and admit an unfenced writer.
+pub(crate) fn upgrade_lease_records(
+ records: Vec<crate::storage::import::Record>,
+) -> Result<Vec<crate::storage::import::Record>> {
+ map_legacy_collection(records, "leases", |batch| {
+ batch
+ .into_iter()
+ .map(|record| {
+ let old = record.body;
+ let owner = old
+ .get("owner")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow::anyhow!("legacy lease has no owner"))?;
+ let epoch = old
+ .get("epoch")
+ .and_then(Value::as_u64)
+ .ok_or_else(|| anyhow::anyhow!("legacy lease has no valid epoch"))?;
+ let expires = old
+ .get("expires_at_ms")
+ .and_then(Value::as_i64)
+ .ok_or_else(|| anyhow::anyhow!("legacy lease has no valid expiry"))?;
+ let mut body = serde_json::Map::new();
+ body.insert("holder".into(), owner.into());
+ body.insert("epoch".into(), epoch.into());
+ body.insert("expires_ms".into(), expires.into());
+ if let Some(endpoint) = old.get("endpoint").and_then(Value::as_str) {
+ body.insert("endpoint".into(), endpoint.into());
+ }
+ if old.get("released").and_then(Value::as_bool) == Some(true) {
+ body.insert("released".into(), true.into());
+ }
+ Ok(TargetRecord::new(
+ "host.leases",
+ key_of(&record.id)?,
+ Value::Object(body),
+ ))
+ })
+ .collect()
+ })
}
#[cfg(test)]
diff --git a/crates/openhuman-core/src/storage/documents.rs b/crates/openhuman-core/src/storage/documents.rs
index 2e78310250..4e61a033d3 100644
--- a/crates/openhuman-core/src/storage/documents.rs
+++ b/crates/openhuman-core/src/storage/documents.rs
@@ -67,6 +67,10 @@ type OnceKey = (usize, String, &'static str, String);
static DONE_ONCE: LazyLock<Mutex<HashMap<OnceKey, Weak<dyn StorageBackend>>>> =
LazyLock::new(Mutex::default);
+type PreparedKey = (usize, String);
+static PREPARED_SCOPES: LazyLock<Mutex<HashMap<PreparedKey, Weak<dyn StorageBackend>>>> =
+ LazyLock::new(Mutex::default);
+
/// The backend and scope a [`Repo`] was opened on, for declaring its
/// collections once per process instead of on every call.
#[derive(Clone)]
@@ -137,6 +141,26 @@ impl Repo {
let scoped = backend
.for_scope(scope)
.with_context(|| format!("[{domain}] open the storage scope"))?;
+ let prepared_key = (backend_addr(backend), scope.as_str().to_owned());
+ let already_prepared = PREPARED_SCOPES
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .get(&prepared_key)
+ .and_then(Weak::upgrade)
+ .is_some_and(|alive| Arc::ptr_eq(&alive, backend));
+ if !already_prepared {
+ let to_prepare = scoped.clone();
+ block_on(async move {
+ super::boot::prepare_scope(&to_prepare).await?;
+ Ok(())
+ })
+ .with_context(|| format!("[{domain}] prepare owner collections"))?;
+ let mut prepared = PREPARED_SCOPES
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner);
+ prepared.retain(|_, weak| weak.strong_count() > 0);
+ prepared.insert(prepared_key, Arc::downgrade(backend));
+ }
let mut repo = Self::over(&scoped, domain, collections);
repo.origin = Some(Origin {
backend: Arc::clone(backend),
@@ -236,6 +260,18 @@ impl Repo {
})
.map_err(|error| anyhow!("[{domain}] storage: {error}"))
}
+
+ /// Runs an operation with the complete scope-bound six-port handle after
+ /// declaring this owner's document collections.
+ pub fn run_storage<T, F, Fut>(&self, op: F) -> Result<T>
+ where
+ F: FnOnce(Storage) -> Fut,
+ Fut: Future<Output = Result<T, StorageError>> + Send + 'static,
+ T: Send + 'static,
+ {
+ let storage = self.scoped.clone();
+ self.run(move |_| op(storage))
+ }
}
/// Applies `change` to document `id` in `collection` under compare-and-swap
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index e3d51621e2..0a06de43d2 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -55,7 +55,7 @@ pub use scope::{
};
use std::future::Future;
-use std::sync::{Arc, LazyLock, Mutex, OnceLock, RwLock};
+use std::sync::{Arc, LazyLock, Mutex, OnceLock, RwLock, mpsc};
/// SQLite bindings re-exported by the storage driver crate, for the two
/// consumers that open a SQLite file that is not a storage backend: the
@@ -110,17 +110,43 @@ impl<T: tinystoragedrivers_api::DocumentStoreExt + ?Sized> DocumentStoreExt for
/// asynchronous storage ports. Keeping it on its own thread also permits a
/// synchronous facade to be called while the caller is on a current-thread
/// Tokio runtime.
-struct Blocking(tokio::runtime::Runtime);
+struct Blocking {
+ tasks: mpsc::Sender<Box<dyn FnOnce(&tokio::runtime::Runtime) + Send>>,
+}
+
+thread_local! {
+ static ON_STORAGE_RUNTIME: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
+}
impl Blocking {
fn new() -> anyhow::Result<Self> {
- Ok(Self(
- tokio::runtime::Builder::new_multi_thread()
- .worker_threads(1)
- .enable_all()
- .thread_name("openhuman-storage")
- .build()?,
- ))
+ let (tasks, receiver) = mpsc::channel::<Box<dyn FnOnce(&tokio::runtime::Runtime) + Send>>();
+ let (started, ready) = mpsc::sync_channel(1);
+ std::thread::Builder::new()
+ .name("openhuman-storage".to_owned())
+ .spawn(move || {
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build();
+ match runtime {
+ Ok(runtime) => {
+ if started.send(Ok(())).is_err() {
+ return;
+ }
+ while let Ok(task) = receiver.recv() {
+ task(&runtime);
+ }
+ }
+ Err(error) => {
+ let _ = started.send(Err(error.to_string()));
+ }
+ }
+ })?;
+ ready
+ .recv()
+ .map_err(|error| anyhow::anyhow!("storage runtime failed to start: {error}"))?
+ .map_err(anyhow::Error::msg)?;
+ Ok(Self { tasks })
}
fn run<T, F>(&self, future: F) -> Result<T, StorageError>
@@ -128,7 +154,23 @@ impl Blocking {
F: Future<Output = Result<T, StorageError>> + Send + 'static,
T: Send + 'static,
{
- Ok(self.0.block_on(future)?)
+ if ON_STORAGE_RUNTIME.with(std::cell::Cell::get) {
+ return Err(StorageError::backend(
+ "synchronous storage bridge cannot be re-entered from its own runtime",
+ ));
+ }
+ let (reply, result) = mpsc::sync_channel(1);
+ self.tasks
+ .send(Box::new(move |runtime| {
+ ON_STORAGE_RUNTIME.with(|active| active.set(true));
+ let outcome = runtime.block_on(future);
+ ON_STORAGE_RUNTIME.with(|active| active.set(false));
+ let _ = reply.send(outcome);
+ }))
+ .map_err(|error| StorageError::backend(format!("storage bridge stopped: {error}")))?;
+ result
+ .recv()
+ .map_err(|error| StorageError::backend(format!("storage bridge stopped: {error}")))?
}
}
@@ -355,7 +397,7 @@ where
.get_or_init(|| Blocking::new().map_err(|error| error.to_string()))
.as_ref()
.map_err(|error| StorageError::backend(error.clone()))?;
- bridge.run(future)?
+ bridge.run(future)
}
/// [`block_on`] for a future whose error is an `anyhow::Error` — the stores
diff --git a/crates/openhuman-core/src/storage/mod_tests.rs b/crates/openhuman-core/src/storage/mod_tests.rs
index e3b803dc68..27b323007e 100644
--- a/crates/openhuman-core/src/storage/mod_tests.rs
+++ b/crates/openhuman-core/src/storage/mod_tests.rs
@@ -131,15 +131,18 @@ fn block_on_runs_a_future_from_sync_code() {
assert_eq!(error.message(), "raced");
}
-#[tokio::test(flavor = "multi_thread")]
-async fn block_on_works_inside_a_runtime_too() {
- let answer = tokio::task::spawn_blocking(|| block_on(async { Ok::<_, StorageError>(7) }))
- .await
- .unwrap()
- .unwrap();
+#[tokio::test(flavor = "current_thread")]
+async fn block_on_works_inside_a_current_thread_runtime() {
+ let answer = block_on(async { Ok::<_, StorageError>(7) }).unwrap();
assert_eq!(answer, 7);
}
+#[tokio::test(flavor = "multi_thread")]
+async fn block_on_works_inside_a_multi_thread_runtime() {
+ let answer = block_on(async { Ok::<_, StorageError>(8) }).unwrap();
+ assert_eq!(answer, 8);
+}
+
#[test]
fn only_mongodb_is_shared_between_processes() {
assert!(driver_is_shared("mongodb"));
diff --git a/crates/openhuman-core/src/storage/state_kv.rs b/crates/openhuman-core/src/storage/state_kv.rs
index b9ca28de25..3da6fee484 100644
--- a/crates/openhuman-core/src/storage/state_kv.rs
+++ b/crates/openhuman-core/src/storage/state_kv.rs
@@ -26,17 +26,18 @@ use anyhow::{Context, Result};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;
-use tinystoragedrivers::{
- CollectionSpec, DocumentStore, ErrorKind, Key, Kv, Scope, ScopedStorage, StorageError,
-};
+use tinystoragedrivers::{CollectionSpec, Cursor, ErrorKind, Precondition, Scope, StorageError};
+use tinystoragedrivers_api::KvStore;
+use sha2::Digest;
use super::documents::Repo;
use super::import_state;
+use super::Storage;
/// The `tsd.migrations` step kind of a state-file import.
const IMPORT_KIND: &str = "state_file";
-/// The body of a `tsd.kv` document, as `Kv` writes it.
+/// The envelope used to preserve exact host namespace/key identity in TSD KV.
#[derive(Serialize, Deserialize)]
struct KvEntry {
owner: String,
@@ -46,7 +47,7 @@ struct KvEntry {
}
fn collections() -> Vec<CollectionSpec> {
- tinystoragedrivers::primitives::collection_specs()
+ Vec::new()
}
/// One `(owner, namespace)` of runtime state values in one scope.
@@ -83,7 +84,7 @@ impl StateKv {
/// The state over an already scoped handle (tests, explicit scopes).
#[must_use]
- pub fn over(scoped: &ScopedStorage, owner: &'static str, ns: &'static str) -> Self {
+ pub fn over(scoped: &Storage, owner: &'static str, ns: &'static str) -> Self {
Self {
repo: Repo::over(scoped, "storage::state_kv", collections),
owner,
@@ -91,12 +92,13 @@ impl StateKv {
}
}
- fn kv(&self, docs: &Arc<dyn DocumentStore>) -> Result<Kv, StorageError> {
- Kv::ns(docs, self.owner, self.ns)
+ fn prefix(&self) -> String {
+ let namespace = format!("{}\0{}", self.owner, self.ns);
+ format!("openhuman-state:{}:", hex::encode(sha2::Sha256::digest(namespace.as_bytes())))
}
fn id(&self, name: &str) -> Result<String, StorageError> {
- Ok(Key::of([self.owner, self.ns, name])?.as_str().to_owned())
+ Ok(format!("{}{}", self.prefix(), hex::encode(sha2::Sha256::digest(name.as_bytes()))))
}
/// The value stored under `name`.
@@ -106,8 +108,15 @@ impl StateKv {
/// A storage error, or a stored value that is not a `T`.
pub fn get<T: DeserializeOwned + Send + 'static>(&self, name: &str) -> Result<Option<T>> {
let (this, name) = (self.clone(), name.to_owned());
- self.repo
- .run(move |docs| async move { this.kv(&docs)?.get::<T>(&name).await })
+ self.repo.run_storage(move |storage| async move {
+ let Some(record) = storage.kv().get(&this.id(&name)?).await? else {
+ return Ok(None);
+ };
+ let entry = this.decode_entry(&record.id, record.doc)?;
+ serde_json::from_value(entry.value)
+ .map(Some)
+ .map_err(|error| StorageError::invalid_input(error.to_string()))
+ })
}
/// Stores `value` under `name`, replacing any earlier one.
@@ -118,8 +127,13 @@ impl StateKv {
pub fn put<T: Serialize>(&self, name: &str, value: &T) -> Result<()> {
let (this, name) = (self.clone(), name.to_owned());
let value = serde_json::to_value(value).context("serialize the state value")?;
- self.repo
- .run(move |docs| async move { this.kv(&docs)?.put(&name, &value).await.map(|_| ()) })
+ self.repo.run_storage(move |storage| async move {
+ storage
+ .kv()
+ .put(&this.id(&name)?, this.entry(&name, value)?)
+ .await?;
+ Ok(())
+ })
}
/// Removes `name`, reporting whether it existed.
@@ -130,7 +144,12 @@ impl StateKv {
pub fn delete(&self, name: &str) -> Result<bool> {
let (this, name) = (self.clone(), name.to_owned());
self.repo
- .run(move |docs| async move { this.kv(&docs)?.delete(&name).await })
+ .run_storage(move |storage| async move {
+ match storage.kv().get(&this.id(&name)?).await? {
+ Some(current) => storage.kv().delete(&current.id, current.unchanged()).await,
+ None => Ok(false),
+ }
+ })
}
/// Every `(name, value)` whose name starts with `prefix`, in name order.
@@ -143,8 +162,27 @@ impl StateKv {
prefix: &str,
) -> Result<Vec<(String, T)>> {
let (this, prefix) = (self.clone(), prefix.to_owned());
- self.repo
- .run(move |docs| async move { this.kv(&docs)?.list::<T>(&prefix).await })
+ self.repo.run_storage(move |storage| async move {
+ let mut cursor: Option<Cursor> = None;
+ let mut entries = Vec::new();
+ loop {
+ let page = storage.kv().list(&this.prefix(), cursor, 256).await?;
+ for record in page.items {
+ let entry = this.decode_entry(&record.id, record.doc)?;
+ if entry.key.starts_with(&prefix) {
+ let value = serde_json::from_value(entry.value)
+ .map_err(|error| StorageError::invalid_input(error.to_string()))?;
+ entries.push((entry.key, value));
+ }
+ }
+ cursor = page.next;
+ if cursor.is_none() {
+ break;
+ }
+ }
+ entries.sort_by(|left, right| left.0.cmp(&right.0));
+ Ok(entries)
+ })
}
/// Stores `value` under `name` only when nothing is stored there yet.
@@ -154,20 +192,14 @@ impl StateKv {
///
/// A storage error.
pub fn insert_if_absent<T: Serialize>(&self, name: &str, value: &T) -> Result<bool> {
- let entry = KvEntry {
- owner: self.owner.to_owned(),
- ns: self.ns.to_owned(),
- key: name.to_owned(),
- value: serde_json::to_value(value).context("serialize the state value")?,
- };
+ let body = self.entry(
+ name,
+ serde_json::to_value(value).context("serialize the state value")?,
+ )?;
let this = self.clone();
let name = name.to_owned();
- self.repo.run(move |docs| async move {
- let repo = tinystoragedrivers::Repo::<KvEntry>::new(
- &docs,
- tinystoragedrivers::primitives::KV,
- )?;
- match repo.insert(&this.id(&name)?, &entry).await {
+ self.repo.run_storage(move |storage| async move {
+ match storage.kv().cas(&this.id(&name)?, body, Precondition::Absent).await {
Ok(_) => Ok(true),
Err(error) if error.kind() == ErrorKind::Conflict => Ok(false),
Err(error) => Err(error),
@@ -194,35 +226,61 @@ impl StateKv {
{
let this = self.clone();
let name = name.to_owned();
- self.repo.run(move |docs| async move {
- let repo = tinystoragedrivers::Repo::<KvEntry>::new(
- &docs,
- tinystoragedrivers::primitives::KV,
- )?
- .with_retry(tinystoragedrivers::RetryPolicy::default());
- let mut result = None;
- repo.update(&this.id(&name)?, |current| {
+ self.repo.run_storage(move |storage| async move {
+ for _ in 0..32 {
+ let current = storage.kv().get(&this.id(&name)?).await?;
let mut value: T = match &current {
- Some(entry) => serde_json::from_value(entry.value.clone())
- .map_err(|e| StorageError::invalid_input(e.to_string()))?,
+ Some(record) => {
+ let entry = this.decode_entry(&record.id, record.doc.clone())?;
+ serde_json::from_value(entry.value)
+ .map_err(|error| StorageError::invalid_input(error.to_string()))?
+ }
None => T::default(),
};
let (outcome, changed) = change(&mut value);
- result = Some(outcome);
if !changed {
- return Ok(None);
+ return Ok(outcome);
}
- Ok(Some(KvEntry {
- owner: this.owner.to_owned(),
- ns: this.ns.to_owned(),
- key: name.clone(),
- value: serde_json::to_value(&value)
- .map_err(|e| StorageError::invalid_input(e.to_string()))?,
- }))
- })
- .await?;
- result.ok_or_else(|| StorageError::backend("state update produced no result"))
+ let body = this.entry(
+ &name,
+ serde_json::to_value(&value)
+ .map_err(|error| StorageError::serialization(error.to_string()))?,
+ )?;
+ let expected = current
+ .as_ref()
+ .map_or(Precondition::Absent, |record| record.unchanged());
+ match storage.kv().cas(&this.id(&name)?, body, expected).await {
+ Ok(_) => return Ok(outcome),
+ Err(error) if error.kind() == ErrorKind::Conflict => continue,
+ Err(error) => return Err(error),
+ }
+ }
+ Err(StorageError::conflict("state update exceeded its retry budget"))
+ })
+ }
+
+ fn entry(&self, key: &str, value: Value) -> Result<Value, StorageError> {
+ serde_json::to_value(KvEntry {
+ owner: self.owner.to_owned(),
+ ns: self.ns.to_owned(),
+ key: key.to_owned(),
+ value,
})
+ .map_err(|error| StorageError::serialization(error.to_string()))
+ }
+
+ fn decode_entry(&self, storage_key: &str, value: Value) -> Result<KvEntry, StorageError> {
+ let entry: KvEntry = serde_json::from_value(value)
+ .map_err(|error| StorageError::invalid_input(error.to_string()))?;
+ if entry.owner != self.owner
+ || entry.ns != self.ns
+ || self.id(&entry.key)? != storage_key
+ {
+ return Err(StorageError::invalid_input(
+ "state KV identity envelope does not match its storage prefix",
+ ));
+ }
+ Ok(entry)
}
/// Copies the legacy JSON file `legacy` into `name` once for this backend