From f5e1ea8469b704282e83466aa75880edeab31d31 Mon Sep 17 00:00:00 2001 From: qima Date: Mon, 10 Jun 2024 20:02:14 +0800 Subject: [PATCH] fix(faucet): intial upload start from max_seen, more error_tolerable --- sn_faucet/src/faucet_server.rs | 64 +++++++++++++++++++++++++++++----- sn_faucet/src/gutenberger.rs | 11 ++++++ 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/sn_faucet/src/faucet_server.rs b/sn_faucet/src/faucet_server.rs index 10a3d6c353..c2ce24b15d 100644 --- a/sn_faucet/src/faucet_server.rs +++ b/sn_faucet/src/faucet_server.rs @@ -90,9 +90,17 @@ async fn upload_initial_data(client: &Client, root_dir: &Path) -> Result<()> { let reqwest_client = ReqwestClient::new(); - let mut uploaded_books = vec![]; + let mut uploaded_books: Vec<(String, String)> = if uploaded_books_file.exists() { + let file = File::open(&uploaded_books_file)?; + serde_json::from_reader(file)? + } else { + vec![] + }; - for book_id in 1..u16::MAX as u32 { + println!("Previous upload state restored"); + info!("Previous upload state restored"); + + for book_id in state.max_seen()..u16::MAX as u32 { if state.has_seen(book_id) { println!("Already seen book ID: {book_id}"); info!("Already seen book ID: {book_id}"); @@ -106,11 +114,18 @@ async fn upload_initial_data(client: &Client, root_dir: &Path) -> Result<()> { let fname = format!("{book_id}.book"); let fpath = temp_dir.join(fname.clone()); - let mut dest = fs::File::create(fpath.clone()).await?; - dest.write_all(&data).await?; - state.mark_seen(book_id); - state.save_to_file(&state_file)?; + match mark_download_progress(book_id, &fpath, data, &mut state, &state_file).await { + Ok(_) => { + println!("Marked download progress book ID: {book_id} completed"); + info!("Marked download progress book ID: {book_id} completed"); + } + Err(err) => { + println!("When marking download progress book ID: {book_id}, encountered error {err:?}"); + error!("When marking download progress book ID: {book_id}, encountered error {err:?}"); + continue; + } + } match upload_downloaded_book(client, root_dir, fpath).await { Ok(head_addresses) => { @@ -120,8 +135,18 @@ async fn upload_initial_data(client: &Client, root_dir: &Path) -> Result<()> { // There shall be just one for head_address in head_addresses { uploaded_books.push((fname.clone(), head_address.to_hex())); - let file = File::create(uploaded_books_file.clone())?; - serde_json::to_writer(file, &uploaded_books)?; + + match mark_upload_progress(&uploaded_books_file, &uploaded_books) { + Ok(_) => { + println!("Marked upload progress book ID: {book_id} completed"); + info!("Marked upload progress book ID: {book_id} completed"); + } + Err(err) => { + println!("When marking upload progress book ID: {book_id}, encountered error {err:?}"); + error!("When marking upload progress book ID: {book_id}, encountered error {err:?}"); + continue; + } + } } } Err(err) => { @@ -142,6 +167,29 @@ async fn upload_initial_data(client: &Client, root_dir: &Path) -> Result<()> { Ok(()) } +#[cfg(feature = "initial-data")] +async fn mark_download_progress( + book_id: u32, + fpath: &Path, + data: Vec, + state: &mut State, + state_file: &Path, +) -> Result<()> { + let mut dest = fs::File::create(fpath).await?; + dest.write_all(&data).await?; + + state.mark_seen(book_id); + state.save_to_file(state_file)?; + Ok(()) +} + +#[cfg(feature = "initial-data")] +fn mark_upload_progress(fpath: &Path, uploaded_books: &Vec<(String, String)>) -> Result<()> { + let file = File::create(fpath)?; + serde_json::to_writer(file, &uploaded_books)?; + Ok(()) +} + #[cfg(feature = "initial-data")] async fn upload_downloaded_book( client: &Client, diff --git a/sn_faucet/src/gutenberger.rs b/sn_faucet/src/gutenberger.rs index 12ea1a38ea..4968c93cc4 100644 --- a/sn_faucet/src/gutenberger.rs +++ b/sn_faucet/src/gutenberger.rs @@ -46,8 +46,19 @@ impl State { } pub(crate) fn has_seen(&self, book_id: u32) -> bool { + if book_id == 0 && self.seen_books.is_empty() { + return true; + } self.seen_books.contains(&book_id) } + + pub(crate) fn max_seen(&self) -> u32 { + if let Some(result) = self.seen_books.iter().max() { + *result + } else { + 0 + } + } } pub(crate) async fn download_book(client: &Client, book_id: u32) -> Result> {