You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2025/05/09 21:30:07 UTC

[PR] fix: Fix data race in memory profiling [datafusion-comet]

andygrove opened a new pull request, #1727:
URL: https://github.com/apache/datafusion-comet/pull/1727

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes https://github.com/apache/datafusion-comet/issues/1726
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   Fix a data race that caused negative memory use to be reported
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   Use AtomicLong instead of long to track used memory.
   
   ## How are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   Manually.
   


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Fix data race in memory profiling [datafusion-comet]

Posted by "parthchandra (via GitHub)" <gi...@apache.org>.
parthchandra commented on code in PR #1727:
URL: https://github.com/apache/datafusion-comet/pull/1727#discussion_r2082639273


##########
spark/src/main/java/org/apache/spark/CometTaskMemoryManager.java:
##########
@@ -30,36 +34,41 @@
  * memory manager. This assumes Spark's off-heap memory mode is enabled.
  */
 public class CometTaskMemoryManager {
+
+  private static final Logger logger = LoggerFactory.getLogger(CometTaskMemoryManager.class);
+
   /** 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;
-  private long used;
+  private final AtomicLong used = new AtomicLong();
 
   public CometTaskMemoryManager(long id) {
     this.id = id;
     this.internal = TaskContext$.MODULE$.get().taskMemoryManager();
     this.nativeMemoryConsumer = new NativeMemoryConsumer();
-    this.used = 0;
   }
 
   // Called by Comet native through JNI.
   // Returns the actual amount of memory (in bytes) granted.
   public long acquireMemory(long size) {
     long acquired = internal.acquireExecutionMemory(size, nativeMemoryConsumer);
-    used += acquired;
+    used.addAndGet(acquired);
     return acquired;
   }
 
   // Called by Comet native through JNI
   public void releaseMemory(long size) {
-    used -= size;
+    long newUsed = used.addAndGet(-size);
+    if (newUsed < 0) {
+      logger.warn("Used memory is negative: " + newUsed);

Review Comment:
   Should probably make this an error message (or maybe even throw an exception since this is a pretty fatal condition?)



-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Fix data race in memory profiling [datafusion-comet]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #1727:
URL: https://github.com/apache/datafusion-comet/pull/1727#issuecomment-2868000912

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion-comet/pull/1727?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   Attention: Patch coverage is `57.14286%` with `3 lines` in your changes missing coverage. Please review.
   > Project coverage is 56.25%. Comparing base [(`f09f8af`)](https://app.codecov.io/gh/apache/datafusion-comet/commit/f09f8af64c6599255e116a376f4f008f2fd63b43?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`9710ba4`)](https://app.codecov.io/gh/apache/datafusion-comet/commit/9710ba4a9a642ef1a5ea6646a08feda22786bc58?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   > Report is 184 commits behind head on main.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/datafusion-comet/pull/1727?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [.../java/org/apache/spark/CometTaskMemoryManager.java](https://app.codecov.io/gh/apache/datafusion-comet/pull/1727?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fspark%2FCometTaskMemoryManager.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NwYXJrL0NvbWV0VGFza01lbW9yeU1hbmFnZXIuamF2YQ==) | 57.14% | [2 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/1727?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   ```diff
   @@             Coverage Diff              @@
   ##               main    #1727      +/-   ##
   ============================================
   + Coverage     56.12%   56.25%   +0.12%     
   - Complexity      976     1058      +82     
   ============================================
     Files           119      129      +10     
     Lines         11743    12684     +941     
     Branches       2251     2358     +107     
   ============================================
   + Hits           6591     7135     +544     
   - Misses         4012     4356     +344     
   - Partials       1140     1193      +53     
   ```
   
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/datafusion-comet/pull/1727?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   </details>


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Fix data race in memory profiling [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #1727:
URL: https://github.com/apache/datafusion-comet/pull/1727#discussion_r2084735443


##########
spark/src/main/java/org/apache/spark/CometTaskMemoryManager.java:
##########
@@ -30,36 +34,41 @@
  * memory manager. This assumes Spark's off-heap memory mode is enabled.
  */
 public class CometTaskMemoryManager {
+
+  private static final Logger logger = LoggerFactory.getLogger(CometTaskMemoryManager.class);
+
   /** 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;
-  private long used;
+  private final AtomicLong used = new AtomicLong();
 
   public CometTaskMemoryManager(long id) {
     this.id = id;
     this.internal = TaskContext$.MODULE$.get().taskMemoryManager();
     this.nativeMemoryConsumer = new NativeMemoryConsumer();
-    this.used = 0;
   }
 
   // Called by Comet native through JNI.
   // Returns the actual amount of memory (in bytes) granted.
   public long acquireMemory(long size) {
     long acquired = internal.acquireExecutionMemory(size, nativeMemoryConsumer);
-    used += acquired;
+    used.addAndGet(acquired);
     return acquired;
   }
 
   // Called by Comet native through JNI
   public void releaseMemory(long size) {
-    used -= size;
+    long newUsed = used.addAndGet(-size);
+    if (newUsed < 0) {
+      logger.warn("Used memory is negative: " + newUsed);

Review Comment:
   I changed this to use error logging rather than a warning. I'd prefer not to throw an exception because many jobs will run to completion without issue even if this variable does go negative.



-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Fix data race in memory profiling [datafusion-comet]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #1727:
URL: https://github.com/apache/datafusion-comet/pull/1727#discussion_r2085153843


##########
spark/src/main/java/org/apache/spark/CometTaskMemoryManager.java:
##########
@@ -30,36 +34,41 @@
  * memory manager. This assumes Spark's off-heap memory mode is enabled.
  */
 public class CometTaskMemoryManager {
+
+  private static final Logger logger = LoggerFactory.getLogger(CometTaskMemoryManager.class);
+
   /** 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;
-  private long used;
+  private final AtomicLong used = new AtomicLong();

Review Comment:
   👍 



-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Fix data race in memory profiling [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove merged PR #1727:
URL: https://github.com/apache/datafusion-comet/pull/1727


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Fix data race in memory profiling [datafusion-comet]

Posted by "wForget (via GitHub)" <gi...@apache.org>.
wForget commented on code in PR #1727:
URL: https://github.com/apache/datafusion-comet/pull/1727#discussion_r2083780711


##########
spark/src/main/java/org/apache/spark/CometTaskMemoryManager.java:
##########
@@ -30,36 +34,41 @@
  * memory manager. This assumes Spark's off-heap memory mode is enabled.
  */
 public class CometTaskMemoryManager {
+
+  private static final Logger logger = LoggerFactory.getLogger(CometTaskMemoryManager.class);
+
   /** 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;
-  private long used;
+  private final AtomicLong used = new AtomicLong();
 
   public CometTaskMemoryManager(long id) {
     this.id = id;
     this.internal = TaskContext$.MODULE$.get().taskMemoryManager();
     this.nativeMemoryConsumer = new NativeMemoryConsumer();
-    this.used = 0;
   }
 
   // Called by Comet native through JNI.
   // Returns the actual amount of memory (in bytes) granted.
   public long acquireMemory(long size) {
     long acquired = internal.acquireExecutionMemory(size, nativeMemoryConsumer);
-    used += acquired;
+    used.addAndGet(acquired);
     return acquired;
   }
 
   // Called by Comet native through JNI
   public void releaseMemory(long size) {
-    used -= size;
+    long newUsed = used.addAndGet(-size);
+    if (newUsed < 0) {
+      logger.warn("Used memory is negative: " + newUsed);

Review Comment:
   I guess this situation is likely to happen, the memeory acquired by `MemoryPool.grow` may be less than the actual request. Filed #1733
   
   https://github.com/apache/datafusion-comet/blob/25e39ab8ba2b882057c5a5c44be79d88015890fb/native/core/src/execution/memory_pools/unified_pool.rs#L93-L95



-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Fix data race in memory profiling [datafusion-comet]

Posted by "wForget (via GitHub)" <gi...@apache.org>.
wForget commented on code in PR #1727:
URL: https://github.com/apache/datafusion-comet/pull/1727#discussion_r2083780711


##########
spark/src/main/java/org/apache/spark/CometTaskMemoryManager.java:
##########
@@ -30,36 +34,41 @@
  * memory manager. This assumes Spark's off-heap memory mode is enabled.
  */
 public class CometTaskMemoryManager {
+
+  private static final Logger logger = LoggerFactory.getLogger(CometTaskMemoryManager.class);
+
   /** 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;
-  private long used;
+  private final AtomicLong used = new AtomicLong();
 
   public CometTaskMemoryManager(long id) {
     this.id = id;
     this.internal = TaskContext$.MODULE$.get().taskMemoryManager();
     this.nativeMemoryConsumer = new NativeMemoryConsumer();
-    this.used = 0;
   }
 
   // Called by Comet native through JNI.
   // Returns the actual amount of memory (in bytes) granted.
   public long acquireMemory(long size) {
     long acquired = internal.acquireExecutionMemory(size, nativeMemoryConsumer);
-    used += acquired;
+    used.addAndGet(acquired);
     return acquired;
   }
 
   // Called by Comet native through JNI
   public void releaseMemory(long size) {
-    used -= size;
+    long newUsed = used.addAndGet(-size);
+    if (newUsed < 0) {
+      logger.warn("Used memory is negative: " + newUsed);

Review Comment:
   I guess this situation is likely to happen, the memeory acquired by `MemoryPool.grow` may be less than the actual request.
   
   https://github.com/apache/datafusion-comet/blob/25e39ab8ba2b882057c5a5c44be79d88015890fb/native/core/src/execution/memory_pools/unified_pool.rs#L93-L95



-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: Fix data race in memory profiling [datafusion-comet]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #1727:
URL: https://github.com/apache/datafusion-comet/pull/1727#discussion_r2085157238


##########
spark/src/main/java/org/apache/spark/CometTaskMemoryManager.java:
##########
@@ -30,36 +34,41 @@
  * memory manager. This assumes Spark's off-heap memory mode is enabled.
  */
 public class CometTaskMemoryManager {
+
+  private static final Logger logger = LoggerFactory.getLogger(CometTaskMemoryManager.class);
+
   /** 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;
-  private long used;
+  private final AtomicLong used = new AtomicLong();
 
   public CometTaskMemoryManager(long id) {
     this.id = id;
     this.internal = TaskContext$.MODULE$.get().taskMemoryManager();
     this.nativeMemoryConsumer = new NativeMemoryConsumer();
-    this.used = 0;
   }
 
   // Called by Comet native through JNI.
   // Returns the actual amount of memory (in bytes) granted.
   public long acquireMemory(long size) {
     long acquired = internal.acquireExecutionMemory(size, nativeMemoryConsumer);
-    used += acquired;
+    used.addAndGet(acquired);
     return acquired;
   }
 
   // Called by Comet native through JNI
   public void releaseMemory(long size) {
-    used -= size;
+    long newUsed = used.addAndGet(-size);
+    if (newUsed < 0) {
+      logger.error("Used memory is negative: " + newUsed);

Review Comment:
   ```suggestion
         logger.error("Used memory is negative: " + newUsed + " after releasing memory chunk of: " + size);
   ```



-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org