pub struct ChunkStore { /* private fields */ }Expand description
Content-addressed chunk storage.
Implementations§
Source§impl ChunkStore
impl ChunkStore
Sourcepub async fn new(config: ChunkStoreConfig) -> Result<Self>
pub async fn new(config: ChunkStoreConfig) -> Result<Self>
Open the store under config.root_dir.
Opens the legacy environment only if one is already on disk. A fresh node never creates one, so it never pays for a memory map it will not use.
§Errors
Returns Error::Storage if either store cannot be opened.
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.
While a legacy environment exists and dual-writing is on, the chunk goes there first. A chunk uploaded during the bridge to holders that all revert to a pre-migration build would otherwise be gone from every one of them, and that is real client data, not a replica.
§Returns
true if the chunk was newly stored, false if either store already had it.
§Errors
Returns Error::Storage if the content does not hash to address, the disk is
too full, 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 and no
intact copy is available.
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, in either backing.
An in-memory lookup: no syscall, no I/O, in both phases.
§Errors
Never fails. The signature is kept because callers treat an error as “absent”.
Sourcepub async fn holds_verified(&self, address: &XorName, content: &[u8]) -> bool
pub async fn holds_verified(&self, address: &XorName, content: &[u8]) -> bool
Does this node already hold address with exactly these bytes, and if it holds a
damaged copy, replace it with these?
The question a responder has to answer before turning away an offered copy. Plain
Self::exists answers from names alone, and a name can outlive the bytes under
it: off Unix a chunk is created under its final name before it is written, so a
crash leaves a short file that exists reports as a chunk, and bit rot leaves a
full-length one. Acknowledging a client on the strength of either throws away the
copy that would repair it, and nothing offers it again.
So this reads. It is affordable because the only caller is the client-facing PUT path, reached when a client offers a chunk this node already has, and because the alternative is keeping a chunk this node cannot serve and being penalised for it at the next audit.
content must already hash to address; the caller checks that before this is
reached, and a repair from bytes that do not would be worse than the damage.
§Errors
Never fails. An unreadable chunk answers false, so the offered copy is stored
through the ordinary path rather than refused.
Sourcepub async fn delete(&self, address: &XorName) -> Result<bool>
pub async fn delete(&self, address: &XorName) -> Result<bool>
Delete a chunk from both backings.
A logical delete has to reach the legacy environment too, or the union view would resurrect the key on the next read. It frees no space there — only removing the environment whole does that — but it keeps the two views honest.
§Errors
Returns Error::Storage if a file exists but cannot be removed.
Sourcepub async fn all_keys(&self) -> Result<Vec<XorName>>
pub async fn all_keys(&self) -> Result<Vec<XorName>>
Every stored key, in ascending order, across both backings.
The order is a correctness requirement: 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 rather than on what it holds.
§Errors
Returns Error::Storage if the file index cannot be read.
Sourcepub async fn committable_keys(&self) -> Result<Vec<XorName>>
pub async fn committable_keys(&self) -> Result<Vec<XorName>>
The keys the commitment builder should commit to.
While the node is still bridging this is the whole union, because it can still serve all of it and dropping the claim early would collapse its commitment (and with it its quoted price) for no reason. Once it has settled on what it will keep, this narrows to the file-backed set, which is exactly the point at which the node stops claiming keys it is about to give up.
§Errors
Returns Error::Storage if the file index cannot be read.
Sourcepub fn current_chunks(&self) -> Result<u64>
pub fn current_chunks(&self) -> Result<u64>
Number of chunks currently stored, counted across both backings without double-counting a chunk that is in each.
§Errors
Never fails.
Sourcepub fn stats(&self) -> StorageStats
pub fn stats(&self) -> StorageStats
Operation statistics.
The cumulative counters are the file store’s; current_chunks is the union.
Sourcepub fn compute_address(content: &[u8]) -> XorName
pub fn compute_address(content: &[u8]) -> XorName
Compute a content address (BLAKE3 hash).
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 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.
Free bytes alone stopped being a sufficient answer once chunks became files.
§Errors
Returns Error::Storage when the write would not fit above the reserve.
Sourcepub fn migration_phase(&self) -> MigrationPhase
pub fn migration_phase(&self) -> MigrationPhase
Where the node is in the migration.
Sourcepub fn migration_state(&self) -> MigrationState
pub fn migration_state(&self) -> MigrationState
A snapshot of the persisted migration marker.
Sourcepub fn migration_config(&self) -> &MigrationConfig
pub fn migration_config(&self) -> &MigrationConfig
The migration settings this store was built with.
Sourcepub fn has_legacy(&self) -> bool
pub fn has_legacy(&self) -> bool
Whether a legacy environment is still open.
Sourcepub fn legacy_only_keys(&self) -> Vec<XorName> ⓘ
pub fn legacy_only_keys(&self) -> Vec<XorName> ⓘ
The keys the legacy environment still holds alone, ascending.
Sourcepub fn legacy_bytes(&self) -> u64
pub fn legacy_bytes(&self) -> u64
Bytes the legacy environment occupies, as the filesystem sees it.
Sourcepub async fn copy_batch(
&self,
keys: &[XorName],
slack: u64,
throttle_mib_per_sec: u64,
shutdown: &CancellationToken,
) -> Result<CopyReport>
pub async fn copy_batch( &self, keys: &[XorName], slack: u64, throttle_mib_per_sec: u64, shutdown: &CancellationToken, ) -> Result<CopyReport>
Copy up to keys.len() chunks out of the legacy environment into files.
Stops as soon as free space would fall below slack above the configured
reserve, so a migration never fills the disk it is trying to free.
§Errors
Returns Error::Storage only for failures that are not per-key: a per-key
problem is counted in the report and the pass continues.
Sourcepub fn commit_to_files(&self) -> Result<()>
pub fn commit_to_files(&self) -> Result<()>
Settle on the file-backed set: from now on the node commits only to what it will keep, while still serving everything it ever committed to.
§Errors
Returns Error::Storage if the marker cannot be persisted.
Sourcepub fn note_commitment_rebuilt(&self)
pub fn note_commitment_rebuilt(&self)
Record that the commitment builder has read and published the committable set.
The retirement gate counts these: one proves the builder saw the new set, two prove it survived a rotation, which is what makes the answerability window meaningful rather than notional.
Sourcepub fn retirement_blocker<F>(&self, still_answerable: F) -> Option<String>
pub fn retirement_blocker<F>(&self, still_answerable: F) -> Option<String>
Whether every gate on deleting the legacy environment is satisfied.
still_answerable is asked of each key the node is about to give up: the pruner’s
existing retention contract, reused verbatim. A key still covered by a retained
commitment slot vetoes the delete, because the node could still be challenged on it.
Sourcepub async fn verify_before_retire(
&self,
throttle_mib_per_sec: u64,
shutdown: &CancellationToken,
) -> Result<VerifyReport>
pub async fn verify_before_retire( &self, throttle_mib_per_sec: u64, shutdown: &CancellationToken, ) -> Result<VerifyReport>
Re-hash every chunk that both stores hold, repairing the file from the legacy copy when they disagree.
A filename is not proof the bytes behind it are good. The startup scan reads names only, so a file that was truncated or that rotted while the node was down is indexed, counted as copied, committed to, and would have its intact legacy copy deleted underneath it. The first verified read would then find the corruption with nothing left to repair from. This pass is what turns “a file with that name exists” into “those bytes are that chunk”, and it is why it runs before retirement rather than after.
§Errors
Returns Error::Storage if the legacy key set cannot be read.
Sourcepub async fn retire_legacy<F>(
&self,
proof: &VerifyReport,
still_answerable: &F,
approved_to_shed: &BTreeSet<XorName>,
) -> Result<u64>
pub async fn retire_legacy<F>( &self, proof: &VerifyReport, still_answerable: &F, approved_to_shed: &BTreeSet<XorName>, ) -> Result<u64>
Close the legacy environment and remove it, returning the bytes freed.
This is the only destructive step in the migration and the only one that cannot be undone. It is also the only moment the disk comes back.
Takes a VerifyReport rather than a flag so the verification pass cannot be
skipped: there is no way to call this without having produced one.
§Errors
Returns Error::Storage if verification did not pass or no longer describes the
store, if a write has not reported back, if the handle is still shared (the caller
should retry on the next tick), or if the directory cannot be removed.
Sourcepub async fn recover_lost_legacy_handle(&self) -> bool
pub async fn recover_lost_legacy_handle(&self) -> bool
Try again to open a legacy environment this node has lost its handle to.
A rename that failed and then could not be reopened leaves the directory on disk with no way to read it, and every chunk that lives only there unserved. Saying so once and waiting for a restart is not enough: the reason is usually transient, and a node that is otherwise healthy should not stay half-blind until somebody notices.
Returns whether it came back. Does nothing when there is a handle already, or when there is nothing on disk to open.
Sourcepub fn has_cleanup_pending(&self) -> bool
pub fn has_cleanup_pending(&self) -> bool
Is there a retired directory still waiting to be deleted?
Separate from having a legacy environment: a node whose removal was interrupted has no handle and nothing to migrate, but its disk has not come back. Something has to keep trying during this uptime rather than leaving it until the next restart.
Sourcepub fn retry_cleanup(&self)
pub fn retry_cleanup(&self)
Try again to finish a removal a previous attempt left behind.
Safe to call at any time: it only ever moves or deletes a directory that carries its own retirement mark.
Sourcepub async fn reconcile_pending_writes(&self)
pub async fn reconcile_pending_writes(&self)
Resolve writes whose outcome was never recorded.
A write announces itself before it starts and clears the note when both halves have returned. A note still there afterwards belongs to a write nobody waited for, and only the disk can say what became of it: the file store has the chunk, or the environment does and nothing else, or neither and there was never anything to protect.
Sourcepub fn has_pending_writes(&self) -> bool
pub fn has_pending_writes(&self) -> bool
Are there writes in flight whose outcome nothing has recorded?
Sourcepub fn legacy_dir_is_on_disk(&self) -> bool
pub fn legacy_dir_is_on_disk(&self) -> bool
Is there anything at the legacy environment’s path at all?
Asked without a handle, and answered conservatively: a path this node cannot even look at counts as present. The migration is not finished while something is there, whether or not this node can currently read it.
Sourcepub fn legacy_is_a_link(&self) -> bool
pub fn legacy_is_a_link(&self) -> bool
Is the legacy environment a link this node must not delete?
Copying out of it works; only the removal is refused. Callers use this to stop waiting for a retirement that is never going to happen.
Sourcepub fn writes_without_a_rollback_copy(&self) -> u64
pub fn writes_without_a_rollback_copy(&self) -> u64
How many writes this node made without a rollback copy, cumulatively.
Zero on a node that is not bridging, and zero on a bridging node whose environment has room. A number that is climbing says this node would lose those chunks on a rollback to a pre-migration build, which is a fleet question the second release turns on and which a per-chunk log line cannot answer.
Attempts rather than distinct chunks, for the reason given on the field: it is a rate, not an inventory.
Sourcepub fn legacy_cannot_be_classified(&self) -> bool
pub fn legacy_cannot_be_classified(&self) -> bool
Is there an environment on disk this node cannot classify at all?
Neither removable nor openable, which is not a state waiting will clear: something about the path has to change first, and until it does the node will refuse to touch it in either direction. The driver treats this the way it treats a lost handle or a link, by standing down from the shared volume and saying so where an operator looks, because holding a disk exclusively to wait for a person is a disk nobody else can use.
Sourcepub fn has_lost_its_legacy_handle(&self) -> bool
pub fn has_lost_its_legacy_handle(&self) -> bool
Is there an environment on disk this node can no longer read?
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<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