-
Notifications
You must be signed in to change notification settings - Fork 1k
Track scheduled function delay #5592
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
base: master
Are you sure you want to change the base?
Changes from all commits
61dfc95
999263f
fe42b8a
9b1c804
9cb3b1d
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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ use crate::db::relational_db::RelationalDB; | |
| use crate::host::module_host::{CallProcedureParams, ModuleInfo}; | ||
| use crate::host::wasm_common::module_host_actor::{InstanceCommon, WasmInstance}; | ||
| use crate::host::{InvalidProcedureArguments, InvalidReducerArguments, NoSuchModule}; | ||
| use crate::worker_metrics::WORKER_METRICS; | ||
| use anyhow::anyhow; | ||
| use core::time::Duration; | ||
| use futures::{FutureExt, StreamExt}; | ||
|
|
@@ -190,6 +191,9 @@ const MAX_SCHEDULE_DELAY: Duration = Duration::from_millis( | |
| (1 << (6 * 6)) - 1, | ||
| ); | ||
|
|
||
| /// Record when a scheduled function starts more than this long after it was due. | ||
| const RECORD_SCHEDULED_FUNCTION_DELAY_THRESHOLD: Duration = Duration::from_millis(50); | ||
|
|
||
| #[derive(thiserror::Error, Debug)] | ||
| pub enum ScheduleError { | ||
| #[error("Unable to schedule with long delay at {0:?}")] | ||
|
|
@@ -447,8 +451,9 @@ struct Reschedule { | |
| enum ScheduledProcedureStep { | ||
| Done(CallScheduledFunctionResult, bool), | ||
| Procedure { | ||
| params: CallProcedureParams, | ||
| params: Box<CallProcedureParams>, | ||
| reschedule: Option<Reschedule>, | ||
| delay: Option<(Arc<str>, Duration)>, | ||
| }, | ||
| } | ||
|
|
||
|
|
@@ -465,9 +470,17 @@ pub(super) async fn call_scheduled_procedure( | |
| // even though it has been already moved during `delete_scheduled_function_row` call. | ||
| match next_step { | ||
| ScheduledProcedureStep::Done(result, trapped) => (result, trapped), | ||
| ScheduledProcedureStep::Procedure { params, reschedule } => { | ||
| ScheduledProcedureStep::Procedure { | ||
| params, | ||
| reschedule, | ||
| delay, | ||
| } => { | ||
| if let Some((function_name, delay)) = delay.as_ref() { | ||
| record_scheduled_function_delay(module_info, function_name, *delay); | ||
| } | ||
|
|
||
| // Execute the procedure. See above for commentary on `catch_unwind()`. | ||
| let result = panic::AssertUnwindSafe(inst_common.call_procedure(params, inst)) | ||
| let result = panic::AssertUnwindSafe(inst_common.call_procedure(*params, inst)) | ||
| .catch_unwind() | ||
| .await; | ||
|
|
||
|
|
@@ -504,6 +517,7 @@ fn prepare_scheduled_procedure_call( | |
| inst: &mut impl WasmInstance, | ||
| ) -> ScheduledProcedureStep { | ||
| let ScheduledFunctionParams(item) = params; | ||
| let delay = scheduled_function_delay_for_item(&item); | ||
| let id = scheduled_item_id(&item); | ||
| let db = &**module_info.relational_db(); | ||
| let tx = db.begin_mut_tx(IsolationLevel::Serializable, Workload::Internal); | ||
|
|
@@ -530,7 +544,11 @@ fn prepare_scheduled_procedure_call( | |
| let reschedule = id.and_then(|id| { | ||
| delete_scheduled_function_row(module_info, db, id, Some(tx), (timestamp, instant), inst_common, inst) | ||
| }); | ||
| ScheduledProcedureStep::Procedure { params, reschedule } | ||
| ScheduledProcedureStep::Procedure { | ||
| params: Box::new(params), | ||
| reschedule, | ||
| delay, | ||
| } | ||
| } | ||
|
|
||
| fn call_scheduled_reducer_until_done( | ||
|
|
@@ -540,6 +558,7 @@ fn call_scheduled_reducer_until_done( | |
| inst: &mut impl WasmInstance, | ||
| ) -> (CallScheduledFunctionResult, bool) { | ||
| let ScheduledFunctionParams(item) = params; | ||
| let delay = scheduled_function_delay_for_item(&item); | ||
| let id = scheduled_item_id(&item); | ||
| let db = &**module_info.relational_db(); | ||
| let tx = db.begin_mut_tx(IsolationLevel::Serializable, Workload::Internal); | ||
|
|
@@ -561,7 +580,12 @@ fn call_scheduled_reducer_until_done( | |
| } | ||
| }; | ||
|
|
||
| call_scheduled_reducer_with_tx(module_info, db, id, tx, (timestamp, instant), params, inst_common, inst) | ||
| let result = | ||
| call_scheduled_reducer_with_tx(module_info, db, id, tx, (timestamp, instant), params, inst_common, inst); | ||
| if let Some((function_name, delay)) = delay.as_ref() { | ||
| record_scheduled_function_delay(module_info, function_name, *delay); | ||
| } | ||
| result | ||
| } | ||
|
|
||
| fn scheduled_item_id(item: &QueueItem) -> Option<ScheduledFunctionId> { | ||
|
|
@@ -571,6 +595,37 @@ fn scheduled_item_id(item: &QueueItem) -> Option<ScheduledFunctionId> { | |
| } | ||
| } | ||
|
|
||
| fn scheduled_function_delay_for_item(item: &QueueItem) -> Option<(Arc<str>, Duration)> { | ||
| match item { | ||
| QueueItem::Id { function_name, at, .. } => { | ||
| Some((function_name.clone(), scheduled_function_delay(Timestamp::now(), *at))) | ||
| } | ||
| QueueItem::VolatileNonatomicImmediate { .. } => None, | ||
| } | ||
| } | ||
|
|
||
| fn record_scheduled_function_delay(module_info: &ModuleInfo, function_name: &str, delay: Duration) { | ||
| if delay <= RECORD_SCHEDULED_FUNCTION_DELAY_THRESHOLD { | ||
| return; | ||
| } | ||
| WORKER_METRICS | ||
| .scheduled_function_delay | ||
| .with_label_values(&module_info.database_identity, function_name) | ||
|
Contributor
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. Can we cache this |
||
| .observe(delay.as_secs_f64()); | ||
|
Comment on lines
+608
to
+614
Contributor
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. Why not observe every function in the metric?
Contributor
Author
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. I didn't because of perfomance reasons with |
||
|
|
||
| log::warn!( | ||
| "scheduled function `{}` for database {} is delayed by {:.3}s, exceeding the {:.3}s threshold", | ||
| function_name, | ||
| module_info.database_identity, | ||
| delay.as_secs_f64(), | ||
| RECORD_SCHEDULED_FUNCTION_DELAY_THRESHOLD.as_secs_f64(), | ||
| ); | ||
| } | ||
|
|
||
| fn scheduled_function_delay(actual: Timestamp, requested: Timestamp) -> Duration { | ||
| actual.duration_since(requested).unwrap_or(Duration::ZERO) | ||
| } | ||
|
|
||
| #[allow(clippy::too_many_arguments)] | ||
| fn call_scheduled_reducer_with_tx( | ||
| module_info: &ModuleInfo, | ||
|
|
||
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.
It looks like
record_scheduled_function_delaycallsTimestamp::now(), in this case aftercall_scheduled_reducer_with_txhas already returned. This appears to mean that we'll measure and report the delay plus the reducer's execution time. It also appears that theparamsalready includeparams.timestamp, the invocation time reported to the reducer. Can we just use that timestamp to report the delay?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.
record_scheduled_function_delaydoes not callTimestamp::now().scheduled_function_delay_for_itemdoes that which is called correctly before calling reducer.I think, I can do better with naming.
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.
Oh, I see. I'd still prefer to use the same timestamp to compute delay as gets passed to the reducer.