-
Notifications
You must be signed in to change notification settings - Fork 174
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
perf: improve performance of update metrics #1329
Draft
wForget
wants to merge
4
commits into
apache:main
Choose a base branch
from
wForget:COMET-1328
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,14 +16,11 @@ | |||||
// under the License. | ||||||
|
||||||
use crate::execution::spark_plan::SparkPlan; | ||||||
use crate::jvm_bridge::jni_new_global_ref; | ||||||
use crate::{ | ||||||
errors::CometError, | ||||||
jvm_bridge::{jni_call, jni_new_string}, | ||||||
}; | ||||||
use crate::{errors::CometError, jvm_bridge::jni_call}; | ||||||
use datafusion::physical_plan::metrics::MetricValue; | ||||||
use jni::objects::{GlobalRef, JString}; | ||||||
use datafusion_comet_proto::spark_metric::NativeMetricNode; | ||||||
use jni::{objects::JObject, JNIEnv}; | ||||||
use prost::Message; | ||||||
use std::collections::HashMap; | ||||||
use std::sync::Arc; | ||||||
|
||||||
|
@@ -34,10 +31,22 @@ pub fn update_comet_metric( | |||||
env: &mut JNIEnv, | ||||||
metric_node: &JObject, | ||||||
spark_plan: &Arc<SparkPlan>, | ||||||
metrics_jstrings: &mut HashMap<String, Arc<GlobalRef>>, | ||||||
) -> Result<(), CometError> { | ||||||
// combine all metrics from all native plans for this SparkPlan | ||||||
let metrics = if spark_plan.additional_native_plans.is_empty() { | ||||||
unsafe { | ||||||
let native_metric = to_native_metric_node(spark_plan); | ||||||
let jbytes = env.byte_array_from_slice(&native_metric?.encode_to_vec())?; | ||||||
jni_call!(env, comet_metric_node(metric_node).set_all_from_bytes(&jbytes) -> ())?; | ||||||
} | ||||||
Ok(()) | ||||||
} | ||||||
|
||||||
pub fn to_native_metric_node(spark_plan: &Arc<SparkPlan>) -> Result<NativeMetricNode, CometError> { | ||||||
let mut native_metric_node = NativeMetricNode { | ||||||
metrics: HashMap::new(), | ||||||
children: Vec::new(), | ||||||
}; | ||||||
|
||||||
let node_metrics = if spark_plan.additional_native_plans.is_empty() { | ||||||
spark_plan.native_plan.metrics() | ||||||
} else { | ||||||
let mut metrics = spark_plan.native_plan.metrics().unwrap_or_default(); | ||||||
|
@@ -55,60 +64,21 @@ pub fn update_comet_metric( | |||||
Some(metrics.aggregate_by_name()) | ||||||
}; | ||||||
|
||||||
update_metrics( | ||||||
env, | ||||||
metric_node, | ||||||
&metrics | ||||||
.unwrap_or_default() | ||||||
.iter() | ||||||
.map(|m| m.value()) | ||||||
.map(|m| (m.name(), m.as_usize() as i64)) | ||||||
.collect::<Vec<_>>(), | ||||||
metrics_jstrings, | ||||||
)?; | ||||||
// add metrics | ||||||
node_metrics | ||||||
.unwrap_or_default() | ||||||
.iter() | ||||||
.map(|m| m.value()) | ||||||
.map(|m| (m.name(), m.as_usize() as i64)) | ||||||
.for_each(|(name, value)| { | ||||||
native_metric_node.metrics.insert(name.to_string(), value); | ||||||
}); | ||||||
|
||||||
unsafe { | ||||||
for (i, child_plan) in spark_plan.children().iter().enumerate() { | ||||||
let child_metric_node: JObject = jni_call!(env, | ||||||
comet_metric_node(metric_node).get_child_node(i as i32) -> JObject | ||||||
)?; | ||||||
if child_metric_node.is_null() { | ||||||
continue; | ||||||
} | ||||||
update_comet_metric(env, &child_metric_node, child_plan, metrics_jstrings)?; | ||||||
} | ||||||
} | ||||||
Ok(()) | ||||||
} | ||||||
// add children | ||||||
spark_plan.children().iter().for_each(|child_plan| { | ||||||
let child_node = to_native_metric_node(child_plan).unwrap(); | ||||||
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 avoid the
Suggested change
|
||||||
native_metric_node.children.push(child_node); | ||||||
}); | ||||||
|
||||||
#[inline] | ||||||
fn update_metrics( | ||||||
env: &mut JNIEnv, | ||||||
metric_node: &JObject, | ||||||
metric_values: &[(&str, i64)], | ||||||
metrics_jstrings: &mut HashMap<String, Arc<GlobalRef>>, | ||||||
) -> Result<(), CometError> { | ||||||
unsafe { | ||||||
for &(name, value) in metric_values { | ||||||
// Perform a lookup in the jstrings cache. | ||||||
if let Some(map_global_ref) = metrics_jstrings.get(name) { | ||||||
// Cache hit. Extract the jstring from the global ref. | ||||||
let jobject = map_global_ref.as_obj(); | ||||||
let jstring = JString::from_raw(**jobject); | ||||||
// Update the metrics using the jstring as a key. | ||||||
jni_call!(env, comet_metric_node(metric_node).set(&jstring, value) -> ())?; | ||||||
} else { | ||||||
// Cache miss. Allocate a new string, promote to global ref, and insert into cache. | ||||||
let local_jstring = jni_new_string!(env, &name)?; | ||||||
let global_ref = jni_new_global_ref!(env, local_jstring)?; | ||||||
let arc_global_ref = Arc::new(global_ref); | ||||||
metrics_jstrings.insert(name.to_string(), Arc::clone(&arc_global_ref)); | ||||||
let jobject = arc_global_ref.as_obj(); | ||||||
let jstring = JString::from_raw(**jobject); | ||||||
// Update the metrics using the jstring as a key. | ||||||
jni_call!(env, comet_metric_node(metric_node).set(&jstring, value) -> ())?; | ||||||
} | ||||||
} | ||||||
} | ||||||
Ok(()) | ||||||
Ok(native_metric_node) | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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. | ||
|
||
|
||
|
||
syntax = "proto3"; | ||
|
||
package spark.spark_metric; | ||
|
||
option java_package = "org.apache.comet.serde"; | ||
|
||
message NativeMetricNode { | ||
map<string, int64> metrics = 1; | ||
repeated NativeMetricNode children = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wonder if we should add a config so that we can choose between frequent metrics updates vs just updating once the query completes. It can sometimes be helpful to see live metrics.
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.
Per-batch is probably always overkill. For long-running jobs is there a period that makes sense? It looks like Spark History defaults to 10s.
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.
I do like the idea of updating metrics every N seconds
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.
I think checking a coarse-grained clock (i.e.,
CLOCK_MONOTONIC_COARSE
) to see if N seconds has elapsed to produce updated metrics would be a reasonable compromise on performance impact vs. fresh metrics.