Change BroadcastResponse from ToXContentFragment to ToXContentObject (#28878)

While working on #27799, we find that it might make sense to change BroadcastResponse from ToXContentFragment to ToXContentObject, seeing that it's rather a complete XContent object and also the other Responses are normally ToXContentObject.

By doing this, we can also move the XContent build logic of BroadcastResponse's subclasses, from Rest Layer to the concrete classes themselves.

Relates to #3889
This commit is contained in:
Yu 2018-03-23 10:53:37 +01:00 committed by Luca Cavanna
parent 8328b9c5cd
commit 4a8099c696
20 changed files with 105 additions and 243 deletions

View File

@ -24,7 +24,6 @@ import org.elasticsearch.action.support.broadcast.BroadcastResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.indices.recovery.RecoveryState;
@ -37,9 +36,8 @@ import java.util.Map;
/**
* Information regarding the recovery state of indices and their associated shards.
*/
public class RecoveryResponse extends BroadcastResponse implements ToXContentFragment {
public class RecoveryResponse extends BroadcastResponse {
private boolean detailed = false;
private Map<String, List<RecoveryState>> shardRecoveryStates = new HashMap<>();
public RecoveryResponse() { }
@ -51,36 +49,26 @@ public class RecoveryResponse extends BroadcastResponse implements ToXContentFra
* @param totalShards Total count of shards seen
* @param successfulShards Count of shards successfully processed
* @param failedShards Count of shards which failed to process
* @param detailed Display detailed metrics
* @param shardRecoveryStates Map of indices to shard recovery information
* @param shardFailures List of failures processing shards
*/
public RecoveryResponse(int totalShards, int successfulShards, int failedShards, boolean detailed,
Map<String, List<RecoveryState>> shardRecoveryStates,
public RecoveryResponse(int totalShards, int successfulShards, int failedShards, Map<String, List<RecoveryState>> shardRecoveryStates,
List<DefaultShardOperationFailedException> shardFailures) {
super(totalShards, successfulShards, failedShards, shardFailures);
this.shardRecoveryStates = shardRecoveryStates;
this.detailed = detailed;
}
public boolean hasRecoveries() {
return shardRecoveryStates.size() > 0;
}
public boolean detailed() {
return detailed;
}
public void detailed(boolean detailed) {
this.detailed = detailed;
}
public Map<String, List<RecoveryState>> shardRecoveryStates() {
return shardRecoveryStates;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (hasRecoveries()) {
for (String index : shardRecoveryStates.keySet()) {
List<RecoveryState> recoveryStates = shardRecoveryStates.get(index);
@ -98,6 +86,7 @@ public class RecoveryResponse extends BroadcastResponse implements ToXContentFra
builder.endObject();
}
}
builder.endObject();
return builder;
}
@ -133,4 +122,4 @@ public class RecoveryResponse extends BroadcastResponse implements ToXContentFra
public String toString() {
return Strings.toString(this, true, true);
}
}
}

View File

@ -87,7 +87,7 @@ public class TransportRecoveryAction extends TransportBroadcastByNodeAction<Reco
shardResponses.get(indexName).add(recoveryState);
}
}
return new RecoveryResponse(totalShards, successfulShards, failedShards, request.detailed(), shardResponses, shardFailures);
return new RecoveryResponse(totalShards, successfulShards, failedShards, shardResponses, shardFailures);
}
@Override
@ -118,4 +118,4 @@ public class TransportRecoveryAction extends TransportBroadcastByNodeAction<Reco
protected ClusterBlockException checkRequestBlock(ClusterState state, RecoveryRequest request, String[] concreteIndices) {
return state.blocks().indicesBlockedException(ClusterBlockLevel.READ, concreteIndices);
}
}
}

View File

