Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.3k
Add memory profiling support to DataFusion CLI and memory pool metrics#17021
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
340f92f1204df1b482cc3b566cf9714ac4c05b5fc22de502f1bd3c188cac4fb8135ec89537eb1aa3ce0bec26adf85481d4d0fb8077a381d307edefb4fcc6a43a3f5cc6087e8f0d63a8862497b42fd387aced090452941fac3f5b74fd9f04707312b7f1b1aa72e05a150cd6d62a6acf764044951cc37d646e1063f4ad99c7c9b04c9ee23d74053745e885b2a14ce2330ce1954d78f79909fccada8e11a2f24b5d3c7daa9993342b17f40b74d65329e6578ad5b4ec463cb23228bf760140265bb38d5284961b8fff055e67381413f0d568e19c0831b3a5456ef1122d5dd989a08f22f0f95d58d13fe3295e7b32cd2be7aacc91655e99405d323c135750682aedf9a4ff0e5e1cf5b9c1e3f16ecbf657df3a9c5cb4a5ddcf4f1ae2797808e02d839b071eb5812981fd96dc827f00dde3efb725953b0dde144551965384a90fc3ac93b84879bf76efd5e5a8448f491b9cf1285fbcea1eb0791bb1f5c694b163a208eaa4ad8f32f75d0e5f5018a59372796591c15b6608e2d2575d7d32de251766e8207d12b9fa4d34e0d7173486c30c22aebfcc17f5aef32661b827a0720d5659b4b1fe394307922cc453a8af559e06580f3aac60e3b731e38b8061deef59b5afd8f51a112e3d82ee4c32749d699226032909ed3c58d5f6c39916caef5480208a540d18838dc2631dfa480b78dc85ae1da2b2c0107a7703e09c22dc38e85c8ce5ed86db315abeeff525195376f6b1111d36617f4013dbd549fe4fe101cf2c9e4b9368c0e57c591c366ca74d3a5694dafa4018e3fb00c9cdef040345c12b08d4e2c6a7ba1dd48d0b40b7663cccdd1453f3a115488251b7014f973b24a4292bdbbecd135f5d83cb816d507211d8b28c78b834ac15b2ae4e1cee3e8feee4829d0151a8a7662edbdb47e9de36b5ffdbcb3dbc2d860a5a7b7c984a55fdc3be5c1b45e9dde7062a19c6d88978caf1863fba5cb13cf46f1b51998a78efcf545bc14dd44798bd80f9945683217b6765ab907c6af921be60822454119e2fb86db9339be23d3928559b327f85d3660899b333e5daeb33d21d847244d47fceca492a67896d16d20eec41fe1b656ab1aaab03ea519efabb4088c6343b6d07fae67f1d916382fc07bd009df45d18e055441b791042c16135538137b184e34585c387e463de515c3d43825b08791fe8f8569dd25180a377c03d00ee2f1cf29ee09db5fa7b04cca064cf0b4b852ae049ebcea0f65dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -20,14 +20,38 @@ use std::sync::Arc; | ||
| use datafusion::{ | ||
| dataframe::DataFrame, | ||
| error::DataFusionError, | ||
| execution::{context::SessionState, TaskContext}, | ||
| execution::{context::SessionState, memory_pool::TrackedPool, TaskContext}, | ||
| logical_expr::LogicalPlan, | ||
| prelude::SessionContext, | ||
| }; | ||
| use object_store::ObjectStore; | ||
| use crate::object_storage::{AwsOptions, GcpOptions}; | ||
| /// Registers table option extensions based on the provided URL scheme. | ||
| /// | ||
| /// Supported schemes are: | ||
| /// * `s3`, `oss`, `cos` - registers [`AwsOptions`] | ||
| /// * `gs`, `gcs` - registers [`GcpOptions`] | ||
| /// | ||
| /// Any other scheme is ignored. | ||
| pub fn register_table_options_from_scheme(ctx: &SessionContext, scheme: &str) { | ||
| match scheme { | ||
| // For Amazon S3 or Alibaba Cloud OSS | ||
| "s3" | "oss" | "cos" => { | ||
| // Register AWS specific table options in the session context: | ||
| ctx.register_table_options_extension(AwsOptions::default()) | ||
| } | ||
| // For Google Cloud Storage | ||
| "gs" | "gcs" => { | ||
| // Register GCP specific table options in the session context: | ||
| ctx.register_table_options_extension(GcpOptions::default()) | ||
| } | ||
| // For unsupported schemes, do nothing: | ||
| _ => {} | ||
| } | ||
| } | ||
| #[async_trait::async_trait] | ||
| /// The CLI session context trait provides a way to have a session context that can be used with datafusion's CLI code. | ||
| pub trait CliSessionContext { | ||
| @@ -52,6 +76,19 @@ pub trait CliSessionContext { | ||
| &self, | ||
| plan: LogicalPlan, | ||
| ) -> Result<DataFrame, DataFusionError>; | ||
| /// Return true if memory profiling is enabled. | ||
| fn memory_profiling(&self) -> bool { | ||
| false | ||
| } | ||
| /// Enable or disable memory profiling. | ||
| fn set_memory_profiling(&self, _enable: bool) {} | ||
| /// Return the tracked memory pool used for profiling, if any. | ||
| fn tracked_memory_pool(&self) -> Option<Arc<dyn TrackedPool>> { | ||
| None | ||
| } | ||
| } | ||
| #[async_trait::async_trait] | ||
| @@ -73,26 +110,82 @@ impl CliSessionContext for SessionContext { | ||
| } | ||
| fn register_table_options_extension_from_scheme(&self, scheme: &str) { | ||
| match scheme { | ||
| // For Amazon S3 or Alibaba Cloud OSS | ||
| "s3" | "oss" | "cos" => { | ||
| // Register AWS specific table options in the session context: | ||
| self.register_table_options_extension(AwsOptions::default()) | ||
| } | ||
| // For Google Cloud Storage | ||
| "gs" | "gcs" => { | ||
| // Register GCP specific table options in the session context: | ||
| self.register_table_options_extension(GcpOptions::default()) | ||
| } | ||
| // For unsupported schemes, do nothing: | ||
| _ => {} | ||
| register_table_options_from_scheme(self, scheme); | ||
| } | ||
| async fn execute_logical_plan( | ||
| &self, | ||
| plan: LogicalPlan, | ||
| ) -> Result<DataFrame, DataFusionError> { | ||
| SessionContext::execute_logical_plan(self, plan).await | ||
| } | ||
| } | ||
| /// Session context used by the CLI with memory profiling support. | ||
| pub struct ReplSessionContext { | ||
| ctx: SessionContext, | ||
| tracked_memory_pool: Option<Arc<dyn TrackedPool>>, | ||
| } | ||
| impl ReplSessionContext { | ||
| pub fn new( | ||
| ctx: SessionContext, | ||
| tracked_memory_pool: Option<Arc<dyn TrackedPool>>, | ||
| ) -> Self { | ||
| Self { | ||
| ctx, | ||
| tracked_memory_pool, | ||
| } | ||
| } | ||
| } | ||
| #[async_trait::async_trait] | ||
| impl CliSessionContext for ReplSessionContext { | ||
alamb marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| fn task_ctx(&self) -> Arc<TaskContext> { | ||
| self.ctx.task_ctx() | ||
| } | ||
| fn session_state(&self) -> SessionState { | ||
| self.ctx.state() | ||
| } | ||
| fn register_object_store( | ||
| &self, | ||
| url: &url::Url, | ||
| object_store: Arc<dyn ObjectStore>, | ||
| ) -> Option<Arc<dyn ObjectStore + 'static>> { | ||
| self.ctx.register_object_store(url, object_store) | ||
| } | ||
| fn register_table_options_extension_from_scheme(&self, scheme: &str) { | ||
| register_table_options_from_scheme(&self.ctx, scheme); | ||
| } | ||
| async fn execute_logical_plan( | ||
| &self, | ||
| plan: LogicalPlan, | ||
| ) -> Result<DataFrame, DataFusionError> { | ||
| self.execute_logical_plan(plan).await | ||
| self.ctx.execute_logical_plan(plan).await | ||
| } | ||
| fn memory_profiling(&self) -> bool { | ||
| self.tracked_memory_pool | ||
| .as_ref() | ||
| .map(|pool| pool.tracking_enabled()) | ||
| .unwrap_or(false) | ||
| } | ||
| fn set_memory_profiling(&self, enable: bool) { | ||
| if let Some(pool) = &self.tracked_memory_pool { | ||
| if enable { | ||
| pool.enable_tracking(); | ||
| } else { | ||
| pool.disable_tracking(); | ||
| } | ||
| } | ||
| } | ||
| fn tracked_memory_pool(&self) -> Option<Arc<dyn TrackedPool>> { | ||
| self.tracked_memory_pool.clone() | ||
| } | ||
| } | ||
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.
I think the memory profiling flag would best be stored on
PrintOptions, similarly to thequietmode flag (that suppresses execution time printing). Then you would not need to introduce so much new code and a new traithttps://github.com/apache/datafusion/blob/df45d186d34f2ac131d64e4a068d9f39b35e99c7/datafusion-cli/src/print_options.rs#L73-L72
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.
That would require print_options to be mut because we can
toggle memory_profiling
It was moved out of print_options after a comment that print_options should not be mut for memory profiling.