Skip to content

Commit

Permalink
Add Index UUID to /_stats Response
Browse files Browse the repository at this point in the history
* Add "uuid" field to each index's section in the `/_stats` response
* closes elastic#31791
  • Loading branch information
original-brownbear committed Jul 6, 2018
1 parent 3c11c7c commit c36f382
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ public class IndexStats implements Iterable<IndexShardStats> {

private final String index;

private final String uuid;

private final ShardStats shards[];

public IndexStats(String index, ShardStats[] shards) {
public IndexStats(String index, String uuid, ShardStats[] shards) {
this.index = index;
this.uuid = uuid;
this.shards = shards;
}

public String getIndex() {
return this.index;
}

public String getUuid() {
return uuid;
}

public ShardStats[] getShards() {
return this.shards;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.Index;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -84,19 +85,22 @@ public Map<String, IndexStats> getIndices() {
}
Map<String, IndexStats> indicesStats = new HashMap<>();

Set<String> indices = new HashSet<>();
Set<Index> indices = new HashSet<>();
for (ShardStats shard : shards) {
indices.add(shard.getShardRouting().getIndexName());
indices.add(shard.getShardRouting().index());
}

for (String indexName : indices) {
for (Index index : indices) {
List<ShardStats> shards = new ArrayList<>();
String indexName = index.getName();
for (ShardStats shard : this.shards) {
if (shard.getShardRouting().getIndexName().equals(indexName)) {
shards.add(shard);
}
}
indicesStats.put(indexName, new IndexStats(indexName, shards.toArray(new ShardStats[shards.size()])));
indicesStats.put(
indexName, new IndexStats(indexName, index.getUUID(), shards.toArray(new ShardStats[shards.size()]))
);
}
this.indicesStats = indicesStats;
return indicesStats;
Expand Down Expand Up @@ -169,7 +173,7 @@ protected void addCustomXContentFields(XContentBuilder builder, Params params) t
builder.startObject(Fields.INDICES);
for (IndexStats indexStats : getIndices().values()) {
builder.startObject(indexStats.getIndex());

builder.field("uuid", indexStats.getUuid());
builder.startObject("primaries");
indexStats.getPrimaries().toXContent(builder, params);
builder.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.engine.CommitStats;
import org.elasticsearch.index.engine.SegmentsStats;
import org.elasticsearch.index.translog.Translog;
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
Expand Down Expand Up @@ -153,12 +157,23 @@ public void testRefreshListeners() throws Exception {
assertEquals(0, common.refresh.getListeners());
}

@SuppressWarnings("unchecked")
public void testUuidOnRootStatsIndices() throws IOException {
String uuid = createIndex("test").indexUUID();
IndicesStatsResponse rsp = client().admin().indices().prepareStats().get();
try (XContentParser parser = createParser(JsonXContent.jsonXContent, rsp.toString())) {
assertEquals(
uuid,
((Map<String, Object>)((Map<String,Object>) parser.map().get("indices")).get("test")).get("uuid")
);
}
}

/**
* Gives access to package private IndicesStatsResponse constructor for test purpose.
**/
public static IndicesStatsResponse newIndicesStatsResponse(ShardStats[] shards, int totalShards, int successfulShards,
int failedShards, List<DefaultShardOperationFailedException> shardFailures) {
return new IndicesStatsResponse(shards, totalShards, successfulShards, failedShards, shardFailures);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class IndicesStatsMonitoringDocTests extends BaseFilteredMonitoringDocTes
@Before
public void setUp() throws Exception {
super.setUp();
indicesStats = Collections.singletonList(new IndexStats("index-0", new ShardStats[] {
indicesStats = Collections.singletonList(new IndexStats("index-0", "dcvO5uZATE-EhIKc3tk9Bg", new ShardStats[] {
// Primaries
new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null),
new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null),
Expand Down

0 comments on commit c36f382

Please sign in to comment.