zhztheplayer commented on code in PR #55953: URL: https://github.com/apache/spark/pull/55953#discussion_r3259449204
########## core/src/main/scala/org/apache/spark/memory/ManagedConsumer.scala: ########## @@ -0,0 +1,70 @@ +/* + * 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 + +/** + * Storage-memory counterpart of [[org.apache.spark.memory.MemoryConsumer]]: holds bytes + * acquired via [[MemoryManager.acquireStorageMemory]] and synchronously releases them on + * Spark's request. Typical implementor: a native off-heap cache (e.g. Velox AsyncDataCache + * via Gluten) sharing `spark.memory.offHeap.size` with Spark's MemoryStore. + * + * == Contract == + * - [[name]] is the registry key (JVM-unique; ON_HEAP and OFF_HEAP share one namespace). + * Use the SAME instance for register / acquire / unregister so identity-based + * self-exclusion works during shrink rounds. + * - A component that also implements [[UnmanagedMemoryConsumer]] MUST NOT report the same + * bytes through both APIs -- they would be subtracted twice from `effectiveMaxMemory`. + * - `MemoryManager.shrinkExternal` owns storage-pool accounting: it deducts exactly + * `shrink`'s return value from the pool. Implementations MUST NOT call + * [[MemoryManager.releaseStorageMemory]] from inside [[shrink]]. + * - [[shrink]] runs inside the `MemoryManager` monitor; it MUST NOT cycle back into + * `MemoryStore.{putBytes, remove, evictBlocksToFreeSpace}` (lock-order cycle on + * `MemoryStore.entries`) and SHOULD return within + * `spark.memory.managedConsumer.shrinkWarnThresholdMs` (default 100ms) to avoid + * blocking other acquisitions. + * - [[shrink]] MUST be synchronous (claimed bytes reclaimable on return). Over-release + * and partial release are fine; negative return is a contract violation. Exceptions + * are caught and treated as 0-byte release. + */ +trait ManagedConsumer { Review Comment: Is the trait name clear? Existing trait `MemoryConsumer` is already managed by Spark. ########## core/src/main/scala/org/apache/spark/memory/StorageMemoryPool.scala: ########## @@ -70,6 +70,16 @@ private[memory] class StorageMemoryPool( acquireMemory(blockId, numBytes, numBytesToFree) } + /** + * Acquire `numBytes` for a [[ManagedConsumer]]: external bytes that never enter + * [[memoryStore]]'s `entries`, falling back to LRU eviction for any deficit. Caller is + * responsible for [[MemoryManager.shrinkExternal]] BEFORE this call; self-exclusion is + * handled in [[MemoryManager.acquireStorageMemory(self:ManagedConsumer,*]]. + */ + def acquireMemoryForManagedConsumer(numBytes: Long): Boolean = lock.synchronized { + acquireMemoryInternal(None, numBytes, math.max(0L, numBytes - memoryFree)) + } Review Comment: Does the API have to be bridged with storage memory pool? IIUC Gluten demands a global memory area in the UMM that is not accounted to particular tasks. Would it be simpler to start from the unmanaged memory API added in https://github.com/apache/spark/pull/51778? -- 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]
