-
Notifications
You must be signed in to change notification settings - Fork 338
feat: Add fair unified memory pool #1369
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 4 commits
6ffcb8d
5cd0dcf
11477be
16bbc8b
ca6f74c
4dae6ca
59810c5
9f4d110
7c83de3
319ac0d
6cfafd2
05fd0f1
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 |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ Comet provides the following configuration settings. | |
| | spark.comet.exec.hashJoin.enabled | Whether to enable hashJoin by default. | true | | ||
| | spark.comet.exec.initCap.enabled | Whether to enable initCap by default. | false | | ||
| | spark.comet.exec.localLimit.enabled | Whether to enable localLimit by default. | true | | ||
| | spark.comet.exec.memoryPool | The type of memory pool to be used for Comet native execution. Available memory pool types are 'greedy', 'fair_spill', 'greedy_task_shared', 'fair_spill_task_shared', 'greedy_global' and 'fair_spill_global', By default, this config is 'greedy_task_shared'. | greedy_task_shared | | ||
| | spark.comet.exec.memoryPool | The type of memory pool to be used for Comet native execution. Available memory pool types are 'greedy', 'fair_spill', 'greedy_task_shared', 'fair_spill_task_shared', 'greedy_global' and 'fair_spill_global'. For off-eap types are 'unified' and `fair_unified`. | greedy_task_shared | | ||
|
comphead marked this conversation as resolved.
Outdated
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. add
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. We tend to omit what the default value is. When default value is changed, the option description is often forgotten to be updated because there is no checking for the description. So we deleted default value explanations in the past |
||
| | spark.comet.exec.project.enabled | Whether to enable project by default. | true | | ||
| | spark.comet.exec.replaceSortMergeJoin | Experimental feature to force Spark to replace SortMergeJoin with ShuffledHashJoin for improved performance. This feature is not stable yet. For more information, refer to the Comet Tuning Guide (https://datafusion.apache.org/comet/user-guide/tuning.html). | false | | ||
| | spark.comet.exec.shuffle.compression.codec | The codec of Comet native shuffle used to compress shuffle data. lz4, zstd, and snappy are supported. Compression can be disabled by setting spark.shuffle.compress=false. | lz4 | | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| // 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 std::{ | ||
| fmt::{Debug, Formatter, Result as FmtResult}, | ||
| sync::Arc, | ||
| }; | ||
|
|
||
| use jni::objects::GlobalRef; | ||
|
|
||
| use crate::{ | ||
| errors::CometResult, | ||
| jvm_bridge::{jni_call, JVMClasses}, | ||
| }; | ||
| use datafusion::{ | ||
| common::{resources_datafusion_err, DataFusionError}, | ||
| execution::memory_pool::{MemoryPool, MemoryReservation}, | ||
| }; | ||
| use datafusion_execution::memory_pool::MemoryConsumer; | ||
| use parking_lot::Mutex; | ||
|
|
||
| /// A DataFusion fair `MemoryPool` implementation for Comet. Internally this is | ||
| /// implemented via delegating calls to [`crate::jvm_bridge::CometTaskMemoryManager`]. | ||
| pub struct CometFairMemoryPool { | ||
|
kazuyukitanimura marked this conversation as resolved.
|
||
| task_memory_manager_handle: Arc<GlobalRef>, | ||
| pool_size: usize, | ||
|
kazuyukitanimura marked this conversation as resolved.
|
||
| state: Mutex<CometFairPoolState>, | ||
| } | ||
|
|
||
| struct CometFairPoolState { | ||
| used: usize, | ||
| num: usize, | ||
| } | ||
|
|
||
| impl Debug for CometFairMemoryPool { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { | ||
| let state = self.state.lock(); | ||
| f.debug_struct("CometFairMemoryPool") | ||
| .field("pool_size", &self.pool_size) | ||
| .field("used", &state.used) | ||
| .field("num", &state.num) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| impl CometFairMemoryPool { | ||
| pub fn new( | ||
| task_memory_manager_handle: Arc<GlobalRef>, | ||
| pool_size: usize, | ||
| ) -> CometFairMemoryPool { | ||
| Self { | ||
| task_memory_manager_handle, | ||
| pool_size, | ||
| state: Mutex::new(CometFairPoolState { used: 0, num: 0 }), | ||
| } | ||
| } | ||
|
|
||
| fn acquire(&self, additional: usize) -> CometResult<i64> { | ||
| let mut env = JVMClasses::get_env()?; | ||
| let handle = self.task_memory_manager_handle.as_obj(); | ||
| unsafe { | ||
| jni_call!(&mut env, | ||
| comet_task_memory_manager(handle).acquire_memory(additional as i64) -> i64) | ||
| } | ||
| } | ||
|
|
||
| fn release(&self, size: usize) -> CometResult<()> { | ||
| let mut env = JVMClasses::get_env()?; | ||
| let handle = self.task_memory_manager_handle.as_obj(); | ||
| unsafe { | ||
| jni_call!(&mut env, comet_task_memory_manager(handle).release_memory(size as i64) -> ()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| unsafe impl Send for CometFairMemoryPool {} | ||
| unsafe impl Sync for CometFairMemoryPool {} | ||
|
|
||
| impl MemoryPool for CometFairMemoryPool { | ||
| fn register(&self, _: &MemoryConsumer) { | ||
|
kazuyukitanimura marked this conversation as resolved.
|
||
| let mut state = self.state.lock(); | ||
| state.num = state.num.checked_add(1).unwrap(); | ||
|
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. this unwraps can be super confusing if they happen
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. This won't happen in reality,
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. changed to |
||
| } | ||
|
|
||
| fn unregister(&self, _: &MemoryConsumer) { | ||
| let mut state = self.state.lock(); | ||
| state.num = state.num.checked_sub(1).unwrap(); | ||
| } | ||
|
|
||
| fn grow(&self, reservation: &MemoryReservation, additional: usize) { | ||
|
Member
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. This method implementation seems to be different from |
||
| self.try_grow(reservation, additional).unwrap(); | ||
| } | ||
|
|
||
| fn shrink(&self, reservation: &MemoryReservation, subtractive: usize) { | ||
| if subtractive > 0 { | ||
| let mut state = self.state.lock(); | ||
| let size = reservation.size(); | ||
| if size < subtractive { | ||
| panic!("Failed to release {subtractive} bytes where only {size} bytes reserved") | ||
| } | ||
| self.release(subtractive) | ||
| .unwrap_or_else(|_| panic!("Failed to release {} bytes", subtractive)); | ||
| state.used = state.used.checked_sub(subtractive).unwrap(); | ||
| } | ||
| } | ||
|
|
||
| fn try_grow( | ||
| &self, | ||
| reservation: &MemoryReservation, | ||
| additional: usize, | ||
| ) -> Result<(), DataFusionError> { | ||
| if additional > 0 { | ||
| let mut state = self.state.lock(); | ||
| let num = state.num; | ||
| let limit = self.pool_size.checked_div(num).unwrap(); | ||
| let size = reservation.size(); | ||
| if limit < size + additional { | ||
| return Err(resources_datafusion_err!( | ||
| "Failed to acquire {additional} bytes where {size} bytes already reserved and the fair limit is {limit} bytes, {num} registered" | ||
| )); | ||
|
kazuyukitanimura marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| let acquired = self.acquire(additional)?; | ||
| // If the number of bytes we acquired is less than the requested, return an error, | ||
| // and hopefully will trigger spilling from the caller side. | ||
| if acquired < additional as i64 { | ||
| // Release the acquired bytes before throwing error | ||
| self.release(acquired as usize)?; | ||
|
|
||
| return Err(resources_datafusion_err!( | ||
| "Failed to acquire {} bytes, only got {} bytes. Reserved: {} bytes", | ||
| additional, | ||
| acquired, | ||
| state.used | ||
| )); | ||
|
kazuyukitanimura marked this conversation as resolved.
Outdated
|
||
| } | ||
| state.used = state.used.checked_add(additional).unwrap(); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn reserved(&self) -> usize { | ||
| self.state.lock().used | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.