dragonfly-client 1.3.6

Dragonfly client written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 *     Copyright 2023 The Dragonfly Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use crate::grpc::scheduler::SchedulerClient;
use chrono::Utc;
use dragonfly_api::scheduler::v2::DeleteTaskRequest;
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::Result;
use dragonfly_client_storage::{metadata, Storage};
use dragonfly_client_util::shutdown;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tracing::{error, info, instrument};

/// Timeout for downloading tasks. Tasks that exceed this timeout will be
/// garbage collected by disk usage. Default is 2 hours.
pub const DOWNLOAD_TASK_TIMEOUT: Duration = Duration::from_secs(2 * 60 * 60);

/// Garbage collector for dfdaemon.
pub struct GC {
    /// Configuration of the dfdaemon.
    config: Arc<Config>,

    /// ID of the host.
    host_id: String,

    /// Local storage instance.
    storage: Arc<Storage>,

    /// gRPC client for the scheduler.
    scheduler_client: Arc<SchedulerClient>,

    /// Used to shut down the garbage collector.
    shutdown: shutdown::Shutdown,

    /// Used to notify that the garbage collector shutdown is complete.
    _shutdown_complete: mpsc::UnboundedSender<()>,
}

impl GC {
    /// Creates a new garbage collector.
    pub fn new(
        config: Arc<Config>,
        host_id: String,
        storage: Arc<Storage>,
        scheduler_client: Arc<SchedulerClient>,
        shutdown: shutdown::Shutdown,
        shutdown_complete_tx: mpsc::UnboundedSender<()>,
    ) -> Self {
        Self {
            config,
            host_id,
            storage,
            scheduler_client,
            shutdown,
            _shutdown_complete: shutdown_complete_tx,
        }
    }

    /// Runs the garbage collector.
    pub async fn run(&self) {
        // Clone the shutdown channel.
        let mut shutdown = self.shutdown.clone();

        // Start the collect loop.
        let mut interval = tokio::time::interval(self.config.gc.interval);
        loop {
            tokio::select! {
                _ = interval.tick() => {
                    // Evict the task by ttl.
                    if let Err(err) = self.evict_task_by_ttl().await {
                        info!("failed to evict task by ttl: {}", err);
                    }

                    // Evict the task by disk usage.
                    if let Err(err) = self.evict_task_by_disk_usage().await {
                        info!("failed to evict task by disk usage: {}", err);
                    }

                    // Evict the persistent cache task by ttl.
                    if let Err(err) = self.evict_persistent_cache_task_by_ttl().await {
                        info!("failed to evict persistent cache task by ttl: {}", err);
                    }

                    // Evict the cache by disk usage.
                    if let Err(err) = self.evict_persistent_cache_task_by_disk_usage().await {
                        info!("failed to evict persistent cache task by disk usage: {}", err);
                    }

                    // Evict the persistent task by ttl.
                    if let Err(err) = self.evict_persistent_task_by_ttl().await {
                        info!("failed to evict persistent task by ttl: {}", err);
                    }

                    // Evict the by disk usage.
                    if let Err(err) = self.evict_persistent_task_by_disk_usage().await {
                        info!("failed to evict persistent task by disk usage: {}", err);
                    }
                }
                _ = shutdown.recv() => {
                    // Shutdown the garbage collector.
                    info!("garbage collector shutting down");
                    return
                }
            }
        }
    }

    /// Evicts tasks that have exceeded their TTL.
    #[instrument(skip_all)]
    async fn evict_task_by_ttl(&self) -> Result<()> {
        info!("start to evict by task ttl");
        for task in self.storage.get_tasks()? {
            // If the task is expired and not uploading, evict the task.
            if task.is_expired(self.config.gc.policy.task_ttl) {
                self.storage.delete_task(&task.id).await;
                info!("evict task {}", task.id);

                self.delete_task_from_scheduler(task.clone()).await;
                info!("delete task {} from scheduler", task.id);
            }
        }

        info!("evict by task ttl done");
        Ok(())
    }

