Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return an HTTP error code when a suggest request failed instead of 200 #10104

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 3 additions & 22 deletions src/main/java/org/elasticsearch/action/count/CountResponse.java
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -62,24 +60,7 @@ public boolean terminatedEarly() {
}

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
Expand Down
Expand Up @@ -69,24 +69,7 @@ public SearchResponse(InternalSearchResponse internalResponse, String scrollId,

@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);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/elasticsearch/rest/RestStatus.java
Expand Up @@ -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;

Expand Down Expand Up @@ -494,4 +495,24 @@ public static RestStatus readFrom(StreamInput in) throws IOException {
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;
}
}
Expand Up @@ -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;
Expand All @@ -28,15 +31,16 @@
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;

/**
*
*/
Expand Down Expand Up @@ -72,14 +76,15 @@ public void handleRequest(final RestRequest request, final RestChannel channel,
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();
if (suggest != null) {
suggest.toXContent(builder, request);
}
builder.endObject();
return new BytesRestResponse(OK, builder);
return new BytesRestResponse(restStatus, builder);
}
});
}
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java
Expand Up @@ -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;
Expand All @@ -28,8 +29,7 @@
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
Expand Down Expand Up @@ -179,15 +179,7 @@ public RestStatus status() {
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 {
Expand Down