Skip to content

Commit

Permalink
Merge pull request #2488 from subspace/uniform-cache-population
Browse files Browse the repository at this point in the history
Fill disk caches uniformly
  • Loading branch information
nazar-pc committed Jan 31, 2024
2 parents 148922f + 4bf3dfd commit 34579c3
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions crates/subspace-farmer/src/piece_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,30 +417,30 @@ where
};

// Find plot in which there is a place for new piece to be stored
if !caches
.iter_mut()
.enumerate()
.any(|(disk_farm_index, cache)| {
let Some(offset) = cache.free_offsets.pop() else {
return false;
};

if let Err(error) = cache.backend.write_piece(offset, piece_index, &piece) {
error!(
%error,
%disk_farm_index,
%piece_index,
%offset,
"Failed to write piece into cache"
);
return false;
}
cache
.stored_pieces
.insert(RecordKey::from(piece_index.to_multihash()), offset);
true
})
{
let mut sorted_caches = caches.iter_mut().enumerate().collect::<Vec<_>>();
// Sort piece caches by number of stored pieces to fill those that are less
// populated first
sorted_caches.sort_by_key(|(_, cache)| cache.stored_pieces.len());
if !sorted_caches.into_iter().any(|(disk_farm_index, cache)| {
let Some(offset) = cache.free_offsets.pop() else {
return false;
};

if let Err(error) = cache.backend.write_piece(offset, piece_index, &piece) {
error!(
%error,
%disk_farm_index,
%piece_index,
%offset,
"Failed to write piece into cache"
);
return false;
}
cache
.stored_pieces
.insert(RecordKey::from(piece_index.to_multihash()), offset);
true
}) {
error!(
%piece_index,
"Failed to store piece in cache, there was no space"
Expand Down Expand Up @@ -697,7 +697,11 @@ where
}
// There is free space in cache, need to find a free spot and place piece there
None => {
for (disk_farm_index, cache) in caches.iter_mut().enumerate() {
let mut sorted_caches = caches.iter_mut().enumerate().collect::<Vec<_>>();
// Sort piece caches by number of stored pieces to fill those that are less
// populated first
sorted_caches.sort_by_key(|(_, cache)| cache.stored_pieces.len());
for (disk_farm_index, cache) in sorted_caches {
let Some(offset) = cache.free_offsets.pop() else {
// Not this disk farm
continue;
Expand Down

0 comments on commit 34579c3

Please sign in to comment.