Skip to main content

FileStore

Struct FileStore 

Source
pub struct FileStore { /* private fields */ }
Expand description

Content-addressed store holding one immutable file per chunk.

The filesystem is the sole authority. The in-memory index is a cache of what the directory tree already contains, rebuilt from directory entries at every open, and every mutation of it mirrors a filesystem operation that has already completed. Bitcask’s issue #114 is the cautionary tale for the opposite order: an index that is rebuilt at startup and then mutated in anticipation drifts, and the drift is silent.

Implementations§

Source§

impl FileStore

Source

pub async fn new(config: FileStoreConfig) -> Result<Self>

Open (or create) the store at {root_dir}/chunks/.

Sweeps orphaned temp files, then rebuilds the index from directory entries. The scan reads names only: it never stats an entry and never reads a chunk.

§Errors

Returns Error::Storage if the directory cannot be created, the layout marker is unreadable or describes a layout this build does not implement, or the scan fails.

Source

pub async fn put(&self, address: &XorName, content: &[u8]) -> Result<bool>

Store a chunk.

On Unix, publishing is a rename within the destination directory, so the final name can never appear on partial content: the name is the hash, and the content is fully written and flushed before the name exists. Off Unix there is no rename, for the reason publish_in_place gives (it is compiled only on those platforms, so this is not a link), and a partial file can wear a real name; that is why a duplicate is read and compared rather than trusted.

§Returns

true if the chunk was newly stored, false if it was already present.

§Errors

Returns Error::Storage if the content does not hash to address, the disk is too full, or the write fails.

Source

pub fn flush_namespace(&self) -> Result<()>

Flush every directory a chunk can live in, so the names in them are durable.

Byte integrity is not the whole of what the pre-retirement proof has to establish. A chunk whose contents are on the platter but whose name is not is still lost to a power loss, and a publish whose rename landed and whose directory flush failed leaves exactly that: the next attempt sees the name, the next verification reads the right bytes, and nothing goes back to retry the flush. So the proof flushes them itself rather than trusting that each publish did.

Cheap: at most 257 directory flushes for a store of any size, and nothing off Unix, where directories cannot be flushed and the retirement marker covers the same ground instead.

§Errors

Returns Error::Storage on the first directory that cannot be flushed. The caller must treat that as a proof it did not get.

Source

pub fn stored_len(&self, address: &XorName) -> Option<usize>

The size of the file behind address, if there is one.

One metadata call, no read. Used where an indexed name has to be checked against what a caller is offering before that offer is turned away.

Source

pub async fn repair(&self, address: &XorName, content: &[u8]) -> Result<()>

Replace the file behind an address with known-good bytes, atomically.

Unlike Self::put, this deliberately publishes over an existing name. It exists for one caller: repairing a file whose bytes no longer hash to their own name, from a copy held elsewhere, before that copy is destroyed. Doing it as delete-then-put would leave a window where the only remaining copy is the one about to be deleted, and any failure in that window is unrecoverable.

§Errors

Returns Error::Storage if content does not hash to address, or the write fails. The old file is left untouched on every error path.

Source

pub async fn get(&self, address: &XorName) -> Result<Option<Vec<u8>>>

Retrieve a chunk, verifying it against its address when configured to.

§Errors

Returns Error::Storage on an I/O failure, or when verification fails. A chunk whose bytes do not hash to its name is removed and dropped from the index before the error is returned, so it leaves all_keys() and ordinary replication repairs it.

Source

pub async fn get_raw(&self, address: &XorName) -> Result<Option<Vec<u8>>>

Retrieve raw chunk bytes without content-address verification.

§Errors

Returns Error::Storage on an I/O failure.

Source

pub fn exists(&self, address: &XorName) -> Result<bool>

Check whether a chunk is stored.

An in-memory lookup: no syscall, no I/O.

§Errors

Never fails. The signature keeps the shape the LMDB store had, because callers treat the error as “assume absent”.

Source

pub fn is_indexed(&self, address: &XorName) -> bool

Is this chunk in the index, whether or not it can currently be read?

