adoroszlai commented on code in PR #10846:
URL: https://github.com/apache/ozone/pull/10846#discussion_r3650339621
##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/NettyMetrics.java:
##########
@@ -46,14 +54,102 @@ public void getMetrics(MetricsCollector collector, boolean
all) {
.setContext("Netty metrics");
recordBuilder
.addGauge(MetricsInfos.USED_DIRECT_MEM, usedDirectMemory())
- .addGauge(MetricsInfos.MAX_DIRECT_MEM, maxDirectMemory());
+ .addGauge(MetricsInfos.MAX_DIRECT_MEM, MAX_DIRECT_MEMORY);
}
public void unregister() {
MetricsSystem ms = DefaultMetricsSystem.instance();
ms.unregisterSource(SOURCE_NAME);
}
+ /**
+ * Direct memory currently used by the default Netty {@link ByteBufAllocator}
+ * (the allocator Ratis and gRPC use). The previous implementation reported
+ * Netty's process-wide direct memory counter; this reports the default
+ * allocator's usage, which is close but not necessarily identical.
+ *
+ * @return used direct memory in bytes, or -1 if it cannot be determined
+ */
+ static long usedDirectMemory() {
+ ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
+ if (allocator instanceof ByteBufAllocatorMetricProvider) {
+ return ((ByteBufAllocatorMetricProvider) allocator).metric()
+ .usedDirectMemory();
+ }
+ return -1L;
+ }
+
+ /**
+ * Resolve the maximum direct memory using only public API, mirroring Netty's
+ * resolution order: the {@code io.netty.maxDirectMemory} system property,
then
+ * the {@code -XX:MaxDirectMemorySize} JVM flag, then the maximum heap size.
+ * The JVM flag is read from the runtime input arguments (command line or
+ * {@code JAVA_TOOL_OPTIONS}); a value set by other means falls back to the
+ * maximum heap size.
+ *
+ * @return maximum direct memory in bytes
+ */
+ static long maxDirectMemory() {
+ return resolveMaxDirectMemory(
+ Long.getLong("io.netty.maxDirectMemory", -1L),
Review Comment:
Ozone uses Netty both from `ratis-thirdparty` (shaded) and directly.
Previously `NettyMetrics` only tracked the shaded one. Now this property
lookup is for the unshaded one, while `usedDirectMemory()` gets memory usage
from the shaded `ByteBufAllocator` class. To avoid mismatch, we need to look
up `org.apache.ratis.thirdparty.io.netty.maxDirectMemory` instead.
--
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]