pub struct WalWriter { /* private fields */ }Expand description
Append-only WAL writer.
Implementations§
Source§impl WalWriter
impl WalWriter
Sourcepub fn open(path: &Path, config: WalWriterConfig) -> Result<Self>
pub fn open(path: &Path, config: WalWriterConfig) -> Result<Self>
Open or create a WAL file at the given path.
Sourcepub fn open_resuming(
path: &Path,
config: WalWriterConfig,
declared_first_lsn: u64,
) -> Result<Self>
pub fn open_resuming( path: &Path, config: WalWriterConfig, declared_first_lsn: u64, ) -> Result<Self>
Resume appending to an existing segment whose filename declares
declared_first_lsn as the lowest LSN it may hold.
Self::open derives the next LSN from the records it finds, and a
segment that holds none — created by a rollover that was never written
to, or one whose every record was torn away — would restart the
sequence at 1. Those LSNs are already spoken for by the earlier
segments, so replay would see duplicates and truncation would delete
the originals. The filename is the authority on where this segment’s
range begins, so the resumed sequence never drops below it.
Sourcepub fn set_encryption_key(&mut self, key: WalEncryptionKey) -> Result<()>
pub fn set_encryption_key(&mut self, key: WalEncryptionKey) -> Result<()>
Set the encryption key. When set, all subsequent records will have their payloads encrypted with AES-256-GCM.
Writes the 16-byte WAL segment preamble at the current file offset.
Must be called before the first append. Calling it after records
have already been written to this file returns an error.
Sourcepub fn set_encryption_ring(&mut self, ring: KeyRing) -> Result<()>
pub fn set_encryption_ring(&mut self, ring: KeyRing) -> Result<()>
Set the key ring directly (for key rotation with dual-key reads).
Writes the 16-byte WAL segment preamble at the current file offset.
Must be called before the first append. Calling it after records
have already been written to this file returns an error.
Sourcepub fn encryption_ring(&self) -> Option<&KeyRing>
pub fn encryption_ring(&self) -> Option<&KeyRing>
Access the key ring (for decryption during replay).
Sourcepub fn segment_preamble(&self) -> Option<&SegmentPreamble>
pub fn segment_preamble(&self) -> Option<&SegmentPreamble>
The preamble for this segment, if encryption was enabled.
Sourcepub fn open_with_start_lsn(
path: &Path,
config: WalWriterConfig,
start_lsn: u64,
) -> Result<Self>
pub fn open_with_start_lsn( path: &Path, config: WalWriterConfig, start_lsn: u64, ) -> Result<Self>
Open a new WAL segment file with a specific starting LSN.
Used by SegmentedWal when rolling to a new segment. The file must
not already exist (or be empty). The writer will assign LSNs starting
from start_lsn.
Sourcepub fn open_without_direct_io(path: &Path) -> Result<Self>
pub fn open_without_direct_io(path: &Path) -> Result<Self>
Open a WAL writer with O_DIRECT disabled, for a test whose subject is buffered I/O or which runs on a filesystem that refuses the flag.
Sourcepub fn append(
&mut self,
record_type: u32,
tenant_id: u64,
vshard_id: u32,
database_id: u64,
payload: &[u8],
) -> Result<u64>
pub fn append( &mut self, record_type: u32, tenant_id: u64, vshard_id: u32, database_id: u64, payload: &[u8], ) -> Result<u64>
Append a record to the WAL. Returns the assigned LSN.
The record is written to the in-memory buffer. Call sync() to
flush to disk and make the write durable.
database_id is stored in header bytes 34-41. Pass 0 for the
default database (backward-compatible with pre-existing records).
The LSN is committed only once the record is in the write buffer. An append that fails — a full device, an oversized payload — leaves the LSN sequence untouched, so the next append reuses it rather than leaving a hole that replay would have to reason about.
Sourcepub fn append_keyed(
&mut self,
target: RecordTarget,
payload: &[u8],
apply_key: u64,
) -> Result<u64>
pub fn append_keyed( &mut self, target: RecordTarget, payload: &[u8], apply_key: u64, ) -> Result<u64>
Self::append for a record appended by the apply of the replicated
proposal apply_key, carrying the event source in target (see
WalRecord::new_stamped).
Sourcepub fn sync(&mut self) -> Result<()>
pub fn sync(&mut self) -> Result<()>
Flush the write buffer to disk (group commit).
This issues a single write + fsync for all records accumulated since the last flush. The DWB is also fsynced (one fsync for all deferred DWB writes in this batch).
An empty buffer alone does not mean there is nothing to do: a flush clears the buffer before the fsync runs, so a batch whose fsync failed leaves the writer empty-buffered with its records still only in the page cache. Returning early there would report durability the log does not have, so the fsync is skipped only when nothing is outstanding.
Sourcepub fn seal(&mut self) -> Result<()>
pub fn seal(&mut self) -> Result<()>
Seal the WAL — no more writes will be accepted.
Flushes any buffered data before sealing. Sealing is a durability
boundary — the next segment starts where this one stops — so it goes
through sync, which fsyncs an outstanding flush even when the buffer
is empty and refuses to succeed on a poisoned writer.
Sourcepub fn file_offset(&self) -> u64
pub fn file_offset(&self) -> u64
Current file size (bytes written to disk).
Sourcepub fn max_payload(&self) -> usize
pub fn max_payload(&self) -> usize
The largest payload one record takes: the write buffer, less the padding and anchor reserves, the header and the encryption tag.
Source§impl WalWriter
impl WalWriter
Sourcepub fn dwb_protection(&self) -> DwbProtection
pub fn dwb_protection(&self) -> DwbProtection
Torn-write protection standing of this writer.
Sourcepub fn dwb_unprotected_records(&self) -> u64
pub fn dwb_unprotected_records(&self) -> u64
Records appended to this segment with no double-write copy behind them.
Non-zero means those records cannot be reconstructed if they are torn by a power loss at the tail of the segment.
Sourcepub fn reattach_double_write(&mut self) -> DwbProtection
pub fn reattach_double_write(&mut self) -> DwbProtection
Reopen a double-write buffer that was detached after a failure.
Detaching keeps a broken device from being hammered once per append, but on its own it makes degradation permanent for the life of the writer — an operator who has fixed the fault would have no way back short of rolling the segment. Returns the resulting standing.
Auto Trait Implementations§
impl !Freeze for WalWriter
impl !Sync for WalWriter
impl RefUnwindSafe for WalWriter
impl Send for WalWriter
impl Unpin for WalWriter
impl UnsafeUnpin for WalWriter
impl UnwindSafe for WalWriter
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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> 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> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.