Skip to content

Commit

Permalink
Simplify indices stats API
Browse files Browse the repository at this point in the history
Note: This breaks backward compatibility

* Removed clear/all parameters, now all stats are returned by default
* Made the metrics part of the URL
* Removed a lot of handlers
* Added shards/indices/cluster level paremeter to change response serialization
* Returning translog statistics in IndicesStats
* Added TranslogStats class
* Added IndexShard.translogStats() method to get the stats from concrete implementation
* Updated documentation

Closes #4054
  • Loading branch information
spinscale committed Jan 6, 2014
1 parent 3024cc2 commit 33878be
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 727 deletions.
34 changes: 6 additions & 28 deletions docs/reference/indices/stats.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Specific index stats can be retrieved using:
curl localhost:9200/index1,index2/_stats
--------------------------------------------------

By default, `docs`, `store`, and `indexing`, `get`, and `search` stats
are returned, other stats can be enabled as well:
By default, all stats are returned, returning only specific stats can be
specified as well in the URI. Those stats can be any of:

[horizontal]
`docs`:: The number of docs / deleted docs (docs not yet merged out).
Expand All @@ -45,7 +45,6 @@ are returned, other stats can be enabled as well:
`flush`:: Flush statistics.
`completion`:: Completion suggest statistics.
`refresh`:: Refresh statistics.
`clear`:: Clears all the flags (first).

Some statistics allow per field granularity which accepts a list comma-separated list of included fields. By default all fields are included:

Expand All @@ -58,14 +57,12 @@ Here are some samples:

[source,js]
--------------------------------------------------
# Get back stats for merge and refresh on top of the defaults
curl 'localhost:9200/_stats?merge=true&refresh=true'
# Get back stats just for flush
curl 'localhost:9200/_stats?clear=true&flush=true'
# Get back stats for merge and refresh only for all indices
curl 'localhost:9200/_stats/merge,refresh'
# Get back stats for type1 and type2 documents for the my_index index
curl 'localhost:9200/my_index/_stats?clear=true&indexing=true&types=type1,type2
curl 'localhost:9200/my_index/_stats/indexing?types=type1,type2
# Get back just search stats for group1 and group2
curl 'localhost:9200/_stats?clear=true&search=true&groups=group1,group2
curl 'localhost:9200/_stats/search?groups=group1,group2
--------------------------------------------------

The stats returned are aggregated on the index level, with
Expand All @@ -77,22 +74,3 @@ they are created on other nodes. On the other hand, even though a shard
"left" a node, that node will still retain the stats that shard
contributed to.

[float]
=== Specific stats endpoints

Instead of using flags to indicate which stats to return, specific REST
endpoints can be used, for example:

[source,js]
--------------------------------------------------
# Merge stats across all indices
curl localhost:9200/_stats/merge
# Merge stats for the my_index index
curl localhost:9200/my_index/_stats/merge
# Indexing stats for my_index
curl localhost:9200/my_index/_stats/indexing
# Indexing stats for my_index for my_type1 and my_type2
curl localhost:9200/my_index/_stats/indexing/my_type1,my_type2
# Search stats for group1 and group2
curl 'localhost:9200/_stats/search/group1,group2
--------------------------------------------------
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.elasticsearch.index.shard.DocsStats;
import org.elasticsearch.index.shard.service.IndexShard;
import org.elasticsearch.index.store.StoreStats;
import org.elasticsearch.index.translog.TranslogStats;
import org.elasticsearch.index.warmer.WarmerStats;
import org.elasticsearch.search.suggest.completion.CompletionStats;

Expand Down Expand Up @@ -103,6 +104,9 @@ public CommonStats(CommonStatsFlags flags) {
case Percolate:
percolate = new PercolateStats();
break;
case Translog:
translog = new TranslogStats();
break;
default:
throw new IllegalStateException("Unknown Flag: " + flag);
}
Expand Down Expand Up @@ -160,6 +164,9 @@ public CommonStats(IndexShard indexShard, CommonStatsFlags flags) {
case Percolate:
percolate = indexShard.shardPercolateService().stats();
break;
case Translog:
translog = indexShard.translogStats();
break;
default:
throw new IllegalStateException("Unknown Flag: " + flag);
}
Expand Down Expand Up @@ -211,6 +218,9 @@ public CommonStats(IndexShard indexShard, CommonStatsFlags flags) {
@Nullable
public SegmentsStats segments;

@Nullable
public TranslogStats translog;

public void add(CommonStats stats) {
if (docs == null) {
if (stats.getDocs() != null) {
Expand Down Expand Up @@ -334,6 +344,14 @@ public void add(CommonStats stats) {
} else {
segments.add(stats.getSegments());
}
if (translog == null) {
if (stats.getTranslog() != null) {
translog = new TranslogStats();
translog.add(stats.getTranslog());
}
} else {
translog.add(stats.getTranslog());
}
}

@Nullable
Expand Down Expand Up @@ -411,6 +429,9 @@ public SegmentsStats getSegments() {
return segments;
}

@Nullable
public TranslogStats getTranslog() { return translog; }

public static CommonStats readCommonStats(StreamInput in) throws IOException {
CommonStats stats = new CommonStats();
stats.readFrom(in);
Expand Down Expand Up @@ -468,6 +489,9 @@ public void readFrom(StreamInput in) throws IOException {
segments = SegmentsStats.readSegmentsStats(in);
}
}
if (in.getVersion().after(Version.V_1_0_0_Beta2)) {
translog = in.readOptionalStreamable(new TranslogStats());
}
}

@Override
Expand Down Expand Up @@ -566,6 +590,9 @@ public void writeTo(StreamOutput out) throws IOException {
segments.writeTo(out);
}
}
if (out.getVersion().after(Version.V_1_0_0_Beta2)) {
out.writeOptionalStreamable(translog);
}
}

