Skip to main content

ChunkStore

Struct ChunkStore 

Source
pub struct ChunkStore { /* 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 ChunkStore

Source

pub async fn new(config: ChunkStoreConfig) -> 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 compute_address(content: &[u8]) -> XorName

The address content hashes to.

A convenience the old facade offered and callers still use, so it stays with the store rather than making every one of them reach for the client module.

Source

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

Does this store already hold exactly these bytes under this address?

Asked by the protocol handler before it accepts a client’s PUT, so the answer has to be about the bytes and not about the name. A name on disk is not evidence: the startup scan indexes by name without reading anything, and a partial file can wear a real chunk name. Answering yes on a name would acknowledge a chunk that was never stored and then discard the good copy that had just arrived to replace it.

A chunk that is on disk with the wrong bytes is repaired from what the caller offered rather than turned away, because the caller has already checked those bytes against the address. A chunk that cannot be read this time is not claimed as held and not replaced either: the offer goes through the ordinary write path instead, which never truncates a healthy file on the strength of an unanswered question.

Source

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

Replace the file behind an address with bytes that hash to it.

Unlike Self::put, this deliberately publishes over an existing name, for repairing a file whose bytes no longer hash to their own. Doing it as delete-then-put would leave a window with no copy at all.

Only call this once a read has shown the existing bytes are wrong. On Unix the replacement is atomic and a failure leaves the old file untouched. Off Unix it is not: there is no durable rename there, so the existing file is truncated and rewritten in place, and a crash part-way leaves a mixture. That is tolerable when the bytes being replaced were already known to be wrong, and is data loss when they were not. Nothing enforces the precondition, which is why it is stated here.

§Errors

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

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. It returns a Result because callers treat an 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.

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. It returns a Result for callers that treat an error as absent.

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.

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 storage stats, the quoting metrics, 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. It returns a Result for callers that treat an error as absent.

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 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 ChunkStore

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