gortiz commented on code in PR #17674: URL: https://github.com/apache/pinot/pull/17674#discussion_r2793478619
########## 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 still tricky: instead of calling a getter (which is private in Netty), we call the method they use to check whether they can call the private constructors of ByteBuffers. As long as Netty keeps using the same code (which seems very likely), this should be a safe, reflection-free way to know whether Netty can use offheap buffers or not. -- 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]
