optimize the index status response

This commit is contained in:
kimchy 2011-03-04 00:55:39 +02:00
parent 41ddcdca93
commit 607ada75a6
2 changed files with 227 additions and 182 deletions

View File

@ -22,10 +22,16 @@ package org.elasticsearch.action.admin.indices.status;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.index.merge.MergeStats;
import java.io.IOException;
import java.util.List;
@ -39,7 +45,7 @@ import static org.elasticsearch.common.settings.ImmutableSettings.*;
/**
* @author kimchy (shay.banon)
*/
public class IndicesStatusResponse extends BroadcastOperationResponse {
public class IndicesStatusResponse extends BroadcastOperationResponse implements ToXContent {
protected ShardStatus[] shards;
@ -124,4 +130,221 @@ public class IndicesStatusResponse extends BroadcastOperationResponse {
indicesSettings.put(in.readUTF(), readSettingsFromStream(in));
}
}
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return toXContent(builder, params, null);
}
public XContentBuilder toXContent(XContentBuilder builder, Params params, @Nullable SettingsFilter settingsFilter) throws IOException {
builder.startObject(Fields.INDICES);
for (IndexStatus indexStatus : indices().values()) {
builder.startObject(indexStatus.index(), XContentBuilder.FieldCaseConversion.NONE);
builder.array(Fields.ALIASES, indexStatus.settings().getAsArray("index.aliases"));
builder.startObject(Fields.SETTINGS);
Settings settings = indexStatus.settings();
if (settingsFilter != null) {
settings = settingsFilter.filterSettings(settings);
}
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();
builder.startObject(Fields.INDEX);
if (indexStatus.storeSize() != null) {
builder.field(Fields.PRIMARY_SIZE, indexStatus.primaryStoreSize().toString());
builder.field(Fields.PRIMARY_SIZE_IN_BYTES, indexStatus.primaryStoreSize().bytes());
builder.field(Fields.SIZE, indexStatus.storeSize().toString());
builder.field(Fields.SIZE_IN_BYTES, indexStatus.storeSize().bytes());
}
builder.endObject();
if (indexStatus.translogOperations() != -1) {
builder.startObject(Fields.TRANSLOG);
builder.field(Fields.OPERATIONS, indexStatus.translogOperations());
builder.endObject();
}
if (indexStatus.docs() != null) {
builder.startObject(Fields.DOCS);
builder.field(Fields.NUM_DOCS, indexStatus.docs().numDocs());
builder.field(Fields.MAX_DOC, indexStatus.docs().maxDoc());
builder.field(Fields.DELETED_DOCS, indexStatus.docs().deletedDocs());
builder.endObject();
}
MergeStats mergeStats = indexStatus.mergeStats();
if (mergeStats != null) {
mergeStats.toXContent(builder, params);
}
builder.startObject(Fields.SHARDS);
for (IndexShardStatus indexShardStatus : indexStatus) {
builder.startArray(Integer.toString(indexShardStatus.shardId().id()));
for (ShardStatus shardStatus : indexShardStatus) {
builder.startObject();
builder.startObject(Fields.ROUTING)
.field(Fields.STATE, shardStatus.shardRouting().state())
.field(Fields.PRIMARY, shardStatus.shardRouting().primary())
.field(Fields.NODE, shardStatus.shardRouting().currentNodeId())
.field(Fields.RELOCATING_NODE, shardStatus.shardRouting().relocatingNodeId())
.field(Fields.SHARD, shardStatus.shardRouting().shardId().id())
.field(Fields.INDEX, shardStatus.shardRouting().shardId().index().name())
.endObject();
builder.field(Fields.STATE, shardStatus.state());
if (shardStatus.storeSize() != null) {
builder.startObject(Fields.INDEX);
builder.field(Fields.SIZE, shardStatus.storeSize().toString());
builder.field(Fields.SIZE_IN_BYTES, shardStatus.storeSize().bytes());
builder.endObject();
}
if (shardStatus.translogId() != -1) {
builder.startObject(Fields.TRANSLOG);
builder.field(Fields.ID, shardStatus.translogId());
builder.field(Fields.OPERATIONS, shardStatus.translogOperations());
builder.endObject();
}
if (shardStatus.docs() != null) {
builder.startObject(Fields.DOCS);
builder.field(Fields.NUM_DOCS, shardStatus.docs().numDocs());
builder.field(Fields.MAX_DOC, shardStatus.docs().maxDoc());
builder.field(Fields.DELETED_DOCS, shardStatus.docs().deletedDocs());
builder.endObject();
}
mergeStats = shardStatus.mergeStats();
if (mergeStats != null) {
mergeStats.toXContent(builder, params);
}
if (shardStatus.peerRecoveryStatus() != null) {
PeerRecoveryStatus peerRecoveryStatus = shardStatus.peerRecoveryStatus();
builder.startObject(Fields.PEER_RECOVERY);
builder.field(Fields.STAGE, peerRecoveryStatus.stage());
builder.field(Fields.START_TIME_IN_MILLIS, peerRecoveryStatus.startTime());
builder.field(Fields.TIME, peerRecoveryStatus.time());
builder.field(Fields.TIME_IN_MILLIS, peerRecoveryStatus.time().millis());
builder.startObject(Fields.INDEX);
builder.field(Fields.PROGRESS, peerRecoveryStatus.indexRecoveryProgress());
builder.field(Fields.SIZE, peerRecoveryStatus.indexSize());
builder.field(Fields.SIZE_IN_BYTES, peerRecoveryStatus.indexSize().bytes());
builder.field(Fields.REUSED_SIZE, peerRecoveryStatus.reusedIndexSize());
builder.field(Fields.REUSED_SIZE_IN_BYTES, peerRecoveryStatus.reusedIndexSize().bytes());
builder.field(Fields.EXPECTED_RECOVERED_SIZE, peerRecoveryStatus.expectedRecoveredIndexSize());
builder.field(Fields.EXPECTED_RECOVERED_SIZE_IN_BYTES, peerRecoveryStatus.expectedRecoveredIndexSize().bytes());
builder.field(Fields.RECOVERED_SIZE, peerRecoveryStatus.recoveredIndexSize());
builder.field(Fields.RECOVERED_SIZE_IN_BYTES, peerRecoveryStatus.recoveredIndexSize().bytes());
builder.endObject();
builder.startObject(Fields.TRANSLOG);
builder.field(Fields.RECOVERED, peerRecoveryStatus.recoveredTranslogOperations());
builder.endObject();
builder.endObject();
}
if (shardStatus.gatewayRecoveryStatus() != null) {
GatewayRecoveryStatus gatewayRecoveryStatus = shardStatus.gatewayRecoveryStatus();
builder.startObject(Fields.GATEWAY_RECOVERY);
builder.field(Fields.STAGE, gatewayRecoveryStatus.stage());
builder.field(Fields.START_TIME_IN_MILLIS, gatewayRecoveryStatus.startTime());
builder.field(Fields.TIME, gatewayRecoveryStatus.time());
builder.field(Fields.TIME_IN_MILLIS, gatewayRecoveryStatus.time().millis());
builder.startObject(Fields.INDEX);
builder.field(Fields.PROGRESS, gatewayRecoveryStatus.indexRecoveryProgress());
builder.field(Fields.SIZE, gatewayRecoveryStatus.indexSize());
builder.field(Fields.SIZE, gatewayRecoveryStatus.indexSize().bytes());
builder.field(Fields.REUSED_SIZE, gatewayRecoveryStatus.reusedIndexSize());
builder.field(Fields.REUSED_SIZE_IN_BYTES, gatewayRecoveryStatus.reusedIndexSize().bytes());
builder.field(Fields.EXPECTED_RECOVERED_SIZE, gatewayRecoveryStatus.expectedRecoveredIndexSize());
builder.field(Fields.EXPECTED_RECOVERED_SIZE_IN_BYTES, gatewayRecoveryStatus.expectedRecoveredIndexSize().bytes());
builder.field(Fields.RECOVERED_SIZE, gatewayRecoveryStatus.recoveredIndexSize());
builder.field(Fields.RECOVERED_SIZE_IN_BYTES, gatewayRecoveryStatus.recoveredIndexSize().bytes());
builder.endObject();
builder.startObject(Fields.TRANSLOG);
builder.field(Fields.RECOVERED, gatewayRecoveryStatus.recoveredTranslogOperations());
builder.endObject();
builder.endObject();
}
if (shardStatus.gatewaySnapshotStatus() != null) {
GatewaySnapshotStatus gatewaySnapshotStatus = shardStatus.gatewaySnapshotStatus();
builder.startObject(Fields.GATEWAY_SNAPSHOT);
builder.field(Fields.STAGE, gatewaySnapshotStatus.stage());
builder.field(Fields.START_TIME_IN_MILLIS, gatewaySnapshotStatus.startTime());
builder.field(Fields.TIME, gatewaySnapshotStatus.time());
builder.field(Fields.TIME_IN_MILLIS, gatewaySnapshotStatus.time().millis());
builder.startObject(Fields.INDEX);
builder.field(Fields.SIZE, gatewaySnapshotStatus.indexSize());
builder.field(Fields.SIZE_IN_BYTES, gatewaySnapshotStatus.indexSize().bytes());
builder.endObject();
builder.startObject(Fields.TRANSLOG);
builder.field(Fields.EXPECTED_OPERATIONS, gatewaySnapshotStatus.expectedNumberOfOperations());
builder.endObject();
builder.endObject();
}
builder.endObject();
}
builder.endArray();
}
builder.endObject();
builder.endObject();
}
builder.endObject();
return builder;
}
static final class Fields {
static final XContentBuilderString INDICES = new XContentBuilderString("indices");
static final XContentBuilderString ALIASES = new XContentBuilderString("aliases");
static final XContentBuilderString SETTINGS = new XContentBuilderString("settings");
static final XContentBuilderString INDEX = new XContentBuilderString("index");
static final XContentBuilderString PRIMARY_SIZE = new XContentBuilderString("primary_size");
static final XContentBuilderString PRIMARY_SIZE_IN_BYTES = new XContentBuilderString("primary_size_in_bytes");
static final XContentBuilderString SIZE = new XContentBuilderString("size");
static final XContentBuilderString SIZE_IN_BYTES = new XContentBuilderString("size_in_bytes");
static final XContentBuilderString TRANSLOG = new XContentBuilderString("translog");
static final XContentBuilderString OPERATIONS = new XContentBuilderString("operations");
static final XContentBuilderString DOCS = new XContentBuilderString("docs");
static final XContentBuilderString NUM_DOCS = new XContentBuilderString("num_docs");
static final XContentBuilderString MAX_DOC = new XContentBuilderString("max_doc");
static final XContentBuilderString DELETED_DOCS = new XContentBuilderString("deleted_docs");
static final XContentBuilderString SHARDS = new XContentBuilderString("shards");
static final XContentBuilderString ROUTING = new XContentBuilderString("routing");
static final XContentBuilderString STATE = new XContentBuilderString("state");
static final XContentBuilderString PRIMARY = new XContentBuilderString("primary");
static final XContentBuilderString NODE = new XContentBuilderString("node");
static final XContentBuilderString RELOCATING_NODE = new XContentBuilderString("relocating_node");
static final XContentBuilderString SHARD = new XContentBuilderString("shard");
static final XContentBuilderString ID = new XContentBuilderString("id");
static final XContentBuilderString PEER_RECOVERY = new XContentBuilderString("peer_recovery");
static final XContentBuilderString STAGE = new XContentBuilderString("stage");
static final XContentBuilderString START_TIME_IN_MILLIS = new XContentBuilderString("start_time_in_millis");
static final XContentBuilderString TIME = new XContentBuilderString("time");
static final XContentBuilderString TIME_IN_MILLIS = new XContentBuilderString("time_in_millis");
static final XContentBuilderString PROGRESS = new XContentBuilderString("progress");
static final XContentBuilderString REUSED_SIZE = new XContentBuilderString("reused_size");
static final XContentBuilderString REUSED_SIZE_IN_BYTES = new XContentBuilderString("reused_size_in_bytes");
static final XContentBuilderString EXPECTED_RECOVERED_SIZE = new XContentBuilderString("expected_recovered_size");
static final XContentBuilderString EXPECTED_RECOVERED_SIZE_IN_BYTES = new XContentBuilderString("expected_recovered_size_in_bytes");
static final XContentBuilderString RECOVERED_SIZE = new XContentBuilderString("recovered_size");
static final XContentBuilderString RECOVERED_SIZE_IN_BYTES = new XContentBuilderString("recovered_size_in_bytes");
static final XContentBuilderString RECOVERED = new XContentBuilderString("recovered");
static final XContentBuilderString GATEWAY_RECOVERY = new XContentBuilderString("gateway_recovery");
static final XContentBuilderString GATEWAY_SNAPSHOT = new XContentBuilderString("gateway_snapshot");
static final XContentBuilderString EXPECTED_OPERATIONS = new XContentBuilderString("expected_operations");
}
}

