return an HTTP code representing the error when a suggest request failed instead of 200

This commit is contained in:
olivier bourgain 2015-03-16 11:54:56 +01:00 committed by Areek Zillur
parent 6e9eea9b98
commit deade2eb71
5 changed files with 41 additions and 59 deletions

View File

@ -19,16 +19,14 @@
package org.elasticsearch.action.count;
import org.elasticsearch.Version;
import java.io.IOException;
import java.util.List;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.rest.RestStatus;
import java.io.IOException;
import java.util.List;
/**
* The response of the count action.
*/
@ -62,24 +60,7 @@ public class CountResponse extends BroadcastOperationResponse {
}
public RestStatus status() {
if (getFailedShards() == 0) {
if (getSuccessfulShards() == 0 && getTotalShards() > 0) {
return RestStatus.SERVICE_UNAVAILABLE;
}
return RestStatus.OK;
}
// if total failure, bubble up the status code to the response level
if (getSuccessfulShards() == 0 && getTotalShards() > 0) {
RestStatus status = RestStatus.OK;
for (ShardOperationFailedException shardFailure : getShardFailures()) {
RestStatus shardStatus = shardFailure.status();
if (shardStatus.getStatus() >= status.getStatus()) {
status = shardStatus;
}
}
return status;
}
return RestStatus.OK;
return RestStatus.status(getSuccessfulShards(), getTotalShards(), getShardFailures());
}
@Override

View File

@ -69,24 +69,7 @@ public class SearchResponse extends ActionResponse implements StatusToXContent {
@Override
public RestStatus status() {
if (shardFailures.length == 0) {
if (successfulShards == 0 && totalShards > 0) {
return RestStatus.SERVICE_UNAVAILABLE;
}
return RestStatus.OK;
}
// if total failure, bubble up the status code to the response level
if (successfulShards == 0 && totalShards > 0) {
RestStatus status = RestStatus.OK;
for (int i = 0; i < shardFailures.length; i++) {
RestStatus shardStatus = shardFailures[i].status();
if (shardStatus.getStatus() >= status.getStatus()) {
status = shardFailures[i].status();
}
}
return status;
}
return RestStatus.OK;
return RestStatus.status(successfulShards, totalShards, shardFailures);
}
/**

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
@ -494,4 +495,24 @@ public enum RestStatus {
public static void writeTo(StreamOutput out, RestStatus status) throws IOException {
out.writeString(status.name());
}
public static RestStatus status(int successfulShards, int totalShards, ShardOperationFailedException... failures) {
if (failures.length == 0) {
if (successfulShards == 0 && totalShards > 0) {
return RestStatus.SERVICE_UNAVAILABLE;
}
return RestStatus.OK;
}
RestStatus status = RestStatus.OK;
if (successfulShards == 0 && totalShards > 0) {
for (ShardOperationFailedException failure : failures) {
RestStatus shardStatus = failure.status();
if (shardStatus.getStatus() >= status.getStatus()) {
status = failure.status();
}
}
return status;
}
return status;
}
}

View File

@ -19,6 +19,9 @@
package org.elasticsearch.rest.action.suggest;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.action.support.RestActions.buildBroadcastShardsHeader;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.suggest.SuggestRequest;
import org.elasticsearch.action.suggest.SuggestResponse;
@ -28,15 +31,16 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.support.RestBuilderListener;
import org.elasticsearch.search.suggest.Suggest;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.OK;
import static org.elasticsearch.rest.action.support.RestActions.buildBroadcastShardsHeader;
/**
*
*/
@ -72,6 +76,7 @@ public class RestSuggestAction extends BaseRestHandler {
client.suggest(suggestRequest, new RestBuilderListener<SuggestResponse>(channel) {
@Override
public RestResponse buildResponse(SuggestResponse response, XContentBuilder builder) throws Exception {
RestStatus restStatus = RestStatus.status(response.getSuccessfulShards(), response.getTotalShards(), response.getShardFailures());
builder.startObject();
buildBroadcastShardsHeader(builder, response);
Suggest suggest = response.getSuggest();
@ -79,7 +84,7 @@ public class RestSuggestAction extends BaseRestHandler {
suggest.toXContent(builder, request);
}
builder.endObject();
return new BytesRestResponse(OK, builder);
return new BytesRestResponse(restStatus, builder);
}
});
}

View File

@ -18,7 +18,8 @@
*/
package org.elasticsearch.snapshots;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
@ -28,8 +29,7 @@ import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.rest.RestStatus;
import java.io.IOException;
import com.google.common.collect.ImmutableList;
/**
* Information about snapshot
@ -179,15 +179,7 @@ public class SnapshotInfo implements ToXContent, Streamable {
if (shardFailures.size() == 0) {
return RestStatus.OK;
}
RestStatus status = RestStatus.OK;
if (successfulShards == 0 && totalShards > 0) {
for (SnapshotShardFailure shardFailure : shardFailures)
if (shardFailure.status().getStatus() > status().getStatus()) {
status = shardFailure.status();
}
return status;
}
return status;
return RestStatus.status(successfulShards, totalShards, shardFailures.toArray(new ShardOperationFailedException[shardFailures.size()]));
}
static final class Fields {