Jackie-Jiang commented on code in PR #18163:
URL: https://github.com/apache/pinot/pull/18163#discussion_r3083197169


##########
pinot-core/src/main/java/org/apache/pinot/core/accounting/QueryResourceAggregator.java:
##########
@@ -266,6 +302,60 @@ private void evalTriggers() {
     }
   }
 
+  /**
+   * Activates an OOM pause. Query threads calling {@link #waitIfPaused()} 
will block until the pause is cleared or the
+   * deadline expires. Also hints the JVM to run garbage collection.
+   */
+  private void activatePause(long timeoutMs) {
+    LOGGER.warn("Activating OOM pause for {}ms to allow garbage collection 
before killing queries. "
+        + "Heap used bytes: {}", timeoutMs, _usedBytes);
+    // Write deadline before the flag - volatile ordering guarantees any 
thread that reads
+    // _pauseActive == true will also see the updated _pauseDeadlineMs.
+    _pauseDeadlineMs = System.currentTimeMillis() + timeoutMs;
+    _pauseActive = true;
+    // Hint the JVM to run GC while query threads are pausing
+    System.gc();
+  }
+
+  /**
+   * Clears the OOM pause and wakes all blocked query threads.
+   */
+  private void clearPause() {
+    LOGGER.info("Clearing OOM pause. Heap used bytes: {}", _usedBytes);
+    _pauseLock.lock();
+    try {
+      _pauseActive = false;
+      _pauseCondition.signalAll();
+    } finally {
+      _pauseLock.unlock();
+    }
+  }
+
+  /**
+   * Called by query threads at sampling checkpoints. Blocks if an OOM pause 
is active. Fast-path: single volatile read
+   * when no pause is active.
+   */
+  void waitIfPaused() {
+    if (!_pauseActive) {
+      return;
+    }
+    _pauseLock.lock();
+    try {
+      while (_pauseActive) {
+        long remaining = _pauseDeadlineMs - System.currentTimeMillis();
+        if (remaining <= 0) {
+          break;
+        }
+        _pauseCondition.await(remaining, TimeUnit.MILLISECONDS);

Review Comment:
   It is a little bit risky because thread might wake up before watcher thread 
tries to terminate the query, causing higher memory usage



-- 
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