Correctly parse BulkItemResponse.Failure's status (#23432)

Today the status is lost when parsing back a BulkItemResponse.Failure. This commit changes the BulkItemResponse.Failure parsing method so that it correctly instantiates a failure with the parsed status instead of realying on the parsed ElasticsearchException (that always return an internal server error status).
This commit is contained in:
Tanguy Leroux 2017-03-02 12:37:07 +01:00 committed by GitHub
parent 22870cc6c0
commit 15c936ec02
3 changed files with 21 additions and 8 deletions

View File

@ -588,10 +588,10 @@ public class CrudIT extends ESRestHighLevelClientTestCase {
DocWriteRequest.OpType requestOpType = bulkRequest.requests().get(i).opType(); DocWriteRequest.OpType requestOpType = bulkRequest.requests().get(i).opType();
if (requestOpType == DocWriteRequest.OpType.INDEX || requestOpType == DocWriteRequest.OpType.CREATE) { if (requestOpType == DocWriteRequest.OpType.INDEX || requestOpType == DocWriteRequest.OpType.CREATE) {
assertEquals(errors[i], bulkItemResponse.isFailed()); assertEquals(errors[i], bulkItemResponse.isFailed());
assertEquals(errors[i] ? RestStatus.INTERNAL_SERVER_ERROR : RestStatus.CREATED, bulkItemResponse.status()); assertEquals(errors[i] ? RestStatus.CONFLICT : RestStatus.CREATED, bulkItemResponse.status());
} else if (requestOpType == DocWriteRequest.OpType.UPDATE) { } else if (requestOpType == DocWriteRequest.OpType.UPDATE) {
assertEquals(errors[i], bulkItemResponse.isFailed()); assertEquals(errors[i], bulkItemResponse.isFailed());
assertEquals(errors[i] ? RestStatus.INTERNAL_SERVER_ERROR : RestStatus.OK, bulkItemResponse.status()); assertEquals(errors[i] ? RestStatus.NOT_FOUND : RestStatus.OK, bulkItemResponse.status());
} else if (requestOpType == DocWriteRequest.OpType.DELETE) { } else if (requestOpType == DocWriteRequest.OpType.DELETE) {
assertFalse(bulkItemResponse.isFailed()); assertFalse(bulkItemResponse.isFailed());
assertEquals(errors[i] ? RestStatus.NOT_FOUND : RestStatus.OK, bulkItemResponse.status()); assertEquals(errors[i] ? RestStatus.NOT_FOUND : RestStatus.OK, bulkItemResponse.status());

View File

@ -122,6 +122,7 @@ public class BulkItemResponse implements Streamable, StatusToXContentObject {
throwUnknownField(currentFieldName, parser.getTokenLocation()); throwUnknownField(currentFieldName, parser.getTokenLocation());
} }
RestStatus status = null;
ElasticsearchException exception = null; ElasticsearchException exception = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
@ -132,7 +133,11 @@ public class BulkItemResponse implements Streamable, StatusToXContentObject {
if (token == XContentParser.Token.START_OBJECT) { if (token == XContentParser.Token.START_OBJECT) {
exception = ElasticsearchException.fromXContent(parser); exception = ElasticsearchException.fromXContent(parser);
} }
} else if (STATUS.equals(currentFieldName) == false) { } else if (STATUS.equals(currentFieldName)) {
if (token == XContentParser.Token.VALUE_NUMBER) {
status = RestStatus.fromCode(parser.intValue());
}
} else {
itemParser.accept(parser); itemParser.accept(parser);
} }
} }
@ -143,7 +148,7 @@ public class BulkItemResponse implements Streamable, StatusToXContentObject {
BulkItemResponse bulkItemResponse; BulkItemResponse bulkItemResponse;
if (exception != null) { if (exception != null) {
Failure failure = new Failure(builder.getShardId().getIndexName(), builder.getType(), builder.getId(), exception); Failure failure = new Failure(builder.getShardId().getIndexName(), builder.getType(), builder.getId(), exception, status);
bulkItemResponse = new BulkItemResponse(id, opType, failure); bulkItemResponse = new BulkItemResponse(id, opType, failure);
} else { } else {
bulkItemResponse = new BulkItemResponse(id, opType, builder.build()); bulkItemResponse = new BulkItemResponse(id, opType, builder.build());
@ -167,12 +172,16 @@ public class BulkItemResponse implements Streamable, StatusToXContentObject {
private final Exception cause; private final Exception cause;
private final RestStatus status; private final RestStatus status;
public Failure(String index, String type, String id, Exception cause) { Failure(String index, String type, String id, Exception cause, RestStatus status) {
this.index = index; this.index = index;
this.type = type; this.type = type;
this.id = id; this.id = id;
this.cause = cause; this.cause = cause;
this.status = ExceptionsHelper.status(cause); this.status = status;
}
public Failure(String index, String type, String id, Exception cause) {
this(index, type, id, cause, ExceptionsHelper.status(cause));
} }
/** /**

View File

@ -20,6 +20,7 @@
package org.elasticsearch.action.bulk; package org.elasticsearch.action.bulk;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse.Failure; import org.elasticsearch.action.bulk.BulkItemResponse.Failure;
@ -99,8 +100,11 @@ public class BulkItemResponseTests extends ESTestCase {
final Tuple<Throwable, ElasticsearchException> exceptions = randomExceptions(); final Tuple<Throwable, ElasticsearchException> exceptions = randomExceptions();
BulkItemResponse bulkItemResponse = new BulkItemResponse(itemId, opType, new Failure(index, type, id, (Exception) exceptions.v1())); Exception bulkItemCause = (Exception) exceptions.v1();
BulkItemResponse expectedBulkItemResponse = new BulkItemResponse(itemId, opType, new Failure(index, type, id, exceptions.v2())); Failure bulkItemFailure = new Failure(index, type, id, bulkItemCause);
BulkItemResponse bulkItemResponse = new BulkItemResponse(itemId, opType, bulkItemFailure);
Failure expectedBulkItemFailure = new Failure(index, type, id, exceptions.v2(), ExceptionsHelper.status(bulkItemCause));
BulkItemResponse expectedBulkItemResponse = new BulkItemResponse(itemId, opType, expectedBulkItemFailure);
BytesReference originalBytes = toXContent(bulkItemResponse, xContentType, randomBoolean()); BytesReference originalBytes = toXContent(bulkItemResponse, xContentType, randomBoolean());
// Shuffle the XContent fields // Shuffle the XContent fields