-
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 DefaultCpuHealthChecker #4673
Changes from 1 commit
f08cb8b
08c706e
551263a
afd1843
5e5069e
e4132b4
35da7bc
a0c04bb
dd68de9
8934066
32fd4f7
0a0f815
3b1770b
c8c1664
eda64c4
a4733f9
8be0675
07c4f02
d5f162f
760496c
a1df8fa
3f4a5fc
0b14883
667a0bf
2dae89c
9d7cd92
69df576
8f2b4a1
f6ef458
a766919
f469ac7
fad1155
29048d3
20e3188
5535d6a
759763b
fb59ec4
d1a06e3
1dda317
828e51a
d619208
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,74 @@ | ||||||||
package com.linecorp.armeria.server.healthcheck; | ||||||||
|
||||||||
import static java.util.Objects.requireNonNull; | ||||||||
|
||||||||
import java.lang.management.ManagementFactory; | ||||||||
import java.lang.management.OperatingSystemMXBean; | ||||||||
import java.lang.reflect.InvocationTargetException; | ||||||||
import java.lang.reflect.Method; | ||||||||
import java.util.Arrays; | ||||||||
import java.util.List; | ||||||||
|
||||||||
public class DefaultCpuHealthChecker 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. Can we also add integration tests for this class? 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. I'm struggling with writing tests.😭 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.
I don't think this is necessary 😅 I think just verifying that |
||||||||
|
||||||||
private static final List<String> OPERATING_SYSTEM_BEAN_CLASS_NAMES = Arrays.asList( | ||||||||
Bue-von-hon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
"com.ibm.lang.management.OperatingSystemMXBean", // J9 | ||||||||
"com.sun.management.OperatingSystemMXBean" // HotSpot | ||||||||
); | ||||||||
|
||||||||
private final OperatingSystemMXBean operatingSystemBean; | ||||||||
|
||||||||
private final Class<?> operatingSystemBeanClass; | ||||||||
|
||||||||
private final Method systemCpuUsage; | ||||||||
|
||||||||
private final double targetCpuUsage; | ||||||||
|
||||||||
public DefaultCpuHealthChecker(final double targetCpuUsage) { | ||||||||
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. Should we accept arguments for both 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. i added! |
||||||||
this.targetCpuUsage = targetCpuUsage; | ||||||||
this.operatingSystemBean = ManagementFactory.getOperatingSystemMXBean(); | ||||||||
this.operatingSystemBeanClass = getFirstClassFound(OPERATING_SYSTEM_BEAN_CLASS_NAMES); | ||||||||
this.systemCpuUsage = detectMethod("getSystemCpuLoad"); | ||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
public boolean isHealthy() { | ||||||||
double cpuUsage = invoke(systemCpuUsage); | ||||||||
return cpuUsage <= targetCpuUsage; | ||||||||
} | ||||||||
|
||||||||
private double invoke(final Method method) { | ||||||||
try { | ||||||||
return (double) method.invoke(operatingSystemBean); | ||||||||
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. This method will be frequently invoked whenever 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. Using the lookup#unreflect method, extracted the MethodHandle object from the Method class! |
||||||||
} | ||||||||
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | ||||||||
return Double.NaN; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
private Method detectMethod(final String name) { | ||||||||
jrhee17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
requireNonNull(name); | ||||||||
Bue-von-hon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
if (operatingSystemBeanClass == null) { | ||||||||
return null; | ||||||||
} | ||||||||
try { | ||||||||
// ensure the Bean we have is actually an instance of the interface | ||||||||
requireNonNull(operatingSystemBeanClass.cast(operatingSystemBean)); | ||||||||
return operatingSystemBeanClass.getMethod(name); | ||||||||
} | ||||||||
catch (ClassCastException | NoSuchMethodException | SecurityException e) { | ||||||||
return null; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
private Class<?> getFirstClassFound(final List<String> classNames) { | ||||||||
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.
Suggested change
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. i added!
Bue-von-hon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
for (String className : classNames) { | ||||||||
try { | ||||||||
return Class.forName(className); | ||||||||
Bue-von-hon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
} | ||||||||
catch (ClassNotFoundException ignore) { | ||||||||
} | ||||||||
} | ||||||||
return null; | ||||||||
} | ||||||||
} |
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.
Can you also leave a link where this class was forked from?
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.
i added!