// note, requires a wrapping object
Expand Down Expand Up @@ -616,6 +643,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (segments != null) {
segments.toXContent(builder, params);
}
if (translog != null) {
translog.toXContent(builder, params);
}
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CommonStatsFlags implements Streamable, Cloneable {
public final static CommonStatsFlags ALL = new CommonStatsFlags().all();
public final static CommonStatsFlags NONE = new CommonStatsFlags().clear();

private EnumSet<Flag> flags = EnumSet.of(Flag.Docs, Flag.Store, Flag.Indexing, Flag.Get, Flag.Search, Flag.Percolate);
private EnumSet<Flag> flags = EnumSet.allOf(Flag.class);
private String[] types = null;
private String[] groups = null;
private String[] fieldDataFields = null;
Expand Down Expand Up @@ -227,7 +227,8 @@ public static enum Flag {
Warmer("warmer"),
Percolate("percolate"),
Completion("completion"),
Segments("segments");
Segments("segments"),
Translog("translog");

private final String restName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
/**
* A request to get indices level stats. Allow to enable different stats to be returned.
* <p/>
* <p>By default, the {@link #docs(boolean)}, {@link #store(boolean)}, {@link #indexing(boolean)}
* are enabled. Other stats can be enabled as well.
* <p>By default, all statistics are enabled.
* <p/>
* <p>All the stats to be returned can be cleared using {@link #clear()}, at which point, specific
* stats can be enabled.
Expand Down Expand Up @@ -239,6 +238,15 @@ public String[] completionFields() {
return flags.completionDataFields();
}

public IndicesStatsRequest translog(boolean translog) {
flags.set(Flag.Translog, translog);
return this;
}

public boolean translog() {
return flags.isSet(Flag.Translog);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ public IndicesStatsRequestBuilder setCompletionFields(String... fields) {
return this;
}

public IndicesStatsRequestBuilder setTranslog(boolean translog) {
request.translog(translog);
return this;
}

@Override
protected void doExecute(ActionListener<IndicesStatsResponse> listener) {
((IndicesAdminClient) client).stats(request, listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ public void writeTo(StreamOutput out) throws IOException {

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
String level = params.param("level", "indices");
boolean isLevelValid = "indices".equalsIgnoreCase(level) || "shards".equalsIgnoreCase(level) || "cluster".equalsIgnoreCase(level);
if (!isLevelValid) {
return builder;
}

builder.startObject("_all");

builder.startObject("primaries");
Expand All @@ -168,35 +174,36 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

builder.endObject();

builder.startObject(Fields.INDICES);
for (IndexStats indexStats : getIndices().values()) {
builder.startObject(indexStats.getIndex(), XContentBuilder.FieldCaseConversion.NONE);
if ("indices".equalsIgnoreCase(level) || "shards".equalsIgnoreCase(level)) {
builder.startObject(Fields.INDICES);
for (IndexStats indexStats : getIndices().values()) {
builder.startObject(indexStats.getIndex(), XContentBuilder.FieldCaseConversion.NONE);

builder.startObject("primaries");
indexStats.getPrimaries().toXContent(builder, params);
builder.endObject();
builder.startObject("primaries");
indexStats.getPrimaries().toXContent(builder, params);
builder.endObject();

builder.startObject("total");
indexStats.getTotal().toXContent(builder, params);
builder.endObject();
builder.startObject("total");
indexStats.getTotal().toXContent(builder, params);
builder.endObject();

if ("shards".equalsIgnoreCase(params.param("level", null))) {
builder.startObject(Fields.SHARDS);
for (IndexShardStats indexShardStats : indexStats) {
builder.startArray(Integer.toString(indexShardStats.getShardId().id()));
for (ShardStats shardStats : indexShardStats) {
builder.startObject();
shardStats.toXContent(builder, params);
builder.endObject();
if ("shards".equalsIgnoreCase(level)) {
builder.startObject(Fields.SHARDS);
for (IndexShardStats indexShardStats : indexStats) {
builder.startArray(Integer.toString(indexShardStats.getShardId().id()));
for (ShardStats shardStats : indexShardStats) {
builder.startObject();
shardStats.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
}
builder.endArray();
builder.endObject();
}
builder.endObject();
}

builder.endObject();
}
builder.endObject();

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ protected ShardStats shardOperation(IndexShardStatsRequest request) throws Elast
flags.set(CommonStatsFlags.Flag.Completion);
flags.completionDataFields(request.request.completionFields());
}
if (request.request.translog()) {
flags.set(CommonStatsFlags.Flag.Translog);
}

return new ShardStats(indexShard, flags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.index.shard.service;

import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.index.translog.TranslogStats;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesReference;
Expand Down Expand Up @@ -108,6 +109,8 @@ public interface IndexShard extends IndexShardComponent {

CompletionStats completionStats(String... fields);

TranslogStats translogStats();

PercolatorQueriesRegistry percolateRegistry();

ShardPercolateService shardPercolateService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.index.translog.TranslogStats;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.common.Booleans;
Expand Down Expand Up @@ -561,6 +562,11 @@ public IdCacheStats idCacheStats() {
return shardIdCache.stats();
}

@Override
public TranslogStats translogStats() {
return translog.stats();
}

@Override
public CompletionStats completionStats(String... fields) {
CompletionStats completionStats = new CompletionStats();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/elasticsearch/index/translog/Translog.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public interface Translog extends IndexShardComponent, CloseableIndexComponent {

void syncOnEachOperation(boolean syncOnEachOperation);

/**
* return stats
*/
TranslogStats stats();

static class Location {
public final long translogId;
public final long translogLocation;
Expand Down

0 comments on commit 33878be

Please sign in to comment.