Skip to content

Commit 832af14

Browse files
authored
feat: wait for piece finished before update piece metadata (#535)
Signed-off-by: Gaius <[email protected]>
1 parent 3fa0e87 commit 832af14

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ members = [
1212
]
1313

1414
[workspace.package]
15-
version = "0.1.79"
15+
version = "0.1.80"
1616
authors = ["The Dragonfly Developers"]
1717
homepage = "https://d7y.io/"
1818
repository = "https://github.com/dragonflyoss/client.git"
@@ -22,13 +22,13 @@ readme = "README.md"
2222
edition = "2021"
2323

2424
[workspace.dependencies]
25-
dragonfly-client = { path = "dragonfly-client", version = "0.1.79" }
26-
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.79" }
27-
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.79" }
28-
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.79" }
29-
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.79" }
30-
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.79" }
31-
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.79" }
25+
dragonfly-client = { path = "dragonfly-client", version = "0.1.80" }
26+
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.80" }
27+
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.80" }
28+
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.80" }
29+
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.80" }
30+
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.80" }
31+
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.80" }
3232
thiserror = "1.0"
3333
dragonfly-api = "2.0.115"
3434
reqwest = { version = "0.12.4", features = ["stream", "native-tls", "default-tls", "rustls-tls"] }

dragonfly-client-storage/src/lib.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::path::Path;
2323
use std::sync::Arc;
2424
use std::time::Duration;
2525
use tokio::io::AsyncRead;
26-
use tracing::info;
26+
use tracing::{error, info};
2727

2828
pub mod content;
2929
pub mod metadata;
@@ -210,6 +210,9 @@ impl Storage {
210210
number: u32,
211211
range: Option<Range>,
212212
) -> Result<impl AsyncRead> {
213+
// Wait for the piece to be finished.
214+
self.wait_for_piece_finished(task_id, number).await?;
215+
213216
// Start uploading the task.
214217
self.metadata.upload_task_started(task_id)?;
215218

@@ -220,16 +223,6 @@ impl Storage {
220223
return Err(err);
221224
}
222225

223-
// Wait for the piece to be finished.
224-
if let Err(err) = self.wait_for_piece_finished(task_id, number).await {
225-
// Failed uploading the task.
226-
self.metadata.upload_task_failed(task_id)?;
227-
228-
// Failed uploading the piece.
229-
self.metadata.upload_piece_failed(task_id, number)?;
230-
return Err(err);
231-
}
232-
233226
// Get the piece metadata and return the content of the piece.
234227
match self.metadata.get_piece(task_id, number)? {
235228
Some(piece) => {
@@ -294,7 +287,6 @@ impl Storage {
294287
loop {
295288
tokio::select! {
296289
_ = interval.tick() => {
297-
298290
let piece = self
299291
.get_piece(task_id, number)?
300292
.ok_or_else(|| Error::PieceNotFound(self.piece_id(task_id, number)))?;
@@ -311,6 +303,7 @@ impl Storage {
311303
wait_for_piece_count += 1;
312304
}
313305
_ = &mut piece_timeout => {
306+
self.metadata.wait_for_piece_finished_failed(task_id, number).unwrap_or_else(|err| error!("delete piece metadata failed: {}", err));
314307
return Err(Error::WaitForPieceFinishedTimeout(self.piece_id(task_id, number)));
315308
}
316309
}

dragonfly-client-storage/src/metadata.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::collections::HashMap;
2424
use std::path::Path;
2525
use std::sync::Arc;
2626
use std::time::Duration;
27-
use tracing::error;
27+
use tracing::{error, info};
2828

2929
use crate::storage_engine::{rocksdb::RocksdbStorageEngine, DatabaseObject, StorageEngineOwned};
3030

@@ -391,6 +391,7 @@ impl<E: StorageEngineOwned> Metadata<E> {
391391

392392
/// delete_task deletes the task metadata.
393393
pub fn delete_task(&self, task_id: &str) -> Result<()> {
394+
info!("delete task metadata {}", task_id);
394395
self.db.delete::<Task>(task_id.as_bytes())
395396
}
396397

@@ -441,8 +442,12 @@ impl<E: StorageEngineOwned> Metadata<E> {
441442

442443
/// download_piece_failed updates the metadata of the piece when the piece downloads failed.
443444
pub fn download_piece_failed(&self, task_id: &str, number: u32) -> Result<()> {
444-
self.db
445-
.delete::<Piece>(self.piece_id(task_id, number).as_bytes())
445+
self.delete_piece(task_id, number)
446+
}
447+
448+
// wait_for_piece_finished_failed waits for the piece to be finished or failed.
449+
pub fn wait_for_piece_finished_failed(&self, task_id: &str, number: u32) -> Result<()> {
450+
self.delete_piece(task_id, number)
446451
}
447452

448453
/// upload_piece_started updates the metadata of the piece when piece uploads started.
@@ -508,6 +513,13 @@ impl<E: StorageEngineOwned> Metadata<E> {
508513
iter.map(|ele| ele.map(|(_, piece)| piece)).collect()
509514
}
510515

516+
/// delete_piece deletes the piece metadata.
517+
pub fn delete_piece(&self, task_id: &str, number: u32) -> Result<()> {
518+
info!("delete piece metadata {}", self.piece_id(task_id, number));
519+
self.db
520+
.delete::<Piece>(self.piece_id(task_id, number).as_bytes())
521+
}
522+
511523
/// delete_pieces deletes the piece metadatas.
512524
pub fn delete_pieces(&self, task_id: &str) -> Result<()> {
513525
let iter = self.db.prefix_iter::<Piece>(task_id.as_bytes())?;

0 commit comments

Comments
 (0)