Skip to content

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

feat(storage): add lease documents for fenced storage access

Steven Enamakel56d5f2403c00
diff --git a/crates/openhuman-core/src/storage/fence.rs b/crates/openhuman-core/src/storage/fence.rs
index afc02483f9..40cf9637c1 100644
--- a/crates/openhuman-core/src/storage/fence.rs
+++ b/crates/openhuman-core/src/storage/fence.rs
@@ -57,9 +57,9 @@ use std::any::Any;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, LazyLock, Mutex, PoisonError, Weak};
-use tinystoragedrivers::{Fence, Filter};
+use tinystoragedrivers::Fence;
-use super::lease::{LeaseStore, LEASE_COLLECTION};
+use super::lease::LeaseStore;
use super::StorageError;
/// Why a fenced write was refused.
@@ -194,20 +194,14 @@ impl LeaseFence {
}
/// The fence a storage driver enforces for this grant: the lease record
- /// (where the lease store keeps it, [`LeaseStore::record_scope`]) must
+ /// (where the lease store keeps it, [`LeaseStore::driver_fence`]) must
/// still name this node at this epoch, unreleased. `None` when the lease
/// store keeps no record in the storage ports (file leases) or is gone;
/// then only [`Self::check`] guards the write.
pub fn driver_fence(&self) -> Option<Fence> {
- let scope = self.leases.upgrade()?.record_scope()?;
- Some(Fence::new(
- scope,
- LEASE_COLLECTION,
- self.key.clone(),
- Filter::eq("epoch", self.epoch)
- .and(Filter::eq("owner", self.node.clone()))
- .and(Filter::eq("released", false)),
- ))
+ self.leases
+ .upgrade()?
+ .driver_fence(&self.key, &self.node, self.epoch)
}
/// The refusal for a write the storage driver fenced off
diff --git a/crates/openhuman-core/src/storage/lease.rs b/crates/openhuman-core/src/storage/lease.rs
index fc9765b0a6..f4f25a36d2 100644
--- a/crates/openhuman-core/src/storage/lease.rs
+++ b/crates/openhuman-core/src/storage/lease.rs
@@ -4,9 +4,10 @@
//! serves a profile) and until when. Two implementations share the
//! [`LeaseStore`] contract:
//!
-//! - [`DocumentLeases`] keeps one record per key in the document port
-//! (scope [`CLUSTER_SCOPE`], collection [`LEASE_COLLECTION`]) and moves it
-//! only by compare-and-swap, so it is correct across processes on a driver
+//! - [`DocumentLeases`] keeps one record per key in tinystoragedrivers'
+//! `tsd.leases` ([`LEASE_COLLECTION`], scope [`CLUSTER_SCOPE`]) through
+//! its [`Leases`](tinystoragedrivers::Leases) primitive, which moves it only
+//! by compare-and-swap, so it is correct across processes on a driver
//! with cross-process CAS ([`super::driver_has_cross_process_cas`]).
//! - [`LocalLeases`] is for hosts without a backend: an exclusive `flock` on
//! `<root>/<sha256(key) hex>/.lease`, which the OS drops when the holder dies.
@@ -28,10 +29,10 @@
//! node supplies its own `now_ms`). This module does not check epochs at
//! the protected resource; [`super::fence`] does, for storage-port writes. An epoch at `u64::MAX` cannot be advanced, so a
//! takeover of it fails with a storage error instead of repeating it.
-//! - **renew** extends a grant by compare-and-swap on the version it holds.
-//! Any write since (a takeover, a release) makes it [`LeaseError::Lost`].
-//! - **release** marks the record `released = true` under the same CAS and
-//! keeps it, so the next acquirer can tell a clean hand-over from a crash.
+//! - **renew** extends a grant while the stored record still names its holder
+//! and epoch. Any takeover or release since makes it [`LeaseError::Lost`].
+//! - **release** marks the record `released = true` and keeps it, so the next
+//! acquirer can tell a clean hand-over from a crash.
//!
//! # `previous_unclean`
//!
@@ -51,7 +52,6 @@ use std::path::PathBuf;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
-use tinystoragedrivers::Version;
use super::StorageError;
@@ -61,8 +61,8 @@ pub use super::lease_local::LocalLeases;
/// The storage scope lease records live under, shared by every node.
pub const CLUSTER_SCOPE: &str = "cluster";
-/// The collection lease records live in.
-pub const LEASE_COLLECTION: &str = "leases";
+/// The collection lease records live in: tinystoragedrivers' `tsd.leases`.
+pub const LEASE_COLLECTION: &str = tinystoragedrivers::primitives::LEASES;
/// The longest key a lease accepts, in bytes.
pub const MAX_KEY_LEN: usize = 200;
@@ -108,8 +108,6 @@ pub struct LeaseGrant {
pub key: String,
/// The epoch this grant fences with.
pub epoch: u64,
- /// The record version this grant wrote; renew and release CAS on it.
- pub version: Version,
/// The grant is void from this instant (milliseconds) on unless renewed.
pub expires_at_ms: u64,
/// The acquisition replaced a record nobody released (see the module
@@ -167,13 +165,18 @@ pub trait LeaseStore: Send + Sync {
/// [`LeaseError::Storage`].
async fn holder(&self, key: &str) -> Result<Option<LeaseRecord>, LeaseError>;
- /// The storage scope whose [`LEASE_COLLECTION`] holds this store's
- /// records (document id = key), when they live in the storage ports at
- /// all. A storage driver with fencing can then check a write against the
- /// record in the same atomic step ([`super::fence::LeaseFence::driver_fence`]).
- /// `None` (the default) for stores that keep records elsewhere, such as
- /// file locks.
- fn record_scope(&self) -> Option<tinystoragedrivers::Scope> {
+ /// The fence a storage driver enforces for `node`'s grant on `key` at
+ /// `epoch`, when the record lives in the storage ports at all: the
+ /// record must still name `node` at `epoch`, unreleased. The driver then
+ /// checks a write against the record in the same atomic step
+ /// ([`super::fence::LeaseFence::driver_fence`]). `None` (the default) for
+ /// stores that keep records elsewhere, such as file locks.
+ fn driver_fence(
+ &self,
+ _key: &str,
+ _node: &str,
+ _epoch: u64,
+ ) -> Option<tinystoragedrivers::Fence> {
None
}
}
diff --git a/crates/openhuman-core/src/storage/lease_documents.rs b/crates/openhuman-core/src/storage/lease_documents.rs
index 5e7c1acb2f..dbd90fc165 100644
--- a/crates/openhuman-core/src/storage/lease_documents.rs
+++ b/crates/openhuman-core/src/storage/lease_documents.rs
@@ -270,10 +270,14 @@ impl LeaseStore for DocumentLeases {
return Err(LeaseError::Lost);
}
self.declare().await?;
- // `release` ends the lease at the moment it is called, by tsd's own
- // clock reading; the caller's clock is only for acquire and renew.
+ // A release carries no clock of its own: it is judged at the instant
+ // the grant was last issued (its expiry less one TTL), when it was
+ // live by construction, so tsd marks the record released.
+ let issued_ms = grant
+ .expires_at_ms
+ .saturating_sub(self.ttl_ms.unsigned_abs());
let released = self
- .leases_at(grant.expires_at_ms.saturating_sub(self.ttl_ms.unsigned_abs()))
+ .leases_at(issued_ms)
.release(LEASE_OWNER, &grant.key, &self.node, grant.epoch)
.await?;
if !released {