-
Notifications
You must be signed in to change notification settings - Fork 24.6k
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
Introduce http and tcp server channels #31446
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
59b9f40
Add tcp server channel
Tim-Brooks afc3477
Work on http
Tim-Brooks ad94ce2
Merge remote-tracking branch 'upstream/master' into add_server_channels
Tim-Brooks 6a7f258
Cleanup http work
Tim-Brooks 56a09ad
Cleanup
Tim-Brooks 2685601
Fix exception handler issues
Tim-Brooks cd915dc
Fix test
Tim-Brooks 4875cae
Review changes
Tim-Brooks c7640d3
Fix unused import
Tim-Brooks 4fdd832
Merge remote-tracking branch 'upstream/master' into add_server_channels
Tim-Brooks a4c5a87
Merge branch 'master' into add_server_channels
Tim-Brooks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.http.netty4; | ||
|
||
import io.netty.channel.Channel; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.common.concurrent.CompletableContext; | ||
import org.elasticsearch.http.HttpServerChannel; | ||
import org.elasticsearch.transport.netty4.Netty4Utils; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
public class Netty4HttpServerChannel implements HttpServerChannel { | ||
|
||
private final Channel channel; | ||
private final CompletableContext<Void> closeContext = new CompletableContext<>(); | ||
|
||
Netty4HttpServerChannel(Channel channel) { | ||
this.channel = channel; | ||
this.channel.closeFuture().addListener(f -> { | ||
if (f.isSuccess()) { | ||
closeContext.complete(null); | ||
} else { | ||
Throwable cause = f.cause(); | ||
if (cause instanceof Error) { | ||
Netty4Utils.maybeDie(cause); | ||
closeContext.completeExceptionally(new Exception(cause)); | ||
} else { | ||
closeContext.completeExceptionally((Exception) cause); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public InetSocketAddress getLocalAddress() { | ||
return (InetSocketAddress) channel.localAddress(); | ||
} | ||
|
||
@Override | ||
public void addCloseListener(ActionListener<Void> listener) { | ||
closeContext.addListener(ActionListener.toBiConsumer(listener)); | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return channel.isOpen(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
channel.close(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Netty4HttpChannel{localAddress=" + getLocalAddress() + "}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerAdapter; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelOption; | ||
|
@@ -42,37 +43,29 @@ | |
import io.netty.util.AttributeKey; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.network.CloseableChannel; | ||
import org.elasticsearch.common.network.NetworkAddress; | ||
import org.elasticsearch.common.network.NetworkService; | ||
import org.elasticsearch.common.settings.Setting; | ||
import org.elasticsearch.common.settings.Setting.Property; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.transport.TransportAddress; | ||
import org.elasticsearch.common.unit.ByteSizeUnit; | ||
import org.elasticsearch.common.unit.ByteSizeValue; | ||
import org.elasticsearch.common.util.BigArrays; | ||
import org.elasticsearch.common.util.concurrent.EsExecutors; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.http.AbstractHttpServerTransport; | ||
import org.elasticsearch.http.BindHttpException; | ||
import org.elasticsearch.http.HttpChannel; | ||
import org.elasticsearch.http.HttpHandlingSettings; | ||
import org.elasticsearch.http.HttpStats; | ||
import org.elasticsearch.http.HttpServerChannel; | ||
import org.elasticsearch.http.netty4.cors.Netty4CorsConfig; | ||
import org.elasticsearch.http.netty4.cors.Netty4CorsConfigBuilder; | ||
import org.elasticsearch.http.netty4.cors.Netty4CorsHandler; | ||
import org.elasticsearch.rest.RestUtils; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.transport.netty4.Netty4Utils; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.elasticsearch.common.util.concurrent.EsExecutors.daemonThreadFactory; | ||
|
@@ -154,21 +147,13 @@ public class Netty4HttpServerTransport extends AbstractHttpServerTransport { | |
|
||
private final int pipeliningMaxEvents; | ||
|
||
private final boolean tcpNoDelay; | ||
private final boolean tcpKeepAlive; | ||
private final boolean reuseAddress; | ||
|
||
private final ByteSizeValue tcpSendBufferSize; | ||
private final ByteSizeValue tcpReceiveBufferSize; | ||
private final RecvByteBufAllocator recvByteBufAllocator; | ||
private final int readTimeoutMillis; | ||
|
||
private final int maxCompositeBufferComponents; | ||
|
||
protected volatile ServerBootstrap serverBootstrap; | ||
|
||
protected final List<Channel> serverChannels = new ArrayList<>(); | ||
|
||
private final Netty4CorsConfig corsConfig; | ||
|
||
public Netty4HttpServerTransport(Settings settings, NetworkService networkService, BigArrays bigArrays, ThreadPool threadPool, | ||
|
@@ -184,11 +169,6 @@ public Netty4HttpServerTransport(Settings settings, NetworkService networkServic | |
this.maxCompositeBufferComponents = SETTING_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS.get(settings); | ||
this.workerCount = SETTING_HTTP_WORKER_COUNT.get(settings); | ||
|
||
this.tcpNoDelay = SETTING_HTTP_TCP_NO_DELAY.get(settings); | ||
this.tcpKeepAlive = SETTING_HTTP_TCP_KEEP_ALIVE.get(settings); | ||
this.reuseAddress = SETTING_HTTP_TCP_REUSE_ADDRESS.get(settings); | ||
this.tcpSendBufferSize = SETTING_HTTP_TCP_SEND_BUFFER_SIZE.get(settings); | ||
this.tcpReceiveBufferSize = SETTING_HTTP_TCP_RECEIVE_BUFFER_SIZE.get(settings); | ||
this.readTimeoutMillis = Math.toIntExact(SETTING_HTTP_READ_TIMEOUT.get(settings).getMillis()); | ||
|
||
ByteSizeValue receivePredictor = SETTING_HTTP_NETTY_RECEIVE_PREDICTOR_SIZE.get(settings); | ||
|
@@ -217,6 +197,7 @@ protected void doStart() { | |
serverBootstrap.channel(NioServerSocketChannel.class); | ||
|
||
serverBootstrap.childHandler(configureServerChannelHandler()); | ||
serverBootstrap.handler(new ServerChannelExceptionHandler()); | ||
|
||
serverBootstrap.childOption(ChannelOption.TCP_NODELAY, SETTING_HTTP_TCP_NO_DELAY.get(settings)); | ||
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, SETTING_HTTP_TCP_KEEP_ALIVE.get(settings)); | ||
|
@@ -238,10 +219,7 @@ protected void doStart() { | |
serverBootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress); | ||
serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, reuseAddress); | ||
|
||
this.boundAddress = createBoundHttpAddress(); | ||
if (logger.isInfoEnabled()) { | ||
logger.info("{}", boundAddress); | ||
} | ||
bindServer(); | ||
success = true; | ||
} finally { | ||
if (success == false) { | ||
|
@@ -284,78 +262,30 @@ static Netty4CorsConfig buildCorsConfig(Settings settings) { | |
} | ||
|
||
@Override | ||
protected TransportAddress bindAddress(final InetAddress hostAddress) { | ||
final AtomicReference<Exception> lastException = new AtomicReference<>(); | ||
final AtomicReference<InetSocketAddress> boundSocket = new AtomicReference<>(); | ||
boolean success = port.iterate(portNumber -> { | ||
try { | ||
synchronized (serverChannels) { | ||
ChannelFuture future = serverBootstrap.bind(new InetSocketAddress(hostAddress, portNumber)).sync(); | ||
serverChannels.add(future.channel()); | ||
boundSocket.set((InetSocketAddress) future.channel().localAddress()); | ||
} | ||
} catch (Exception e) { | ||
lastException.set(e); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
if (!success) { | ||
throw new BindHttpException("Failed to bind to [" + port.getPortRangeString() + "]", lastException.get()); | ||
} | ||
|
||
if (logger.isDebugEnabled()) { | ||
logger.debug("Bound http to address {{}}", NetworkAddress.format(boundSocket.get())); | ||
} | ||
return new TransportAddress(boundSocket.get()); | ||
protected HttpServerChannel bind(InetSocketAddress socketAddress) throws Exception { | ||
ChannelFuture future = serverBootstrap.bind(socketAddress).sync(); | ||
Channel channel = future.channel(); | ||
Netty4HttpServerChannel httpServerChannel = new Netty4HttpServerChannel(channel); | ||
channel.attr(HTTP_SERVER_CHANNEL_KEY).set(httpServerChannel); | ||
return httpServerChannel; | ||
} | ||
|
||
@Override | ||
protected void doStop() { | ||
synchronized (serverChannels) { | ||
if (!serverChannels.isEmpty()) { | ||
try { | ||
Netty4Utils.closeChannels(serverChannels); | ||
} catch (IOException e) { | ||
logger.trace("exception while closing channels", e); | ||
} finally { | ||
serverChannels.clear(); | ||
} | ||
} | ||
} | ||
|
||
// TODO: Move all of channel closing to abstract class once server channels are handled | ||
try { | ||
CloseableChannel.closeChannels(new ArrayList<>(httpChannels), true); | ||
} catch (Exception e) { | ||
logger.warn("unexpected exception while closing http channels", e); | ||
} | ||
httpChannels.clear(); | ||
|
||
|
||
|
||
protected void stopInternal() { | ||
if (serverBootstrap != null) { | ||
serverBootstrap.config().group().shutdownGracefully(0, 5, TimeUnit.SECONDS).awaitUninterruptibly(); | ||
serverBootstrap = null; | ||
} | ||
} | ||
|
||
@Override | ||
protected void doClose() { | ||
} | ||
|
||
@Override | ||
public HttpStats stats() { | ||
return new HttpStats(httpChannels.size(), totalChannelsAccepted.get()); | ||
} | ||
|
||
@Override | ||
protected void onException(HttpChannel channel, Exception cause) { | ||
if (cause instanceof ReadTimeoutException) { | ||
if (logger.isTraceEnabled()) { | ||
logger.trace("Http read timeout {}", channel); | ||
} | ||
CloseableChannel.closeChannel(channel);; | ||
CloseableChannel.closeChannel(channel); | ||
; | ||
} else { | ||
super.onException(channel, cause); | ||
} | ||
|
@@ -366,6 +296,7 @@ public ChannelHandler configureServerChannelHandler() { | |
} | ||
|
||
static final AttributeKey<Netty4HttpChannel> HTTP_CHANNEL_KEY = AttributeKey.newInstance("es-http-channel"); | ||
static final AttributeKey<Netty4HttpServerChannel> HTTP_SERVER_CHANNEL_KEY = AttributeKey.newInstance("es-http-server-channel"); | ||
|
||
protected static class HttpChannelHandler extends ChannelInitializer<Channel> { | ||
|
||
|
@@ -413,4 +344,18 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E | |
} | ||
} | ||
|
||
@ChannelHandler.Sharable | ||
private class ServerChannelExceptionHandler extends ChannelHandlerAdapter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be static? |
||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
Netty4Utils.maybeDie(cause); | ||
Netty4HttpServerChannel httpServerChannel = ctx.channel().attr(HTTP_SERVER_CHANNEL_KEY).get(); | ||
if (cause instanceof Error) { | ||
onServerException(httpServerChannel, new Exception(cause)); | ||
} else { | ||
onServerException(httpServerChannel, (Exception) cause); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary line?