-
Notifications
You must be signed in to change notification settings - Fork 927
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
Added DefaultMemoryHealthChecker #4711
base: main
Are you sure you want to change the base?
Changes from all commits
e233572
1a7bfbb
f4b4ca9
a5eccc9
13f0361
7403228
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||
package com.linecorp.armeria.server.healthcheck; | ||||||
|
||||||
import java.lang.management.BufferPoolMXBean; | ||||||
import java.lang.management.ManagementFactory; | ||||||
import java.lang.management.MemoryPoolMXBean; | ||||||
import java.lang.management.MemoryType; | ||||||
import java.lang.management.MemoryUsage; | ||||||
import java.util.List; | ||||||
import java.util.stream.Collectors; | ||||||
|
||||||
import com.google.common.base.MoreObjects; | ||||||
|
||||||
public class DefaultMemoryHealthChecker implements HealthChecker { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be package private final:
Suggested change
|
||||||
|
||||||
private final double targetHeapMemoryUtilizationRate; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we change Rate to Ratio in this PR? Rate means speed of change which is not what we meant here. |
||||||
private final double targetNonHeapMemoryUtilizationRate; | ||||||
private final double targetTotalMemoryUtilizationRate; | ||||||
|
||||||
public DefaultMemoryHealthChecker(final double targetHeapMemoryUtilizationRate, final double targetNonHeapMemoryUtilizationRate, final double targetMemoryTotalUsage) { | ||||||
this.targetHeapMemoryUtilizationRate = targetHeapMemoryUtilizationRate; | ||||||
this.targetNonHeapMemoryUtilizationRate = targetNonHeapMemoryUtilizationRate; | ||||||
this.targetTotalMemoryUtilizationRate = targetMemoryTotalUsage; | ||||||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we could add some checks here, e.g. this.heapUsageRatioThreshold = validateRatio(heapUsageRatioThreshold, "heapUsageRatioThreshold");
this.nonHeapUsageRatioThreshold = validateRatio(nonHeapUsageRatioThreshold, "nonHeapUsageRatioThreshold");
this.totalUsageRatioThreshold = validateRatio(totalUsageRatioThreshold, "totalUsageRatioThreshold");
...
private static void validateRatio(double ratio, String paramName) {
checkArgument(ratio >= 0 && ratio <= 1, "%s (expected: >= 0 and <= 1)", paramName);
} Also, I suggested renaming the parameters and member fields as shown in the above example.
|
||||||
} | ||||||
|
||||||
@Override | ||||||
public boolean isHealthy() { | ||||||
final List<MemoryPoolMXBean> heapMemories = getHeapMemories(); | ||||||
final double heapMemoryUsage = heapMemories.stream().map(MemoryPoolMXBean::getUsage).mapToDouble(MemoryUsage::getUsed).sum(); | ||||||
final double maximumHeapMemory = heapMemories.stream().map(MemoryPoolMXBean::getUsage).mapToDouble(MemoryUsage::getMax).sum(); | ||||||
final long runtimeMaxMemory = Runtime.getRuntime().maxMemory(); | ||||||
|
||||||
final BufferPoolMXBean nonHeapMemoryUsage = ManagementFactory.getPlatformMXBean(BufferPoolMXBean.class); | ||||||
final double totalMemoryUsage = Runtime.getRuntime().totalMemory(); | ||||||
return (heapMemoryUsage / maximumHeapMemory) <= targetHeapMemoryUtilizationRate && | ||||||
(nonHeapMemoryUsage == null || (nonHeapMemoryUsage.getMemoryUsed() / nonHeapMemoryUsage.getTotalCapacity()) <= targetNonHeapMemoryUtilizationRate) && | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convert from |
||||||
(runtimeMaxMemory == Long.MAX_VALUE || totalMemoryUsage / runtimeMaxMemory <= targetTotalMemoryUtilizationRate); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we log a warning message if We could keep a boolean flag so make sure that the log message is logged only once, so we don't pollute the log file. |
||||||
} | ||||||
|
||||||
@Override | ||||||
public String toString() { | ||||||
return MoreObjects.toStringHelper(this) | ||||||
.add("targetTotalMemoryUtilizationRate", this.targetTotalMemoryUtilizationRate) | ||||||
.add("targetHeapMemoryUtilizationRate", this.targetHeapMemoryUtilizationRate) | ||||||
.add("targetNonHeapMemoryUtilizationRate", this.targetNonHeapMemoryUtilizationRate) | ||||||
.toString(); | ||||||
} | ||||||
|
||||||
private List<MemoryPoolMXBean> getHeapMemories() { | ||||||
return ManagementFactory.getMemoryPoolMXBeans().stream().filter(e -> MemoryType.HEAP.equals(e.getType())).collect(Collectors.toList()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could call |
||||||
} | ||||||
} |
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.
Missing copyright header