You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maka.apache.org by wy...@apache.org on 2026/09/16 01:18:32 UTC

(maka) 13/29: fix(storage): retain and revalidate named writer leases

This is an automated email from the ASF dual-hosted git repository.

M4n5ter pushed a commit to branch feat/runtime-host-rust
in repository https://gitbox.apache.org/repos/asf/maka.git

commit c6c0852c67761643fbeb34f6cdf98010a4d063fe
Author: Wang <m4...@gmail.com>
AuthorDate: Tue Sep 15 16:53:56 2026 +0800

    fix(storage): retain and revalidate named writer leases
    
    Generated-by: OpenAI Codex
---
 crates/event-log/src/connection.rs           | 22 ++++++++++++----------
 crates/event-log/src/connection/authority.rs | 14 ++++++++++----
 crates/event-log/src/lib.rs                  | 19 +++++++------------
 crates/event-log/src/root.rs                 |  1 +
 crates/event-log/src/root_lock.rs            | 26 +++++++++++++++++++++++++-
 5 files changed, 55 insertions(+), 27 deletions(-)

diff --git a/crates/event-log/src/connection.rs b/crates/event-log/src/connection.rs
index c84796e751..c65de9eff4 100644
--- a/crates/event-log/src/connection.rs
+++ b/crates/event-log/src/connection.rs
@@ -177,14 +177,7 @@ mod tests {
         let directory = tempfile::tempdir().unwrap();
         let path = directory.path().join("owner.sqlite");
         let lock = directory.path().join("writer.lock");
-        let lease = OpenOptions::new()
-            .read(true)
-            .write(true)
-            .create(true)
-            .truncate(false)
-            .open(&lock)
-            .unwrap();
-        lease.try_lock().unwrap();
+        let lease = Arc::new(crate::root::FileLease::acquire(&lock).unwrap());
         let contender = OpenOptions::new()
             .read(true)
             .write(true)
@@ -273,8 +266,7 @@ mod tests {
     async fn shutdown_is_idempotent_while_handles_remain_alive() {
         let directory = tempfile::tempdir().unwrap();
         let path = directory.path().join("writer.lock");
-        let lease = File::create(&path).unwrap();
-        lease.try_lock().unwrap();
+        let lease = Arc::new(crate::root::FileLease::acquire(&path).unwrap());
         let contender = File::open(&path).unwrap();
         let owner = Arc::new(
             OwnedConnection::open(
@@ -286,6 +278,16 @@ mod tests {
             .unwrap(),
         );
         let retained = owner.clone();
+        // Replacing the pathname must prevent the old worker from accepting new work.
+        std::fs::rename(&path, directory.path().join("displaced.lock")).unwrap();
+        let replacement = crate::root::FileLease::acquire(&path).unwrap();
+        assert!(matches!(
+            retained
+                .run::<()>(|_| Box::pin(async { panic!("replaced lease must reject work") }))
+                .await,
+            Err(StoreError::Io(_))
+        ));
+        drop(replacement);
         let (first, second) = tokio::join!(owner.shutdown(), retained.shutdown());
         first.unwrap();
         second.unwrap();
diff --git a/crates/event-log/src/connection/authority.rs b/crates/event-log/src/connection/authority.rs
index 8808c2cb12..c2742786c3 100644
--- a/crates/event-log/src/connection/authority.rs
+++ b/crates/event-log/src/connection/authority.rs
@@ -17,14 +17,17 @@
  * under the License.
  */
 
-use std::{fs::File, sync::Arc};
+use std::sync::Arc;
 
-use crate::{StoreError, root::RootOwner};
+use crate::{
+    StoreError,
+    root::{FileLease, RootOwner},
+};
 
 /// Authority retained until the database worker has actually closed.
 pub enum ConnectionAuthority {
     Writer {
-        lease: File,
+        lease: Arc<FileLease>,
         root: Option<Arc<RootOwner>>,
     },
     Root(Arc<RootOwner>),
@@ -33,7 +36,10 @@ pub enum ConnectionAuthority {
 impl ConnectionAuthority {
     pub(super) fn validate(&self) -> Result<(), StoreError> {
         let root = match self {
-            Self::Writer { root, .. } => root.as_ref(),
+            Self::Writer { root, lease } => {
+                lease.validate()?;
+                root.as_ref()
+            }
             Self::Root(root) => Some(root),
         };
         if let Some(root) = root {
diff --git a/crates/event-log/src/lib.rs b/crates/event-log/src/lib.rs
index d5108de4e2..8578d422f3 100644
--- a/crates/event-log/src/lib.rs
+++ b/crates/event-log/src/lib.rs
@@ -53,7 +53,6 @@ pub mod transcript;
 pub mod turns;
 pub mod workhub;
 
-use std::fs::OpenOptions;
 use std::path::Path;
 use std::sync::Arc;
 
@@ -189,17 +188,13 @@ impl EventLog {
         // byte-range locks on the database. Never unlink it while in use.
         let mut lock_path = path.as_os_str().to_os_string();
         lock_path.push(".writer.lock");
-        let lease = OpenOptions::new()
-            .read(true)
-            .write(true)
-            .create(true)
-            .truncate(false)
-            .open(lock_path)?;
-        match lease.try_lock() {
-            Ok(()) => {}
-            Err(std::fs::TryLockError::WouldBlock) => return Err(StoreError::WriterBusy),
-            Err(std::fs::TryLockError::Error(error)) => return Err(StoreError::Io(error)),
-        }
+        let lease = match root::FileLease::acquire(Path::new(&lock_path)) {
+            Ok(lease) => Arc::new(lease),
+            Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
+                return Err(StoreError::WriterBusy);
+            }
+            Err(error) => return Err(StoreError::Io(error)),
+        };
         let connection = OwnedConnection::open(
             SqliteConnectOptions::new()
                 .filename(path)
diff --git a/crates/event-log/src/root.rs b/crates/event-log/src/root.rs
index ccd3e2028f..29d23ae572 100644
--- a/crates/event-log/src/root.rs
+++ b/crates/event-log/src/root.rs
@@ -21,6 +21,7 @@
 
 #[path = "root_lock.rs"]
 mod lock;
+pub use lock::{FileLease, private_directory};
 
 #[cfg(windows)]
 pub mod windows;
diff --git a/crates/event-log/src/root_lock.rs b/crates/event-log/src/root_lock.rs
index f316dfa732..d0f683d480 100644
--- a/crates/event-log/src/root_lock.rs
+++ b/crates/event-log/src/root_lock.rs
@@ -27,6 +27,30 @@ use std::{
     path::{Path, PathBuf},
 };
 
+/// A stable, exclusively locked file. Never unlink it as part of cleanup.
+/// Uses native whole-file locks; State Root's TS-compatible leases are separate.
+pub struct FileLease {
+    file: File,
+    path: PathBuf,
+}
+
+impl FileLease {
+    pub fn acquire(path: &Path) -> io::Result<Self> {
+        let file = open_regular(path, true)?;
+        file.try_lock().map_err(io::Error::from)?;
+        let lease = Self {
+            file,
+            path: path.to_owned(),
+        };
+        lease.validate()?;
+        Ok(lease)
+    }
+
+    pub fn validate(&self) -> io::Result<()> {
+        stable(&self.file, &self.path)
+    }
+}
+
 #[cfg(unix)]
 pub(super) fn identity(metadata: &Metadata) -> io::Result<(u64, u64)> {
     use std::os::unix::fs::MetadataExt;
@@ -108,7 +132,7 @@ pub(super) fn acquire(path: &Path) -> io::Result<File> {
     Ok(file)
 }
 
-pub(super) fn private_directory(path: &Path) -> io::Result<()> {
+pub fn private_directory(path: &Path) -> io::Result<()> {
     #[cfg(unix)]
     {
         use std::os::unix::fs::{DirBuilderExt, MetadataExt, PermissionsExt};


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org