pub struct Journal<E: Context, V: Codec>(/* private fields */);Expand description
A contiguous journal with variable-size entries.
This journal manages blob assignment automatically, allowing callers to append items sequentially without manually tracking blob indexes.
§Repair
Like sqlite and rocksdb, the first invalid data read above the recovery watermark’s acknowledged prefix is considered the new end of the journal (and the underlying blob is truncated to the last valid item). Data at or below that prefix is never repaired: a blob that no longer backs its acknowledged items fails init, and other damage there surfaces as a read error. Repair is performed during init. Incomplete trailing frames are repaired as torn writes. Complete frames whose payloads fail to decode are treated as corruption.
§Invariants
§1. Data Blobs are the Source of Truth
The data blobs are always the source of truth. The offsets journal is an index that may temporarily diverge during crashes. Divergences are automatically aligned during init():
- If offsets are behind data after the recovery watermark: rebuild missing offsets by replaying data from the recovery anchor.
- If offsets are ahead of the retained data prefix but the data still reaches the recovery watermark, truncate offsets to match the data-backed size. Retained data ending before the watermark is corruption because acknowledged data is missing.
- If offsets.bounds().start < the oldest data blob’s start: prune offsets to match (this can happen if we crash after pruning the data blobs but before pruning the offsets journal).
Offsets may start after the data’s blob-aligned start when both are in the same blob, as in a
mid-blob init_at_size. Offsets starting in a later blob imply corruption because we
always prune the data blobs before the offsets journal.
§2. Offsets Recovery Watermark
The offsets journal’s recovery watermark records a durable lower bound on the journal size and a preferred point for replaying data to rebuild offset entries after a crash. Fixed-journal recovery rejects watermarks beyond the recovered offsets size as corruption. A watermark below the recovered offsets start is stale after a prune, so init falls back to the offsets start. If retained data exists but ends before the watermark, init returns corruption because acknowledged data is missing. If no retained data exists, init reconciles both sides to an empty journal. Replay after a valid anchor stops at the first short data blob and truncates newer blobs so the recovered journal remains a contiguous prefix.
Mutating functions consume the journal and return it only on success: an error (or a dropped future) destroys the handle.
Implementations§
Source§impl<E: Context, V: CodecShared> Journal<E, V>
impl<E: Context, V: CodecShared> Journal<E, V>
Sourcepub async fn init(context: E, cfg: Config<V::Cfg>) -> Result<Self, Error>
pub async fn init(context: E, cfg: Config<V::Cfg>) -> Result<Self, Error>
Initialize a contiguous variable journal.
§Crash Recovery
The data blobs are the source of truth. If the offsets journal is inconsistent it will be updated to match the data blobs.
Sourcepub async fn init_at_most(
context: E,
cfg: Config<V::Cfg>,
max_size: u64,
) -> Result<Self, Error>
pub async fn init_at_most( context: E, cfg: Config<V::Cfg>, max_size: u64, ) -> Result<Self, Error>
Open at most max_size items, including pruned positions.
The discarded suffix is durably removed before success. Subsequent appends may exceed the cap. A cap below the retained start returns Error::ItemPruned.
Sourcepub async fn init_at_size(
context: E,
cfg: Config<V::Cfg>,
size: u64,
) -> Result<Self, Error>
pub async fn init_at_size( context: E, cfg: Config<V::Cfg>, size: u64, ) -> Result<Self, Error>
Initialize an empty Journal at the given logical size.
This discards any existing data and offsets. The offsets reset intent is staged before the data partition is cleared so recovery can complete the requested reset if a crash interrupts the operation.
Returns a journal with journal.bounds() == Range{start: size, end: size}
and next append at position size.
§Errors
Returns Error::SizeOverflow if size is u64::MAX.
Sourcepub async fn append(self, item: &V) -> Result<(Self, u64), Error>
pub async fn append(self, item: &V) -> Result<(Self, u64), Error>
Append a new item to the journal, returning its position.
The position returned is a stable, consecutively increasing value starting from 0. This position remains constant after pruning.
§Errors
Returns an error if the underlying storage operation fails or if the item cannot be encoded.
Sourcepub async fn append_many(self, items: Many<'_, V>) -> Result<(Self, u64), Error>
pub async fn append_many(self, items: Many<'_, V>) -> Result<(Self, u64), Error>
Append items to the journal, returning the position of the last item appended.
Returns Error::EmptyAppend if items is empty.
Sourcepub fn prepare_append(
&self,
items: Many<'_, V>,
) -> Result<PreparedAppend<V>, Error>
pub fn prepare_append( &self, items: Many<'_, V>, ) -> Result<PreparedAppend<V>, Error>
Encode items into a buffer that can be appended later with Self::append_prepared.
This lets callers serialize borrowed items synchronously, release those borrows, and perform the append without holding unrelated locks across journal I/O.
Sourcepub async fn append_prepared(
self,
prepared: PreparedAppend<V>,
) -> Result<(Self, u64), Error>
pub async fn append_prepared( self, prepared: PreparedAppend<V>, ) -> Result<(Self, u64), Error>
Append items encoded by Self::prepare_append, returning the position of the last item
appended.
Returns Error::EmptyAppend if prepared contains no items.
Returns Error::InvalidConfiguration if prepared was encoded with different compression
settings than this journal uses.
Sourcepub async fn snapshot(self) -> Result<(Self, Reader<'static, E, V>), Error>
pub async fn snapshot(self) -> Result<(Self, Reader<'static, E, V>), Error>
Capture an owned snapshot (Reader) over the current journal. Bounds are frozen at
creation, and the snapshot stays readable across concurrent appends and prunes. It keeps
the journal’s blobs open, so reopening a partition that still holds one of them fails
while the snapshot is alive.
Sourcepub fn size(&self) -> u64
pub fn size(&self) -> u64
Return the total number of items in the journal, irrespective of pruning. The next value appended to the journal will be at this position.
Sourcepub async fn prune(self, min_position: u64) -> Result<(Self, bool), Error>
pub async fn prune(self, min_position: u64) -> Result<(Self, bool), Error>
Prune items at positions strictly less than min_position.
Returns true if any data was pruned, false otherwise.
§Errors
Returns an error if the underlying storage operation fails.
Sourcepub async fn commit(self) -> Result<Self, Error>
pub async fn commit(self) -> Result<Self, Error>
Persist data blobs so committed data survives a crash.
Does not advance the recovery watermark, so reopen may replay entries above it.
Sourcepub async fn start_sync(self) -> Result<(Self, Handle<()>), Error>
pub async fn start_sync(self) -> Result<(Self, Handle<()>), Error>
Begin durably persisting the current state of the journal.
Awaiting the returned Handle guarantees state appended before this call survives a
crash. Also tries to advance the recovery watermark to the previous proven durable
size, bounding startup recovery. Only sync() guarantees a current watermark.
At most one data sync and one watermark sync are in flight at a time: this call waits for the prior call’s syncs before starting new ones. It does not wait for a pending rollover fsync: the returned handle joins it, so an earlier call’s handle may still be pending when this call returns. Reads proceed while the returned handle is pending, and appends proceed while they fit in the write buffer.
Flush errors are returned directly. Dropping the handle does not cancel the sync or lose its failure: a later operation reports it, no later than the next Self::sync.
Sourcepub async fn sync(self) -> Result<Self, Error>
pub async fn sync(self) -> Result<Self, Error>
Persist data blobs and all metadata for both the data and offsets journals.
Sourcepub async fn destroy(self) -> Result<(), Error>
pub async fn destroy(self) -> Result<(), Error>
Remove any underlying blobs created by the journal.
This destroys both the data blobs and the offsets journal. A started sync that failed is reported before anything is removed.
§Crash Safety
This operation is intended for final teardown and is not crash-safe. If interrupted, reopening the same partitions may observe partially removed state. Use Self::init_at_size for a recoverable reset.
Trait Implementations§
Source§impl<E: Context, V: CodecShared> Backing<E> for Journal<E, V>
Available on neither commonware_stability_BETA nor commonware_stability_DELTA nor commonware_stability_EPSILON nor commonware_stability_GAMMA nor commonware_stability_RESERVED.
impl<E: Context, V: CodecShared> Backing<E> for Journal<E, V>
commonware_stability_BETA nor commonware_stability_DELTA nor commonware_stability_EPSILON nor commonware_stability_GAMMA nor commonware_stability_RESERVED.Source§type Recovery = Recovery<E, V>
type Recovery = Recovery<E, V>
Source§async fn recover(
context: E,
cfg: Self::Config,
max_size: Option<u64>,
) -> Result<Self::Recovery, Error>
async fn recover( context: E, cfg: Self::Config, max_size: Option<u64>, ) -> Result<Self::Recovery, Error>
max_size lies below the retained start.Source§impl<E: Context, V: CodecShared> Contiguous for Journal<E, V>
impl<E: Context, V: CodecShared> Contiguous for Journal<E, V>
Source§fn bounds(&self) -> Range<u64> ⓘ
fn bounds(&self) -> Range<u64> ⓘ
Source§async fn read(&self, position: u64) -> Result<V, Error>
async fn read(&self, position: u64) -> Result<V, Error>
Source§async fn read_many(&self, positions: &[u64]) -> Result<Vec<V>, Error>
async fn read_many(&self, positions: &[u64]) -> Result<Vec<V>, Error>
Source§fn try_read_sync(&self, position: u64) -> Option<V>
fn try_read_sync(&self, position: u64) -> Option<V>
None
otherwise. Decode failures surface as None and the async read path reports the error.Source§fn try_read_many_sync(&self, positions: &[u64]) -> Vec<Option<V>>
fn try_read_many_sync(&self, positions: &[u64]) -> Vec<Option<V>>
bounds() decline to None. The
async read paths are the sole error authority for declined positions.Source§impl<E: Context, V: CodecShared> Mutable for Journal<E, V>
impl<E: Context, V: CodecShared> Mutable for Journal<E, V>
Source§async fn append(self, item: &Self::Item) -> Result<(Self, u64), Error>
async fn append(self, item: &Self::Item) -> Result<(Self, u64), Error>
Source§async fn append_many(
self,
items: Many<'_, Self::Item>,
) -> Result<(Self, u64), Error>
async fn append_many( self, items: Many<'_, Self::Item>, ) -> Result<(Self, u64), Error>
Source§async fn prune(self, min_position: u64) -> Result<(Self, bool), Error>
async fn prune(self, min_position: u64) -> Result<(Self, bool), Error>
min_position. Read moreSource§async fn start_sync(self) -> Result<(Self, Handle<()>), Error>
async fn start_sync(self) -> Result<(Self, Handle<()>), Error>
Source§async fn commit(self) -> Result<Self, Error>
async fn commit(self) -> Result<Self, Error>
Auto Trait Implementations§
impl<E, V> !RefUnwindSafe for Journal<E, V>
impl<E, V> !UnwindSafe for Journal<E, V>
impl<E, V> Freeze for Journal<E, V>
impl<E, V> Send for Journal<E, V>
impl<E, V> Sync for Journal<E, V>
impl<E, V> Unpin for Journal<E, V>
impl<E, V> UnsafeUnpin for Journal<E, V>where
Box<Inner<E, V>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more