Skip to content

Commit

Permalink
Merge pull request #2496 from subspace/gemini-3g-backport-fix-piece-c…
Browse files Browse the repository at this point in the history
…ache-sync-restart

Gemini 3g backport: fix piece cache sync restart by storing pieces in lower piece index offsets
  • Loading branch information
nazar-pc authored Feb 1, 2024
2 parents 8ca4ca0 + b614881 commit 5014213
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crates/subspace-farmer/src/piece_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures::channel::oneshot;
use futures::stream::{FuturesOrdered, FuturesUnordered};
use futures::{select, FutureExt, StreamExt};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::collections::{HashMap, VecDeque};
use std::num::NonZeroU16;
use std::sync::Arc;
use std::{fmt, mem};
Expand Down Expand Up @@ -42,7 +42,7 @@ struct Handlers {
#[derive(Debug, Clone)]
struct DiskPieceCacheState {
stored_pieces: HashMap<RecordKey, Offset>,
free_offsets: Vec<Offset>,
free_offsets: VecDeque<Offset>,
backend: DiskPieceCache,
}

Expand Down Expand Up @@ -178,7 +178,7 @@ where
};

// Making offset as unoccupied and remove corresponding key from heap
cache.free_offsets.push(offset);
cache.free_offsets.push_front(offset);
match cache.backend.read_piece_index(offset) {
Ok(Some(piece_index)) => {
worker_state.heap.remove(KeyWrapper(piece_index));
Expand Down Expand Up @@ -227,7 +227,7 @@ where
free_offsets.push(state.free_offsets);
}
stored_pieces.resize(new_caches.len(), HashMap::default());
free_offsets.resize(new_caches.len(), Vec::default());
free_offsets.resize(new_caches.len(), VecDeque::default());

debug!("Collecting pieces that were in the cache before");

Expand All @@ -253,7 +253,7 @@ where
);
}
None => {
free_offsets.push(offset);
free_offsets.push_back(offset);
}
}

Expand Down Expand Up @@ -356,7 +356,7 @@ where
.stored_pieces
.extract_if(|key, _offset| piece_indices_to_store.remove(key).is_none())
.for_each(|(_piece_index, offset)| {
state.free_offsets.push(offset);
state.free_offsets.push_front(offset);
});
});

Expand Down Expand Up @@ -421,7 +421,7 @@ where
.iter_mut()
.enumerate()
.any(|(disk_farm_index, cache)| {
let Some(offset) = cache.free_offsets.pop() else {
let Some(offset) = cache.free_offsets.pop_front() else {
return false;
};

Expand Down Expand Up @@ -698,7 +698,7 @@ 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 Some(offset) = cache.free_offsets.pop() else {
let Some(offset) = cache.free_offsets.pop_front() else {
// Not this disk farm
continue;
};
Expand Down

0 comments on commit 5014213

Please sign in to comment.