From deade2eb714f5bfabb3022f5297f29b02be0bd9a Mon Sep 17 00:00:00 2001 From: olivier bourgain Date: Mon, 16 Mar 2015 11:54:56 +0100 Subject: [PATCH] return an HTTP code representing the error when a suggest request failed instead of 200 --- .../action/count/CountResponse.java | 25 +++---------------- .../action/search/SearchResponse.java | 19 +------------- .../org/elasticsearch/rest/RestStatus.java | 21 ++++++++++++++++ .../action/suggest/RestSuggestAction.java | 19 ++++++++------ .../elasticsearch/snapshots/SnapshotInfo.java | 16 +++--------- 5 files changed, 41 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/count/CountResponse.java b/src/main/java/org/elasticsearch/action/count/CountResponse.java index 16e7d185585..394d266eedc 100644 --- a/src/main/java/org/elasticsearch/action/count/CountResponse.java +++ b/src/main/java/org/elasticsearch/action/count/CountResponse.java @@ -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 diff --git a/src/main/java/org/elasticsearch/action/search/SearchResponse.java b/src/main/java/org/elasticsearch/action/search/SearchResponse.java index 76b1ea7c0a6..f4f05366c6b 100644 --- a/src/main/java/org/elasticsearch/action/search/SearchResponse.java +++ b/src/main/java/org/elasticsearch/action/search/SearchResponse.java @@ -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); } /** diff --git a/src/main/java/org/elasticsearch/rest/RestStatus.java b/src/main/java/org/elasticsearch/rest/RestStatus.java index 5b1c1ef9ed2..ee0e7ef57a1 100644 --- a/src/main/java/org/elasticsearch/rest/RestStatus.java +++ b/src/main/java/org/elasticsearch/rest/RestStatus.java @@ -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; + } } diff --git a/src/main/java/org/elasticsearch/rest/action/suggest/RestSuggestAction.java b/src/main/java/org/elasticsearch/rest/action/suggest/RestSuggestAction.java index ed64b69ab07..2a226222fd6 100644 --- a/src/main/java/org/elasticsearch/rest/action/suggest/RestSuggestAction.java +++ b/src/main/java/org/elasticsearch/rest/action/suggest/RestSuggestAction.java @@ -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(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); } }); } diff --git a/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java b/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java index 768a0157945..8e09fbd2c23 100644 --- a/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java +++ b/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java @@ -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 {