View File

@ -20,19 +20,18 @@
package org.elasticsearch.rest.action.admin.indices.status;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.status.*;
import org.elasticsearch.action.admin.indices.status.IndicesStatusRequest;
import org.elasticsearch.action.admin.indices.status.IndicesStatusResponse;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.merge.MergeStats;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.rest.RestRequest.Method.*;
import static org.elasticsearch.rest.RestStatus.*;
@ -70,185 +69,8 @@ public class RestIndicesStatusAction extends BaseRestHandler {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("ok", true);
buildBroadcastShardsHeader(builder, response);
builder.startObject("indices");
for (IndexStatus indexStatus : response.indices().values()) {
builder.startObject(indexStatus.index(), XContentBuilder.FieldCaseConversion.NONE);
builder.array("aliases", indexStatus.settings().getAsArray("index.aliases"));
builder.startObject("settings");
Settings settings = settingsFilter.filterSettings(indexStatus.settings());
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();
builder.startObject("index");
if (indexStatus.storeSize() != null) {
builder.field("primary_size", indexStatus.primaryStoreSize().toString());
builder.field("primary_size_in_bytes", indexStatus.primaryStoreSize().bytes());
builder.field("size", indexStatus.storeSize().toString());
builder.field("size_in_bytes", indexStatus.storeSize().bytes());
}
builder.endObject();
if (indexStatus.translogOperations() != -1) {
builder.startObject("translog");
builder.field("operations", indexStatus.translogOperations());
builder.endObject();
}
if (indexStatus.docs() != null) {
builder.startObject("docs");
builder.field("num_docs", indexStatus.docs().numDocs());
builder.field("max_doc", indexStatus.docs().maxDoc());
builder.field("deleted_docs", indexStatus.docs().deletedDocs());
builder.endObject();
}
MergeStats mergeStats = indexStatus.mergeStats();
if (mergeStats != null) {
builder.startObject("merges");
builder.field("current", mergeStats.currentMerges());
builder.field("total", mergeStats.totalMerges());
builder.field("total_time", mergeStats.totalMergeTime());
builder.field("total_time_in_millis", mergeStats.totalMergeTimeInMillis());
builder.endObject();
}
builder.startObject("shards");
for (IndexShardStatus indexShardStatus : indexStatus) {
builder.startArray(Integer.toString(indexShardStatus.shardId().id()));
for (ShardStatus shardStatus : indexShardStatus) {
builder.startObject();
builder.startObject("routing")
.field("state", shardStatus.shardRouting().state())
.field("primary", shardStatus.shardRouting().primary())
.field("node", shardStatus.shardRouting().currentNodeId())
.field("relocating_node", shardStatus.shardRouting().relocatingNodeId())
.field("shard", shardStatus.shardRouting().shardId().id())
.field("index", shardStatus.shardRouting().shardId().index().name())
.endObject();
builder.field("state", shardStatus.state());
if (shardStatus.storeSize() != null) {
builder.startObject("index");
builder.field("size", shardStatus.storeSize().toString());
builder.field("size_in_bytes", shardStatus.storeSize().bytes());
builder.endObject();
}
if (shardStatus.translogId() != -1) {
builder.startObject("translog");
builder.field("id", shardStatus.translogId());
builder.field("operations", shardStatus.translogOperations());
builder.endObject();
}
if (shardStatus.docs() != null) {
builder.startObject("docs");
builder.field("num_docs", shardStatus.docs().numDocs());
builder.field("max_doc", shardStatus.docs().maxDoc());
builder.field("deleted_docs", shardStatus.docs().deletedDocs());
builder.endObject();
}
mergeStats = shardStatus.mergeStats();
if (mergeStats != null) {
builder.startObject("merges");
builder.field("current", mergeStats.currentMerges());
builder.field("total", mergeStats.totalMerges());
builder.field("total_time", mergeStats.totalMergeTime());
builder.field("total_time_in_millis", mergeStats.totalMergeTimeInMillis());
builder.endObject();
}
if (shardStatus.peerRecoveryStatus() != null) {
PeerRecoveryStatus peerRecoveryStatus = shardStatus.peerRecoveryStatus();
builder.startObject("peer_recovery");
builder.field("stage", peerRecoveryStatus.stage());
builder.field("start_time_in_millis", peerRecoveryStatus.startTime());
builder.field("time", peerRecoveryStatus.time());
builder.field("time_in_millis", peerRecoveryStatus.time().millis());
builder.startObject("index");
builder.field("progress", peerRecoveryStatus.indexRecoveryProgress());
builder.field("size", peerRecoveryStatus.indexSize());
builder.field("size_in_bytes", peerRecoveryStatus.indexSize().bytes());
builder.field("reused_size", peerRecoveryStatus.reusedIndexSize());
builder.field("reused_size_in_bytes", peerRecoveryStatus.reusedIndexSize().bytes());
builder.field("expected_recovered_size", peerRecoveryStatus.expectedRecoveredIndexSize());
builder.field("expected_recovered_size_in_bytes", peerRecoveryStatus.expectedRecoveredIndexSize().bytes());
builder.field("recovered_size", peerRecoveryStatus.recoveredIndexSize());
builder.field("recovered_size_in_bytes", peerRecoveryStatus.recoveredIndexSize().bytes());
builder.endObject();
builder.startObject("translog");
builder.field("recovered", peerRecoveryStatus.recoveredTranslogOperations());
builder.endObject();
builder.endObject();
}
if (shardStatus.gatewayRecoveryStatus() != null) {
GatewayRecoveryStatus gatewayRecoveryStatus = shardStatus.gatewayRecoveryStatus();
builder.startObject("gateway_recovery");
builder.field("stage", gatewayRecoveryStatus.stage());
builder.field("start_time_in_millis", gatewayRecoveryStatus.startTime());
builder.field("time", gatewayRecoveryStatus.time());
builder.field("time_in_millis", gatewayRecoveryStatus.time().millis());
builder.startObject("index");
builder.field("progress", gatewayRecoveryStatus.indexRecoveryProgress());
builder.field("size", gatewayRecoveryStatus.indexSize());
builder.field("size_in_bytes", gatewayRecoveryStatus.indexSize().bytes());
builder.field("reused_size", gatewayRecoveryStatus.reusedIndexSize());
builder.field("reused_size_in_bytes", gatewayRecoveryStatus.reusedIndexSize().bytes());
builder.field("expected_recovered_size", gatewayRecoveryStatus.expectedRecoveredIndexSize());
builder.field("expected_recovered_size_in_bytes", gatewayRecoveryStatus.expectedRecoveredIndexSize().bytes());
builder.field("recovered_size", gatewayRecoveryStatus.recoveredIndexSize());
builder.field("recovered_size_in_bytes", gatewayRecoveryStatus.recoveredIndexSize().bytes());
builder.endObject();
builder.startObject("translog");
builder.field("recovered", gatewayRecoveryStatus.recoveredTranslogOperations());
builder.endObject();
builder.endObject();
}
if (shardStatus.gatewaySnapshotStatus() != null) {
GatewaySnapshotStatus gatewaySnapshotStatus = shardStatus.gatewaySnapshotStatus();
builder.startObject("gateway_snapshot");
builder.field("stage", gatewaySnapshotStatus.stage());
builder.field("start_time_in_millis", gatewaySnapshotStatus.startTime());
builder.field("time", gatewaySnapshotStatus.time());
builder.field("time_in_millis", gatewaySnapshotStatus.time().millis());
builder.startObject("index");
builder.field("size", gatewaySnapshotStatus.indexSize());
builder.field("size_in_bytes", gatewaySnapshotStatus.indexSize().bytes());
builder.endObject();
builder.startObject("index");
builder.field("expected_operations", gatewaySnapshotStatus.expectedNumberOfOperations());
builder.endObject();
builder.endObject();
}
builder.endObject();
}
builder.endArray();
}
builder.endObject();
builder.endObject();
}
builder.endObject();
response.toXContent(builder, request, settingsFilter);
builder.endObject();
channel.sendResponse(new XContentRestResponse(request, OK, builder));
} catch (Exception e) {