generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
ResourceMonitor only checks the memory health by calculating the memory usage after GC of old gen in v3 #3983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
qianheng-aws
merged 13 commits into
opensearch-project:main
from
LantaoJin:pr/issues/3750_2_gc_listener
Aug 22, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
28e35d8
ResourceMonitor only checks the memory health by calculating the memo…
LantaoJin 24e330e
address comments
LantaoJin 4838a57
address comments
LantaoJin 355b7bd
Add memory usage fallback
LantaoJin 46a147d
Fix IT
LantaoJin 9621cdd
Merge remote-tracking branch 'upstream/main' into issues/3750_2_gc_li…
LantaoJin 87eaba3
Generate script code lazily to avoid unnecessary usage in Calcite
LantaoJin 66b75c3
typo
LantaoJin d3f9c8f
Merge remote-tracking branch 'upstream/main' into issues/3750_2_gc_li…
LantaoJin cc8941c
Address the comments of 3929 followup
LantaoJin dbf362a
Catch throwable instead of MemoryUsageException
LantaoJin 36dbfe1
Merge remote-tracking branch 'upstream/main' into issues/3750_2_gc_li…
LantaoJin 60a596b
Fix unit test when the code in script query expression generated lazily
LantaoJin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
opensearch/src/main/java/org/opensearch/sql/opensearch/monitor/GCedMemoryUsage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.opensearch.monitor; | ||
|
|
||
| import com.sun.management.GarbageCollectionNotificationInfo; | ||
| import java.lang.management.GarbageCollectorMXBean; | ||
| import java.lang.management.ManagementFactory; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import javax.management.Notification; | ||
| import javax.management.NotificationEmitter; | ||
| import javax.management.NotificationListener; | ||
| import javax.management.openmbean.CompositeData; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| /** Get memory usage from GC notification listener, which is used in Calcite engine. */ | ||
| public class GCedMemoryUsage implements MemoryUsage { | ||
| private static final Logger LOG = LogManager.getLogger(); | ||
| private static final List<String> OLD_GEN_GC_ACTION_KEYWORDS = | ||
| List.of("major", "concurrent", "old", "full", "marksweep"); | ||
|
|
||
| private GCedMemoryUsage() { | ||
| registerGCListener(); | ||
| } | ||
|
|
||
| // Lazy initialize the instance to avoid register GCListener in v2. | ||
| private static class Holder { | ||
| static final MemoryUsage INSTANCE = new GCedMemoryUsage(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the singleton instance of GCedMemoryUsage. | ||
| * | ||
| * @return GCedMemoryUsage instance | ||
| */ | ||
| public static MemoryUsage getInstance() { | ||
| return Holder.INSTANCE; | ||
| } | ||
|
|
||
| private final AtomicLong usage = new AtomicLong(-1); | ||
|
|
||
| @Override | ||
| public long usage() { | ||
| return usage.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setUsage(long value) { | ||
| usage.set(value); | ||
| } | ||
|
|
||
| private void registerGCListener() { | ||
| boolean registered = false; | ||
| List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); | ||
| for (GarbageCollectorMXBean gcBean : gcBeans) { | ||
LantaoJin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (gcBean instanceof NotificationEmitter && isOldGenGc(gcBean.getName())) { | ||
| LOG.info("{} listener registered for memory usage monitor.", gcBean.getName()); | ||
| registered = true; | ||
| NotificationEmitter emitter = (NotificationEmitter) gcBean; | ||
| emitter.addNotificationListener( | ||
| new OldGenGCListener(), | ||
| notification -> { | ||
| if (!notification | ||
| .getType() | ||
| .equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { | ||
| return false; | ||
| } | ||
| CompositeData cd = (CompositeData) notification.getUserData(); | ||
| GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd); | ||
| return isOldGenGc(info.getGcAction()); | ||
| }, | ||
| null); | ||
| } | ||
| } | ||
| if (!registered) { | ||
| // fallback to RuntimeMemoryUsage | ||
| LOG.info("No old gen GC listener registered, fallback to RuntimeMemoryUsage"); | ||
| throw new OpenSearchMemoryHealthy.MemoryUsageException(); | ||
| } | ||
| } | ||
|
|
||
| private boolean isOldGenGc(String gcKeyword) { | ||
| String keyword = gcKeyword.toLowerCase(Locale.ROOT); | ||
| return OLD_GEN_GC_ACTION_KEYWORDS.stream().anyMatch(keyword::contains); | ||
| } | ||
|
|
||
| private static class OldGenGCListener implements NotificationListener { | ||
| @Override | ||
| public void handleNotification(Notification notification, Object handback) { | ||
| CompositeData cd = (CompositeData) notification.getUserData(); | ||
| GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd); | ||
| Map<String, java.lang.management.MemoryUsage> memoryUsageAfterGc = | ||
| info.getGcInfo().getMemoryUsageAfterGc(); | ||
| // Skip Metaspace and CodeHeap spaces which the GC scope is out of stack GC. | ||
| long totalStackUsed = | ||
| memoryUsageAfterGc.entrySet().stream() | ||
| .filter( | ||
| entry -> | ||
| !entry.getKey().equals("Metaspace") | ||
| && !entry.getKey().equals("Compressed Class Space") | ||
| && !entry.getKey().startsWith("CodeHeap")) | ||
| .mapToLong(entry -> entry.getValue().getUsed()) | ||
| .sum(); | ||
| getInstance().setUsage(totalStackUsed); | ||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("Old Gen GC detected, memory usage after GC is {} bytes.", totalStackUsed); | ||
| } | ||
| } | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
opensearch/src/main/java/org/opensearch/sql/opensearch/monitor/MemoryUsage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.opensearch.monitor; | ||
|
|
||
| /** Memory usage interface. It is used to get the memory usage of the VM. */ | ||
| public interface MemoryUsage { | ||
| long usage(); | ||
|
|
||
| void setUsage(long usage); | ||
| } | ||
dai-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
opensearch/src/main/java/org/opensearch/sql/opensearch/monitor/RuntimeMemoryUsage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.opensearch.monitor; | ||
|
|
||
| /** Get memory usage from runtime, which is used in v2. */ | ||
| public class RuntimeMemoryUsage implements MemoryUsage { | ||
| private RuntimeMemoryUsage() {} | ||
|
|
||
| private static class Holder { | ||
| static final MemoryUsage INSTANCE = new RuntimeMemoryUsage(); | ||
| } | ||
|
|
||
| public static MemoryUsage getInstance() { | ||
| return Holder.INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public long usage() { | ||
| final long freeMemory = Runtime.getRuntime().freeMemory(); | ||
| final long totalMemory = Runtime.getRuntime().totalMemory(); | ||
| return totalMemory - freeMemory; | ||
| } | ||
|
|
||
| @Override | ||
| public void setUsage(long usage) { | ||
| throw new UnsupportedOperationException("Cannot set usage in RuntimeMemoryUsage"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To align with behaviour of v2, OOM error will fail the whole JVM. We caught OOM error in previous implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It think it's OK to keep align with v2. But will it be better to trigger GC and then check memory usage again before making the service crash? Anyway, the primary purpose of this PR should not be blocked by this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe more discussion is required and track in a follow-up issue.