use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread::JoinHandle;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use anyhow::{Context, Result, anyhow};
use serde::{Deserialize, Serialize};
use super::atomic::atomic_write_bytes;
pub const LOCK_FILENAME: &str = ".rgstore.lock";
pub const STALE_LOCK_PREFIX: &str = ".rgstore.lock.stale.";
pub const BREAK_FILENAME: &str = ".rgstore.lock.break";
const BREAK_STALE_AFTER: Duration = Duration::from_secs(60);
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(15);
const HEARTBEAT_TICK: Duration = Duration::from_millis(100);
const DEFAULT_TIMEOUT_SECS: u64 = 30 * 60;
const DEFAULT_STALE_AFTER_SECS: u64 = 120;
const BACKOFF_START: Duration = Duration::from_millis(50);
const BACKOFF_MAX: Duration = Duration::from_secs(5);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockInfo {
pub pid: u32,
pub hostname: String,
pub started_at: String,
pub heartbeat_at: String,
pub heartbeat_epoch: u64,
pub operation: String,
pub gtars_version: String,
}
impl LockInfo {
fn new(operation: &str) -> Self {
let now = chrono::Utc::now().to_rfc3339();
LockInfo {
pid: std::process::id(),
hostname: hostname(),
started_at: now.clone(),
heartbeat_at: now,
heartbeat_epoch: epoch_secs(),
operation: operation.to_string(),
gtars_version: env!("CARGO_PKG_VERSION").to_string(),
}
}
pub fn describe(&self) -> String {
format!(
"pid {} on {} (operation={}, gtars {}, since {})",
self.pid, self.hostname, self.operation, self.gtars_version, self.started_at
)
}
}
#[derive(Debug, Clone)]
pub struct LockOptions {
pub timeout: Option<Duration>,
pub stale_after: Duration,
pub force: bool,
}
impl Default for LockOptions {
fn default() -> Self {
LockOptions {
timeout: env_secs("GTARS_STORE_LOCK_TIMEOUT")
.unwrap_or(Some(Duration::from_secs(DEFAULT_TIMEOUT_SECS))),
stale_after: env_secs("GTARS_STORE_LOCK_STALE_AFTER")
.flatten()
.unwrap_or(Duration::from_secs(DEFAULT_STALE_AFTER_SECS)),
force: false,
}
}
}
impl LockOptions {
pub fn non_blocking() -> Self {
LockOptions {
timeout: Some(Duration::ZERO),
..Default::default()
}
}
#[must_use]
pub fn timeout_secs(mut self, secs: u64) -> Self {
self.timeout = if secs == 0 {
None
} else {
Some(Duration::from_secs(secs))
};
self
}
#[must_use]
pub fn force(mut self, force: bool) -> Self {
self.force = force;
self
}
}
pub struct StoreLock {
path: PathBuf,
info: LockInfo,
stop: Arc<AtomicBool>,
heartbeat: Option<JoinHandle<()>>,
}
impl std::fmt::Debug for StoreLock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StoreLock")
.field("path", &self.path)
.field("pid", &self.info.pid)
.field("operation", &self.info.operation)
.finish()
}
}
impl StoreLock {
pub fn lock_path(store_dir: &Path) -> PathBuf {
store_dir.join(LOCK_FILENAME)
}
pub fn acquire(store_dir: &Path, operation: &str, opts: LockOptions) -> Result<Self> {
let path = Self::lock_path(store_dir);
fs::create_dir_all(store_dir)
.with_context(|| format!("creating store directory {:?} for locking", store_dir))?;
if opts.force {
steal(&path)?;
}
let deadline = opts.timeout.map(|t| SystemTime::now() + t);
let mut backoff = BACKOFF_START;
loop {
match try_create(&path, operation) {
Ok(Some(info)) => {
let stop = Arc::new(AtomicBool::new(false));
let heartbeat = spawn_heartbeat(path.clone(), info.clone(), stop.clone());
return Ok(StoreLock {
path,
info,
stop,
heartbeat: Some(heartbeat),
});
}
Ok(None) => {} Err(e) => return Err(e),
}
match read_info(&path) {
Ok(Some(holder)) => {
if let Some(reason) = staleness(&path, &holder, opts.stale_after) {
eprintln!(
"warning: breaking stale RefgetStore lock {:?}: {} ({})",
path,
holder.describe(),
reason
);
let _ = break_stale(&path, &holder);
continue;
}
if deadline.is_some_and(|d| SystemTime::now() >= d) {
return Err(anyhow!(
"timed out waiting for the RefgetStore write lock at {:?}, \
held by {}. If that writer is gone, clear it with \
`gtars refget lock-status <store> --force-unlock`, or raise \
GTARS_STORE_LOCK_TIMEOUT.",
path,
holder.describe()
));
}
}
Ok(None) | Err(_) => {
if mtime_age(&path).is_none_or(|age| age > opts.stale_after) {
eprintln!(
"warning: breaking unreadable RefgetStore lock {:?} (no valid payload)",
path
);
let _ = break_unreadable(&path, opts.stale_after);
continue;
}
if deadline.is_some_and(|d| SystemTime::now() >= d) {
return Err(anyhow!(
"timed out waiting for the RefgetStore write lock at {:?} \
(lockfile payload unreadable)",
path
));
}
}
}
std::thread::sleep(backoff);
backoff = std::cmp::min(backoff * 2, BACKOFF_MAX);
}
}
pub fn try_acquire(store_dir: &Path, operation: &str) -> Result<Option<Self>> {
match Self::acquire(store_dir, operation, LockOptions::non_blocking()) {
Ok(lock) => Ok(Some(lock)),
Err(_) => Ok(None),
}
}
pub fn info(&self) -> &LockInfo {
&self.info
}
}
impl Drop for StoreLock {
fn drop(&mut self) {
self.stop.store(true, Ordering::Relaxed);
if let Some(handle) = self.heartbeat.take() {
let _ = handle.join();
}
if let Ok(Some(current)) = read_info(&self.path) {
if current.pid != self.info.pid || current.hostname != self.info.hostname {
return;
}
}
let _ = fs::remove_file(&self.path);
}
}
pub fn lock_status(store_dir: &Path) -> Result<Option<LockInfo>> {
read_info(&StoreLock::lock_path(store_dir))
}
pub fn force_unlock(store_dir: &Path) -> Result<bool> {
let path = StoreLock::lock_path(store_dir);
let existed = path.exists();
if existed {
steal(&path)?;
}
if let Ok(entries) = fs::read_dir(store_dir) {
for entry in entries.filter_map(|e| e.ok()) {
if entry
.file_name()
.to_str()
.is_some_and(|n| n.starts_with(STALE_LOCK_PREFIX) || n == BREAK_FILENAME)
{
let _ = fs::remove_file(entry.path());
}
}
}
Ok(existed)
}
fn try_create(path: &Path, operation: &str) -> Result<Option<LockInfo>> {
match OpenOptions::new().write(true).create_new(true).open(path) {
Ok(mut file) => {
let info = LockInfo::new(operation);
let json = serde_json::to_vec_pretty(&info)?;
file.write_all(&json)?;
file.sync_all()?;
Ok(Some(info))
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(None),
Err(e) => Err(e).with_context(|| format!("creating lockfile {:?}", path)),
}
}
fn read_info(path: &Path) -> Result<Option<LockInfo>> {
match fs::read_to_string(path) {
Ok(s) => Ok(serde_json::from_str(&s).ok()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e).with_context(|| format!("reading lockfile {:?}", path)),
}
}
fn steal(path: &Path) -> Result<()> {
let dir = path.parent().unwrap_or_else(|| Path::new("."));
let stolen = dir.join(format!(
"{}{}.{}",
STALE_LOCK_PREFIX,
std::process::id(),
nanos()
));
match fs::rename(path, &stolen) {
Ok(()) => {
let _ = fs::remove_file(&stolen);
Ok(())
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e).with_context(|| format!("stealing stale lockfile {:?}", path)),
}
}
struct BreakClaim(PathBuf);
impl Drop for BreakClaim {
fn drop(&mut self) {
let _ = fs::remove_file(&self.0);
}
}
fn claim_break(path: &Path) -> Result<Option<BreakClaim>> {
let dir = path.parent().unwrap_or_else(|| Path::new("."));
let marker = dir.join(BREAK_FILENAME);
match OpenOptions::new().write(true).create_new(true).open(&marker) {
Ok(mut f) => {
let _ = write!(f, "{} {}", std::process::id(), hostname());
Ok(Some(BreakClaim(marker)))
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
if mtime_age(&marker).is_some_and(|age| age > BREAK_STALE_AFTER) {
let dead = dir.join(format!("{}break.{}.{}", STALE_LOCK_PREFIX, std::process::id(), nanos()));
if fs::rename(&marker, &dead).is_ok() {
let _ = fs::remove_file(&dead);
}
}
Ok(None)
}
Err(e) => Err(e).with_context(|| format!("creating break marker {:?}", marker)),
}
}
fn break_stale(path: &Path, judged: &LockInfo) -> Result<()> {
let Some(_claim) = claim_break(path)? else {
return Ok(());
};
match read_info(path)? {
Some(current)
if current.pid == judged.pid
&& current.hostname == judged.hostname
&& current.started_at == judged.started_at =>
{
steal(path)
}
_ => Ok(()),
}
}
fn break_unreadable(path: &Path, stale_after: Duration) -> Result<()> {
let Some(_claim) = claim_break(path)? else {
return Ok(());
};
match read_info(path)? {
Some(_) => Ok(()), None => {
if mtime_age(path).is_none_or(|age| age > stale_after) {
steal(path)
} else {
Ok(())
}
}
}
}
fn staleness(path: &Path, holder: &LockInfo, stale_after: Duration) -> Option<String> {
if holder.hostname == hostname() && !pid_alive(holder.pid) {
return Some(format!("pid {} is not running on this host", holder.pid));
}
let now = epoch_secs();
let heartbeat_age = Duration::from_secs(now.saturating_sub(holder.heartbeat_epoch));
let file_age = mtime_age(path)?;
if heartbeat_age > stale_after && file_age > stale_after {
return Some(format!(
"no heartbeat for {}s (threshold {}s)",
heartbeat_age.as_secs(),
stale_after.as_secs()
));
}
None
}
fn mtime_age(path: &Path) -> Option<Duration> {
let modified = fs::metadata(path).ok()?.modified().ok()?;
SystemTime::now().duration_since(modified).ok()
}
#[cfg(target_os = "linux")]
fn pid_alive(pid: u32) -> bool {
Path::new(&format!("/proc/{}", pid)).exists()
}
#[cfg(not(target_os = "linux"))]
fn pid_alive(_pid: u32) -> bool {
true
}
fn hostname() -> String {
for path in ["/proc/sys/kernel/hostname", "/etc/hostname"] {
if let Ok(s) = fs::read_to_string(path) {
let s = s.trim();
if !s.is_empty() {
return s.to_string();
}
}
}
for var in ["HOSTNAME", "COMPUTERNAME"] {
if let Ok(s) = std::env::var(var) {
if !s.is_empty() {
return s;
}
}
}
"unknown-host".to_string()
}
fn epoch_secs() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
fn nanos() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)
}
fn env_secs(var: &str) -> Option<Option<Duration>> {
let raw = std::env::var(var).ok()?;
let secs: u64 = raw.trim().parse().ok()?;
Some(if secs == 0 {
None
} else {
Some(Duration::from_secs(secs))
})
}
fn spawn_heartbeat(path: PathBuf, info: LockInfo, stop: Arc<AtomicBool>) -> JoinHandle<()> {
std::thread::spawn(move || {
let mut elapsed = Duration::ZERO;
while !stop.load(Ordering::Relaxed) {
std::thread::sleep(HEARTBEAT_TICK);
elapsed += HEARTBEAT_TICK;
if elapsed < HEARTBEAT_INTERVAL {
continue;
}
elapsed = Duration::ZERO;
match read_info(&path) {
Ok(Some(current))
if current.pid == info.pid && current.hostname == info.hostname => {}
_ => return,
}
let mut refreshed = info.clone();
refreshed.heartbeat_at = chrono::Utc::now().to_rfc3339();
refreshed.heartbeat_epoch = epoch_secs();
if let Ok(json) = serde_json::to_vec_pretty(&refreshed) {
let _ = atomic_write_bytes(&path, &json);
}
}
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn acquire_and_release() {
let dir = tempfile::tempdir().unwrap();
let path = StoreLock::lock_path(dir.path());
{
let lock = StoreLock::acquire(dir.path(), "test", LockOptions::default()).unwrap();
assert!(path.exists());
assert_eq!(lock.info().operation, "test");
assert!(lock_status(dir.path()).unwrap().is_some());
}
assert!(!path.exists());
assert!(lock_status(dir.path()).unwrap().is_none());
}
#[test]
fn second_acquire_times_out_while_first_is_held() {
let dir = tempfile::tempdir().unwrap();
let _held = StoreLock::acquire(dir.path(), "first", LockOptions::default()).unwrap();
let err = StoreLock::acquire(
dir.path(),
"second",
LockOptions::default().timeout_secs(1),
);
assert!(err.is_err());
let msg = err.unwrap_err().to_string();
assert!(msg.contains("timed out"), "unexpected error: {}", msg);
assert!(msg.contains("first"), "error should name the holder: {}", msg);
}
#[test]
fn stale_lock_from_dead_pid_on_this_host_is_broken() {
let dir = tempfile::tempdir().unwrap();
let path = StoreLock::lock_path(dir.path());
let dead_pid = 4_194_305u32;
let mut info = LockInfo::new("abandoned");
info.pid = dead_pid;
info.hostname = hostname();
fs::write(&path, serde_json::to_vec_pretty(&info).unwrap()).unwrap();
let lock = StoreLock::acquire(dir.path(), "recovering", LockOptions::default()).unwrap();
assert_eq!(lock.info().pid, std::process::id());
}
#[test]
fn old_heartbeat_from_another_host_is_presumed_stale() {
let dir = tempfile::tempdir().unwrap();
let path = StoreLock::lock_path(dir.path());
let mut info = LockInfo::new("remote");
info.hostname = "some-other-node".to_string();
info.heartbeat_epoch = epoch_secs().saturating_sub(10_000);
fs::write(&path, serde_json::to_vec_pretty(&info).unwrap()).unwrap();
let backdated = SystemTime::now() - Duration::from_secs(10_000);
let f = fs::File::open(&path).unwrap();
f.set_modified(backdated).unwrap();
drop(f);
let opts = LockOptions {
stale_after: Duration::from_secs(120),
..LockOptions::default()
};
let lock = StoreLock::acquire(dir.path(), "taking-over", opts).unwrap();
assert_eq!(lock.info().hostname, hostname());
}
#[test]
fn live_remote_holder_is_not_broken() {
let dir = tempfile::tempdir().unwrap();
let path = StoreLock::lock_path(dir.path());
let mut info = LockInfo::new("remote");
info.hostname = "some-other-node".to_string();
fs::write(&path, serde_json::to_vec_pretty(&info).unwrap()).unwrap();
let result = StoreLock::acquire(
dir.path(),
"contender",
LockOptions::default().timeout_secs(1),
);
assert!(result.is_err(), "a live remote holder must not be broken");
}
#[test]
fn force_unlock_clears_a_held_lock() {
let dir = tempfile::tempdir().unwrap();
let mut info = LockInfo::new("remote");
info.hostname = "some-other-node".to_string();
fs::write(
StoreLock::lock_path(dir.path()),
serde_json::to_vec_pretty(&info).unwrap(),
)
.unwrap();
assert!(force_unlock(dir.path()).unwrap());
assert!(lock_status(dir.path()).unwrap().is_none());
assert!(!force_unlock(dir.path()).unwrap());
}
#[test]
fn drop_does_not_remove_a_lock_another_writer_took_over() {
let dir = tempfile::tempdir().unwrap();
let path = StoreLock::lock_path(dir.path());
let lock = StoreLock::acquire(dir.path(), "original", LockOptions::default()).unwrap();
let mut other = LockInfo::new("takeover");
other.pid = lock.info().pid + 1;
fs::write(&path, serde_json::to_vec_pretty(&other).unwrap()).unwrap();
drop(lock);
let remaining = lock_status(dir.path()).unwrap().expect("lock was clobbered");
assert_eq!(remaining.operation, "takeover");
}
}