4040import io .netty .handler .timeout .ReadTimeoutException ;
4141import io .netty .handler .timeout .ReadTimeoutHandler ;
4242import io .netty .util .AttributeKey ;
43- import org .apache .logging .log4j .message .ParameterizedMessage ;
44- import org .apache .logging .log4j .util .Supplier ;
4543import org .elasticsearch .common .Strings ;
44+ import org .elasticsearch .common .network .CloseableChannel ;
4645import org .elasticsearch .common .network .NetworkAddress ;
4746import org .elasticsearch .common .network .NetworkService ;
4847import org .elasticsearch .common .settings .Setting ;
4948import org .elasticsearch .common .settings .Setting .Property ;
5049import org .elasticsearch .common .settings .Settings ;
51- import org .elasticsearch .common .transport .NetworkExceptionHelper ;
5250import org .elasticsearch .common .transport .TransportAddress ;
5351import org .elasticsearch .common .unit .ByteSizeUnit ;
5452import org .elasticsearch .common .unit .ByteSizeValue ;
5755import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
5856import org .elasticsearch .http .AbstractHttpServerTransport ;
5957import org .elasticsearch .http .BindHttpException ;
58+ import org .elasticsearch .http .HttpChannel ;
6059import org .elasticsearch .http .HttpHandlingSettings ;
6160import org .elasticsearch .http .HttpStats ;
6261import org .elasticsearch .http .netty4 .cors .Netty4CorsConfig ;
6362import org .elasticsearch .http .netty4 .cors .Netty4CorsConfigBuilder ;
6463import org .elasticsearch .http .netty4 .cors .Netty4CorsHandler ;
6564import org .elasticsearch .rest .RestUtils ;
6665import org .elasticsearch .threadpool .ThreadPool ;
67- import org .elasticsearch .transport .netty4 .Netty4OpenChannelsHandler ;
6866import org .elasticsearch .transport .netty4 .Netty4Utils ;
6967
7068import java .io .IOException ;
@@ -171,10 +169,6 @@ public class Netty4HttpServerTransport extends AbstractHttpServerTransport {
171169
172170 protected final List <Channel > serverChannels = new ArrayList <>();
173171
174- // package private for testing
175- Netty4OpenChannelsHandler serverOpenChannels ;
176-
177-
178172 private final Netty4CorsConfig corsConfig ;
179173
180174 public Netty4HttpServerTransport (Settings settings , NetworkService networkService , BigArrays bigArrays , ThreadPool threadPool ,
@@ -216,8 +210,6 @@ public Settings settings() {
216210 protected void doStart () {
217211 boolean success = false ;
218212 try {
219- this .serverOpenChannels = new Netty4OpenChannelsHandler (logger );
220-
221213 serverBootstrap = new ServerBootstrap ();
222214
223215 serverBootstrap .group (new NioEventLoopGroup (workerCount , daemonThreadFactory (settings ,
@@ -281,10 +273,9 @@ static Netty4CorsConfig buildCorsConfig(Settings settings) {
281273 builder .allowCredentials ();
282274 }
283275 String [] strMethods = Strings .tokenizeToStringArray (SETTING_CORS_ALLOW_METHODS .get (settings ), "," );
284- HttpMethod [] methods = Arrays .asList (strMethods )
285- .stream ()
276+ HttpMethod [] methods = Arrays .stream (strMethods )
286277 .map (HttpMethod ::valueOf )
287- .toArray (size -> new HttpMethod [size ] );
278+ .toArray (HttpMethod []:: new );
288279 return builder .allowedRequestMethods (methods )
289280 .maxAge (SETTING_CORS_MAX_AGE .get (settings ))
290281 .allowedRequestHeaders (Strings .tokenizeToStringArray (SETTING_CORS_ALLOW_HEADERS .get (settings ), "," ))
@@ -327,15 +318,21 @@ protected void doStop() {
327318 Netty4Utils .closeChannels (serverChannels );
328319 } catch (IOException e ) {
329320 logger .trace ("exception while closing channels" , e );
321+ } finally {
322+ serverChannels .clear ();
330323 }
331- serverChannels .clear ();
332324 }
333325 }
334326
335- if (serverOpenChannels != null ) {
336- serverOpenChannels .close ();
337- serverOpenChannels = null ;
327+ // TODO: Move all of channel closing to abstract class once server channels are handled
328+ try {
329+ CloseableChannel .closeChannels (new ArrayList <>(httpChannels ), true );
330+ } catch (Exception e ) {
331+ logger .warn ("unexpected exception while closing http channels" , e );
338332 }
333+ httpChannels .clear ();
334+
335+
339336
340337 if (serverBootstrap != null ) {
341338 serverBootstrap .config ().group ().shutdownGracefully (0 , 5 , TimeUnit .SECONDS ).awaitUninterruptibly ();
@@ -349,38 +346,18 @@ protected void doClose() {
349346
350347 @ Override
351348 public HttpStats stats () {
352- Netty4OpenChannelsHandler channels = serverOpenChannels ;
353- return new HttpStats (channels == null ? 0 : channels .numberOfOpenChannels (), channels == null ? 0 : channels .totalChannels ());
349+ return new HttpStats (httpChannels .size (), totalChannelsAccepted .get ());
354350 }
355351
356- public Netty4CorsConfig getCorsConfig () {
357- return corsConfig ;
358- }
359-
360- protected void exceptionCaught (ChannelHandlerContext ctx , Throwable cause ) throws Exception {
352+ @ Override
353+ protected void onException (HttpChannel channel , Exception cause ) {
361354 if (cause instanceof ReadTimeoutException ) {
362355 if (logger .isTraceEnabled ()) {
363- logger .trace ("Read timeout [{}] " , ctx . channel (). remoteAddress () );
356+ logger .trace ("Http read timeout {} " , channel );
364357 }
365- ctx . channel (). close () ;
358+ CloseableChannel . closeChannel ( channel ); ;
366359 } else {
367- if (!lifecycle .started ()) {
368- // ignore
369- return ;
370- }
371- if (!NetworkExceptionHelper .isCloseConnectionException (cause )) {
372- logger .warn (
373- (Supplier <?>) () -> new ParameterizedMessage (
374- "caught exception while handling client http traffic, closing connection {}" , ctx .channel ()),
375- cause );
376- ctx .channel ().close ();
377- } else {
378- logger .debug (
379- (Supplier <?>) () -> new ParameterizedMessage (
380- "caught exception while handling client http traffic, closing connection {}" , ctx .channel ()),
381- cause );
382- ctx .channel ().close ();
383- }
360+ super .onException (channel , cause );
384361 }
385362 }
386363
@@ -404,9 +381,8 @@ protected HttpChannelHandler(final Netty4HttpServerTransport transport, final Ht
404381
405382 @ Override
406383 protected void initChannel (Channel ch ) throws Exception {
407- Netty4HttpChannel nettyTcpChannel = new Netty4HttpChannel (ch );
408- ch .attr (HTTP_CHANNEL_KEY ).set (nettyTcpChannel );
409- ch .pipeline ().addLast ("openChannels" , transport .serverOpenChannels );
384+ Netty4HttpChannel nettyHttpChannel = new Netty4HttpChannel (ch );
385+ ch .attr (HTTP_CHANNEL_KEY ).set (nettyHttpChannel );
410386 ch .pipeline ().addLast ("read_timeout" , new ReadTimeoutHandler (transport .readTimeoutMillis , TimeUnit .MILLISECONDS ));
411387 final HttpRequestDecoder decoder = new HttpRequestDecoder (
412388 handlingSettings .getMaxInitialLineLength (),
@@ -423,10 +399,11 @@ protected void initChannel(Channel ch) throws Exception {
423399 ch .pipeline ().addLast ("encoder_compress" , new HttpContentCompressor (handlingSettings .getCompressionLevel ()));
424400 }
425401 if (handlingSettings .isCorsEnabled ()) {
426- ch .pipeline ().addLast ("cors" , new Netty4CorsHandler (transport .getCorsConfig () ));
402+ ch .pipeline ().addLast ("cors" , new Netty4CorsHandler (transport .corsConfig ));
427403 }
428404 ch .pipeline ().addLast ("pipelining" , new Netty4HttpPipeliningHandler (transport .logger , transport .pipeliningMaxEvents ));
429405 ch .pipeline ().addLast ("handler" , requestHandler );
406+ transport .serverAcceptedChannel (nettyHttpChannel );
430407 }
431408
432409 @ Override
0 commit comments