risingwave_storage/hummock/compactor/
task_progress.rs

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
// Copyright 2024 RisingWave Labs
//
// 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 std::collections::HashMap;
use std::sync::atomic::{AtomicU32, AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;

use parking_lot::Mutex;
use risingwave_hummock_sdk::HummockCompactionTaskId;
use risingwave_pb::hummock::CompactTaskProgress;

pub type TaskProgressManagerRef = Arc<Mutex<HashMap<HummockCompactionTaskId, Arc<TaskProgress>>>>;

/// The progress of a compaction task.
#[derive(Default)]
pub struct TaskProgress {
    num_ssts_sealed: AtomicU32,
    num_ssts_uploaded: AtomicU32,
    num_progress_key: AtomicU64,
    num_pending_read_io: AtomicUsize,
    num_pending_write_io: AtomicUsize,
}

impl TaskProgress {
    pub fn inc_ssts_sealed(&self) {
        self.num_ssts_sealed.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_ssts_uploaded(&self) {
        self.num_ssts_uploaded.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_progress_key(&self, inc_key_num: u64) {
        self.num_progress_key
            .fetch_add(inc_key_num, Ordering::Relaxed);
    }

    pub fn inc_num_pending_read_io(&self) {
        self.num_pending_read_io.fetch_add(1, Ordering::SeqCst);
    }

    pub fn inc_num_pending_write_io(&self) {
        self.num_pending_write_io.fetch_add(1, Ordering::SeqCst);
    }

    pub fn dec_num_pending_read_io(&self) {
        self.num_pending_read_io.fetch_sub(1, Ordering::SeqCst);
    }

    pub fn dec_num_pending_write_io(&self) {
        self.num_pending_write_io.fetch_sub(1, Ordering::SeqCst);
    }

    pub fn snapshot(&self, task_id: u64) -> CompactTaskProgress {
        CompactTaskProgress {
            task_id,
            num_ssts_sealed: self.num_ssts_sealed.load(Ordering::Relaxed),
            num_ssts_uploaded: self.num_ssts_uploaded.load(Ordering::Relaxed),
            num_pending_read_io: self.num_pending_read_io.load(Ordering::Relaxed) as u64,
            num_pending_write_io: self.num_pending_write_io.load(Ordering::Relaxed) as u64,
            num_progress_key: self.num_progress_key.load(Ordering::Relaxed),
            ..Default::default()
        }
    }
}

/// An RAII object that contains a [`TaskProgress`] and shares it to all the splits of the task.
#[derive(Default)]
pub struct TaskProgressGuard {
    task_id: HummockCompactionTaskId,
    progress_manager: TaskProgressManagerRef,

    pub progress: Arc<TaskProgress>,
}

impl TaskProgressGuard {
    pub fn new(task_id: HummockCompactionTaskId, progress_manager: TaskProgressManagerRef) -> Self {
        let progress = progress_manager.lock().entry(task_id).or_default().clone();
        Self {
            task_id,
            progress_manager,
            progress,
        }
    }
}

impl Drop for TaskProgressGuard {
    fn drop(&mut self) {
        // The entry is created along with `TaskProgress`, so it must exist.
        self.progress_manager
            .lock()
            .remove(&self.task_id)
            .expect("task progress should exist when task is finished");
    }
}

#[cfg(test)]
mod tests {
    use super::{TaskProgressGuard, TaskProgressManagerRef};

    #[test]
    fn test_progress_removal() {
        let task_progress_manager = TaskProgressManagerRef::default();
        {
            let _guard = TaskProgressGuard::new(1, task_progress_manager.clone());
            assert_eq!(task_progress_manager.lock().len(), 1);
        }
        assert_eq!(task_progress_manager.lock().len(), 0);
    }
}