yashmayya commented on code in PR #17674:
URL: https://github.com/apache/pinot/pull/17674#discussion_r2819278441


##########
pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInspector.java:
##########
@@ -0,0 +1,158 @@
+/**
+ * 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.pinot.core.transport;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.function.Function;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.metrics.AbstractMetrics;
+import org.apache.pinot.spi.utils.DataSizeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/// Utility class to inspect Netty constants and log their values, with the 
ability to check for specific conditions
+/// and log warnings if they are not met.
+public class NettyInspector {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(NettyInspector.class);
+  /// We use a CopyOnWriteArrayList to allow dynamic addition of checks at 
runtime, if needed.
+  public static final CopyOnWriteArrayList<Check> CHECKS;
+  /// We use a CopyOnWriteArrayList to allow dynamic addition of Netty 
instances at runtime,
+  /// if needed (e.g. if we want to support other shaded versions of Netty).
+  public static final CopyOnWriteArrayList<NettyInstance> KNOWN_INSTANCES;
+
+  static {
+    CHECKS = new CopyOnWriteArrayList<>(
+        new Check[] {
+            NettyInspector::checkDirectMemory
+        }
+    );
+    KNOWN_INSTANCES = new CopyOnWriteArrayList<>(new NettyInstance[] {
+        new NettyInstance.UnshadedNettyInstance(),
+        new NettyInstance.GrpcNettyInstance()
+    });
+  }
+
+  private NettyInspector() {
+    // Private constructor to prevent instantiation
+  }
+
+  public static void registerMetrics(AbstractMetrics<?, ?, ?, ?> metrics) {
+    for (NettyInstance instance : KNOWN_INSTANCES) {
+      metrics.setOrUpdateGauge(instance.getName() + "NettyDirectMemoryUsed",
+          instance::getUsedDirectMemory);
+      metrics.setOrUpdateGauge(instance.getName() + "NettyDirectMemoryMax",
+          instance::getMaxDirectMemory);
+    }
+  }
+
+  /// Logs the memory used by each known Netty instance as long as the max 
memory it can use.
+  /// It also logs the total memory used by all Netty instances and its max 
memory.
+  public static void logMemory() {
+    long totalUsedMemory = 0;
+    long totalMaxMemory = 0;
+    for (NettyInstance instance : KNOWN_INSTANCES) {
+      long usedMemory = instance.getUsedDirectMemory();
+      long maxMemory = instance.getMaxDirectMemory();
+      totalUsedMemory += usedMemory;
+      totalMaxMemory += maxMemory;
+      LOGGER.info("Netty instance '{}' is using {} of direct memory (max 
{}).", instance.getName(),
+          DataSizeUtils.fromBytes(usedMemory), 
DataSizeUtils.fromBytes(maxMemory));
+    }
+    LOGGER.info("Total direct memory used by all Netty instances: {} (max 
{}).",
+        DataSizeUtils.fromBytes(totalUsedMemory), 
DataSizeUtils.fromBytes(totalMaxMemory));
+  }
+
+  /// Logs the values of all constants for all Netty instances, and logs 
warnings if any checks fail.
+  public static void logAllChecks() {
+    for (Map.Entry<NettyInstance, List<CheckResult>> entry : 
checkAllConstants().entrySet()) {
+      NettyInstance instance = entry.getKey();
+      List<CheckResult> results = entry.getValue();
+      for (CheckResult result : results) {
+        switch (result._status) {
+          case PASS:
+            // Do nothing

Review Comment:
   Since this is only being called once on startup, maybe we could include an 
info log?



##########
pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInspector.java:
##########
@@ -0,0 +1,158 @@
+/**
+ * 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.pinot.core.transport;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.function.Function;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.metrics.AbstractMetrics;
+import org.apache.pinot.spi.utils.DataSizeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/// Utility class to inspect Netty constants and log their values, with the 
ability to check for specific conditions
+/// and log warnings if they are not met.
+public class NettyInspector {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(NettyInspector.class);
+  /// We use a CopyOnWriteArrayList to allow dynamic addition of checks at 
runtime, if needed.
+  public static final CopyOnWriteArrayList<Check> CHECKS;
+  /// We use a CopyOnWriteArrayList to allow dynamic addition of Netty 
instances at runtime,
+  /// if needed (e.g. if we want to support other shaded versions of Netty).
+  public static final CopyOnWriteArrayList<NettyInstance> KNOWN_INSTANCES;

Review Comment:
   I don't get why these need to be copy on write arrays? They aren't mutated 
after creation, no?



##########
pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInstance.java:
##########
@@ -0,0 +1,147 @@
+/**
+ * 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.pinot.core.transport;
+
+import java.lang.reflect.Constructor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/// Represents an instance of Netty, allowing access to certain static 
properties via reflection, with support for
+/// shaded Netty versions.
+///
+/// We know 2 common Netty instances:
+/// - Unshaded Netty, which uses the standard `io.netty` package
+/// - gRPC-shaded Netty, shaded by gRPC and included as a dependency. It uses 
`io.grpc.netty.shaded.io.netty` package.
+///
+/// This is important because Netty defines is not designed to be shade, and 
it uses some static attributes to determine
+/// its behavior, specially whether it can use `Unsafe` or not or how much 
memory to allocate for direct buffers.
+/// These attributes are set using JAVA_OPTs. Each shaded version uses 
different JAVA_OPT properties. If we forget to
+/// set one of these properties for a shaded version, that Netty _instance_ 
will fall back to some default behavior that
+/// may not be optimal, and we won't have any indication of that happening.
+///
+/// By using reflection to access these properties, we can log their values at 
startup and check if they are set to the
+/// expected values, logging warnings if they are not.
+/// This allows us to catch misconfigurations early.
+///
+/// **It is critical to not shade this class**, otherwise the literals used 
for reflection
+/// (ie `io.netty.util.internal.PlatformDependent`) will be shaded too, so 
instead of looking for
+/// `io.grpc.netty.shaded.io.netty.util.internal.PlatformDependent`, as you 
may think reading this class, the shaded
+/// version of this class will look for
+/// 
`io.grpc.netty.shaded.org.apache.pinot.shaded.io.netty.util.internal.PlatformDependent`.
+/// At the moment this is written, Pinot _does not_ shade Netty, so it is 
safe. Just to be sure, this class should be
+/// excluded in the maven shade plugin configuration (see pom.xml on the root 
of the project).
+public abstract class NettyInstance {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(NettyInstance.class);
+  private static final Constructor<DummyClass> CONSTRUCTOR;
+
+  static {
+    try {
+      CONSTRUCTOR = DummyClass.class.getConstructor();
+    } catch (NoSuchMethodException e) {
+      throw new RuntimeException("This should never happen, DummyClass has a 
default constructor", e);
+    }
+  }
+
+  /// The name of the Netty instance. It will be used on logs but also on 
metric names, so it should be
+  /// something short and without spaces like "Unshaded", "Pinot", "gRPC".
+  /// Add underscores if you need to separate words, but avoid other special 
characters.
+  public abstract String getName();
+
+  public abstract String getShadePrefix();
+
+  public abstract boolean isExplicitTryReflectionSetAccessible();
+
+  public abstract long getUsedDirectMemory();
+
+  public abstract long getMaxDirectMemory();
+
+  public abstract long getEstimateMaxDirectMemory();
+
+  public static class UnshadedNettyInstance extends NettyInstance {
+    @Override
+    public String getName() {
+      return "unshaded";
+    }
+
+    @Override
+    public String getShadePrefix() {
+      return "";
+    }
+
+    @Override
+    public boolean isExplicitTryReflectionSetAccessible() {
+      return 
io.netty.util.internal.ReflectionUtil.trySetAccessible(CONSTRUCTOR, true) == 
null;

Review Comment:
   This is a neat hack lol, but there's no way we can be sure that Netty will 
continue using the same method to decide whether offheap buffers can be used in 
future versions right?



##########
pom.xml:
##########
@@ -2929,6 +2929,9 @@
 
                 <!-- Solve NoClassDefFoundError. Borrowed from 
https://github.com/prometheus/jmx_exporter/issues/802 -->
                 
<exclude>META-INF/versions/9/org/yaml/snakeyaml/internal/**</exclude>
+
+                <!-- Exclude NettyInstacne because it includes Netty package 
literals -->

Review Comment:
   ```suggestion
                   <!-- Exclude NettyInstance because it includes Netty package 
literals -->
   ```
   nit



##########
pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInspector.java:
##########
@@ -0,0 +1,158 @@
+/**
+ * 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.pinot.core.transport;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.function.Function;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.metrics.AbstractMetrics;
+import org.apache.pinot.spi.utils.DataSizeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/// Utility class to inspect Netty constants and log their values, with the 
ability to check for specific conditions
+/// and log warnings if they are not met.
+public class NettyInspector {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(NettyInspector.class);
+  /// We use a CopyOnWriteArrayList to allow dynamic addition of checks at 
runtime, if needed.
+  public static final CopyOnWriteArrayList<Check> CHECKS;
+  /// We use a CopyOnWriteArrayList to allow dynamic addition of Netty 
instances at runtime,
+  /// if needed (e.g. if we want to support other shaded versions of Netty).
+  public static final CopyOnWriteArrayList<NettyInstance> KNOWN_INSTANCES;
+
+  static {
+    CHECKS = new CopyOnWriteArrayList<>(
+        new Check[] {
+            NettyInspector::checkDirectMemory
+        }
+    );
+    KNOWN_INSTANCES = new CopyOnWriteArrayList<>(new NettyInstance[] {
+        new NettyInstance.UnshadedNettyInstance(),
+        new NettyInstance.GrpcNettyInstance()
+    });
+  }
+
+  private NettyInspector() {
+    // Private constructor to prevent instantiation
+  }
+
+  public static void registerMetrics(AbstractMetrics<?, ?, ?, ?> metrics) {
+    for (NettyInstance instance : KNOWN_INSTANCES) {
+      metrics.setOrUpdateGauge(instance.getName() + "NettyDirectMemoryUsed",
+          instance::getUsedDirectMemory);
+      metrics.setOrUpdateGauge(instance.getName() + "NettyDirectMemoryMax",
+          instance::getMaxDirectMemory);
+    }
+  }
+
+  /// Logs the memory used by each known Netty instance as long as the max 
memory it can use.
+  /// It also logs the total memory used by all Netty instances and its max 
memory.
+  public static void logMemory() {

Review Comment:
   Unused?



-- 
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]

Reply via email to