Skip to content

Commit

Permalink
chore(node): only reset farthest_acceptance_distance when being reall…
Browse files Browse the repository at this point in the history
…y free
  • Loading branch information
maqi authored and JasonPaulGithub committed Apr 17, 2024
1 parent 6da215f commit f8a678d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
14 changes: 11 additions & 3 deletions sn_networking/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,17 @@ impl SwarmDriver {

match result {
Ok(_) => {
// We've stored the record fine, which means our replication
// fetcher should not be limited
self.replication_fetcher.clear_farthest_on_full();
// We've stored the record fine, if node is no longer full or nearly full,
// our replication_fetcher can no longer be restricted.
if self
.swarm
.behaviour_mut()
.kademlia
.store_mut()
.has_certain_free_space()
{
self.replication_fetcher.clear_farthest_on_full();
}
}
Err(StoreError::MaxRecords) => {
// In case the capacity reaches full, restrict replication_fetcher to
Expand Down
18 changes: 11 additions & 7 deletions sn_networking/src/record_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ impl NodeRecordStore {
self.responsible_distance_range
}

/// Consider having at least 1/4 max_records free space as `certain free`.
pub fn has_certain_free_space(&self) -> bool {
self.records.len() * 4 < self.config.max_records * 3
}

// Converts a Key into a Hex string.
fn generate_filename(key: &Key) -> String {
hex::encode(key.as_ref())
Expand Down Expand Up @@ -387,14 +392,13 @@ impl NodeRecordStore {
}

if let Some(last_record) = self.get_farthest() {
// if we're full and the incoming record is farther than the farthest record, we can't store it
if num_records >= self.config.max_records
&& self
// if the incoming record is farther than the farthest record, we can't store it
if self
.local_address
.distance(&NetworkAddress::from_record_key(&last_record))
< self
.local_address
.distance(&NetworkAddress::from_record_key(&last_record))
< self
.local_address
.distance(&NetworkAddress::from_record_key(incoming_record_key))
.distance(&NetworkAddress::from_record_key(incoming_record_key))
{
return Err(Error::MaxRecords);
}
Expand Down
10 changes: 10 additions & 0 deletions sn_networking/src/record_store_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ impl UnifiedRecordStore {
}
}

pub(crate) fn has_certain_free_space(&self) -> bool {
match self {
Self::Client(_store) => {
warn!("Calling has_certain_free_space at Client. This should not happen");
true
}
Self::Node(store) => store.has_certain_free_space(),
}
}

pub(crate) fn set_distance_range(&mut self, distance: u32) {
match self {
Self::Client(_store) => {
Expand Down

0 comments on commit f8a678d

Please sign in to comment.