Skip to content

Make some Netty Handlers Singletons #80156

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ public Exception getInboundException() {
return inboundException;
}

public FullHttpRequest nettyRequest() {
return request;
}

/**
* A wrapper of {@link HttpHeaders} that implements a map to prevent copying unnecessarily. This class does not support modifications
* and due to the underlying implementation, it performs case insensitive lookups of key to values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
import java.util.List;

@ChannelHandler.Sharable
class Netty4HttpRequestCreator extends MessageToMessageDecoder<FullHttpRequest> {
final class Netty4HttpRequestCreator extends MessageToMessageDecoder<FullHttpRequest> {

static final Netty4HttpRequestCreator INSTANCE = new Netty4HttpRequestCreator();

private Netty4HttpRequestCreator() {}

@Override
protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
* Split up large responses to prevent batch compression {@link JdkZlibEncoder} down the pipeline.
*/
@ChannelHandler.Sharable
class Netty4HttpResponseCreator extends MessageToMessageEncoder<Netty4HttpResponse> {
final class Netty4HttpResponseCreator extends MessageToMessageEncoder<Netty4HttpResponse> {

static final Netty4HttpResponseCreator INSTANCE = new Netty4HttpResponseCreator();

private Netty4HttpResponseCreator() {}

private static final String DO_NOT_SPLIT = "es.unsafe.do_not_split_http_responses";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ public class Netty4HttpServerTransport extends AbstractHttpServerTransport {
Property.NodeScope
);

private final ByteSizeValue maxInitialLineLength;
private final ByteSizeValue maxHeaderSize;
private final ByteSizeValue maxChunkSize;

private final int pipeliningMaxEvents;

private final SharedGroupFactory sharedGroupFactory;
Expand All @@ -157,9 +153,6 @@ public Netty4HttpServerTransport(
NettyAllocator.logAllocatorDescriptionIfNeeded();
this.sharedGroupFactory = sharedGroupFactory;

this.maxChunkSize = SETTING_HTTP_MAX_CHUNK_SIZE.get(settings);
this.maxHeaderSize = SETTING_HTTP_MAX_HEADER_SIZE.get(settings);
this.maxInitialLineLength = SETTING_HTTP_MAX_INITIAL_LINE_LENGTH.get(settings);
this.pipeliningMaxEvents = SETTING_PIPELINING_MAX_EVENTS.get(settings);

this.maxCompositeBufferComponents = SETTING_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS.get(settings);
Expand All @@ -172,9 +165,9 @@ public Netty4HttpServerTransport(
logger.debug(
"using max_chunk_size[{}], max_header_size[{}], max_initial_line_length[{}], max_content_length[{}], "
+ "receive_predictor[{}], max_composite_buffer_components[{}], pipelining_max_events[{}]",
maxChunkSize,
maxHeaderSize,
maxInitialLineLength,
SETTING_HTTP_MAX_CHUNK_SIZE.get(settings),
SETTING_HTTP_MAX_HEADER_SIZE.get(settings),
SETTING_HTTP_MAX_INITIAL_LINE_LENGTH.get(settings),
maxContentLength,
receivePredictor,
maxCompositeBufferComponents,
Expand Down Expand Up @@ -297,17 +290,13 @@ public ChannelHandler configureServerChannelHandler() {
protected static class HttpChannelHandler extends ChannelInitializer<Channel> {

private final Netty4HttpServerTransport transport;
private final Netty4HttpRequestCreator requestCreator;
private final Netty4HttpRequestHandler requestHandler;
private final Netty4HttpResponseCreator responseCreator;
private final HttpHandlingSettings handlingSettings;

protected HttpChannelHandler(final Netty4HttpServerTransport transport, final HttpHandlingSettings handlingSettings) {
this.transport = transport;
this.handlingSettings = handlingSettings;
this.requestCreator = new Netty4HttpRequestCreator();
this.requestHandler = new Netty4HttpRequestHandler(transport);
this.responseCreator = new Netty4HttpResponseCreator();
}

@Override
Expand All @@ -331,8 +320,8 @@ protected void initChannel(Channel ch) throws Exception {
if (handlingSettings.isCompression()) {
ch.pipeline().addLast("encoder_compress", new HttpContentCompressor(handlingSettings.getCompressionLevel()));
}
ch.pipeline().addLast("request_creator", requestCreator);
ch.pipeline().addLast("response_creator", responseCreator);
ch.pipeline().addLast("request_creator", Netty4HttpRequestCreator.INSTANCE);
ch.pipeline().addLast("response_creator", Netty4HttpResponseCreator.INSTANCE);
ch.pipeline().addLast("pipelining", new Netty4HttpPipeliningHandler(logger, transport.pipeliningMaxEvents));
ch.pipeline().addLast("handler", requestHandler);
transport.serverAcceptedChannel(nettyHttpChannel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

package org.elasticsearch.transport.netty4;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

@ChannelHandler.Sharable
final class ESLoggingHandler extends LoggingHandler {

ESLoggingHandler() {
static final ESLoggingHandler INSTANCE = new ESLoggingHandler();

private ESLoggingHandler() {
super(LogLevel.TRACE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ protected void initChannel(Channel ch) throws Exception {
assert ch instanceof Netty4NioSocketChannel;
NetUtils.tryEnsureReasonableKeepAliveConfig(((Netty4NioSocketChannel) ch).javaChannel());
ch.pipeline().addLast("byte_buf_sizer", NettyByteBufSizer.INSTANCE);
ch.pipeline().addLast("logging", new ESLoggingHandler());
ch.pipeline().addLast("logging", ESLoggingHandler.INSTANCE);
// using a dot as a prefix means this cannot come from any settings parsed
ch.pipeline().addLast("dispatcher", new Netty4MessageChannelHandler(Netty4Transport.this, recycler));
}
Expand Down Expand Up @@ -362,7 +362,7 @@ protected void initChannel(Channel ch) throws Exception {
Netty4TcpChannel nettyTcpChannel = new Netty4TcpChannel(ch, true, name, ch.newSucceededFuture());
ch.attr(CHANNEL_KEY).set(nettyTcpChannel);
ch.pipeline().addLast("byte_buf_sizer", NettyByteBufSizer.INSTANCE);
ch.pipeline().addLast("logging", new ESLoggingHandler());
ch.pipeline().addLast("logging", ESLoggingHandler.INSTANCE);
ch.pipeline().addLast("dispatcher", new Netty4MessageChannelHandler(Netty4Transport.this, recycler));
serverAcceptedChannel(nettyTcpChannel);
}
Expand Down