use crate::ObjectLogError;
use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use std::collections::BTreeMap;
use std::io::{Read, Seek, SeekFrom, Write};
use std::ops::Range;
use std::os::unix::io::AsRawFd;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Instant, SystemTime, UNIX_EPOCH};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CasOutcome {
Stored,
Conflict {
current: Option<Bytes>,
},
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct MediaOpStats {
pub media_ops: u64,
pub bytes: u64,
}
const TMP_SUFFIX: &str = ".olog-tmp";
fn diagnostic_unix_us() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_micros()
}
fn object_class(key: &str) -> &'static str {
if key.starts_with("fwlog/") {
"segment"
} else if key.starts_with("fwmeta/manifest/") {
"manifest"
} else {
"metadata"
}
}
fn diagnostic_store_tag(path: &Path) -> String {
let mut fallback = None;
for component in path.components() {
let name = component.as_os_str().to_string_lossy();
if let Some(rest) = name.strip_prefix("shard-")
&& !rest.is_empty()
&& rest.bytes().all(|b| b.is_ascii_digit())
{
return name.into_owned();
}
if !matches!(
name.as_ref(),
"log" | "fwlog" | "fwmeta" | "manifest" | "projection.db" | "/" | "."
) {
fallback = Some(name.into_owned());
}
}
fallback
.filter(|name| !name.is_empty())
.unwrap_or_else(|| "unknown".into())
}
#[async_trait]
pub trait BlobStore: Send + Sync {
async fn put(&self, key: &str, value: Bytes) -> Result<(), ObjectLogError>;
async fn put_chunks(&self, key: &str, chunks: Vec<Bytes>) -> Result<(), ObjectLogError> {
match chunks.len() {
0 => self.put(key, Bytes::new()).await,
1 => self.put(key, chunks.into_iter().next().unwrap()).await,
_ => {
let total = chunks.iter().map(Bytes::len).sum();
let mut value = BytesMut::with_capacity(total);
for chunk in chunks {
value.extend_from_slice(&chunk);
}
self.put(key, value.freeze()).await
}
}
}
async fn get(&self, key: &str) -> Result<Option<Bytes>, ObjectLogError>;
async fn get_range(
&self,
key: &str,
range: Range<u64>,
) -> Result<Option<Bytes>, ObjectLogError>;
async fn list(&self, prefix: &str) -> Result<Vec<String>, ObjectLogError>;
async fn delete(&self, key: &str) -> Result<(), ObjectLogError>;
async fn compare_and_swap(
&self,
key: &str,
expected: Option<Bytes>,
new_value: Bytes,
) -> Result<CasOutcome, ObjectLogError> {
let current = self.get(key).await?;
let matches = match (&expected, ¤t) {
(None, None) => true,
(Some(expected), Some(current)) => expected == current,
_ => false,
};
if !matches {
return Ok(CasOutcome::Conflict { current });
}
self.put(key, new_value).await?;
Ok(CasOutcome::Stored)
}
fn take_media_op_stats(&self) -> Option<MediaOpStats> {
None
}
}
fn slice_range(bytes: &Bytes, range: Range<u64>) -> Result<Bytes, ObjectLogError> {
if range.start > range.end {
return Err(ObjectLogError::RangeOutOfBounds(format!(
"start {} > end {}",
range.start, range.end
)));
}
let len = bytes.len() as u64;
if range.end > len {
return Err(ObjectLogError::RangeOutOfBounds(format!(
"end {} > len {len}",
range.end
)));
}
Ok(bytes.slice(range.start as usize..range.end as usize))
}
#[derive(Clone, Default)]
pub struct MemoryBlobStore {
objects: Arc<Mutex<BTreeMap<String, Bytes>>>,
bytes_written: Arc<AtomicU64>,
}
impl MemoryBlobStore {
pub fn new() -> Self {
Self::default()
}
pub fn object_count(&self) -> usize {
self.objects.lock().expect("poisoned").len()
}
pub fn total_bytes(&self) -> usize {
self.objects
.lock()
.expect("poisoned")
.values()
.map(|v| v.len())
.sum()
}
}
#[async_trait]
impl BlobStore for MemoryBlobStore {
async fn put(&self, key: &str, value: Bytes) -> Result<(), ObjectLogError> {
self.bytes_written
.fetch_add(value.len() as u64, Ordering::Relaxed);
self.objects
.lock()
.expect("poisoned")
.insert(key.to_string(), value);
Ok(())
}
async fn compare_and_swap(
&self,
key: &str,
expected: Option<Bytes>,
new_value: Bytes,
) -> Result<CasOutcome, ObjectLogError> {
let mut objects = self.objects.lock().expect("poisoned");
let current = objects.get(key).cloned();
let matches = match (&expected, ¤t) {
(None, None) => true,
(Some(expected), Some(current)) => expected == current,
_ => false,
};
if !matches {
return Ok(CasOutcome::Conflict { current });
}
self.bytes_written
.fetch_add(new_value.len() as u64, Ordering::Relaxed);
objects.insert(key.to_string(), new_value);
Ok(CasOutcome::Stored)
}
async fn get(&self, key: &str) -> Result<Option<Bytes>, ObjectLogError> {
Ok(self.objects.lock().expect("poisoned").get(key).cloned())
}
async fn get_range(
&self,
key: &str,
range: Range<u64>,
) -> Result<Option<Bytes>, ObjectLogError> {
let object = self.objects.lock().expect("poisoned").get(key).cloned();
match object {
Some(bytes) => Ok(Some(slice_range(&bytes, range)?)),
None => Ok(None),
}
}
async fn list(&self, prefix: &str) -> Result<Vec<String>, ObjectLogError> {
Ok(self
.objects
.lock()
.expect("poisoned")
.keys()
.filter(|k| k.starts_with(prefix))
.cloned()
.collect())
}
async fn delete(&self, key: &str) -> Result<(), ObjectLogError> {
self.objects.lock().expect("poisoned").remove(key);
Ok(())
}
fn take_media_op_stats(&self) -> Option<MediaOpStats> {
Some(MediaOpStats {
media_ops: 0,
bytes: self.bytes_written.swap(0, Ordering::Relaxed),
})
}
}
#[derive(Clone)]
pub struct LocalBlobStore {
root: Arc<PathBuf>,
media_ops: Arc<AtomicU64>,
bytes_written: Arc<AtomicU64>,
}
impl LocalBlobStore {
pub fn new(root: impl Into<PathBuf>) -> Self {
Self {
root: Arc::new(root.into()),
media_ops: Arc::new(AtomicU64::new(0)),
bytes_written: Arc::new(AtomicU64::new(0)),
}
}
fn path_for(&self, key: &str) -> Result<PathBuf, ObjectLogError> {
if key.is_empty() || key.contains('\0') || key.split('/').any(|p| p == "..") {
return Err(ObjectLogError::InvalidObjectKey(key.to_string()));
}
Ok(self.root.join(key))
}
fn durable_publish_chunks(
path: PathBuf,
chunks: Vec<Bytes>,
key: &str,
) -> std::io::Result<u64> {
static TRACE: OnceLock<bool> = OnceLock::new();
let trace =
*TRACE.get_or_init(|| std::env::var_os("OBJECT_LOG_LOCAL_PUBLISH_TRACE").is_some());
let started = trace.then(Instant::now);
let started_unix_us = trace.then(diagnostic_unix_us);
let mut phase = started;
let mut elapsed = || {
phase.map_or(0, |before| {
let now = Instant::now();
phase = Some(now);
now.duration_since(before).as_micros()
})
};
let parent = path.parent().expect("object path has a parent");
std::fs::create_dir_all(parent)?;
let mkdir_us = elapsed();
let mut tmp = path.clone().into_os_string();
tmp.push(TMP_SUFFIX);
let tmp = PathBuf::from(tmp);
let mut byte_len = 0u64;
let (create_us, write_us, data_sync_us);
{
let mut f = std::fs::File::create(&tmp)?;
create_us = elapsed();
for chunk in &chunks {
f.write_all(chunk)?;
byte_len += chunk.len() as u64;
}
write_us = elapsed();
f.sync_data()?;
data_sync_us = elapsed();
}
std::fs::rename(&tmp, &path)?;
let rename_us = elapsed(); let directory = std::fs::File::open(parent)?;
let dir_open_us = elapsed();
directory.sync_all()?;
let dir_sync_us = elapsed();
if let (Some(started), Some(started_unix_us)) = (started, started_unix_us) {
eprintln!(
"local_publish bytes={byte_len} mkdir_us={mkdir_us} create_us={create_us} write_us={write_us} data_sync_us={data_sync_us} rename_us={rename_us} dir_open_us={dir_open_us} dir_sync_us={dir_sync_us} total_us={} class={} store={} started_unix_us={started_unix_us}",
started.elapsed().as_micros(),
object_class(key),
diagnostic_store_tag(&path),
);
}
Ok(byte_len)
}
fn record_put(&self, byte_len: u64) {
self.media_ops.fetch_add(2, Ordering::Relaxed);
self.bytes_written.fetch_add(byte_len, Ordering::Relaxed);
}
async fn publish_async(
&self,
path: PathBuf,
chunks: Vec<Bytes>,
key: String,
) -> Result<u64, ObjectLogError> {
let run = move || {
Self::durable_publish_chunks(path, chunks, &key)
.map_err(|e| ObjectLogError::StorageUnavailable(e.to_string()))
};
match tokio::runtime::Handle::try_current() {
Ok(h) if h.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread => {
tokio::task::block_in_place(run)
}
Ok(_) => tokio::task::spawn_blocking(run)
.await
.map_err(|e| ObjectLogError::StorageUnavailable(e.to_string()))?,
Err(_) => run(),
}
}
}
#[async_trait]
impl BlobStore for LocalBlobStore {
async fn put(&self, key: &str, value: Bytes) -> Result<(), ObjectLogError> {
let path = self.path_for(key)?;
let byte_len = self
.publish_async(path, vec![value], key.to_string())
.await?;
self.record_put(byte_len);
Ok(())
}
async fn put_chunks(&self, key: &str, chunks: Vec<Bytes>) -> Result<(), ObjectLogError> {
match chunks.len() {
0 => self.put(key, Bytes::new()).await,
1 => self.put(key, chunks.into_iter().next().unwrap()).await,
_ => {
let path = self.path_for(key)?;
let byte_len = self.publish_async(path, chunks, key.to_string()).await?;
self.record_put(byte_len);
Ok(())
}
}
}
async fn get(&self, key: &str) -> Result<Option<Bytes>, ObjectLogError> {
let path = self.path_for(key)?;
match tokio::fs::read(&path).await {
Ok(v) => Ok(Some(Bytes::from(v))),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
}
async fn get_range(
&self,
key: &str,
range: Range<u64>,
) -> Result<Option<Bytes>, ObjectLogError> {
if range.start > range.end {
return Err(ObjectLogError::RangeOutOfBounds(format!(
"start {} > end {}",
range.start, range.end
)));
}
let path = self.path_for(key)?;
tokio::task::spawn_blocking(move || -> Result<Option<Bytes>, ObjectLogError> {
let mut f = match std::fs::File::open(&path) {
Ok(f) => f,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e.into()),
};
let len = f.metadata()?.len();
if range.end > len {
return Err(ObjectLogError::RangeOutOfBounds(format!(
"end {} > len {len}",
range.end
)));
}
let count = (range.end - range.start) as usize;
let mut buf = vec![0u8; count];
f.seek(SeekFrom::Start(range.start))?;
f.read_exact(&mut buf)?;
Ok(Some(Bytes::from(buf)))
})
.await
.map_err(|e| ObjectLogError::StorageUnavailable(e.to_string()))?
}
async fn list(&self, prefix: &str) -> Result<Vec<String>, ObjectLogError> {
let root = self.root.clone();
let prefix = prefix.to_string();
let keys = tokio::task::spawn_blocking(move || -> std::io::Result<Vec<String>> {
let mut out = Vec::new();
collect_files(&root, &root, &mut out)?;
Ok(out)
})
.await
.map_err(|e| ObjectLogError::StorageUnavailable(e.to_string()))??;
Ok(keys
.into_iter()
.filter(|k| k.starts_with(&prefix))
.collect())
}
async fn delete(&self, key: &str) -> Result<(), ObjectLogError> {
let path = self.path_for(key)?;
match tokio::fs::remove_file(&path).await {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
}
async fn compare_and_swap(
&self,
key: &str,
expected: Option<Bytes>,
new_value: Bytes,
) -> Result<CasOutcome, ObjectLogError> {
let path = self.path_for(key)?;
let key = key.to_string();
let byte_len = new_value.len() as u64;
let outcome = tokio::task::spawn_blocking(move || {
local_compare_and_swap(&path, &key, expected, new_value)
})
.await
.map_err(|e| ObjectLogError::StorageUnavailable(e.to_string()))??;
if matches!(outcome, CasOutcome::Stored) {
self.record_put(byte_len);
}
Ok(outcome)
}
fn take_media_op_stats(&self) -> Option<MediaOpStats> {
Some(MediaOpStats {
media_ops: self.media_ops.swap(0, Ordering::Relaxed),
bytes: self.bytes_written.swap(0, Ordering::Relaxed),
})
}
}
fn local_compare_and_swap(
path: &Path,
key: &str,
expected: Option<Bytes>,
new_value: Bytes,
) -> Result<CasOutcome, ObjectLogError> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let mut lock_name = path.as_os_str().to_os_string();
lock_name.push(".caslock");
let lock = std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.read(true)
.write(true)
.open(lock_name)?;
flock_exclusive(&lock)?;
let current = match std::fs::read(path) {
Ok(bytes) => Some(Bytes::from(bytes)),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
Err(error) => {
let _ = flock_unlock(&lock);
return Err(error.into());
}
};
let matches = match (&expected, ¤t) {
(None, None) => true,
(Some(expected), Some(current)) => expected == current,
_ => false,
};
if !matches {
let _ = flock_unlock(&lock);
return Ok(CasOutcome::Conflict { current });
}
let published =
LocalBlobStore::durable_publish_chunks(path.to_path_buf(), vec![new_value], key);
let _ = flock_unlock(&lock);
published?;
Ok(CasOutcome::Stored)
}
fn flock_exclusive(file: &std::fs::File) -> std::io::Result<()> {
let rc = unsafe { flock(file.as_raw_fd(), 2) };
if rc == 0 {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}
fn flock_unlock(file: &std::fs::File) -> std::io::Result<()> {
let rc = unsafe { flock(file.as_raw_fd(), 8) };
if rc == 0 {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}
unsafe extern "C" {
fn flock(fd: i32, operation: i32) -> i32;
}
fn collect_files(root: &Path, dir: &Path, out: &mut Vec<String>) -> std::io::Result<()> {
if !dir.exists() {
return Ok(());
}
for entry in std::fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
collect_files(root, &path, out)?;
} else if !path.to_string_lossy().ends_with(TMP_SUFFIX)
&& !path.to_string_lossy().ends_with(".caslock")
&& let Ok(rel) = path.strip_prefix(root)
{
out.push(rel.to_string_lossy().replace('\\', "/"));
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn diagnostic_store_tag_prefers_workload_shard_directory() {
assert_eq!(
diagnostic_store_tag(Path::new(
"/tmp/run/shard-12/log/fwlog/00000000000000000001"
)),
"shard-12"
);
assert_eq!(
diagnostic_store_tag(Path::new("/data/shard-0/projection.db")),
"shard-0"
);
assert_eq!(object_class("fwlog/1"), "segment");
assert_eq!(object_class("fwmeta/manifest/1"), "manifest");
assert_eq!(object_class("fwmeta/high-water/1"), "metadata");
}
}