You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zifeif2 (via GitHub)" <gi...@apache.org> on 2025/10/06 17:31:32 UTC

[PR] [SPARK-53792] Fix rocksdbPinnedBlocksMemoryUsage when bounded memory … [spark]

zifeif2 opened a new pull request, #52527:
URL: https://github.com/apache/spark/pull/52527

   
   …usage is enabled
   
   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   Changing the way we calculate `pinnedBlocksMemUsage` when an instance has bounded memory enabled. 
   Before the change, we always report back `getDBProperty("rocksdb.block-cache-pinned-usage")` which returns that size of pinned block requested by an instance. This is not accurate when instances share the same cached, because instances might share the same pinned block
   
   After this change, when isMemoryBounded is enabled for an instance, we call `lruCache.getPinnedUsage()` to get the global memory usage of SHARED pinned blocks, and divide the global usage with the number of IS_MEMORY_BOUNDED instances. 
   
   
   ### Why are the changes needed?
   See above for why this is a bug. 
   This fix prevents us from over-reporting `pinnedBlocksMemUsage` when isBoundedMemory is enabled for an instance 
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
   
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   No
   
   
   ### How was this patch tested?
   <!--If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   See added unit tests on the correctness of the calculation. 
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   Yes. Generated-by cursor and 'claude-4-sonnet'


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53792][SS] Fix rocksdbPinnedBlocksMemoryUsage when bounded memory … [spark]

Posted by "micheal-o (via GitHub)" <gi...@apache.org>.
micheal-o commented on code in PR #52527:
URL: https://github.com/apache/spark/pull/52527#discussion_r2411966110


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/RocksDBMemoryManager.scala:
##########
@@ -121,6 +121,29 @@ object RocksDBMemoryManager extends Logging with UnmanagedMemoryConsumer {
     }
   }
 
+  /**
+   * Get the pinned blocks memory usage for a specific instance, accounting for bounded memory
+   * sharing.
+   * @param uniqueId The instance's unique identifier
+   * @param globalPinnedUsage The total pinned usage from the cache
+   * @return The adjusted pinned blocks memory usage accounting for sharing in bounded memory mode
+   */
+  def getInstancePinnedBlocksMemUsage(
+      uniqueId: String,
+      pinnedUsage: Long): Long = {

Review Comment:
   nit: param name `pinnedUsage` is different from what is in the description `globalPinnedUsage`. Lets just name this param as: `totalPinnedUsage`



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53792][SS] Fix rocksdbPinnedBlocksMemoryUsage when bounded memory … [spark]

Posted by "anishshri-db (via GitHub)" <gi...@apache.org>.
anishshri-db closed pull request #52527: [SPARK-53792][SS] Fix rocksdbPinnedBlocksMemoryUsage when bounded memory …
URL: https://github.com/apache/spark/pull/52527


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53792] Fix rocksdbPinnedBlocksMemoryUsage when bounded memory … [spark]

Posted by "micheal-o (via GitHub)" <gi...@apache.org>.
micheal-o commented on code in PR #52527:
URL: https://github.com/apache/spark/pull/52527#discussion_r2408433814


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/RocksDBMemoryManager.scala:
##########
@@ -117,10 +117,45 @@ object RocksDBMemoryManager extends Logging with UnmanagedMemoryConsumer {
       totalMemoryUsage / numBoundedInstances
     } else {
       // In unbounded memory mode, each instance has its own memory
+
+      // Question: Should we return 0L if the instance is not registed in instanceMemoryMap?
+      // but when it is not registed?
       totalMemoryUsage
     }
   }
 
