Skip to content

Commit 298e50e

Browse files
committed
resolve comments
Signed-off-by: glorv <glorvs@163.com>
1 parent 4a056c4 commit 298e50e

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

src/pool/builder.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,15 @@ impl Builder {
290290
self.build_with_queue_and_runner(QueueType::Multilevel(queue_builder), runner_builder)
291291
}
292292

293+
/// Spawn a priority future pool.
293294
///
295+
/// It setups the pool with priority queue. Caller must provide a `TaskPriorityProvider` implementation to generate the proper priority value for each spawned task.
294296
pub fn build_priority_future_pool(
295297
&self,
296-
priority_priovider: Arc<dyn priority::TaskPriorityProvider>,
298+
priority_provider: Arc<dyn priority::TaskPriorityProvider>,
297299
) -> ThreadPool<future::TaskCell> {
298300
let fb = CloneRunnerBuilder(future::Runner::default());
299-
let queue_builder = priority::Builder::new(priority::Config::default(), priority_priovider);
301+
let queue_builder = priority::Builder::new(priority::Config::default(), priority_provider);
300302
let runner_builder = queue_builder.runner_builder(fb);
301303
self.build_with_queue_and_runner(QueueType::Priority(queue_builder), runner_builder)
302304
}

src/queue/extras.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ pub struct Extras {
2828
pub(crate) fixed_level: Option<u8>,
2929
/// Number of execute times
3030
pub(crate) exec_times: u32,
31-
/// The task group name. Used in priority queue.
32-
pub(crate) group_name: String,
31+
/// Extra metadata of this task. User can use this field to store arbitrary data. It is useful
32+
/// in some case to implement more complext `TaskPriorityProvider` in the priority task queue.
33+
pub(crate) metadata: Vec<u8>,
3334
}
3435

3536
impl Extras {
@@ -45,7 +46,7 @@ impl Extras {
4546
current_level: 0,
4647
fixed_level: None,
4748
exec_times: 0,
48-
group_name: String::new(),
49+
metadata: Vec::new(),
4950
}
5051
}
5152

@@ -68,7 +69,7 @@ impl Extras {
6869
current_level: fixed_level.unwrap_or(0),
6970
fixed_level,
7071
exec_times: 0,
71-
group_name: String::new(),
72+
metadata: Vec::new(),
7273
}
7374
}
7475

@@ -94,13 +95,18 @@ impl Extras {
9495
self.current_level
9596
}
9697

97-
/// Gets the group name of this task.
98-
pub fn group_name(&self) -> &str {
99-
&self.group_name
98+
/// Gets the metadata of this task.
99+
pub fn metadata(&self) -> &[u8] {
100+
&self.metadata
101+
}
102+
103+
/// Gets the mutable metadata of this task.
104+
pub fn metadata_mut(&mut self) -> &mut Vec<u8> {
105+
&mut self.metadata
100106
}
101107

102108
/// Set the group name of this task.
103-
pub fn set_group_name(&mut self, name: String) {
104-
self.group_name = name;
109+
pub fn set_metadata(&mut self, metadata: Vec<u8>) {
110+
self.metadata = metadata;
105111
}
106112
}

src/queue/priority.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.
22

33
//! A priority task queue. Tasks are scheduled based on its priority, tasks with small priority
4-
//! value will be scheduler earlier than bigger onces.. User should implement The [`TaskPriorityProvider`]
4+
//! value will be scheduler earlier than bigger onces. User should implement The [`TaskPriorityProvider`]
55
//! to provide the priority value for each task. The priority value is fetched from the
66
//! [`TaskPriorityProvider`] at each time the task is scheduled.
77
//!
@@ -86,11 +86,11 @@ where
8686

8787
/// A trait used to generate priority value for each task.
8888
pub trait TaskPriorityProvider: Send + Sync + 'static {
89-
/// Return a priority value of this task, all tasks in the priority queue is ordered by this value.
89+
/// Return a priority value of this task, all tasks in the priority
90+
/// queue is ordered by this value.
9091
fn get_priority(&self, extras: &Extras) -> u64;
9192
}
9293

93-
///
9494
#[derive(Clone)]
9595
struct PriorityTaskManager {
9696
level_manager: Arc<TaskLevelManager>,
@@ -152,8 +152,8 @@ impl<T: TaskCell + Send + 'static> QueueCore<T> {
152152
}
153153

154154
#[inline]
155-
fn gen_key(&self, weight: u64) -> MapKey {
156-
MapKey(weight, self.sequence.fetch_add(1, Ordering::Relaxed))
155+
fn gen_key(&self, priority: u64) -> MapKey {
156+
MapKey(priority, self.sequence.fetch_add(1, Ordering::Relaxed))
157157
}
158158
}
159159

@@ -265,8 +265,7 @@ pub struct Config {
265265
}
266266

267267
impl Config {
268-
/// Sets the name of the multilevel task queue. Metrics of multilevel
269-
/// task queues are available if name is provided.
268+
/// Sets the name of the priority task queue. Metrics are available if name is provided.
270269
pub fn name(mut self, name: Option<impl Into<String>>) -> Self {
271270
self.name = name.map(Into::into);
272271
self
@@ -290,7 +289,7 @@ pub struct Builder {
290289
}
291290

292291
impl Builder {
293-
/// Creates a multilevel task queue builder with specified config and [`TaskPriorityProvider`].
292+
/// Creates a priority task queue builder with specified config and [`TaskPriorityProvider`].
294293
pub fn new(config: Config, priority_manager: Arc<dyn TaskPriorityProvider>) -> Builder {
295294
let Config {
296295
name,

0 commit comments

Comments
 (0)