-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce
CometTaskMemoryManager
and native side memory pool (#…
…83)
- Loading branch information
Showing
15 changed files
with
370 additions
and
41 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
common/src/main/java/org/apache/comet/CometOutOfMemoryError.java
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,27 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.apache.comet; | ||
|
||
/** OOM error specific for Comet memory management */ | ||
public class CometOutOfMemoryError extends OutOfMemoryError { | ||
public CometOutOfMemoryError(String msg) { | ||
super(msg); | ||
} | ||
} |
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,119 @@ | ||
// 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::{ | ||
atomic::{AtomicUsize, Ordering::Relaxed}, | ||
Arc, | ||
}, | ||
}; | ||
|
||
use jni::objects::GlobalRef; | ||
|
||
use datafusion::{ | ||
common::DataFusionError, | ||
execution::memory_pool::{MemoryPool, MemoryReservation}, | ||
}; | ||
|
||
use crate::{ | ||
errors::CometResult, | ||
jvm_bridge::{jni_call, JVMClasses}, | ||
}; | ||
|
||
/// A DataFusion `MemoryPool` implementation for Comet. Internally this is | ||
/// implemented via delegating calls to [`crate::jvm_bridge::CometTaskMemoryManager`]. | ||
pub struct CometMemoryPool { | ||
task_memory_manager_handle: Arc<GlobalRef>, | ||
used: AtomicUsize, | ||
} | ||
|
||
impl Debug for CometMemoryPool { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { | ||
f.debug_struct("CometMemoryPool") | ||
.field("used", &self.used.load(Relaxed)) | ||
.finish() | ||
} | ||
} | ||
|
||
impl CometMemoryPool { | ||
pub fn new(task_memory_manager_handle: Arc<GlobalRef>) -> CometMemoryPool { | ||
Self { | ||
task_memory_manager_handle, | ||
used: AtomicUsize::new(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 CometMemoryPool {} | ||
unsafe impl Sync for CometMemoryPool {} | ||
|
||
impl MemoryPool for CometMemoryPool { | ||
fn grow(&self, _: &MemoryReservation, additional: usize) { | ||
self.acquire(additional) | ||
.unwrap_or_else(|_| panic!("Failed to acquire {} bytes", additional)); | ||
self.used.fetch_add(additional, Relaxed); | ||
} | ||
|
||
fn shrink(&self, _: &MemoryReservation, size: usize) { | ||
self.release(size) | ||
.unwrap_or_else(|_| panic!("Failed to release {} bytes", size)); | ||
self.used.fetch_sub(size, Relaxed); | ||
} | ||
|
||
fn try_grow(&self, _: &MemoryReservation, additional: usize) -> Result<(), DataFusionError> { | ||
if additional > 0 { | ||
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(DataFusionError::Execution(format!( | ||
"Failed to acquire {} bytes, only got {}. Reserved: {}", | ||
additional, | ||
acquired, | ||
self.reserved(), | ||
))); | ||
} | ||
self.used.fetch_add(additional, Relaxed); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn reserved(&self) -> usize { | ||
self.used.load(Relaxed) | ||
} | ||
} |
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,62 @@ | ||
// 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 jni::{ | ||
errors::Result as JniResult, | ||
objects::{JClass, JMethodID}, | ||
signature::{Primitive, ReturnType}, | ||
JNIEnv, | ||
}; | ||
|
||
use crate::jvm_bridge::get_global_jclass; | ||
|
||
/// A wrapper which delegate acquire/release memory calls to the | ||
/// JVM side `CometTaskMemoryManager`. | ||
#[derive(Debug)] | ||
pub struct CometTaskMemoryManager<'a> { | ||
pub class: JClass<'a>, | ||
pub method_acquire_memory: JMethodID, | ||
pub method_release_memory: JMethodID, | ||
|
||
pub method_acquire_memory_ret: ReturnType, | ||
pub method_release_memory_ret: ReturnType, | ||
} | ||
|
||
impl<'a> CometTaskMemoryManager<'a> { | ||
pub const JVM_CLASS: &'static str = "org/apache/spark/CometTaskMemoryManager"; | ||
|
||
pub fn new(env: &mut JNIEnv<'a>) -> JniResult<CometTaskMemoryManager<'a>> { | ||
let class = get_global_jclass(env, Self::JVM_CLASS)?; | ||
|
||
let result = CometTaskMemoryManager { | ||
class, | ||
method_acquire_memory: env.get_method_id( | ||
Self::JVM_CLASS, | ||
"acquireMemory", | ||
"(J)J".to_string(), | ||
)?, | ||
method_release_memory: env.get_method_id( | ||
Self::JVM_CLASS, | ||
"releaseMemory", | ||
"(J)V".to_string(), | ||
)?, | ||
method_acquire_memory_ret: ReturnType::Primitive(Primitive::Long), | ||
method_release_memory_ret: ReturnType::Primitive(Primitive::Void), | ||
}; | ||
Ok(result) | ||
} | ||
} |
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
Oops, something went wrong.