+  /**
+   * Get the pinned blocks memory usage for a specific instance, accounting for bounded memory
+   * sharing.
+   * @param uniqueId The instance's unique identifier
+   * @param globalPinnedUsage The total pinned usage from the cache
+   * @return The adjusted pinned blocks memory usage accounting for sharing in bounded memory mode
+   */
+  def getInstancePinnedBlocksMemUsage(
+      uniqueId: String,
+      globalPinnedUsage: Long,
+      requestedPinnedUsage: Long): Long = {
+    if (!instanceMemoryMap.containsKey(uniqueId)) {
+      // Instance not registered, return 0
+      return 0L
+    }
+
+    val instanceInfo = instanceMemoryMap.get(uniqueId)
+    if (instanceInfo.isBoundedMemory) {
+      // In bounded memory mode, divide by the number of bounded instances
+      // since they share the same cache
+      val numBoundedInstances = getNumRocksDBInstances(true /* boundedMemory */)
+      if (numBoundedInstances > 0) {
+        globalPinnedUsage / numBoundedInstances
+      } else {
+        0L

Review Comment:
   when would this happen? Given that we already found a boundedMemory instance for this when we did `instanceMemoryMap.get(uniqueId)` above. So don't think this is necessary.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/RocksDBMemoryManager.scala:
##########
@@ -117,10 +117,45 @@ object RocksDBMemoryManager extends Logging with UnmanagedMemoryConsumer {
       totalMemoryUsage / numBoundedInstances
     } else {
       // In unbounded memory mode, each instance has its own memory
+
+      // Question: Should we return 0L if the instance is not registed in instanceMemoryMap?
+      // but when it is not registed?
       totalMemoryUsage
     }
   }
 
+  /**
+   * Get the pinned blocks memory usage for a specific instance, accounting for bounded memory
+   * sharing.
+   * @param uniqueId The instance's unique identifier
+   * @param globalPinnedUsage The total pinned usage from the cache
+   * @return The adjusted pinned blocks memory usage accounting for sharing in bounded memory mode
+   */
+  def getInstancePinnedBlocksMemUsage(
+      uniqueId: String,
+      globalPinnedUsage: Long,
+      requestedPinnedUsage: Long): Long = {
+    if (!instanceMemoryMap.containsKey(uniqueId)) {
+      // Instance not registered, return 0
+      return 0L

Review Comment:
   why not just return the `requestedPinnedUsage` instead of 0L?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/RocksDBMemoryManager.scala:
##########
@@ -117,10 +117,45 @@ object RocksDBMemoryManager extends Logging with UnmanagedMemoryConsumer {
       totalMemoryUsage / numBoundedInstances
     } else {
       // In unbounded memory mode, each instance has its own memory
+
+      // Question: Should we return 0L if the instance is not registed in instanceMemoryMap?
+      // but when it is not registed?

Review Comment:
   If not registered, we currently just return the totalMemory passed in. Why do you think 0L is better? 



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-53792] Fix rocksdbPinnedBlocksMemoryUsage when bounded memory … [spark]

Posted by "micheal-o (via GitHub)" <gi...@apache.org>.
micheal-o commented on code in PR #52527:
URL: https://github.com/apache/spark/pull/52527#discussion_r2408812940


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/streaming/state/RocksDBSuite.scala:
##########
@@ -3671,6 +3671,53 @@ class RocksDBSuite extends AlsoTestWithRocksDBFeatures with SharedSparkSession
   }
 
   def listFiles(file: String): Seq[File] = listFiles(new File(file))
+
+  test("SPARK-53792: RocksDBMemoryManager getInstancePinnedBlocksMemUsage") {

Review Comment:
   nit: move this closer to the other memory manager test case



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/RocksDB.scala:
##########
@@ -1496,6 +1495,11 @@ class RocksDB(
     // Use RocksDBMemoryManager to calculate the memory usage accounting
     val memoryUsage = RocksDBMemoryManager.getInstanceMemoryUsage(instanceUniqueId, getMemoryUsage)
 
+    val requestedPinnedBlocksMemUsage = getDBProperty("rocksdb.block-cache-pinned-usage")

Review Comment:
   do we actually need the `getDBProperty("rocksdb.block-cache-pinned-usage")` here? Because in unbounded mode, the `getDBProperty("rocksdb.block-cache-pinned-usage")` will be equal to `lruCache.getPinnedUsage()`



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org