
/*
 * 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.pipelining;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.LastHttpContent;
import org.elasticsearch.transport.netty4.Netty4Utils;

import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Implements HTTP pipelining ordering, ensuring that responses are completely served in the same order as their
 * corresponding requests. NOTE: A side effect of using this handler is that upstream HttpRequest objects will
 * cause the original message event to be effectively transformed into an OrderedUpstreamMessageEvent. Conversely
 * OrderedDownstreamChannelEvent objects are expected to be received for the correlating response objects.
 */
public class HttpPipeliningHandler extends ChannelDuplexHandler {

    private static final int INITIAL_EVENTS_HELD = 3;

    private final int maxEventsHeld;

    private int readSequence;
    private int writeSequence;

    private final Queue<HttpPipelinedResponse> holdingQueue;

    /**
     * @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; we would run out of
     *                      memory if this was the case.
     */
    public HttpPipeliningHandler(final int maxEventsHeld) {
        this.maxEventsHeld = maxEventsHeld;
        this.holdingQueue = new PriorityQueue<>(INITIAL_EVENTS_HELD);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof LastHttpContent) {
            ctx.fireChannelRead(new HttpPipelinedRequest(((LastHttpContent) msg).retain(), readSequence++));
        } else {
            ctx.fireChannelRead(msg);
        }
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (msg instanceof HttpPipelinedResponse) {
            boolean channelShouldClose = false;

            synchronized (holdingQueue) {
                if (holdingQueue.size() < maxEventsHeld) {
                    holdingQueue.add((HttpPipelinedResponse) msg);

                    while (!holdingQueue.isEmpty()) {
                        final HttpPipelinedResponse response = holdingQueue.peek();
                        if (response.sequence() != writeSequence) {
                            break;
                        }
                        holdingQueue.remove();
                        ctx.write(response.response(), promise);
                        writeSequence++;
                    }
                } else {
                    channelShouldClose = true;
                }
            }

            if (channelShouldClose) {
                try {
                    Netty4Utils.closeChannels(Collections.singletonList(ctx.channel()));
                } finally {
                    ((HttpPipelinedResponse) msg).release();
                    promise.setSuccess();
                }
            }
        } else {
            ctx.write(msg, promise);
        }
    }

}

@@ -1,9 +1,8 @@
-%0A
 /*%0A * Li
@@ -1140,32 +1140,8 @@
 eue;
-%0Aimport java.util.Queue;
 %0A%0A/*
@@ -1254,19 +1254,16 @@
 as their
-%0A *
  corresp
@@ -1282,477 +1282,547 @@
 sts.
- NOTE: A side effect of using thi
+%0A */%0Apublic class HttpPipeliningHandler extend
 s 
+C
 han
-dler is that upstream HttpRequest objects will%0A * cause the original message event to be effectively transformed into an OrderedUpstreamMessageEvent. Conversely%0A * OrderedDownstreamChannelEvent objects are expected to be received for the correlating response objects.%0A */%0Apublic class HttpPipeliningHandler extends ChannelDuplexHandler %7B%0A%0A    private static final int INITIAL_EVENTS_HELD = 3;%0A%0A    private final int maxEventsHeld;%0A
+nelDuplexHandler %7B%0A%0A    private static final int INITIAL_EVENTS_HELD = 8;%0A%0A    private final int maxEventsHeld;%0A%0A    /*%0A     * The current read and write sequence numbers. Read sequence numbers are attached to requests in the order they are read from the%0A     * channel, and then transferred to responses. A response is not written to the channel context until its sequence number matches the%0A     * current write sequence, implying that all preceding messages have been written.%0A     */
 %0A   
@@ -1876,24 +1876,110 @@
 eSequence;%0A%0A
+    // we use a priority queue so that responses are ordered by their sequence number%0A
     private 
@@ -1984,16 +1984,24 @@
 e final 
+Priority
 Queue%3CHt
@@ -2044,16 +2044,135 @@
     /**%0A
+     * Construct a new pipelining handler; this handler should be used downstream of HTTP decoding/aggregation.%0A     *%0A
      * @
@@ -2279,16 +2279,36 @@
  channel
+ connection; this is
 %0A     * 
@@ -2332,28 +2332,8 @@
     
-connection. This is 
 requ
@@ -2379,86 +2379,8 @@
 tely
-; we would run out of%0A     *                      memory if this was the case.
 %0A   
@@ -2607,16 +2607,22 @@
 nelRead(
+final 
 ChannelH
@@ -2631,32 +2631,38 @@
 dlerContext ctx,
+ final
  Object msg) thr
@@ -2940,16 +2940,22 @@
 d write(
+final 
 ChannelH
@@ -2972,16 +2972,22 @@
 ext ctx,
+ final
  Object 
@@ -2990,16 +2990,22 @@
 ect msg,
+ final
  Channel
@@ -3360,24 +3360,347 @@
 sEmpty()) %7B%0A
+                        /*%0A                         * Since the response with the lowest sequence number is the top of the priority queue, we know if its sequence%0A                         * number does not match the current write sequence then we have not processed all preceding responses yet.%0A                         */%0A
             

Add comments to HttpPipeliningHandler