@ -29,7 +29,6 @@ import org.elasticsearch.action.support.broadcast.BroadcastResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.engine.Segment;
@ -43,7 +42,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
public class IndicesSegmentResponse extends BroadcastResponse implements ToXContentFragment {
public class IndicesSegmentResponse extends BroadcastResponse {
private ShardSegments[] shards;
@ -103,7 +102,7 @@ public class IndicesSegmentResponse extends BroadcastResponse implements ToXCont
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
protected void addCustomXContentFields(XContentBuilder builder, Params params) throws IOException {
builder.startObject(Fields.INDICES);
for (IndexSegments indexSegments : getIndices().values()) {
@ -173,10 +172,9 @@ public class IndicesSegmentResponse extends BroadcastResponse implements ToXCont
}
builder.endObject();
return builder;
}
static void toXContent(XContentBuilder builder, Sort sort) throws IOException {
private static void toXContent(XContentBuilder builder, Sort sort) throws IOException {
builder.startArray("sort");
for (SortField field : sort.getSort()) {
builder.startObject();
@ -195,7 +193,7 @@ public class IndicesSegmentResponse extends BroadcastResponse implements ToXCont
builder.endArray();
}
static void toXContent(XContentBuilder builder, Accountable tree) throws IOException {
private static void toXContent(XContentBuilder builder, Accountable tree) throws IOException {
builder.startObject();
builder.field(Fields.DESCRIPTION, tree.toString());
builder.humanReadableField(Fields.SIZE_IN_BYTES, Fields.SIZE, new ByteSizeValue(tree.ramBytesUsed()));

View File

@ -25,9 +25,7 @@ import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import java.io.IOException;
import java.util.ArrayList;
@ -39,7 +37,7 @@ import java.util.Set;
import static java.util.Collections.unmodifiableMap;
public class IndicesStatsResponse extends BroadcastResponse implements ToXContentFragment {
public class IndicesStatsResponse extends BroadcastResponse {
private ShardStats[] shards;
@ -147,7 +145,7 @@ public class IndicesStatsResponse extends BroadcastResponse implements ToXConten
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
protected void addCustomXContentFields(XContentBuilder builder, Params params) throws IOException {
final String level = params.param("level", "indices");
final boolean isLevelValid =
"cluster".equalsIgnoreCase(level) || "indices".equalsIgnoreCase(level) || "shards".equalsIgnoreCase(level);
@ -155,7 +153,6 @@ public class IndicesStatsResponse extends BroadcastResponse implements ToXConten
throw new IllegalArgumentException("level parameter must be one of [cluster] or [indices] or [shards] but was [" + level + "]");
}
builder.startObject("_all");
builder.startObject("primaries");
@ -198,8 +195,6 @@ public class IndicesStatsResponse extends BroadcastResponse implements ToXConten
}
builder.endObject();
}
return builder;
}
static final class Fields {
@ -209,14 +204,6 @@ public class IndicesStatsResponse extends BroadcastResponse implements ToXConten
@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
builder.startObject();
toXContent(builder, EMPTY_PARAMS);
builder.endObject();
return Strings.toString(builder);
} catch (IOException e) {
return "{ \"error\" : \"" + e.getMessage() + "\"}";
}
return Strings.toString(this, true, false);
}
}

View File

@ -23,7 +23,6 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -34,7 +33,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
public class UpgradeStatusResponse extends BroadcastResponse implements ToXContentFragment {
public class UpgradeStatusResponse extends BroadcastResponse {
private ShardUpgradeStatus[] shards;
private Map<String, IndexUpgradeStatus> indicesUpgradeStatus;
@ -116,6 +115,7 @@ public class UpgradeStatusResponse extends BroadcastResponse implements ToXConte
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.byteSizeField(Fields.SIZE_IN_BYTES, Fields.SIZE, getTotalBytes());
builder.byteSizeField(Fields.SIZE_TO_UPGRADE_IN_BYTES, Fields.SIZE_TO_UPGRADE, getToUpgradeBytes());
builder.byteSizeField(Fields.SIZE_TO_UPGRADE_ANCIENT_IN_BYTES, Fields.SIZE_TO_UPGRADE_ANCIENT, getToUpgradeBytesAncient());
@ -161,6 +161,7 @@ public class UpgradeStatusResponse extends BroadcastResponse implements ToXConte
}
builder.endObject();
}
builder.endObject();
return builder;
}

View File

@ -25,6 +25,7 @@ import org.elasticsearch.action.support.broadcast.BroadcastResponse;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.HashMap;
@ -74,6 +75,18 @@ public class UpgradeResponse extends BroadcastResponse {
}
}
@Override
protected void addCustomXContentFields(XContentBuilder builder, Params params) throws IOException {
builder.startObject("upgraded_indices");
for (Map.Entry<String, Tuple<Version, String>> entry : versions.entrySet()) {
builder.startObject(entry.getKey());
builder.field("upgrade_version", entry.getValue().v1());
builder.field("oldest_lucene_segment_version", entry.getValue().v2());
builder.endObject();
}
builder.endObject();
}
/**
* Returns the highest upgrade version of the node that performed metadata upgrade and the
* the version of the oldest lucene segment for each index that was upgraded.

View File

@ -23,6 +23,7 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.ArrayList;
@ -38,8 +39,15 @@ import static org.elasticsearch.action.admin.indices.validate.query.QueryExplana
*/
public class ValidateQueryResponse extends BroadcastResponse {
public static final String INDEX_FIELD = "index";
public static final String SHARD_FIELD = "shard";
public static final String VALID_FIELD = "valid";
public static final String EXPLANATIONS_FIELD = "explanations";
public static final String ERROR_FIELD = "error";
public static final String EXPLANATION_FIELD = "explanation";
private boolean valid;
private List<QueryExplanation> queryExplanations;
ValidateQueryResponse() {
@ -96,4 +104,30 @@ public class ValidateQueryResponse extends BroadcastResponse {
}
}
@Override
protected void addCustomXContentFields(XContentBuilder builder, Params params) throws IOException {
builder.field(VALID_FIELD, isValid());
if (getQueryExplanation() != null && !getQueryExplanation().isEmpty()) {
builder.startArray(EXPLANATIONS_FIELD);
for (QueryExplanation explanation : getQueryExplanation()) {
builder.startObject();
if (explanation.getIndex() != null) {
builder.field(INDEX_FIELD, explanation.getIndex());
}
if(explanation.getShard() >= 0) {
builder.field(SHARD_FIELD, explanation.getShard());
}
builder.field(VALID_FIELD, explanation.isValid());
if (explanation.getError() != null) {
builder.field(ERROR_FIELD, explanation.getError());
}
if (explanation.getExplanation() != null) {
builder.field(EXPLANATION_FIELD, explanation.getExplanation());
}
builder.endObject();
}
builder.endArray();
}
}
}

View File

@ -25,7 +25,7 @@ import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestActions;
@ -40,7 +40,7 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optiona
/**
* Base class for all broadcast operation based responses.
*/
public class BroadcastResponse extends ActionResponse implements ToXContentFragment {
public class BroadcastResponse extends ActionResponse implements ToXContentObject {
public static final DefaultShardOperationFailedException[] EMPTY = new DefaultShardOperationFailedException[0];
@ -149,7 +149,16 @@ public class BroadcastResponse extends ActionResponse implements ToXContentFragm
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
RestActions.buildBroadcastShardsHeader(builder, params, this);
addCustomXContentFields(builder, params);
builder.endObject();
return builder;
}
/**
* Override in subclass to add custom fields following the common `_shards` field
*/
protected void addCustomXContentFields(XContentBuilder builder, Params params) throws IOException {
}
}

View File

@ -20,24 +20,19 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest;
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.OK;
public class RestClearIndicesCacheAction extends BaseRestHandler {
@ -61,16 +56,7 @@ public class RestClearIndicesCacheAction extends BaseRestHandler {
Strings.splitStringByCommaToArray(request.param("index")));
clearIndicesCacheRequest.indicesOptions(IndicesOptions.fromRequest(request, clearIndicesCacheRequest.indicesOptions()));
fromRequest(request, clearIndicesCacheRequest);
return channel ->
client.admin().indices().clearCache(clearIndicesCacheRequest, new RestBuilderListener<ClearIndicesCacheResponse>(channel) {
@Override
public RestResponse buildResponse(ClearIndicesCacheResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
return channel -> client.admin().indices().clearCache(clearIndicesCacheRequest, new RestToXContentListener<>(channel));
}
@Override

View File

@ -20,24 +20,19 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
import org.elasticsearch.action.admin.indices.flush.FlushResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.OK;
public class RestFlushAction extends BaseRestHandler {
public RestFlushAction(Settings settings, RestController controller) {
@ -60,14 +55,6 @@ public class RestFlushAction extends BaseRestHandler {
flushRequest.indicesOptions(IndicesOptions.fromRequest(request, flushRequest.indicesOptions()));
flushRequest.force(request.paramAsBoolean("force", flushRequest.force()));
flushRequest.waitIfOngoing(request.paramAsBoolean("wait_if_ongoing", flushRequest.waitIfOngoing()));
return channel -> client.admin().indices().flush(flushRequest, new RestBuilderListener<FlushResponse>(channel) {
@Override
public RestResponse buildResponse(FlushResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
return channel -> client.admin().indices().flush(flushRequest, new RestToXContentListener<>(channel));
}
}

View File

@ -20,23 +20,18 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.OK;
public class RestForceMergeAction extends BaseRestHandler {
public RestForceMergeAction(Settings settings, RestController controller) {
@ -57,14 +52,6 @@ public class RestForceMergeAction extends BaseRestHandler {
mergeRequest.maxNumSegments(request.paramAsInt("max_num_segments", mergeRequest.maxNumSegments()));
mergeRequest.onlyExpungeDeletes(request.paramAsBoolean("only_expunge_deletes", mergeRequest.onlyExpungeDeletes()));
mergeRequest.flush(request.paramAsBoolean("flush", mergeRequest.flush()));
return channel -> client.admin().indices().forceMerge(mergeRequest, new RestBuilderListener<ForceMergeResponse>(channel) {
@Override
public RestResponse buildResponse(ForceMergeResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
return channel -> client.admin().indices().forceMerge(mergeRequest, new RestToXContentListener<>(channel));
}
}

View File

@ -19,25 +19,19 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse;
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentsRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;
import static org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader;
public class RestIndicesSegmentsAction extends BaseRestHandler {
public RestIndicesSegmentsAction(Settings settings, RestController controller) {
@ -57,16 +51,6 @@ public class RestIndicesSegmentsAction extends BaseRestHandler {
Strings.splitStringByCommaToArray(request.param("index")));
indicesSegmentsRequest.verbose(request.paramAsBoolean("verbose", false));
indicesSegmentsRequest.indicesOptions(IndicesOptions.fromRequest(request, indicesSegmentsRequest.indicesOptions()));
return channel ->
client.admin().indices().segments(indicesSegmentsRequest, new RestBuilderListener<IndicesSegmentResponse>(channel) {
@Override
public RestResponse buildResponse(IndicesSegmentResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
buildBroadcastShardsHeader(builder, request, response);
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
return channel -> client.admin().indices().segments(indicesSegmentsRequest, new RestToXContentListener<>(channel));
}
}

View File

@ -20,18 +20,14 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import java.util.Collections;
@ -43,8 +39,6 @@ import java.util.TreeSet;
import java.util.function.Consumer;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;
import static org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader;
public class RestIndicesStatsAction extends BaseRestHandler {
public RestIndicesStatsAction(Settings settings, RestController controller) {
@ -141,16 +135,7 @@ public class RestIndicesStatsAction extends BaseRestHandler {
indicesStatsRequest.includeSegmentFileSizes(request.paramAsBoolean("include_segment_file_sizes", false));
}
return channel -> client.admin().indices().stats(indicesStatsRequest, new RestBuilderListener<IndicesStatsResponse>(channel) {
@Override
public RestResponse buildResponse(IndicesStatsResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
buildBroadcastShardsHeader(builder, request, response);
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
return channel -> client.admin().indices().stats(indicesStatsRequest, new RestToXContentListener<>(channel));
}
@Override

View File

@ -20,23 +20,18 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.action.admin.indices.recovery.RecoveryRequest;
import org.elasticsearch.action.admin.indices.recovery.RecoveryResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;
/**
* REST handler to report on index recoveries.
@ -60,18 +55,7 @@ public class RestRecoveryAction extends BaseRestHandler {
recoveryRequest.detailed(request.paramAsBoolean("detailed", false));
recoveryRequest.activeOnly(request.paramAsBoolean("active_only", false));
recoveryRequest.indicesOptions(IndicesOptions.fromRequest(request, recoveryRequest.indicesOptions()));
return channel -> client.admin().indices().recoveries(recoveryRequest, new RestBuilderListener<RecoveryResponse>(channel) {
@Override
public RestResponse buildResponse(RecoveryResponse response, XContentBuilder builder) throws Exception {
response.detailed(recoveryRequest.detailed());
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
return channel -> client.admin().indices().recoveries(recoveryRequest, new RestToXContentListener<>(channel));
}
}

View File

@ -25,13 +25,11 @@ import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
@ -57,13 +55,10 @@ public class RestRefreshAction extends BaseRestHandler {
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
RefreshRequest refreshRequest = new RefreshRequest(Strings.splitStringByCommaToArray(request.param("index")));
refreshRequest.indicesOptions(IndicesOptions.fromRequest(request, refreshRequest.indicesOptions()));
return channel -> client.admin().indices().refresh(refreshRequest, new RestBuilderListener<RefreshResponse>(channel) {
return channel -> client.admin().indices().refresh(refreshRequest, new RestToXContentListener<RefreshResponse>(channel) {
@Override
public RestResponse buildResponse(RefreshResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(response.getStatus(), builder);
protected RestStatus getStatus(RefreshResponse response) {
return response.getStatus();
}
});
}

View File

@ -19,28 +19,20 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.upgrade.get.UpgradeStatusRequest;
import org.elasticsearch.action.admin.indices.upgrade.post.UpgradeRequest;
import org.elasticsearch.action.admin.indices.upgrade.post.UpgradeResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.OK;
import static org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader;
public class RestUpgradeAction extends BaseRestHandler {
public RestUpgradeAction(Settings settings, RestController controller) {
@ -59,22 +51,6 @@ public class RestUpgradeAction extends BaseRestHandler {
UpgradeRequest upgradeReq = new UpgradeRequest(Strings.splitStringByCommaToArray(request.param("index")));
upgradeReq.indicesOptions(IndicesOptions.fromRequest(request, upgradeReq.indicesOptions()));
upgradeReq.upgradeOnlyAncientSegments(request.paramAsBoolean("only_ancient_segments", false));
return channel -> client.admin().indices().upgrade(upgradeReq, new RestBuilderListener<UpgradeResponse>(channel) {
@Override
public RestResponse buildResponse(UpgradeResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
buildBroadcastShardsHeader(builder, request, response);
builder.startObject("upgraded_indices");
for (Map.Entry<String, Tuple<Version, String>> entry : response.versions().entrySet()) {
builder.startObject(entry.getKey());
builder.field("upgrade_version", entry.getValue().v1());
builder.field("oldest_lucene_segment_version", entry.getValue().v2());
builder.endObject();
}
builder.endObject();
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
return channel -> client.admin().indices().upgrade(upgradeReq, new RestToXContentListener<>(channel));
}
}

View File

@ -20,23 +20,18 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.action.admin.indices.upgrade.get.UpgradeStatusRequest;
import org.elasticsearch.action.admin.indices.upgrade.get.UpgradeStatusResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;
public class RestUpgradeStatusAction extends BaseRestHandler {
@ -50,15 +45,7 @@ public class RestUpgradeStatusAction extends BaseRestHandler {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
UpgradeStatusRequest statusRequest = new UpgradeStatusRequest(Strings.splitStringByCommaToArray(request.param("index")));
statusRequest.indicesOptions(IndicesOptions.fromRequest(request, statusRequest.indicesOptions()));
return channel -> client.admin().indices().upgradeStatus(statusRequest, new RestBuilderListener<UpgradeStatusResponse>(channel) {
@Override
public RestResponse buildResponse(UpgradeStatusResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
return channel -> client.admin().indices().upgradeStatus(statusRequest, new RestToXContentListener<>(channel));
}
@Override

View File

@ -19,7 +19,6 @@
package org.elasticsearch.rest.action.admin.indices;
import org.elasticsearch.action.admin.indices.validate.query.QueryExplanation;
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
import org.elasticsearch.action.support.IndicesOptions;
@ -33,16 +32,14 @@ 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.action.RestActions;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
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.RestActions.buildBroadcastShardsHeader;
public class RestValidateQueryAction extends BaseRestHandler {
public RestValidateQueryAction(Settings settings, RestController controller) {
@ -91,37 +88,7 @@ public class RestValidateQueryAction extends BaseRestHandler {
handleException(validateQueryRequest, finalBodyParsingException.getMessage(), channel);
}
} else {
client.admin().indices().validateQuery(validateQueryRequest, new RestBuilderListener<ValidateQueryResponse>(channel) {
@Override
public RestResponse buildResponse(ValidateQueryResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
builder.field(VALID_FIELD, response.isValid());
buildBroadcastShardsHeader(builder, request, response);
if (response.getQueryExplanation() != null && !response.getQueryExplanation().isEmpty()) {
builder.startArray(EXPLANATIONS_FIELD);
for (QueryExplanation explanation : response.getQueryExplanation()) {
builder.startObject();
if (explanation.getIndex() != null) {
builder.field(INDEX_FIELD, explanation.getIndex());
}
if(explanation.getShard() >= 0) {
builder.field(SHARD_FIELD, explanation.getShard());
}
builder.field(VALID_FIELD, explanation.isValid());
if (explanation.getError() != null) {
builder.field(ERROR_FIELD, explanation.getError());
}
if (explanation.getExplanation() != null) {
builder.field(EXPLANATION_FIELD, explanation.getExplanation());
}
builder.endObject();
}
builder.endArray();
}
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
client.admin().indices().validateQuery(validateQueryRequest, new RestToXContentListener<>(channel));
}
};
}
@ -132,18 +99,11 @@ public class RestValidateQueryAction extends BaseRestHandler {
private static BytesRestResponse buildErrorResponse(XContentBuilder builder, String error, boolean explain) throws IOException {
builder.startObject();
builder.field(VALID_FIELD, false);
builder.field(ValidateQueryResponse.VALID_FIELD, false);
if (explain) {
builder.field(ERROR_FIELD, error);
builder.field(ValidateQueryResponse.ERROR_FIELD, error);
}
builder.endObject();
return new BytesRestResponse(OK, builder);
}
private static final String INDEX_FIELD = "index";
private static final String SHARD_FIELD = "shard";
private static final String VALID_FIELD = "valid";
private static final String EXPLANATIONS_FIELD = "explanations";
private static final String ERROR_FIELD = "error";
private static final String EXPLANATION_FIELD = "explanation";
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.action.admin.indices.stats;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.test.ESTestCase;
import java.util.Collections;
@ -34,7 +35,8 @@ public class IndicesStatsResponseTests extends ESTestCase {
final IndicesStatsResponse response = new IndicesStatsResponse();
final String level = randomAlphaOfLength(16);
final ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap("level", level));
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> response.toXContent(null, params));
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> response.toXContent(JsonXContent.contentBuilder(), params));
assertThat(
e,
hasToString(containsString("level parameter must be one of [cluster] or [indices] or [shards] but was [" + level + "]")));

View File

@ -57,7 +57,6 @@ public class RestRecoveryActionTests extends ESTestCase {
final int totalShards = randomIntBetween(1, 32);
final int successfulShards = Math.max(0, totalShards - randomIntBetween(1, 2));
final int failedShards = totalShards - successfulShards;
final boolean detailed = randomBoolean();
final Map<String, List<RecoveryState>> shardRecoveryStates = new HashMap<>();
final List<RecoveryState> recoveryStates = new ArrayList<>();
@ -115,7 +114,6 @@ public class RestRecoveryActionTests extends ESTestCase {
totalShards,
successfulShards,
failedShards,
detailed,
shardRecoveryStates,
shardFailures);
final Table table = action.buildRecoveryTable(null, response);