forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make http pipelining support mandatory (elastic#30695)
This is related to elastic#29500 and elastic#28898. This commit removes the abilitiy to disable http pipelining. After this commit, any elasticsearch node will support pipelined requests from a client. Additionally, it extracts some of the http pipelining work to the server module. This extracted work is used to implement pipelining for the nio plugin.
- Loading branch information
1 parent
37f67d9
commit 31251c9
Showing
33 changed files
with
990 additions
and
647 deletions.
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
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
102 changes: 102 additions & 0 deletions
102
...sport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandler.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,102 @@ | ||
/* | ||
* 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.ChannelDuplexHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPromise; | ||
import io.netty.handler.codec.http.LastHttpContent; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.common.collect.Tuple; | ||
import org.elasticsearch.http.HttpPipelinedRequest; | ||
import org.elasticsearch.http.HttpPipeliningAggregator; | ||
import org.elasticsearch.transport.netty4.Netty4Utils; | ||
|
||
import java.nio.channels.ClosedChannelException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Implements HTTP pipelining ordering, ensuring that responses are completely served in the same order as their corresponding requests. | ||
*/ | ||
public class Netty4HttpPipeliningHandler extends ChannelDuplexHandler { | ||
|
||
private final Logger logger; | ||
private final HttpPipeliningAggregator<Netty4HttpResponse, ChannelPromise> aggregator; | ||
|
||
/** | ||
* Construct a new pipelining handler; this handler should be used downstream of HTTP decoding/aggregation. | ||
* | ||
* @param logger for logging unexpected errors | ||
* @param maxEventsHeld the maximum number of channel events that will be retained prior to aborting the channel connection; this is | ||
* required as events cannot queue up indefinitely | ||
*/ | ||
public Netty4HttpPipeliningHandler(Logger logger, final int maxEventsHeld) { | ||
this.logger = logger; | ||
this.aggregator = new HttpPipeliningAggregator<>(maxEventsHeld); | ||
} | ||
|
||
@Override | ||
public void channelRead(final ChannelHandlerContext ctx, final Object msg) { | ||
if (msg instanceof LastHttpContent) { | ||
HttpPipelinedRequest<LastHttpContent> pipelinedRequest = aggregator.read(((LastHttpContent) msg).retain()); | ||
ctx.fireChannelRead(pipelinedRequest); | ||
} else { | ||
ctx.fireChannelRead(msg); | ||
} | ||
} | ||
|
||
@Override | ||
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) { | ||
assert msg instanceof Netty4HttpResponse : "Message must be type: " + Netty4HttpResponse.class; | ||
Netty4HttpResponse response = (Netty4HttpResponse) msg; | ||
boolean success = false; | ||
try { | ||
List<Tuple<Netty4HttpResponse, ChannelPromise>> readyResponses = aggregator.write(response, promise); | ||
for (Tuple<Netty4HttpResponse, ChannelPromise> readyResponse : readyResponses) { | ||
ctx.write(readyResponse.v1().getResponse(), readyResponse.v2()); | ||
} | ||
success = true; | ||
} catch (IllegalStateException e) { | ||
ctx.channel().close(); | ||
} finally { | ||
if (success == false) { | ||
promise.setFailure(new ClosedChannelException()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close(ChannelHandlerContext ctx, ChannelPromise promise) { | ||
List<Tuple<Netty4HttpResponse, ChannelPromise>> inflightResponses = aggregator.removeAllInflightResponses(); | ||
|
||
if (inflightResponses.isEmpty() == false) { | ||
ClosedChannelException closedChannelException = new ClosedChannelException(); | ||
for (Tuple<Netty4HttpResponse, ChannelPromise> inflightResponse : inflightResponses) { | ||
try { | ||
inflightResponse.v2().setFailure(closedChannelException); | ||
} catch (RuntimeException e) { | ||
logger.error("unexpected error while releasing pipelined http responses", e); | ||
} | ||
} | ||
} | ||
ctx.close(promise); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponse.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,37 @@ | ||
/* | ||
* 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.handler.codec.http.FullHttpResponse; | ||
import org.elasticsearch.http.HttpPipelinedMessage; | ||
|
||
public class Netty4HttpResponse extends HttpPipelinedMessage { | ||
|
||
private final FullHttpResponse response; | ||
|
||
public Netty4HttpResponse(int sequence, FullHttpResponse response) { | ||
super(sequence); | ||
this.response = response; | ||
} | ||
|
||
public FullHttpResponse getResponse() { | ||
return response; | ||
} | ||
} |
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.