Skip to content

Commit

Permalink
Tune intervals and number of attempts for downloading pieces, try sec…
Browse files Browse the repository at this point in the history
…tor plotting until it succeeds instead of exiting on failure
  • Loading branch information
nazar-pc committed Apr 17, 2023
1 parent d18a73e commit 776f678
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/subspace-farmer-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bench = false

[dependencies]
async-trait = "0.1.68"
backoff = { version = "0.4.0", features = ["futures", "tokio"] }
fs2 = "0.4.3"
futures = "0.3.28"
libc = "0.2.139"
Expand Down
56 changes: 43 additions & 13 deletions crates/subspace-farmer-components/src/plotting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@ use crate::piece_caching::PieceMemoryCache;
use crate::segment_reconstruction::recover_missing_piece;
use crate::{FarmerProtocolInfo, SectorMetadata};
use async_trait::async_trait;
use backoff::future::retry;
use backoff::{Error as BackoffError, ExponentialBackoff};
use futures::stream::FuturesOrdered;
use futures::StreamExt;
use parity_scale_codec::Encode;
use parking_lot::Mutex;
use std::error::Error;
use std::io;
use std::sync::Arc;
use std::time::Duration;
use subspace_core_primitives::crypto::kzg::{Commitment, Kzg};
use subspace_core_primitives::crypto::{Scalar, ScalarLegacy};
use subspace_core_primitives::sector_codec::{SectorCodec, SectorCodecError};
use subspace_core_primitives::{
LegacySectorId, Piece, PieceIndex, PieceIndexHash, PublicKey, SectorIndex, PLOT_SECTOR_SIZE,
};
use thiserror::Error;
use tracing::info;
use tracing::{info, warn};

fn default_backoff() -> ExponentialBackoff {
ExponentialBackoff {
initial_interval: Duration::from_secs(15),
max_interval: Duration::from_secs(10 * 60),
// Try until we get a valid piece
max_elapsed_time: None,
..ExponentialBackoff::default()
}
}

/// Defines retry policy on error during piece acquiring.
#[derive(PartialEq, Eq, Clone, Debug, Copy)]
Expand Down Expand Up @@ -139,20 +153,36 @@ where
})
.collect();

let mut in_memory_sector_scalars =
Vec::with_capacity(PLOT_SECTOR_SIZE as usize / Scalar::FULL_BYTES);
let in_memory_sector_scalars = Mutex::new(Vec::with_capacity(
PLOT_SECTOR_SIZE as usize / Scalar::FULL_BYTES,
));

plot_pieces_in_batches_non_blocking(
&mut in_memory_sector_scalars,
sector_index,
piece_getter,
piece_getter_retry_policy,
kzg,
&piece_indexes,
piece_memory_cache,
)
retry(default_backoff(), || async {
let mut in_memory_sector_scalars = in_memory_sector_scalars.lock();
in_memory_sector_scalars.clear();

if let Err(error) = download_pieces_in_batches_non_blocking(
&mut in_memory_sector_scalars,
sector_index,
piece_getter,
piece_getter_retry_policy,
kzg,
&piece_indexes,
piece_memory_cache.clone(),
)
.await
{
warn!(%error, "Sector plotting attempt failed, will retry later");

return Err(BackoffError::transient(error));
}

Ok(())
})
.await?;

let mut in_memory_sector_scalars = in_memory_sector_scalars.into_inner();

sector_codec
.encode(&mut in_memory_sector_scalars)
.map_err(PlottingError::FailedToEncodeSector)?;
Expand Down Expand Up @@ -213,7 +243,7 @@ where
})
}

async fn plot_pieces_in_batches_non_blocking<PG: PieceGetter>(
async fn download_pieces_in_batches_non_blocking<PG: PieceGetter>(
in_memory_sector_scalars: &mut Vec<ScalarLegacy>,
sector_index: u64,
piece_getter: &PG,
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-farmer/src/single_disk_plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use tracing::{debug, error, info, info_span, trace, warn, Instrument, Span};
use ulid::Ulid;

/// Get piece retry attempts number.
const PIECE_GETTER_RETRY_NUMBER: NonZeroU16 = NonZeroU16::new(30).expect("Not zero; qed");
const PIECE_GETTER_RETRY_NUMBER: NonZeroU16 = NonZeroU16::new(3).expect("Not zero; qed");

// Refuse to compile on non-64-bit platforms, offsets may fail on those when converting from u64 to
// usize depending on chain parameters
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-networking/src/utils/piece_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use subspace_core_primitives::{Piece, PieceIndex, PieceIndexHash};
use tracing::{debug, error, trace, warn};

/// Defines initial duration between get_piece calls.
const GET_PIECE_INITIAL_INTERVAL: Duration = Duration::from_secs(1);
const GET_PIECE_INITIAL_INTERVAL: Duration = Duration::from_secs(3);
/// Defines max duration between get_piece calls.
const GET_PIECE_MAX_INTERVAL: Duration = Duration::from_secs(5);
const GET_PIECE_MAX_INTERVAL: Duration = Duration::from_secs(10);

#[async_trait]
pub trait PieceValidator: Sync + Send {
Expand Down

0 comments on commit 776f678

Please sign in to comment.