    /// Evicts tasks when disk usage exceeds the threshold.
    #[instrument(skip_all)]
    async fn evict_task_by_disk_usage(&self) -> Result<()> {
        let available_space = self.storage.available_space()?;
        let total_space = self.storage.total_space()?;

        // Calculate the usage percent.
        let usage_percent = (100 - available_space * 100 / total_space) as u8;
        if usage_percent >= self.config.gc.policy.disk_high_threshold_percent {
            info!(
                "start to evict task by disk usage, disk usage {}% is higher than high threshold {}%",
                usage_percent, self.config.gc.policy.disk_high_threshold_percent
            );

            // Calculate the need evict space.
            let need_evict_space = total_space as f64
                * ((usage_percent - self.config.gc.policy.disk_low_threshold_percent) as f64
                    / 100.0);

            // Evict the task by the need evict space.
            if let Err(err) = self.evict_task_space(need_evict_space as u64).await {
                info!("failed to evict task by disk usage: {}", err);
            }

            info!("evict task by disk usage done");
        }

        Ok(())
    }

    /// Evicts tasks to free up the given amount of space.
    #[instrument(skip_all)]
    async fn evict_task_space(&self, need_evict_space: u64) -> Result<()> {
        let mut tasks = self.storage.get_tasks()?;
        tasks.sort_by(|a, b| a.updated_at.cmp(&b.updated_at));

        let mut evicted_space = 0;
        for task in tasks {
            // Evict enough space.
            if evicted_space >= need_evict_space {
                break;
            }

            // If the task has downloaded finished, task has the content length, evicted space is the
            // content length. If the task has started and did not download the data, and content
            // length is 0, evicted space is 0.
            let task_space = match task.content_length() {
                Some(content_length) => content_length,
                None => {
                    // If the task has no content length, skip it.
                    if !task.is_failed() {
                        error!("task {} has no content length", task.id);
                        continue;
                    }

                    // If the task has started and did not download the data, and content length is 0.
                    info!("task {} is failed, has no content length", task.id);
                    0
                }
            };

            //  If the task is started and not finished, and the task download is not timeout,
            //  skip it.
            if task.is_started()
                && !task.is_finished()
                && !task.is_failed()
                && (task.created_at + DOWNLOAD_TASK_TIMEOUT > Utc::now().naive_utc())
            {
                info!("task {} is started and not finished, skip it", task.id);
                continue;
            }

            // Evict the task.
            self.storage.delete_task(&task.id).await;

            // Update the evicted space.
            evicted_space += task_space;
            info!("evict task {} size {}", task.id, task_space);

            self.delete_task_from_scheduler(task.clone()).await;
            info!("delete task {} from scheduler", task.id);
        }

        info!("evict total size {}", evicted_space);
        Ok(())
    }

    /// Deletes the task from the scheduler.
    #[instrument(skip_all)]
    async fn delete_task_from_scheduler(&self, task: metadata::Task) {
        self.scheduler_client
            .delete_task(DeleteTaskRequest {
                host_id: self.host_id.clone(),
                task_id: task.id.clone(),
            })
            .await
            .unwrap_or_else(|err| {
                error!("failed to delete peer {}: {}", task.id, err);
            });
    }

    /// Evicts persistent tasks that have exceeded their TTL.
    #[instrument(skip_all)]
    async fn evict_persistent_task_by_ttl(&self) -> Result<()> {
        info!("start to evict by persistent task ttl");
        for task in self.storage.get_persistent_tasks()? {
            // If the persistent task is expired and not uploading, evict the persistent task.
            if task.is_expired() {
                self.storage.delete_persistent_task(&task.id).await;
                info!("evict persistent task {}", task.id);
            }
        }

        info!("evict by persistent task ttl done");
        Ok(())
    }

    /// Evicts persistent tasks when disk usage exceeds the threshold.
    #[instrument(skip_all)]
    async fn evict_persistent_task_by_disk_usage(&self) -> Result<()> {
        let available_space = self.storage.available_space()?;
        let total_space = self.storage.total_space()?;

        // Calculate the usage percent.
        let usage_percent = (100 - available_space * 100 / total_space) as u8;
        if usage_percent >= self.config.gc.policy.disk_high_threshold_percent {
            info!(
                "start to evict persistent task by disk usage, disk usage {}% is higher than high threshold {}%",
                usage_percent, self.config.gc.policy.disk_high_threshold_percent
            );

            // Calculate the need evict space.
            let need_evict_space = total_space as f64
                * ((usage_percent - self.config.gc.policy.disk_low_threshold_percent) as f64
                    / 100.0);

            // Evict the persistent task by the need evict space.
            if let Err(err) = self
                .evict_persistent_task_space(need_evict_space as u64)
                .await
            {
                info!("failed to evict task by disk usage: {}", err);
            }

            info!("evict persistent task by disk usage done");
        }

        Ok(())
    }

