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

import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;

import java.io.IOException;
import java.util.Objects;

public class BulkItemRequest implements Streamable {

    private int id;
    private DocWriteRequest request;
    private volatile BulkItemResponse primaryResponse;

    BulkItemRequest() {

    }

    // NOTE: public for testing only
    public BulkItemRequest(int id, DocWriteRequest request) {
        this.id = id;
        this.request = request;
    }

    public int id() {
        return id;
    }

    public DocWriteRequest request() {
        return request;
    }

    public String index() {
        assert request.indices().length == 1;
        return request.indices()[0];
    }

    BulkItemResponse getPrimaryResponse() {
        return primaryResponse;
    }

    void setPrimaryResponse(BulkItemResponse primaryResponse) {
        this.primaryResponse = primaryResponse;
    }

    /**
     * Abort this request, and store a {@link org.elasticsearch.action.bulk.BulkItemResponse.Failure} response.
     *
     * @param index The concrete index that was resolved for this request
     * @param cause The cause of the rejection (may not be null)
     * @throws IllegalStateException If a response already exists for this request
     */
    public void abort(String index, Exception cause) {
        if (primaryResponse != null) {
            assert false : "Response already exists " + primaryResponse.status();
            throw new IllegalStateException("Item already has a response (status=" + primaryResponse.status() + ")");
        }
        final BulkItemResponse.Failure failure = new BulkItemResponse.Failure(index, request.type(), request.id(),
            Objects.requireNonNull(cause), true);
        setPrimaryResponse(new BulkItemResponse(id, request.opType(), failure));
    }

    public static BulkItemRequest readBulkItem(StreamInput in) throws IOException {
        BulkItemRequest item = new BulkItemRequest();
        item.readFrom(in);
        return item;
    }

    @Override
    public void readFrom(StreamInput in) throws IOException {
        id = in.readVInt();
        request = DocWriteRequest.readDocumentRequest(in);
        if (in.readBoolean()) {
            primaryResponse = BulkItemResponse.readBulkItem(in);
        }
    }

    @Override
    public void writeTo(StreamOutput out) throws IOException {
        out.writeVInt(id);
        DocWriteRequest.writeDocumentRequest(out, request);
        out.writeOptionalStreamable(primaryResponse);
    }
}

@@ -2334,9 +2334,9 @@
 nse 
-!
+=
 = nu
@@ -2357,194 +2357,446 @@
     
-assert false : %22Response already exists %22 + primaryResponse.status();%0A            throw new IllegalStateException(%22Item already has a response (status=%22 + primaryResponse.status()
+final BulkItemResponse.Failure failure = new BulkItemResponse.Failure(index, request.type(), request.id(),%0A                    Objects.requireNonNull(cause), true);%0A            setPrimaryResponse(new BulkItemResponse(id, request.opType(), failure));%0A        %7D else %7B%0A            assert primaryResponse.isFailed() && primaryResponse.getFailure().isAborted()%0A                    : %22response %5B%22 + primaryResponse + %22%5D; cause %5B%22 + cause
  + %22
-)%22)
+%5D%22
 ;%0A  
@@ -2805,32 +2805,53 @@
     
-%7D%0A
     
-    final BulkItem
+if (primaryResponse.isFailed() && primary
 Resp
@@ -2859,38 +2859,59 @@
 nse.
+get
 Failure
- failure = new BulkItem
+().isAborted()) %7B%0A                primary
 Resp
@@ -2911,32 +2911,35 @@
 aryResponse.
+get
 Failure(
 index, reque
@@ -2930,175 +2930,260 @@
 ure(
-index, request.type(), request.id(),%0A            Objects.requireNonNull(cause), true);%0A        setPrimaryResponse(new BulkItemResponse(id, request.opType(), failure));
+).getCause().addSuppressed(cause);%0A            %7D else %7B%0A                throw new IllegalStateException(%0A                        %22aborting item that with response %5B%22 + primaryResponse + %22%5D that was previously processed%22, cause);%0A            %7D%0A        %7D
 %0A   

Allow double aborts on bulk item requests