mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-26 14:54:56 +00:00
Added a status method CountResponse that resolve shard failures into rest status code. That method is now used in RestCountAction to return proper status.
Closes #3585
This commit is contained in:
parent
db11c30dd5
commit
86cb76a0ce
@ -23,14 +23,13 @@ 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.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class CountResponse extends BroadcastOperationResponse {
|
||||
|
||||
@ -52,6 +51,27 @@ public class CountResponse extends BroadcastOperationResponse {
|
||||
return count;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
super.readFrom(in);
|
||||
|
@ -39,7 +39,6 @@ import static org.elasticsearch.action.count.CountRequest.DEFAULT_MIN_SCORE;
|
||||
import static org.elasticsearch.rest.RestRequest.Method.GET;
|
||||
import static org.elasticsearch.rest.RestRequest.Method.POST;
|
||||
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST;
|
||||
import static org.elasticsearch.rest.RestStatus.OK;
|
||||
import static org.elasticsearch.rest.action.support.RestActions.buildBroadcastShardsHeader;
|
||||
import static org.elasticsearch.rest.action.support.RestActions.splitTypes;
|
||||
|
||||
@ -111,7 +110,7 @@ public class RestCountAction extends BaseRestHandler {
|
||||
buildBroadcastShardsHeader(builder, response);
|
||||
|
||||
builder.endObject();
|
||||
channel.sendResponse(new XContentRestResponse(request, OK, builder));
|
||||
channel.sendResponse(new XContentRestResponse(request, response.status(), builder));
|
||||
} catch (Throwable e) {
|
||||
onFailure(e);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user