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
impl ChunkStore
Sourcepub async fn new(config: ChunkStoreConfig) -> Result<Self>
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.
Sourcepub async fn put(&self, address: &XorName, content: &[u8]) -> Result<bool>
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.
Sourcepub fn compute_address(content: &[u8]) -> XorName
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.
Sourcepub async fn holds_verified(&self, address: &XorName, content: &[u8]) -> bool
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.
Sourcepub async fn repair(&self, address: &XorName, content: &[u8]) -> Result<()>
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.
Sourcepub async fn get(&self, address: &XorName) -> Result<Option<Vec<u8>>>
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.
Sourcepub async fn get_raw(&self, address: &XorName) -> Result<Option<Vec<u8>>>
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.
Sourcepub fn exists(&self, address: &XorName) -> Result<bool>
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”.
Sourcepub fn is_indexed(&self, address: &XorName) -> bool
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.
Sourcepub async fn delete(&self, address: &XorName) -> Result<bool>
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.
Sourcepub async fn all_keys(&self) -> Result<Vec<XorName>>
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.
Sourcepub fn note_known_wrong(&self, address: &XorName)
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.
Sourcepub fn note_bytes_proven_good(&self, address: &XorName)
pub fn note_bytes_proven_good(&self, address: &XorName)
A caller outside this module has proven the stored bytes are right.
Sourcepub fn current_chunks(&self) -> Result<u64>
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.
Sourcepub fn stats(&self) -> StorageStats
pub fn stats(&self) -> StorageStats
Operation statistics, with the live chunk count filled in.
Sourcepub fn chunks_dir(&self) -> &Path
pub fn chunks_dir(&self) -> &Path
The directory holding the shard tree.
Sourcepub fn disk_space(&self) -> Option<DiskSpace>
pub fn disk_space(&self) -> Option<DiskSpace>
Free and total bytes on the partition holding this store, or None if
the filesystem could not say.
The pointer store keeps its files on this partition too, so this is the one measurement both record kinds’ retention decides by (ADR-0028).
Sourcepub fn disk_reserve(&self) -> u64
pub fn disk_reserve(&self) -> u64
Free bytes this store keeps unused: below them it refuses writes.
Sourcepub fn check_capacity(&self) -> Result<()>
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.
Sourcepub fn capacity_verdict(&self) -> CapacityVerdict
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.
Sourcepub fn check_capacity_for(&self, bytes: u64) -> Result<()>
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.
Sourcepub async fn wait_for_write(&self, address: &XorName)
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.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for ChunkStore
impl !RefUnwindSafe for ChunkStore
impl !UnwindSafe for ChunkStore
impl Send for ChunkStore
impl Sync for ChunkStore
impl Unpin for ChunkStore
impl UnsafeUnpin for ChunkStore
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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> 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