Skip to content

Commit

Permalink
Add sector expiration updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Jan 20, 2024
1 parent 3cdc6b3 commit 1a45992
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
17 changes: 17 additions & 0 deletions crates/subspace-farmer/src/single_disk_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,27 @@ pub enum SectorPlottingDetails {
},
}

/// Details about sector expiration
#[derive(Debug, Clone, Encode, Decode)]
pub enum SectorExpirationDetails {
/// Sector expiration became known
Determined {
/// Segment index at which sector expires
expires_at: SegmentIndex,
},
/// Sector will expire at the next segment index and should be replotted
AboutToExpire,
/// Sector already expired
Expired,
}

/// Various sector updates
#[derive(Debug, Clone, Encode, Decode)]
pub enum SectorUpdate {
/// Sector is is being plotted
Plotting(SectorPlottingDetails),
/// Sector expiration information updated
Expiration(SectorExpirationDetails),
}

#[derive(Default, Debug)]
Expand Down Expand Up @@ -1013,6 +1029,7 @@ impl SingleDiskFarm {
last_archived_segment_index: farmer_app_info.protocol_info.history_size.segment_index(),
min_sector_lifetime: farmer_app_info.protocol_info.min_sector_lifetime,
node_client: node_client.clone(),
handlers: Arc::clone(&handlers),
sectors_metadata: Arc::clone(&sectors_metadata),
sectors_to_plot_sender,
initial_plotting_finished: farming_delay_sender,
Expand Down
54 changes: 44 additions & 10 deletions crates/subspace-farmer/src/single_disk_farm/plotting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::single_disk_farm::{
BackgroundTaskError, Handlers, PlotMetadataHeader, SectorPlottingDetails, SectorUpdate,
RESERVED_PLOT_METADATA,
BackgroundTaskError, Handlers, PlotMetadataHeader, SectorExpirationDetails,
SectorPlottingDetails, SectorUpdate, RESERVED_PLOT_METADATA,
};
use crate::thread_pool_manager::PlottingThreadPoolManager;
use crate::utils::AsyncJoinOnDrop;
Expand Down Expand Up @@ -482,6 +482,7 @@ pub(super) struct PlottingSchedulerOptions<NC> {
pub(super) last_archived_segment_index: SegmentIndex,
pub(super) min_sector_lifetime: HistorySize,
pub(super) node_client: NC,
pub(super) handlers: Arc<Handlers>,
pub(super) sectors_metadata: Arc<RwLock<Vec<SectorMetadataChecksummed>>>,
pub(super) sectors_to_plot_sender: mpsc::Sender<SectorToPlot>,
pub(super) initial_plotting_finished: Option<oneshot::Sender<()>>,
Expand All @@ -503,6 +504,7 @@ where
last_archived_segment_index,
min_sector_lifetime,
node_client,
handlers,
sectors_metadata,
sectors_to_plot_sender,
initial_plotting_finished,
Expand Down Expand Up @@ -550,6 +552,7 @@ where
target_sector_count,
min_sector_lifetime,
&node_client,
&handlers,
sectors_metadata,
&last_archived_segment,
archived_segments_receiver,
Expand Down Expand Up @@ -695,6 +698,7 @@ async fn send_plotting_notifications<NC>(
target_sector_count: SectorIndex,
min_sector_lifetime: HistorySize,
node_client: &NC,
handlers: &Handlers,
sectors_metadata: Arc<RwLock<Vec<SectorMetadataChecksummed>>>,
last_archived_segment: &Atomic<SegmentHeader>,
mut archived_segments_receiver: mpsc::Receiver<()>,
Expand Down Expand Up @@ -771,6 +775,18 @@ where
%expires_at,
"Sector expires soon #1, scheduling replotting"
);

handlers.sector_update.call_simple(&(
sector_index,
SectorUpdate::Expiration(
if expires_at <= archived_segment_header.segment_index() {
SectorExpirationDetails::Expired
} else {
SectorExpirationDetails::AboutToExpire
},
),
));

// Time to replot
sectors_to_replot.push(SectorToReplot {
sector_index,
Expand Down Expand Up @@ -827,24 +843,35 @@ where
metadata; qed",
);

let expires_at = expiration_history_size.segment_index();

trace!(
%sector_index,
%history_size,
sector_expire_at = %expiration_history_size.segment_index(),
sector_expire_at = %expires_at,
"Determined sector expiration segment index"
);
// +1 means we will start replotting a bit before it actually expires to avoid
// storing expired sectors
if expiration_history_size.segment_index()
<= (archived_segment_header.segment_index() + SegmentIndex::ONE)
{
let expires_at = expiration_history_size.segment_index();
if expires_at <= (archived_segment_header.segment_index() + SegmentIndex::ONE) {
debug!(
%sector_index,
%history_size,
%expires_at,
"Sector expires soon #2, scheduling replotting"
);

handlers.sector_update.call_simple(&(
sector_index,
SectorUpdate::Expiration(
if expires_at <= archived_segment_header.segment_index() {
SectorExpirationDetails::Expired
} else {
SectorExpirationDetails::AboutToExpire
},
),
));

// Time to replot
sectors_to_replot.push(SectorToReplot {
sector_index,
Expand All @@ -854,12 +881,19 @@ where
trace!(
%sector_index,
%history_size,
sector_expire_at = %expiration_history_size.segment_index(),
sector_expire_at = %expires_at,
"Sector expires later, remembering sector expiration"
);

handlers.sector_update.call_simple(&(
sector_index,
SectorUpdate::Expiration(SectorExpirationDetails::Determined {
expires_at,
}),
));

// Store expiration so we don't have to recalculate it later
sectors_expire_at
.insert(sector_index, expiration_history_size.segment_index());
sectors_expire_at.insert(sector_index, expires_at);
}
}
}
Expand Down

0 comments on commit 1a45992

Please sign in to comment.