Skip to content

Commit 05d0f93

Browse files
authored
feat: add leave task and remove task finished only download from local (#480)
Signed-off-by: Gaius <[email protected]>
1 parent 5c8e9f5 commit 05d0f93

File tree

9 files changed

+52
-132
lines changed

9 files changed

+52
-132
lines changed

Cargo.lock

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

Cargo.toml

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

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

2424
[workspace.dependencies]
25-
dragonfly-client = { path = "dragonfly-client", version = "0.1.64" }
26-
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.64" }
27-
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.64" }
28-
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.64" }
29-
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.64" }
30-
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.64" }
31-
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.64" }
25+
dragonfly-client = { path = "dragonfly-client", version = "0.1.65" }
26+
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.65" }
27+
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.65" }
28+
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.65" }
29+
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.65" }
30+
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.65" }
31+
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.65" }
3232
thiserror = "1.0"
33-
dragonfly-api = "2.0.112"
33+
dragonfly-api = "2.0.113"
3434
reqwest = { version = "0.11.27", features = ["stream", "native-tls", "default-tls", "rustls-tls"] }
3535
rcgen = { version = "0.12.1", features = ["x509-parser"] }
3636
hyper = { version = "1.2", features = ["full"] }

dragonfly-client-storage/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ impl Storage {
7979
pub fn download_task_started(
8080
&self,
8181
id: &str,
82-
peer_id: &str,
8382
piece_length: u64,
8483
response_header: Option<HeaderMap>,
8584
) -> Result<metadata::Task> {
8685
self.metadata
87-
.download_task_started(id, peer_id, piece_length, response_header)
86+
.download_task_started(id, piece_length, response_header)
8887
}
8988

9089
// download_task_finished updates the metadata of the task when the task downloads finished.

dragonfly-client-storage/src/metadata.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use dragonfly_client_core::{Error, Result};
1919
use dragonfly_client_util::http::reqwest_headermap_to_hashmap;
2020
use reqwest::header::{self, HeaderMap};
2121
use serde::{Deserialize, Serialize};
22-
use std::collections::{HashMap, HashSet};
22+
use std::collections::HashMap;
2323
use std::path::Path;
2424
use std::time::Duration;
2525
use tracing::error;
@@ -32,8 +32,6 @@ pub struct Task {
3232
// id is the task id.
3333
pub id: String,
3434

35-
pub peer_ids: HashSet<String>,
36-
3735
// piece_length is the length of the piece.
3836
pub piece_length: u64,
3937

@@ -226,7 +224,6 @@ impl<E: StorageEngineOwned> Metadata<E> {
226224
pub fn download_task_started(
227225
&self,
228226
id: &str,
229-
peer_id: &str,
230227
piece_length: u64,
231228
response_header: Option<HeaderMap>,
232229
) -> Result<Task> {
@@ -241,7 +238,6 @@ impl<E: StorageEngineOwned> Metadata<E> {
241238
// If the task exists, update the task metadata.
242239
task.updated_at = Utc::now().naive_utc();
243240
task.failed_at = None;
244-
task.peer_ids.insert(peer_id.to_string());
245241

246242
// If the task has the response header, the response header
247243
// will not be covered.
@@ -253,7 +249,6 @@ impl<E: StorageEngineOwned> Metadata<E> {
253249
}
254250
None => Task {
255251
id: id.to_string(),
256-
peer_ids: vec![peer_id.to_string()].into_iter().collect(),
257252
piece_length,
258253
response_header,
259254
updated_at: Utc::now().naive_utc(),
@@ -556,18 +551,14 @@ mod tests {
556551
let metadata = Metadata::new(dir.path()).unwrap();
557552

558553
let task_id = "task1";
559-
let peer_id = "peer1";
560554

561555
// Test download_task_started.
562-
metadata
563-
.download_task_started(task_id, peer_id, 1024, None)
564-
.unwrap();
556+
metadata.download_task_started(task_id, 1024, None).unwrap();
565557
let mut task = metadata
566558
.get_task(task_id)
567559
.unwrap()
568560
.expect("task should exist after download_task_started");
569561
assert_eq!(task.id, task_id);
570-
assert_eq!(task.peer_ids.take(peer_id), Some(peer_id.to_string()));
571562
assert_eq!(task.piece_length, 1024);
572563
assert!(task.response_header.is_empty());
573564
assert_eq!(task.uploading_count, 0);
@@ -616,11 +607,8 @@ mod tests {
616607

617608
// Test get_tasks.
618609
let task_id = "task2";
619-
let peer_id = "peer2";
620610

621-
metadata
622-
.download_task_started(task_id, peer_id, 1024, None)
623-
.unwrap();
611+
metadata.download_task_started(task_id, 1024, None).unwrap();
624612
let tasks = metadata.get_tasks().unwrap();
625613
assert_eq!(tasks.len(), 2, "should get 2 tasks in total");
626614

dragonfly-client/src/gc/mod.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use crate::grpc::scheduler::SchedulerClient;
1818
use crate::shutdown;
19-
use dragonfly_api::scheduler::v2::LeavePeerRequest;
19+
use dragonfly_api::scheduler::v2::LeaveTaskRequest;
2020
use dragonfly_client_config::dfdaemon::Config;
2121
use dragonfly_client_core::Result;
2222
use dragonfly_client_storage::{metadata, Storage};
@@ -195,20 +195,17 @@ impl GC {
195195

196196
// leave_task_from_scheduler leaves the task from the scheduler.
197197
async fn leave_task_from_scheduler(&self, task: metadata::Task) {
198-
for peer_id in task.peer_ids {
199-
self.scheduler_client
200-
.leave_peer(
201-
task.id.as_str(),
202-
LeavePeerRequest {
203-
host_id: self.host_id.clone(),
204-
task_id: task.id.clone(),
205-
peer_id: peer_id.clone(),
206-
},
207-
)
208-
.await
209-
.unwrap_or_else(|err| {
210-
error!("failed to leave peer {}: {}", peer_id, err);
211-
});
212-
}
198+
self.scheduler_client
199+
.leave_task(
200+
task.id.as_str(),
201+
LeaveTaskRequest {
202+
host_id: self.host_id.clone(),
203+
task_id: task.id.clone(),
204+
},
205+
)
206+
.await
207+
.unwrap_or_else(|err| {
208+
error!("failed to leave peer {}: {}", task.id, err);
209+
});
213210
}
214211
}

dragonfly-client/src/grpc/dfdaemon_download.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
208208
info!("download task started: {:?}", download);
209209
let task = match self
210210
.task
211-
.download_started(task_id.as_str(), peer_id.as_str(), download.clone())
211+
.download_started(task_id.as_str(), download.clone())
212212
.await
213213
{
214214
Err(ClientError::HTTP(err)) => {

dragonfly-client/src/grpc/dfdaemon_upload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl DfdaemonUpload for DfdaemonUploadServerHandler {
394394
info!("download task started: {:?}", download);
395395
let task = match self
396396
.task
397-
.download_started(task_id.as_str(), peer_id.as_str(), download.clone())
397+
.download_started(task_id.as_str(), download.clone())
398398
.await
399399
{
400400
Err(ClientError::HTTP(err)) => {

dragonfly-client/src/grpc/scheduler.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use dragonfly_api::manager::v2::Scheduler;
2121
use dragonfly_api::scheduler::v2::{
2222
scheduler_client::SchedulerClient as SchedulerGRPCClient, AnnounceHostRequest,
2323
AnnouncePeerRequest, AnnouncePeerResponse, ExchangePeerRequest, ExchangePeerResponse,
24-
LeaveHostRequest, LeavePeerRequest, StatPeerRequest, StatTaskRequest,
24+
LeaveHostRequest, LeavePeerRequest, LeaveTaskRequest, StatPeerRequest, StatTaskRequest,
2525
};
2626
use dragonfly_client_core::error::{ErrorType, ExternalError, OrErr};
2727
use dragonfly_client_core::{Error, Result};
@@ -146,6 +146,17 @@ impl SchedulerClient {
146146
Ok(response.into_inner())
147147
}
148148

149+
// leave_task tells the scheduler that the task is leaving.
150+
#[instrument(skip(self))]
151+
pub async fn leave_task(&self, task_id: &str, request: LeaveTaskRequest) -> Result<()> {
152+
let request = Self::make_request(request);
153+
self.client(task_id.to_string())
154+
.await?
155+
.leave_task(request)
156+
.await?;
157+
Ok(())
158+
}
159+
149160
// init_announce_host announces the host to the scheduler.
150161
#[instrument(skip(self))]
151162
pub async fn init_announce_host(&self, request: AnnounceHostRequest) -> Result<()> {

0 commit comments

Comments
 (0)