LuciferYang commented on code in PR #13618:
URL: https://github.com/apache/gluten/pull/13618#discussion_r4539877633


##########
gluten-core/src/main/java/org/apache/gluten/memory/memtarget/MemoryTargets.java:
##########
@@ -37,8 +37,15 @@ private MemoryTargets() {
     // enclose factory ctor
   }
 
+  /**
+   * Creates the process-wide memory target that backs {@code 
GlobalOffHeapMemory}, i.e. the
+   * reservations that are not owned by any Spark task (Arrow's global 
allocator, the Velox global
+   * memory listener, ...). Those reservations are backed by real native 
memory, so with dynamic
+   * off-heap sizing on they must be booked in the same shared ledger as the 
task-level targets,
+   * which is what the wrapper below does.
+   */
   public static MemoryTarget global() {
-    return new GlobalOffHeapMemoryTarget();
+    return dynamicOffHeapSizingIfEnabled(new GlobalOffHeapMemoryTarget());

Review Comment:
   Thanks for switching `mode` back to a `def`. The wrap here still has the 
lifetime problem: `GlobalOffHeapMemory.target` is built once per JVM, so 
whether global reservations go through `DynamicOffHeapSizingMemoryTarget` is 
decided by the first SparkContext, while `mode` and the task consumers follow 
the current one.
   
   I reran my gluten-core repro (Spark 3.4.4) at 6bffaf26f. A non-dynamic 
context followed by a dynamic one books nothing in the ledger for global 
reservations, which is the bypass this PR sets out to fix. In the reverse 
order, global bytes are booked with the flag off, and a reservation the size of 
the max heap on a 64g off-heap pool fails with `OutOfMemoryException` where 
base succeeds.
   
   Since the ledger is JVM-wide, one way is to decide the wrap at borrow time 
and have repay unbook only bytes the global target itself booked. A two-context 
test in one JVM would cover it.



##########
gluten-core/src/test/scala/org/apache/spark/memory/GlobalOffHeapDynamicSizingSuite.scala:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.memory
+
+import org.apache.gluten.config.GlutenCoreConfig
+import org.apache.gluten.memory.memtarget.{DynamicOffHeapSizingMemoryTarget, 
MemoryTargets}
+
+import org.apache.spark.{SparkConf, SparkEnv}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.storage.BlockManager
+import org.apache.spark.storage.memory.MemoryStore
+
+import org.mockito.Mockito.{mock, when}
+import org.scalatest.funsuite.AnyFunSuite
+
+/**
+ * Covers how the global (non-task) off-heap reservations pick their backing 
memory pool when
+ * dynamic off-heap sizing is on.
+ *
+ * The suite deliberately runs *without* an active SQL session, because that 
is the situation of the
+ * thread that builds the global memory target in production: the Gluten 
plugin initializes it from
+ * the driver/executor plugin callback, long before any SparkSession is 
attached to the thread. A
+ * conf lookup that falls back to SQLConf defaults there would silently read 
"disabled" and leave
+ * the global target half-wired - charging the on-heap pools from one place 
while the dynamic-sizing
+ * ledger is bypassed in another.
+ */
+class GlobalOffHeapDynamicSizingSuite extends AnyFunSuite {

Review Comment:
   `GlobalOffHeapDynamicSizingSuite` is in `org.apache.spark.memory`, and none 
of the `-DwildcardSuites` groups in `velox_backend_x86.yml` covers that 
package, so the Velox PR CI never runs it. Only the `velox_nightly.yml` test 
job and the manually triggered workflows do. The gap predates this PR: the 
existing `GlobalOffHeapMemorySuite` in backends-velox has been skipped the same 
way since the CI split in #12624. It matters more now because this suite is the 
only test of the new global wrap and its ledger booking. 
`GlutenDynamicOffHeapSizingSuite`, which group 1 does run, checks the 
available-memory metric and never touches the ledger.
   
   Adding `org.apache.spark.memory` to group 1 of each Spark UT job would cover 
both suites. If you'd rather keep CI changes out of this PR, a follow-up issue 
works too.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to