Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/ourios-parquet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ object_store = { version = "0.13" }
# the RFC 0013 buffer-and-put read path (`Reader::open_bytes`). Already in the
# arrow/parquet tree; declared directly.
bytes = { version = "1", default-features = false }
# Drives the async `object_store` calls from the sync storage API
# (`Store::*_blocking` bridge — `Writer`/`Reader`/compaction are sync,
# `object_store` is async). `rt-multi-thread` so concurrent `block_on` from the
# compaction sweep's blocking thread and parallel test threads is safe. Already
# in the workspace tree via DataFusion / object_store.
tokio = { version = "1", default-features = false, features = ["rt", "rt-multi-thread"] }
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
# UUIDv7 file naming per RFC 0005 §3.4 — timestamp-in-high-bits
# means lexicographic sort = creation-order.
uuid = { version = "1", default-features = false, features = ["v7", "std"] }
Expand Down
70 changes: 52 additions & 18 deletions crates/ourios-parquet/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
//! surface records rather than making up defaults.

use std::fmt;
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};

Expand All @@ -45,6 +44,7 @@ use parquet::errors::ParquetError;

use crate::columns;
use crate::partition::{PartitionKey, TimestampOverflowError};
use crate::store::{Store, StoreError};

/// Streaming Parquet reader for one data file.
///
Expand Down Expand Up @@ -91,22 +91,8 @@ impl Reader {
///
/// Same set as [`Self::open_partition`].
pub fn open_file(path: &Path) -> Result<Self, ReaderError> {
let file = File::open(path).map_err(|source| ReaderError::Io {
op: "open",
path: path.to_path_buf(),
source,
})?;

let builder =
ParquetRecordBatchReaderBuilder::try_new(file).map_err(ReaderError::Parquet)?;
require_baseline_columns(builder.schema())?;
let inner = builder.build().map_err(ReaderError::Parquet)?;

Ok(Self {
inner,
partition: None,
file_path: path.to_path_buf(),
})
let bytes = read_object(path)?;
Self::from_bytes(bytes, path.to_path_buf())
}
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved

/// Open a reader over in-memory Parquet `bytes` — the RFC 0013
Expand All @@ -120,6 +106,14 @@ impl Reader {
/// [`ReaderError::MissingRequiredColumn`] if a baseline REQUIRED column
/// is absent (§3.9).
pub fn open_bytes(bytes: bytes::Bytes) -> Result<Self, ReaderError> {
Self::from_bytes(bytes, PathBuf::from("<object-store>"))
}

/// Build a reader over in-memory Parquet `bytes`, recording `file_path`
/// for diagnostics (the real path on the [`Self::open_file`] /
/// [`Self::open_partition`] seam, a sentinel on [`Self::open_bytes`]).
/// Applies the §3.9 baseline-column check; leaves `partition` unset.
fn from_bytes(bytes: bytes::Bytes, file_path: PathBuf) -> Result<Self, ReaderError> {
let builder =
ParquetRecordBatchReaderBuilder::try_new(bytes).map_err(ReaderError::Parquet)?;
require_baseline_columns(builder.schema())?;
Expand All @@ -128,7 +122,7 @@ impl Reader {
Ok(Self {
inner,
partition: None,
file_path: PathBuf::from("<object-store>"),
file_path,
})
}

Expand Down Expand Up @@ -316,6 +310,46 @@ fn validate_row_vs_partition(
/// RFC 0005 §3.9: every baseline REQUIRED (non-nullable) column must be
/// present in the file's schema, else a hard error. Shared by
/// [`Reader::open_file`] and [`Reader::open_bytes`].
/// Read a data file's bytes through the object-storage [`Store`] seam
/// (RFC 0013): a `LocalFileSystem`-backed store rooted at the file's parent
/// directory, keyed by the file name. The sync read path (compaction, tests)
/// thus goes through the same seam the S3 backend will, while keeping the
/// `&Path` API — `Store.get` is async, so this uses the blocking bridge.
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
fn read_object(path: &Path) -> Result<bytes::Bytes, ReaderError> {
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
let parent = path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let name = path
.file_name()
.and_then(|n| n.to_str())
.ok_or_else(|| ReaderError::Io {
op: "object key",
path: path.to_path_buf(),
source: io::Error::new(
io::ErrorKind::InvalidInput,
"file name is empty or not valid UTF-8",
),
})?;
let store = Store::local(parent).map_err(|e| store_io_err("open", path, &e))?;
let bytes = store
.get_blocking(name)
.map_err(|e| store_io_err("get", path, &e))?;
Ok(bytes::Bytes::from(bytes))
}

/// Map a [`StoreError`] from the read seam onto [`ReaderError::Io`], preserving
/// the file path and op for diagnostics. No consumer inspects the io kind of a
/// read failure (compaction wraps it opaquely; the querier reads via
/// `DataFusion`), so the backend cause rides in the message.
fn store_io_err(op: &'static str, path: &Path, err: &StoreError) -> ReaderError {
ReaderError::Io {
op,
path: path.to_path_buf(),
source: io::Error::other(err.to_string()),
}
}
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved

fn require_baseline_columns(file_schema: &arrow_schema::Schema) -> Result<(), ReaderError> {
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
for expected_field in crate::data_schema().fields() {
if !expected_field.is_nullable()
Expand Down
111 changes: 110 additions & 1 deletion crates/ourios-parquet/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,58 @@
//! (not a new crate): `ourios-querier`, `-ingester`, and `-server` already
//! depend on this crate, so the type is visible to every storage consumer.

use std::sync::Arc;
use std::future::Future;
use std::sync::{Arc, OnceLock};

use object_store::local::LocalFileSystem;
use object_store::path::Path as ObjectPath;
use object_store::{ObjectStore, ObjectStoreExt, PutPayload};
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
use tokio::runtime::Runtime;

/// Shared runtime that drives the async `object_store` calls from the **sync**
/// storage API (`Writer`, `Reader`, `compaction`, and the manifest are sync;
/// `object_store` is async, and compaction must reach S3 per RFC0013.3, so the
/// bridge can't be a local-only `std::fs` shortcut).
///
/// One process-wide multi-threaded(1-worker) runtime so concurrent `block_on`
/// from the storage call sites is safe (each runs on its own thread; see
/// [`block_on_off_runtime`]).
fn bridge_runtime() -> Result<&'static Runtime, StoreError> {
static RT: OnceLock<Runtime> = OnceLock::new();
if let Some(rt) = RT.get() {
return Ok(rt);
}
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
.map_err(StoreError::Runtime)?;
// A racing thread may have initialised it first; if so our runtime is
// dropped and we use theirs (the configuration is identical).
let _ = RT.set(rt);
RT.get()
.ok_or_else(|| StoreError::Runtime(std::io::Error::other("bridge runtime vanished")))
}

/// Drive `fut` to completion synchronously, running `block_on` on a **fresh OS
/// thread** rather than the caller's. A plain thread never inherits the tokio
/// runtime context, so this is safe even when the caller is *inside* a runtime
/// (e.g. a `#[tokio::test]` that opens the reader, or any future async
/// consumer) — `block_on` on the calling thread would panic "runtime within a
/// runtime" there. [`std::thread::scope`] lets `fut` borrow the caller's
/// `self`/`key` while still running off-thread.
fn block_on_off_runtime<F>(fut: F) -> Result<F::Output, StoreError>
where
F: Future + Send,
F::Output: Send,
{
let rt = bridge_runtime()?;
Ok(std::thread::scope(|s| {
s.spawn(|| rt.block_on(fut))
.join()
.unwrap_or_else(|payload| std::panic::resume_unwind(payload))
}))
}

/// A handle to the object store backing a tenant store's Parquet + manifest
/// objects, addressed by key under `prefix`. Wraps an [`ObjectStore`] so the
Expand Down Expand Up @@ -67,13 +114,17 @@ pub enum StoreError {
/// Returned (rather than panicking) so an accidental call fails
/// gracefully while `red`.
Unimplemented(&'static str),
/// The sync→async bridge runtime could not be built (resource
/// exhaustion). Surfaced by the `*_blocking` methods.
Runtime(std::io::Error),
}

impl std::fmt::Display for StoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Backend(e) => write!(f, "object-store backend: {e}"),
Self::Unimplemented(what) => write!(f, "not implemented (RFC 0013 red): {what}"),
Self::Runtime(e) => write!(f, "object-store bridge runtime: {e}"),
}
}
}
Expand All @@ -83,6 +134,7 @@ impl std::error::Error for StoreError {
match self {
Self::Backend(e) => Some(e),
Self::Unimplemented(_) => None,
Self::Runtime(e) => Some(e),
}
}
}
Expand Down Expand Up @@ -186,6 +238,28 @@ impl Store {
.await
.map_err(StoreError::Backend)
}

/// Blocking [`Self::get`] for the **sync** storage call sites (`Reader`,
/// compaction). Safe to call from any thread, including inside a tokio
/// runtime — the `block_on` runs off-thread (see [`block_on_off_runtime`]).
///
/// # Errors
/// [`StoreError::Runtime`] if the bridge runtime can't be built;
/// otherwise as [`Self::get`].
pub fn get_blocking(&self, key: &str) -> Result<Vec<u8>, StoreError> {
block_on_off_runtime(self.get(key))?
}
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved

/// Blocking [`Self::put`] for the **sync** storage call sites (`Writer`,
/// compaction). Safe to call from inside a tokio runtime (see
/// [`Self::get_blocking`]).
///
/// # Errors
/// [`StoreError::Runtime`] if the bridge runtime can't be built;
/// otherwise as [`Self::put`].
pub fn put_blocking(&self, key: &str, bytes: Vec<u8>) -> Result<(), StoreError> {
block_on_off_runtime(self.put(key, bytes))?
}
}
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved

#[cfg(test)]
Expand All @@ -205,4 +279,39 @@ mod tests {
store.delete(key).await.expect("delete");
assert!(store.get(key).await.is_err(), "object gone after delete");
}

/// The sync `*_blocking` bridge round-trips a byte object — the path the
/// sync `Writer` / `Reader` / compaction take onto `Store`. Runs on a
/// plain test thread (no ambient runtime), exercising `block_on`.
#[test]
fn blocking_bridge_put_get_round_trip() {
let dir = tempfile::TempDir::new().expect("temp dir");
let store = Store::local(dir.path()).expect("local store");
let key = "data/tenant_id=t/year=2026/x.parquet";
store
.put_blocking(key, b"hello-blocking".to_vec())
.expect("put_blocking");
assert_eq!(
store.get_blocking(key).expect("get_blocking"),
b"hello-blocking"
);
}

/// The `*_blocking` bridge is safe to call from *within* a tokio runtime —
/// some consumers (e.g. a `#[tokio::test]` that reads back via `Reader`)
/// do exactly that. The `block_on` runs off the caller's thread, so it
/// must not panic "runtime within a runtime".
#[tokio::test(flavor = "current_thread")]
async fn blocking_bridge_is_safe_inside_a_runtime() {
let dir = tempfile::TempDir::new().expect("temp dir");
let store = Store::local(dir.path()).expect("local store");
let key = "data/tenant_id=t/year=2026/x.parquet";
store
.put_blocking(key, b"inside-runtime".to_vec())
.expect("put_blocking");
assert_eq!(
store.get_blocking(key).expect("get_blocking"),
b"inside-runtime"
);
}
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
}
Loading