From 15b3344b2dfad124f6308e6be180e95eae8e7e3c Mon Sep 17 00:00:00 2001 From: larry-safran Date: Wed, 6 Sep 2023 18:36:10 -0700 Subject: [PATCH] netty: Respect io.netty.allocator.type="unpooled" when getting Netty Allocator Fixes #10292 --- netty/src/main/java/io/grpc/netty/Utils.java | 8 ++++++++ .../src/test/java/io/grpc/netty/UtilsTest.java | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/netty/src/main/java/io/grpc/netty/Utils.java b/netty/src/main/java/io/grpc/netty/Utils.java index 4af2f8463a5..ab684896e9b 100644 --- a/netty/src/main/java/io/grpc/netty/Utils.java +++ b/netty/src/main/java/io/grpc/netty/Utils.java @@ -37,6 +37,7 @@ import io.grpc.netty.NettySocketSupport.NativeSocketOptions; import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.PooledByteBufAllocator; +import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.channel.Channel; import io.netty.channel.ChannelConfig; import io.netty.channel.ChannelFactory; @@ -60,6 +61,7 @@ import java.lang.reflect.Constructor; import java.nio.channels.ClosedChannelException; import java.nio.channels.UnresolvedAddressException; +import java.util.Locale; import java.util.Map; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; @@ -133,6 +135,12 @@ private static final class ByteBufAllocatorPreferHeapHolder { } public static ByteBufAllocator getByteBufAllocator(boolean forceHeapBuffer) { + String allocType = System.getProperty("io.netty.allocator.type", "pooled"); + if (allocType.toLowerCase(Locale.ROOT).equals("unpooled")) { + logger.log(Level.FINE, "Using unpooled allocator"); + return UnpooledByteBufAllocator.DEFAULT; + } + if (Boolean.parseBoolean( System.getProperty("io.grpc.netty.useCustomAllocator", "true"))) { boolean defaultPreferDirect = PooledByteBufAllocator.defaultPreferDirect(); diff --git a/netty/src/test/java/io/grpc/netty/UtilsTest.java b/netty/src/test/java/io/grpc/netty/UtilsTest.java index 90330a5fe96..95f57732117 100644 --- a/netty/src/test/java/io/grpc/netty/UtilsTest.java +++ b/netty/src/test/java/io/grpc/netty/UtilsTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.TruthJUnit.assume; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; @@ -28,6 +29,7 @@ import io.grpc.Metadata; import io.grpc.Status; import io.grpc.internal.GrpcUtil; +import io.netty.buffer.ByteBufAllocator; import io.netty.channel.Channel; import io.netty.channel.ChannelFactory; import io.netty.channel.ChannelOption; @@ -70,6 +72,21 @@ public void testStatusFromThrowable() { assertStatusEquals(Status.UNKNOWN.withCause(t), Utils.statusFromThrowable(t)); } + @Test + public void testGetBufferAllocator() { + ByteBufAllocator heapAllocator = Utils.getByteBufAllocator(true); + ByteBufAllocator directAllocator = Utils.getByteBufAllocator(false); + assertNotEquals(heapAllocator, directAllocator); + + System.setProperty("io.netty.allocator.type", "unpooled"); + ByteBufAllocator unpooled1 = Utils.getByteBufAllocator(false); + assertThat(unpooled1.getClass().getName()).isNotEqualTo("UnpooledByteBufAllocator"); + + System.setProperty("io.netty.allocator.type", "pooled"); + ByteBufAllocator unpooled2 = Utils.getByteBufAllocator(false); + assertEquals(directAllocator, unpooled2); + } + @Test public void convertClientHeaders_sanitizes() { Metadata metaData = new Metadata();