-
Notifications
You must be signed in to change notification settings - Fork 176
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
e2c0178
958476b
8c5724d
642c737
b52b6ae
c869bbd
71394ae
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 |
---|---|---|
|
@@ -91,6 +91,10 @@ struct ExecutionContext { | |
pub runtime: Runtime, | ||
/// Native metrics | ||
pub metrics: Arc<GlobalRef>, | ||
// The interval in milliseconds to update metrics | ||
pub metrics_update_interval: Option<Duration>, | ||
// The last update time of metrics | ||
pub metrics_last_update_time: Instant, | ||
/// The time it took to create the native plan and configure the context | ||
pub plan_creation_time: Duration, | ||
/// DataFusion SessionContext | ||
|
@@ -99,8 +103,6 @@ struct ExecutionContext { | |
pub debug_native: bool, | ||
/// Whether to write native plans with metrics to stdout | ||
pub explain_native: bool, | ||
/// Map of metrics name -> jstring object to cache jni_NewStringUTF calls. | ||
pub metrics_jstrings: HashMap<String, Arc<GlobalRef>>, | ||
/// Memory pool config | ||
pub memory_pool_config: MemoryPoolConfig, | ||
} | ||
|
@@ -160,6 +162,7 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_createPlan( | |
serialized_query: jbyteArray, | ||
partition_count: jint, | ||
metrics_node: JObject, | ||
metrics_update_interval: jlong, | ||
comet_task_memory_manager_obj: JObject, | ||
batch_size: jint, | ||
use_unified_memory_manager: jboolean, | ||
|
@@ -222,6 +225,12 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_createPlan( | |
|
||
let plan_creation_time = start.elapsed(); | ||
|
||
let metrics_update_interval = if metrics_update_interval > 0 { | ||
Some(Duration::from_millis(metrics_update_interval as u64)) | ||
} else { | ||
None | ||
}; | ||
|
||
let exec_context = Box::new(ExecutionContext { | ||
id, | ||
task_attempt_id, | ||
|
@@ -233,11 +242,12 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_createPlan( | |
stream: None, | ||
runtime, | ||
metrics, | ||
metrics_update_interval, | ||
metrics_last_update_time: Instant::now(), | ||
plan_creation_time, | ||
session_ctx: Arc::new(session), | ||
debug_native: debug_native == 1, | ||
explain_native: explain_native == 1, | ||
metrics_jstrings: HashMap::new(), | ||
memory_pool_config, | ||
}); | ||
|
||
|
@@ -508,8 +518,14 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_executePlan( | |
let next_item = exec_context.stream.as_mut().unwrap().next(); | ||
let poll_output = exec_context.runtime.block_on(async { poll!(next_item) }); | ||
|
||
// Update metrics | ||
update_metrics(&mut env, exec_context)?; | ||
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 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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I think checking a coarse-grained clock (i.e., 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 also like the idea of updating every N seconds. One good reason for updating frequently is to keep updating the live UI. 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. @mbutrovich Thank you for your idea, sounds great to me, I will try to do that later. |
||
// update metrics at interval | ||
if let Some(interval) = exec_context.metrics_update_interval { | ||
let now = Instant::now(); | ||
if now - exec_context.metrics_last_update_time >= interval { | ||
update_metrics(&mut env, exec_context)?; | ||
exec_context.metrics_last_update_time = now; | ||
} | ||
} | ||
|
||
match poll_output { | ||
Poll::Ready(Some(output)) => { | ||
|
@@ -561,8 +577,12 @@ pub extern "system" fn Java_org_apache_comet_Native_releasePlan( | |
_class: JClass, | ||
exec_context: jlong, | ||
) { | ||
try_unwrap_or_throw(&e, |_| unsafe { | ||
try_unwrap_or_throw(&e, |mut env| unsafe { | ||
let execution_context = get_execution_context(exec_context); | ||
|
||
// Update metrics | ||
update_metrics(&mut env, execution_context)?; | ||
|
||
if execution_context.memory_pool_config.pool_type == MemoryPoolType::FairSpillTaskShared | ||
|| execution_context.memory_pool_config.pool_type == MemoryPoolType::GreedyTaskShared | ||
{ | ||
|
@@ -586,10 +606,13 @@ pub extern "system" fn Java_org_apache_comet_Native_releasePlan( | |
|
||
/// Updates the metrics of the query plan. | ||
fn update_metrics(env: &mut JNIEnv, exec_context: &mut ExecutionContext) -> CometResult<()> { | ||
let native_query = exec_context.root_op.as_ref().unwrap(); | ||
let metrics = exec_context.metrics.as_obj(); | ||
let metrics_jstrings = &mut exec_context.metrics_jstrings; | ||
update_comet_metric(env, metrics, native_query, metrics_jstrings) | ||
if exec_context.root_op.is_some() { | ||
let native_query = exec_context.root_op.as_ref().unwrap(); | ||
let metrics = exec_context.metrics.as_obj(); | ||
update_comet_metric(env, metrics, native_query) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn convert_datatype_arrays( | ||
|
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(); | ||||||||||||||||||
andygrove marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
native_metric_node.children.push(child_node); | ||||||||||||||||||
}); | ||||||||||||||||||
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. If you change this to a
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. Thank you for your suggestion, changed. I am not familiar with rust yet, and I hope to learn rust by contributing to comet. 😁 |
||||||||||||||||||
|
||||||||||||||||||
#[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) | ||||||||||||||||||
} |
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; | ||
} |
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.
https://github.com/jedisct1/rust-coarsetime
@andygrove thoughts on a coarse time crate? The overhead on these clock_gettime() as used underneath Instant::now() can really add up. Maybe it's a premature optimization, but I also don't want a "death by 1000 cuts" scenario with gettime() all over the place.
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 ran
coarsetime
's benchmark on my laptop:I'm a bit stunned that Rust's stdlib doesn't provide a nice way to get coarse time on its own, since the performance difference can be quite large and a lot of tasks don't need nanosecond precision.