-
Notifications
You must be signed in to change notification settings - Fork 590
feat(server): support in-heap memory JVM monitor #2650
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
63e687c
Add jvm memory monitor feature
MingzhenHan c1439c9
Update jvm memory monitor parameters
MingzhenHan ec594be
Merge branch 'apache:master' into master
MingzhenHan 30219d7
Update rest-server.properties
MingzhenHan 9f67893
code review advice modify
MingzhenHan 310c8d7
modify LOG type
MingzhenHan 219f48f
improve code
MingzhenHan 2092e11
Merge branch 'apache:master' into master
MingzhenHan 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
120 changes: 120 additions & 0 deletions
120
hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/dist/MemoryMonitor.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,120 @@ | ||
| /* | ||
| * 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.hugegraph.dist; | ||
|
|
||
| import java.lang.management.ManagementFactory; | ||
| import java.lang.management.MemoryUsage; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.apache.hugegraph.config.HugeConfig; | ||
| import org.apache.hugegraph.config.ServerOptions; | ||
| import org.apache.hugegraph.util.ExecutorUtil; | ||
| import org.apache.hugegraph.util.Log; | ||
| import org.slf4j.Logger; | ||
|
|
||
| import com.sun.management.ThreadMXBean; | ||
|
|
||
| public class MemoryMonitor { | ||
|
|
||
| private static final Logger LOG = Log.logger(MemoryMonitor.class); | ||
| private final double MEMORY_MONITOR_THRESHOLD; | ||
MingzhenHan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private final int MEMORY_MONITOR_DETECT_PERIOD; | ||
| private final ScheduledExecutorService scheduler; | ||
|
|
||
| public MemoryMonitor(String restServerConf) { | ||
| HugeConfig restServerConfig = new HugeConfig(restServerConf); | ||
| MEMORY_MONITOR_THRESHOLD = | ||
| restServerConfig.get(ServerOptions.JVM_MEMORY_MONITOR_THRESHOLD); | ||
| MEMORY_MONITOR_DETECT_PERIOD = | ||
| restServerConfig.get(ServerOptions.JVM_MEMORY_MONITOR_DETECT_PERIOD); | ||
| this.scheduler = ExecutorUtil.newScheduledThreadPool("memory-monitor-thread-%d"); | ||
| } | ||
|
|
||
| private void runMemoryDetect() { | ||
| double memoryUsagePercentage = getMemoryUsageRatio(); | ||
|
|
||
| if (memoryUsagePercentage > MEMORY_MONITOR_THRESHOLD) { | ||
| LOG.warn("JVM memory usage is '{}', exceeding the threshold of '{}'.", | ||
| memoryUsagePercentage, MEMORY_MONITOR_THRESHOLD); | ||
| System.gc(); | ||
| LOG.warn("Trigger System.gc()"); | ||
|
|
||
| double doubleCheckUsage = getMemoryUsageRatio(); | ||
| if (doubleCheckUsage > MEMORY_MONITOR_THRESHOLD) { | ||
MingzhenHan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LOG.warn("JVM memory usage is '{}', exceeding the threshold of '{}'.", | ||
| doubleCheckUsage, MEMORY_MONITOR_THRESHOLD); | ||
| interruptHighestMemoryThread(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private double getMemoryUsageRatio() { | ||
| MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); | ||
| return (double) heapMemoryUsage.getUsed() / heapMemoryUsage.getMax(); | ||
| } | ||
|
|
||
| private Thread getHighestMemoryThread() { | ||
| long highestMemory = 0; | ||
| Thread highestThread = null; | ||
|
|
||
| ThreadMXBean threadMXBean = (ThreadMXBean) ManagementFactory.getThreadMXBean(); | ||
|
|
||
| Thread[] threads = new Thread[Thread.activeCount()]; | ||
| Thread.enumerate(threads); | ||
| for (Thread thread : threads) { | ||
| if (thread.getState() != Thread.State.RUNNABLE || thread.getName() == null || | ||
| !thread.getName().startsWith("grizzly-http-server-")) { | ||
MingzhenHan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continue; | ||
| } | ||
|
|
||
| long threadMemory = threadMXBean.getThreadAllocatedBytes(thread.getId()); | ||
| if (threadMemory > highestMemory) { | ||
| highestMemory = threadMemory; | ||
| highestThread = thread; | ||
| } | ||
| } | ||
| return highestThread; | ||
| } | ||
|
|
||
| private void interruptHighestMemoryThread() { | ||
| Thread targetThread = getHighestMemoryThread(); | ||
| if (targetThread != null) { | ||
| targetThread.interrupt(); | ||
| LOG.warn("Send interrupt to '{}' thread", targetThread.getName()); | ||
| } | ||
| } | ||
|
|
||
| public void start() { | ||
| if (MEMORY_MONITOR_THRESHOLD >= 1.0) { | ||
MingzhenHan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LOG.info("Invalid parameter, MEMORY_MONITOR_THRESHOLD should ≤ 1.0."); | ||
| return; | ||
MingzhenHan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| this.scheduler.scheduleAtFixedRate(this::runMemoryDetect, 0, MEMORY_MONITOR_DETECT_PERIOD, | ||
| TimeUnit.MILLISECONDS); | ||
| LOG.info("Memory monitoring started."); | ||
| } | ||
|
|
||
| public void stop() { | ||
| if (MEMORY_MONITOR_THRESHOLD >= 1.0) { | ||
| return; | ||
| } | ||
| this.scheduler.shutdownNow(); | ||
| LOG.info("Memory monitoring stopped."); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.