-
-
Notifications
You must be signed in to change notification settings - Fork 162
refactor: Use u64 task IDs #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9616102
fc6cc74
00bc96e
91eaa2a
b8344a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
tonic::include_proto!("rs.tokio.console.tasks"); | ||
|
||
// === IDs === | ||
|
||
impl From<u64> for TaskId { | ||
fn from(id: u64) -> Self { | ||
TaskId { id } | ||
} | ||
} | ||
|
||
impl From<TaskId> for u64 { | ||
fn from(id: TaskId) -> Self { | ||
id.id | ||
} | ||
} | ||
|
||
impl Copy for TaskId {} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,7 +6,7 @@ use tokio::sync::{mpsc, Notify}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use futures::FutureExt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use std::{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
collections::HashMap, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
collections::{hash_map::Entry, HashMap}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
convert::TryInto, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ops::{Deref, DerefMut}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sync::{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -22,6 +22,8 @@ use hdrhistogram::{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Histogram, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub type TaskId = u64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub(crate) struct Aggregator { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Channel of incoming events emitted by `TaskLayer`s. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
events: mpsc::Receiver<Event>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -42,7 +44,7 @@ pub(crate) struct Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
watchers: Vec<Watch<proto::tasks::TaskUpdate>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Currently active RPCs streaming task details events, by task ID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
details_watchers: HashMap<span::Id, Vec<Watch<proto::tasks::TaskDetails>>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
details_watchers: HashMap<TaskId, Vec<Watch<proto::tasks::TaskDetails>>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// *All* metadata for task spans and user-defined spans that we care about. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -59,6 +61,12 @@ pub(crate) struct Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// Map of task IDs to task stats. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stats: TaskData<Stats>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// A counter for the pretty task IDs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id_counter: TaskId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/// A table that contains the span ID to pretty task ID mappings. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id_mappings: HashMap<span::Id, TaskId>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Debug)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -89,7 +97,7 @@ struct Stats { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Default)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct TaskData<T> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
data: HashMap<span::Id, (T, bool)>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
data: HashMap<TaskId, (T, bool)>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct Task { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -139,9 +147,11 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
all_metadata: Vec::new(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
new_metadata: Vec::new(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tasks: TaskData { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
data: HashMap::<span::Id, (Task, bool)>::new(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
data: HashMap::<TaskId, (Task, bool)>::new(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stats: TaskData::default(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id_counter: 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id_mappings: HashMap::new(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -223,13 +233,13 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let new_tasks = self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.tasks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.all() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|(id, task)| task.to_proto(id.clone())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|(&id, task)| task.to_proto(id)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.collect(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let now = SystemTime::now(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let stats_update = self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.stats | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.all() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|(id, stats)| (id.into_u64(), stats.to_proto())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|(&id, stats)| (id, stats.to_proto())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.collect(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Send the initial state --- if this fails, the subscription is already dead | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if subscription.update(&proto::tasks::TaskUpdate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -256,22 +266,21 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
buffer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} = watch_request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tracing::debug!(id = ?id, "new task details subscription"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let task_id: span::Id = id.into(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if let Some(stats) = self.stats.get(&task_id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if let Some(stats) = self.stats.get(&id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let (tx, rx) = mpsc::channel(buffer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let subscription = Watch(tx); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let now = SystemTime::now(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Send back the stream receiver. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Then send the initial state --- if this fails, the subscription is already dead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if stream_sender.send(rx).is_ok() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
&& subscription.update(&proto::tasks::TaskDetails { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id: Some(task_id.clone().into()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id: Some(id.into()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
now: Some(now.into()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
poll_times_histogram: serialize_histogram(&stats.poll_times_histogram).ok(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.details_watchers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.entry(task_id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.entry(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.or_insert_with(Vec::new) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.push(subscription); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -294,13 +303,13 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let new_tasks = self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.tasks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.since_last_update() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|(id, task)| task.to_proto(id.clone())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|(&id, task)| task.to_proto(id)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.collect(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let now = SystemTime::now(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let stats_update = self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.stats | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.since_last_update() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|(id, stats)| (id.into_u64(), stats.to_proto())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|(&id, stats)| (id, stats.to_proto())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.collect(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let update = proto::tasks::TaskUpdate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -315,10 +324,10 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let stats = &self.stats; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Assuming there are much fewer task details subscribers than there are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// stats updates, iterate over `details_watchers` and compact the map. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.details_watchers.retain(|id, watchers| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if let Some(task_stats) = stats.get(id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.details_watchers.retain(|&id, watchers| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if let Some(task_stats) = stats.get(&id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let details = proto::tasks::TaskDetails { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id: Some(id.clone().into()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id: Some(id.into()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
now: Some(now.into()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
poll_times_histogram: serialize_histogram(&task_stats.poll_times_histogram) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.ok(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -345,16 +354,17 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
at, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fields, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let task_id = self.get_or_insert_task_id(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.tasks.insert( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
id.clone(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Task { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metadata, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fields, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// TODO: parents | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.stats.insert( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Stats { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
polls: 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
created_at: Some(at), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -363,7 +373,8 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Event::Enter { id, at } => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut stats = self.stats.update_or_default(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let task_id = self.get_or_insert_task_id(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut stats = self.stats.update_or_default(task_id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if stats.current_polls == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stats.last_poll_started = Some(at); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if stats.first_poll == None { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -375,7 +386,8 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Event::Exit { id, at } => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut stats = self.stats.update_or_default(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let task_id = self.get_or_insert_task_id(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut stats = self.stats.update_or_default(task_id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stats.current_polls -= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if stats.current_polls == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if let Some(last_poll_started) = stats.last_poll_started { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -391,17 +403,19 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Event::Close { id, at } => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.stats.update_or_default(id).closed_at = Some(at); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let task_id = self.get_or_insert_task_id(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.stats.update_or_default(task_id).closed_at = Some(at); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Event::Waker { id, op, at } => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let task_id = self.get_or_insert_task_id(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// It's possible for wakers to exist long after a task has | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// finished. We don't want those cases to create a "new" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// task that isn't closed, just to insert some waker stats. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// It may be useful to eventually be able to report about | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// "wasted" waker ops, but we'll leave that for another time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if let Some(mut stats) = self.stats.update(&id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if let Some(mut stats) = self.stats.update(&task_id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match op { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
WakeOp::Wake | WakeOp::WakeByRef => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stats.wakes += 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -431,6 +445,18 @@ impl Aggregator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn get_or_insert_task_id(&mut self, span_id: span::Id) -> TaskId { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match self.task_id_mappings.entry(span_id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Entry::Occupied(entry) => *entry.get(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Entry::Vacant(entry) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let task_id = self.task_id_counter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
entry.insert(task_id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.task_id_counter = self.task_id_counter.wrapping_add(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
task_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+449
to
+457
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit/TIOLI: I think this
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was actually the first thing I tried, but unfortunately the borrow checker is not happy about the mutation inside the closure 😕 I'm going to do a bit more reading to see if there are any other idiomatic ways.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, huh, never mind — the current approach is fine, then, if the borrow checker doesn't like this. I think we could "split" the borrows, like this:
Suggested change
but this is the same number of lines as the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn drop_closed_tasks(&mut self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let tasks = &mut self.tasks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let stats = &mut self.stats; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -510,22 +536,22 @@ impl Flush { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl<T> TaskData<T> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn update_or_default(&mut self, id: span::Id) -> Updating<'_, T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn update_or_default(&mut self, id: TaskId) -> Updating<'_, T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
where | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
T: Default, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Updating(self.data.entry(id).or_default()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn update(&mut self, id: &span::Id) -> Option<Updating<'_, T>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn update(&mut self, id: &TaskId) -> Option<Updating<'_, T>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.data.get_mut(id).map(Updating) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn insert(&mut self, id: span::Id, data: T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn insert(&mut self, id: TaskId, data: T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.data.insert(id, (data, true)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn since_last_update(&mut self) -> impl Iterator<Item = (&span::Id, &mut T)> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn since_last_update(&mut self) -> impl Iterator<Item = (&TaskId, &mut T)> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.data.iter_mut().filter_map(|(id, (data, dirty))| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if *dirty { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*dirty = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -536,11 +562,11 @@ impl<T> TaskData<T> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn all(&self) -> impl Iterator<Item = (&span::Id, &T)> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn all(&self) -> impl Iterator<Item = (&TaskId, &T)> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.data.iter().map(|(id, (data, _))| (id, data)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn get(&self, id: &span::Id) -> Option<&T> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn get(&self, id: &TaskId) -> Option<&T> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.data.get(id).map(|(data, _)| data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -603,7 +629,7 @@ impl Stats { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl Task { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn to_proto(&self, id: span::Id) -> proto::tasks::Task { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn to_proto(&self, id: u64) -> proto::tasks::Task { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
proto::tasks::Task { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
id: Some(id.into()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// TODO: more kinds of tasks... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a possible optimization for this is adding a custom
Hasher
implementation that always just callsspan::Id::into_u64
and returns that value as the hash, rather than actually hashing them. but, we can do that in a follow-up...especially because I don't really think the performance impact is particularly significant.