    /// Evicts persistent cache tasks that have exceeded their TTL.
    #[instrument(skip_all)]
    async fn evict_persistent_cache_task_by_ttl(&self) -> Result<()> {
        info!("start to evict by persistent cache task ttl");
        for task in self.storage.get_persistent_cache_tasks()? {
            // If the persistent cache task is expired and not uploading, evict the persistent cache task.
            if task.is_expired() {
                self.storage.delete_persistent_cache_task(&task.id).await;
                info!("evict persistent cache task {}", task.id);
            }
        }

        info!("evict by persistent cache task ttl done");
        Ok(())
    }

    /// Evicts persistent cache tasks when disk usage exceeds the threshold.
    #[instrument(skip_all)]
    async fn evict_persistent_cache_task_by_disk_usage(&self) -> Result<()> {
        let available_space = self.storage.available_space()?;
        let total_space = self.storage.total_space()?;

        // Calculate the usage percent.
        let usage_percent = (100 - available_space * 100 / total_space) as u8;
        if usage_percent >= self.config.gc.policy.disk_high_threshold_percent {
            info!(
                "start to evict persistent cache task by disk usage, disk usage {}% is higher than high threshold {}%",
                usage_percent, self.config.gc.policy.disk_high_threshold_percent
            );

            // Calculate the need evict space.
            let need_evict_space = total_space as f64
                * ((usage_percent - self.config.gc.policy.disk_low_threshold_percent) as f64
                    / 100.0);

            // Evict the persistent cache task by the need evict space.
            if let Err(err) = self
                .evict_persistent_cache_task_space(need_evict_space as u64)
                .await
            {
                info!("failed to evict task by disk usage: {}", err);
            }

            info!("evict persistent cache task by disk usage done");
        }

        Ok(())
    }

    /// Evicts persistent tasks to free up the given amount of space.
    #[instrument(skip_all)]
    async fn evict_persistent_task_space(&self, need_evict_space: u64) -> Result<()> {
        let mut tasks = self.storage.get_persistent_tasks()?;
        tasks.sort_by(|a, b| a.updated_at.cmp(&b.updated_at));

        let mut evicted_space = 0;
        for task in tasks {
            // Evict enough space.
            if evicted_space >= need_evict_space {
                break;
            }

            // If the persistent task is persistent, skip it.
            if task.is_persistent() {
                continue;
            }

            //  If the task is started and not finished, and the task download is not timeout,
            //  skip it.
            if task.is_started()
                && !task.is_finished()
                && !task.is_failed()
                && (task.created_at + DOWNLOAD_TASK_TIMEOUT > Utc::now().naive_utc())
            {
                info!(
                    "persistent task {} is started and not finished, skip it",
                    task.id
                );
                continue;
            }

            // Evict the task.
            self.storage.delete_persistent_task(&task.id).await;

            // Update the evicted space.
            let task_space = task.content_length();
            evicted_space += task_space;
            info!("evict persistent task {} size {}", task.id, task_space);
        }

        info!("evict total size {}", evicted_space);
        Ok(())
    }

    /// Evicts persistent cache tasks to free up the given amount of space.
    #[instrument(skip_all)]
    async fn evict_persistent_cache_task_space(&self, need_evict_space: u64) -> Result<()> {
        let mut tasks = self.storage.get_persistent_cache_tasks()?;
        tasks.sort_by(|a, b| a.updated_at.cmp(&b.updated_at));

        let mut evicted_space = 0;
        for task in tasks {
            // Evict enough space.
            if evicted_space >= need_evict_space {
                break;
            }

            // If the persistent cache task is persistent, skip it.
            if task.is_persistent() {
                continue;
            }

            //  If the task is started and not finished, and the task download is not timeout,
            //  skip it.
            if task.is_started()
                && !task.is_finished()
                && !task.is_failed()
                && (task.created_at + DOWNLOAD_TASK_TIMEOUT > Utc::now().naive_utc())
            {
                info!(
                    "persistent cache task {} is started and not finished, skip it",
                    task.id
                );
                continue;
            }

            // Evict the task.
            self.storage.delete_persistent_cache_task(&task.id).await;

            // Update the evicted space.
            let task_space = task.content_length();
            evicted_space += task_space;
            info!(
                "evict persistent cache task {} size {}",
                task.id, task_space
            );
        }

        info!("evict total size {}", evicted_space);
        Ok(())
    }
}