-
Notifications
You must be signed in to change notification settings - Fork 337
fix: Various metrics bug fixes and improvements #1111
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 13 commits
725ccb4
581984e
d25f2d3
2027dc9
8c8c9a5
baae1ba
68d0fdf
2347203
d0aeda1
3359738
0a8a06b
40e90d5
5bfe334
0a3044e
d430175
ff5076d
ca66095
6fd5723
c512187
56e3ead
36e6233
ff26736
82c9c7c
c77b144
11b5fb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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::execution::operators::{CopyExec, ScanExec}; | ||
| use arrow_schema::SchemaRef; | ||
| use datafusion::physical_plan::ExecutionPlan; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Wrapper around a native plan that maps to a Spark plan and can optionally contain | ||
| /// references to other native plans that should contribute to the Spark SQL metrics | ||
| /// for the root plan (such as CopyExec and ScanExec nodes) | ||
| #[derive(Debug, Clone)] | ||
| pub(crate) struct SparkPlan { | ||
| /// Spark plan ID which is passed down in the protobuf | ||
| pub(crate) plan_id: u32, | ||
| /// The root native plan that was generated for this Spark plan | ||
| pub(crate) native_plan: Arc<dyn ExecutionPlan>, | ||
|
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. so
Member
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. yes |
||
| /// Child Spark plans | ||
| pub(crate) children: Vec<Arc<SparkPlan>>, | ||
| /// Additional native plans that were generated for this Spark plan that we need | ||
| /// to collect metrics for (such as CopyExec and ScanExec) | ||
| pub(crate) additional_native_plans: Vec<Arc<dyn ExecutionPlan>>, | ||
| } | ||
|
|
||
| impl SparkPlan { | ||
| /// Create a SparkPlan that consists of a single native plan | ||
| pub(crate) fn new( | ||
| plan_id: u32, | ||
| native_plan: Arc<dyn ExecutionPlan>, | ||
| children: Vec<Arc<SparkPlan>>, | ||
| ) -> Self { | ||
| let mut additional_native_plans: Vec<Arc<dyn ExecutionPlan>> = vec![]; | ||
| for child in &children { | ||
| collect_additional_plans(Arc::clone(&child.native_plan), &mut additional_native_plans); | ||
| } | ||
| Self { | ||
| plan_id, | ||
| native_plan, | ||
| children, | ||
| additional_native_plans, | ||
| } | ||
| } | ||
|
|
||
| /// Create a SparkPlan that consists of more than one native plan | ||
| pub(crate) fn new_with_additional( | ||
| plan_id: u32, | ||
| native_plan: Arc<dyn ExecutionPlan>, | ||
| children: Vec<Arc<SparkPlan>>, | ||
| additional_native_plans: Vec<Arc<dyn ExecutionPlan>>, | ||
| ) -> Self { | ||
| let mut accum: Vec<Arc<dyn ExecutionPlan>> = vec![]; | ||
| for plan in &additional_native_plans { | ||
| accum.push(Arc::clone(plan)); | ||
| } | ||
| for child in &children { | ||
| collect_additional_plans(Arc::clone(&child.native_plan), &mut accum); | ||
| } | ||
| Self { | ||
| plan_id, | ||
| native_plan, | ||
| children, | ||
| additional_native_plans: accum, | ||
| } | ||
| } | ||
|
|
||
| /// Get the schema of the native plan | ||
| pub(crate) fn schema(&self) -> SchemaRef { | ||
| self.native_plan.schema() | ||
| } | ||
|
|
||
| /// Get the child SparkPlan instances | ||
| pub(crate) fn children(&self) -> &Vec<Arc<SparkPlan>> { | ||
| &self.children | ||
| } | ||
| } | ||
|
|
||
| fn collect_additional_plans( | ||
| child: Arc<dyn ExecutionPlan>, | ||
| additional_native_plans: &mut Vec<Arc<dyn ExecutionPlan>>, | ||
| ) { | ||
| if child.as_any().is::<CopyExec>() { | ||
| additional_native_plans.push(Arc::clone(&child)); | ||
| // CopyExec may be wrapping a ScanExec | ||
| collect_additional_plans(Arc::clone(child.children()[0]), additional_native_plans); | ||
| } else if child.as_any().is::<ScanExec>() { | ||
| additional_native_plans.push(Arc::clone(&child)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,6 +77,8 @@ pub struct ScanExec { | |
| metrics: ExecutionPlanMetricsSet, | ||
| /// Baseline metrics | ||
| baseline_metrics: BaselineMetrics, | ||
| /// Timer | ||
| jvm_fetch_time: Time, | ||
| } | ||
|
|
||
| impl ScanExec { | ||
|
|
@@ -88,6 +90,7 @@ impl ScanExec { | |
| ) -> Result<Self, CometError> { | ||
| let metrics_set = ExecutionPlanMetricsSet::default(); | ||
| let baseline_metrics = BaselineMetrics::new(&metrics_set, 0); | ||
| let jvm_fetch_time = MetricBuilder::new(&metrics_set).subset_time("jvm_fetch_time", 0); | ||
|
|
||
| // Scan's schema is determined by the input batch, so we need to set it before execution. | ||
| // Note that we determine if arrays are dictionary-encoded based on the | ||
|
|
@@ -97,8 +100,12 @@ impl ScanExec { | |
| // Dictionary-encoded primitive arrays are always unpacked. | ||
| let first_batch = if let Some(input_source) = input_source.as_ref() { | ||
| let mut timer = baseline_metrics.elapsed_compute().timer(); | ||
| let batch = | ||
| ScanExec::get_next(exec_context_id, input_source.as_obj(), data_types.len())?; | ||
| let batch = ScanExec::get_next( | ||
| exec_context_id, | ||
| input_source.as_obj(), | ||
| data_types.len(), | ||
| &jvm_fetch_time, | ||
| )?; | ||
| timer.stop(); | ||
| batch | ||
| } else { | ||
|
|
@@ -124,6 +131,7 @@ impl ScanExec { | |
| cache, | ||
| metrics: metrics_set, | ||
| baseline_metrics, | ||
| jvm_fetch_time, | ||
| schema, | ||
| }) | ||
| } | ||
|
|
@@ -171,6 +179,7 @@ impl ScanExec { | |
| self.exec_context_id, | ||
| self.input_source.as_ref().unwrap().as_obj(), | ||
| self.data_types.len(), | ||
| &self.jvm_fetch_time, | ||
| )?; | ||
| *current_batch = Some(next_batch); | ||
| } | ||
|
|
@@ -185,6 +194,7 @@ impl ScanExec { | |
| exec_context_id: i64, | ||
| iter: &JObject, | ||
| num_cols: usize, | ||
| jvm_fetch_time: &Time, | ||
| ) -> Result<InputBatch, CometError> { | ||
| if exec_context_id == TEST_EXEC_CONTEXT_ID { | ||
| // This is a unit test. We don't need to call JNI. | ||
|
|
@@ -197,6 +207,7 @@ impl ScanExec { | |
| exec_context_id | ||
| )))); | ||
| } | ||
| let mut timer = jvm_fetch_time.timer(); | ||
|
|
||
| let mut env = JVMClasses::get_env()?; | ||
|
|
||
|
|
@@ -255,6 +266,8 @@ impl ScanExec { | |
| } | ||
| } | ||
|
|
||
| timer.stop(); | ||
|
|
||
| Ok(InputBatch::new(inputs, Some(num_rows as usize))) | ||
| } | ||
| } | ||
|
|
@@ -324,7 +337,7 @@ impl ExecutionPlan for ScanExec { | |
| self.clone(), | ||
| self.schema(), | ||
| partition, | ||
| self.baseline_metrics.clone(), | ||
| self.jvm_fetch_time.clone(), | ||
| ))) | ||
| } | ||
|
|
||
|
|
@@ -365,28 +378,23 @@ struct ScanStream<'a> { | |
| scan: ScanExec, | ||
| /// Schema representing the data | ||
| schema: SchemaRef, | ||
| /// Metrics | ||
|
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. is it dropped because it repeats what we have on SparkPlan?
Member
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 have reverted some of these changes now |
||
| baseline_metrics: BaselineMetrics, | ||
| /// Cast options | ||
| cast_options: CastOptions<'a>, | ||
| /// elapsed time for casting columns to different data types during scan | ||
| cast_time: Time, | ||
| /// elapsed time for fetching arrow arrays from JVM | ||
| jvm_fetch_time: Time, | ||
| } | ||
|
|
||
| impl<'a> ScanStream<'a> { | ||
| pub fn new( | ||
| scan: ScanExec, | ||
| schema: SchemaRef, | ||
| partition: usize, | ||
| baseline_metrics: BaselineMetrics, | ||
| ) -> Self { | ||
| pub fn new(scan: ScanExec, schema: SchemaRef, partition: usize, jvm_fetch_time: Time) -> Self { | ||
|
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. perhaps |
||
| let cast_time = MetricBuilder::new(&scan.metrics).subset_time("cast_time", partition); | ||
| Self { | ||
| scan, | ||
| schema, | ||
| baseline_metrics, | ||
| cast_options: CastOptions::default(), | ||
| cast_time, | ||
| jvm_fetch_time, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -426,7 +434,7 @@ impl<'a> Stream for ScanStream<'a> { | |
| type Item = DataFusionResult<RecordBatch>; | ||
|
|
||
| fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
| let mut timer = self.baseline_metrics.elapsed_compute().timer(); | ||
| let mut timer = self.jvm_fetch_time.timer(); | ||
| let mut scan_batch = self.scan.batch.try_lock().unwrap(); | ||
|
|
||
| let input_batch = &*scan_batch; | ||
|
|
@@ -440,7 +448,6 @@ impl<'a> Stream for ScanStream<'a> { | |
| let result = match input_batch { | ||
| InputBatch::EOF => Poll::Ready(None), | ||
| InputBatch::Batch(columns, num_rows) => { | ||
| self.baseline_metrics.record_output(*num_rows); | ||
| let maybe_batch = self.build_record_batch(columns, *num_rows); | ||
| Poll::Ready(Some(maybe_batch)) | ||
| } | ||
|
|
||
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.
Is the
plan_idused somehow?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.
Or additional/child plans searches for the parent by the id?
Uh oh!
There was an error while loading. Please reload this page.
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 was not being used yet, but I have now pushed a commit to include it in the "native explain" output, to make it easier to debug performance issues