sunchao commented on code in PR #83:
URL: 
https://github.com/apache/arrow-datafusion-comet/pull/83#discussion_r1511494816


##########
core/src/execution/memory_pool.rs:
##########
@@ -0,0 +1,102 @@
+// 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::jvm_bridge::{jni_call, JVMClasses};
+
+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),
+        }
+    }
+}
+
+unsafe impl Send for CometMemoryPool {}
+unsafe impl Sync for CometMemoryPool {}
+
+impl MemoryPool for CometMemoryPool {
+    fn grow(&self, _: &MemoryReservation, additional: usize) {
+        self.used.fetch_add(additional, Relaxed);
+    }

Review Comment:
   I think `grow` is not really used by DataFusion except in tests, that's why 
I didn't do it. But you are right, it's better to add it too for future proof.



##########
core/src/execution/memory_pool.rs:
##########
@@ -0,0 +1,102 @@
+// 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::jvm_bridge::{jni_call, JVMClasses};
+
+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),
+        }
+    }
+}
+
+unsafe impl Send for CometMemoryPool {}
+unsafe impl Sync for CometMemoryPool {}
+
+impl MemoryPool for CometMemoryPool {
+    fn grow(&self, _: &MemoryReservation, additional: usize) {
+        self.used.fetch_add(additional, Relaxed);
+    }
+
+    fn shrink(&self, _: &MemoryReservation, size: usize) {
+        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) -> ())
+                .unwrap();
+        }
+        self.used.fetch_sub(size, Relaxed);
+    }
+
+    fn try_grow(&self, _: &MemoryReservation, additional: usize) -> Result<(), 
DataFusionError> {
+        if additional > 0 {
+            let mut env = JVMClasses::get_env();
+            let handle = self.task_memory_manager_handle.as_obj();
+            unsafe {
+                let acquired = jni_call!(&mut env,
+                  comet_task_memory_manager(handle).acquire_memory(additional 
as i64) -> i64)?;
+
+                // 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 {
+                    return Err(DataFusionError::Execution(format!(
+                        "Failed to acquire {} bytes, only got {}. Reserved: 
{}",
+                        additional,
+                        acquired,
+                        self.reserved(),
+                    )));
+                }

Review Comment:
   Good point. Will add.



##########
core/src/jvm_bridge/comet_task_memory_manager.rs:
##########
@@ -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 DataFusion `MemoryPool` implementation for Comet, which delegate to the 
JVM
+/// side `CometTaskMemoryManager`.

Review Comment:
   Yea let me move it. 



##########
spark/src/test/scala/org/apache/spark/sql/CometTPCHQuerySuite.scala:
##########
@@ -87,10 +89,11 @@ class CometTPCHQuerySuite extends QueryTest with 
CometTPCBase with SQLQueryTestH
       "org.apache.spark.sql.comet.execution.shuffle.CometShuffleManager")
     conf.set(CometConf.COMET_ENABLED.key, "true")
     conf.set(CometConf.COMET_EXEC_ENABLED.key, "true")
-    conf.set(CometConf.COMET_MEMORY_OVERHEAD.key, "2g")

Review Comment:
   This is now replaced by 
   
   ```
   conf.set(MEMORY_OFFHEAP_ENABLED.key, "true")
   conf.set(MEMORY_OFFHEAP_SIZE.key, "2g")
   ```
   
   below



##########
spark/src/test/scala/org/apache/spark/sql/CometTestBase.scala:
##########
@@ -65,28 +64,18 @@ abstract class CometTestBase
     val conf = new SparkConf()
     conf.set("spark.hadoop.fs.file.impl", classOf[DebugFilesystem].getName)
     conf.set(SQLConf.SHUFFLE_PARTITIONS, 10) // reduce parallelism in tests
-    conf.set("spark.shuffle.manager", shuffleManager)
+    conf.set(SQLConf.ANSI_ENABLED.key, "false")
+    conf.set(SHUFFLE_MANAGER, shuffleManager)
+    conf.set(MEMORY_OFFHEAP_ENABLED.key, "true")
+    conf.set(MEMORY_OFFHEAP_SIZE.key, "2g")
+    conf.set(CometConf.COMET_ENABLED.key, "true")
+    conf.set(CometConf.COMET_EXEC_ENABLED.key, "true")
+    conf.set(CometConf.COMET_EXEC_ALL_OPERATOR_ENABLED.key, "true")
+    conf.set(CometConf.COMET_EXEC_ALL_EXPR_ENABLED.key, "true")
     conf.set(CometConf.COMET_MEMORY_OVERHEAD.key, "2g")
     conf
   }
 
-  override protected def test(testName: String, testTags: Tag*)(testFun: => 
Any)(implicit
-      pos: Position): Unit = {
-    super.test(testName, testTags: _*) {
-      withSQLConf(
-        CometConf.COMET_ENABLED.key -> "true",
-        CometConf.COMET_EXEC_ENABLED.key -> "true",
-        CometConf.COMET_EXEC_ALL_OPERATOR_ENABLED.key -> "true",
-        CometConf.COMET_EXEC_ALL_EXPR_ENABLED.key -> "true",
-        CometConf.COMET_COLUMNAR_SHUFFLE_MEMORY_SIZE.key -> "2g",
-        SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1g",
-        SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "1g",

Review Comment:
   Oops I accidentally removed these when rebasing. Let me add them back.



##########
spark/src/main/java/org/apache/spark/CometTaskMemoryManager.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.spark;
+
+import java.io.IOException;
+
+import org.apache.spark.memory.MemoryConsumer;
+import org.apache.spark.memory.MemoryMode;
+import org.apache.spark.memory.TaskMemoryManager;
+
+/**
+ * A adapter class that is used by Comet native to acquire & release memory 
through Spark's unified
+ * memory manager. This assumes Spark's off-heap memory mode is enabled.
+ */
+public class CometTaskMemoryManager {
+  /** The id uniquely identifies the native plan this memory manager is 
associated to */
+  private final long id;
+
+  private final TaskMemoryManager internal;
+  private final NativeMemoryConsumer nativeMemoryConsumer;
+
+  public CometTaskMemoryManager(long id) {
+    this.id = id;
+    this.internal = TaskContext$.MODULE$.get().taskMemoryManager();
+    this.nativeMemoryConsumer = new NativeMemoryConsumer();
+  }
+
+  // Called by Comet native through JNI.
+  // Returns the actual amount of memory (in bytes) granted.
+  public long acquireMemory(long size) {
+    return internal.acquireExecutionMemory(size, nativeMemoryConsumer);
+  }

Review Comment:
   I think `TaskMemoryManager` is already synchronized on the 
`acquireExecutionMemory` and `releaseExecutionMemory`, so it doesn't seem 
necessary.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to