The physical question, as against Self::exists’s question about what the node is willing to claim. The migration must ask this one: a suspect chunk is still a file this store has, and treating it as absent would put the key in the legacy-only set, from where the union view advertises it again — a key the node claims through one view and cannot serve through either.

Source

pub async fn delete(&self, address: &XorName) -> Result<bool>

Delete a chunk, returning whether it was present.

unlink returns the blocks to the filesystem immediately. That is the whole point of this store: no free list, no compaction, no free space required to reclaim space.

§Errors

Returns Error::Storage if the file exists but cannot be removed. The index keeps the key in that case, because the bytes are still on disk.

Source

pub async fn all_keys(&self) -> Result<Vec<XorName>>

Return every stored key, in ascending order.

The order is a correctness requirement, not a convenience: the commitment builder truncates the responsible subset with take(cap) before the Merkle tree sorts it, so an unstable order would make the node’s published commitment depend on iteration luck.

§Errors

Never fails. The signature matches the LMDB store’s.

Source

pub fn health_generation(&self) -> u64

What the store’s health looked like at this moment.

Compare a value taken before a long-running check with one taken after, or after taking a lock: different means a chunk stopped being servable in between and any conclusion drawn from that check is out of date.

Source

pub fn note_known_wrong(&self, address: &XorName)

Stop answering for a chunk a read has proven wrong.

Unlike a chunk that merely could not be read, a later read does not clear this. The bytes are wrong, and reading them again says the same thing; only replacing them or removing them settles it. Bumping health matters as much as the suppression: a chunk that has become unservable since the last pre-retirement pass must invalidate that pass, or a repair that fails leaves the node deleting the copy it would have repaired from.

For callers outside this module that have proven it themselves.

Source

pub fn note_bytes_proven_good(&self, address: &XorName)

A caller outside this module has proven the stored bytes are right.

Source

pub fn current_chunks(&self) -> Result<u64>

Number of chunks currently stored.

The physical count: every name in the index, including chunks the node has stopped answering for because a read found them wrong or could not read them at all. It is deliberately not the same number as all_keys().len(), which is what the node is willing to claim and so leaves those out.

Anything asking “how much is on this disk” wants this one, and that is what its callers ask: the migration’s progress, the storage stats, and the size an audit is built for. Anything asking “what will this node answer for” wants all_keys. Quietly filtering this one would move all three of those without saying so, which is why the difference is written down here rather than removed.

§Errors

Never fails. The signature matches the LMDB store’s.

Source

pub fn stats(&self) -> StorageStats

Operation statistics, with the live chunk count filled in.

Source

pub fn root_dir(&self) -> &Path

The node root directory this store was configured with.

Source

pub fn chunks_dir(&self) -> &Path

The directory holding the shard tree.

Source

pub fn check_capacity(&self) -> Result<()>

Reject work early when the disk cannot take another chunk at all.

§Errors

Returns Error::Storage when free space is below the configured reserve.

Source

pub fn capacity_verdict(&self) -> CapacityVerdict

Three-way answer to “can this store take a write right now”.

Kept distinct from Self::check_capacity because a failed free-space query and a genuinely full disk are not the same thing, and the replication verification cycle depends on the difference: a full disk is a standing condition worth minutes of backoff, while a statvfs that failed says nothing about available space and may well succeed on the next pass.

Source

pub fn check_capacity_for(&self, bytes: u64) -> Result<()>

Reject work early when the disk cannot take bytes more.

§Errors

Returns Error::Storage when the write would not fit above the reserve.

Source

pub fn invalidate_capacity_cache(&self)

Force the next capacity question to re-measure the filesystem.

Called after the legacy environment is removed, because that is a step change in free space that the short-lived cache would otherwise hide for a few seconds.

Source

pub async fn wait_for_write(&self, address: &XorName)

Wait until nothing is part-way through writing address.

For callers that must be last: a delete whose key still has a write in flight would be undone by that write landing afterwards.

Source

pub async fn wait_idle(&self)

Wait until every blocking task this store spawned has finished.

Dropping the awaiting future does not cancel a spawn_blocking closure, so shutdown has to wait for the closure itself.

Trait Implementations§

Source§

impl Debug for FileStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more